blob: 863b0b9c3a0c1581df604027cca9dc69fc9001f3 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
David Brazdil8d5b8b22015-03-24 10:51:52 +000020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010022#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "handle.h"
25#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000028#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000031#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "utils/growable_array.h"
33
34namespace art {
35
David Brazdil1abb4192015-02-17 18:33:36 +000036class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HBasicBlock;
David Brazdil8d5b8b22015-03-24 10:51:52 +000038class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010039class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000040class HFloatConstant;
41class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000043class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000044class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000046class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010047class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010048class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010049class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000050class LocationSummary;
David Brazdil8d5b8b22015-03-24 10:51:52 +000051class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
53static const int kDefaultNumberOfBlocks = 8;
54static const int kDefaultNumberOfSuccessors = 2;
55static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010056static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000057static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000058
Calin Juravle9aec02f2014-11-18 23:06:35 +000059static constexpr uint32_t kMaxIntShiftValue = 0x1f;
60static constexpr uint64_t kMaxLongShiftValue = 0x3f;
61
Dave Allison20dfc792014-06-16 20:44:29 -070062enum IfCondition {
63 kCondEQ,
64 kCondNE,
65 kCondLT,
66 kCondLE,
67 kCondGT,
68 kCondGE,
69};
70
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010071class HInstructionList {
72 public:
73 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
74
75 void AddInstruction(HInstruction* instruction);
76 void RemoveInstruction(HInstruction* instruction);
77
David Brazdilc3d743f2015-04-22 13:40:50 +010078 // Insert `instruction` before/after an existing instruction `cursor`.
79 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
80 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
81
Roland Levillain6b469232014-09-25 10:10:38 +010082 // Return true if this list contains `instruction`.
83 bool Contains(HInstruction* instruction) const;
84
Roland Levillainccc07a92014-09-16 14:48:16 +010085 // Return true if `instruction1` is found before `instruction2` in
86 // this instruction list and false otherwise. Abort if none
87 // of these instructions is found.
88 bool FoundBefore(const HInstruction* instruction1,
89 const HInstruction* instruction2) const;
90
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000091 bool IsEmpty() const { return first_instruction_ == nullptr; }
92 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
93
94 // Update the block of all instructions to be `block`.
95 void SetBlockOfInstructions(HBasicBlock* block) const;
96
97 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
98 void Add(const HInstructionList& instruction_list);
99
David Brazdil2d7352b2015-04-20 14:52:42 +0100100 // Return the number of instructions in the list. This is an expensive operation.
101 size_t CountSize() const;
102
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100103 private:
104 HInstruction* first_instruction_;
105 HInstruction* last_instruction_;
106
107 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000108 friend class HGraph;
109 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100110 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100111 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100112
113 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
114};
115
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000116// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700117class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000118 public:
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000119 HGraph(ArenaAllocator* arena, bool debuggable = false, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000120 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000121 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100122 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100123 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700124 entry_block_(nullptr),
125 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100126 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100127 number_of_vregs_(0),
128 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000129 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400130 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000131 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000132 current_instruction_id_(start_instruction_id),
133 cached_null_constant_(nullptr),
134 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
135 cached_long_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000136
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000137 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100138 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100139 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000140
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000141 HBasicBlock* GetEntryBlock() const { return entry_block_; }
142 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000143
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000144 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
145 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000146
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000147 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100148
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000149 // Try building the SSA form of this graph, with dominance computation and loop
150 // recognition. Returns whether it was successful in doing all these steps.
151 bool TryBuildingSsa() {
152 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000153 // The SSA builder requires loops to all be natural. Specifically, the dead phi
154 // elimination phase checks the consistency of the graph when doing a post-order
155 // visit for eliminating dead phis: a dead phi can only have loop header phi
156 // users remaining when being visited.
157 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000158 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000159 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 }
161
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000162 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000163 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100164 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000165
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000166 // Analyze all natural loops in this graph. Returns false if one
167 // loop is not natural, that is the header does not dominate the
168 // back edge.
169 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100170
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000171 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
172 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
173
David Brazdil2d7352b2015-04-20 14:52:42 +0100174 // Removes `block` from the graph.
175 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000176
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100177 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
178 void SimplifyLoop(HBasicBlock* header);
179
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000180 int32_t GetNextInstructionId() {
181 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000182 return current_instruction_id_++;
183 }
184
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000185 int32_t GetCurrentInstructionId() const {
186 return current_instruction_id_;
187 }
188
189 void SetCurrentInstructionId(int32_t id) {
190 current_instruction_id_ = id;
191 }
192
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100193 uint16_t GetMaximumNumberOfOutVRegs() const {
194 return maximum_number_of_out_vregs_;
195 }
196
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000197 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
198 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100199 }
200
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000201 void UpdateTemporariesVRegSlots(size_t slots) {
202 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100203 }
204
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000205 size_t GetTemporariesVRegSlots() const {
206 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100207 }
208
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100209 void SetNumberOfVRegs(uint16_t number_of_vregs) {
210 number_of_vregs_ = number_of_vregs;
211 }
212
213 uint16_t GetNumberOfVRegs() const {
214 return number_of_vregs_;
215 }
216
217 void SetNumberOfInVRegs(uint16_t value) {
218 number_of_in_vregs_ = value;
219 }
220
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100221 uint16_t GetNumberOfLocalVRegs() const {
222 return number_of_vregs_ - number_of_in_vregs_;
223 }
224
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100225 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
226 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100227 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100228
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100229 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
230 return linear_order_;
231 }
232
Mark Mendell1152c922015-04-24 17:06:35 -0400233 bool HasBoundsChecks() const {
234 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800235 }
236
Mark Mendell1152c922015-04-24 17:06:35 -0400237 void SetHasBoundsChecks(bool value) {
238 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800239 }
240
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000241 bool IsDebuggable() const { return debuggable_; }
242
David Brazdil8d5b8b22015-03-24 10:51:52 +0000243 // Returns a constant of the given type and value. If it does not exist
244 // already, it is created and inserted into the graph. Only integral types
Nicolas Geoffray067cae22015-04-26 16:43:00 +0000245 // are currently supported.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000246 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000247 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000248 HIntConstant* GetIntConstant(int32_t value) {
249 return CreateConstant(value, &cached_int_constants_);
250 }
251 HLongConstant* GetLongConstant(int64_t value) {
252 return CreateConstant(value, &cached_long_constants_);
253 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000254
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000255 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100256
257 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000258 void VisitBlockForDominatorTree(HBasicBlock* block,
259 HBasicBlock* predecessor,
260 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100261 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000262 void VisitBlockForBackEdges(HBasicBlock* block,
263 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100264 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000265 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100266 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000267
David Brazdil8d5b8b22015-03-24 10:51:52 +0000268 template <class InstType, typename ValueType>
269 InstType* CreateConstant(ValueType value, ArenaSafeMap<ValueType, InstType*>* cache);
270 void InsertConstant(HConstant* instruction);
271
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000272 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000273
274 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000275 GrowableArray<HBasicBlock*> blocks_;
276
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100277 // List of blocks to perform a reverse post order tree traversal.
278 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000279
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100280 // List of blocks to perform a linear order tree traversal.
281 GrowableArray<HBasicBlock*> linear_order_;
282
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000283 HBasicBlock* entry_block_;
284 HBasicBlock* exit_block_;
285
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100286 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100287 uint16_t maximum_number_of_out_vregs_;
288
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100289 // The number of virtual registers in this method. Contains the parameters.
290 uint16_t number_of_vregs_;
291
292 // The number of virtual registers used by parameters of this method.
293 uint16_t number_of_in_vregs_;
294
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000295 // Number of vreg size slots that the temporaries use (used in baseline compiler).
296 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100297
Mark Mendell1152c922015-04-24 17:06:35 -0400298 // Has bounds checks. We can totally skip BCE if it's false.
299 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800300
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000301 // Indicates whether the graph should be compiled in a way that
302 // ensures full debuggability. If false, we can apply more
303 // aggressive optimizations that may limit the level of debugging.
304 const bool debuggable_;
305
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000306 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000307 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000308
David Brazdil46e2a392015-03-16 17:31:52 +0000309 // Cached common constants often needed by optimization passes.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000310 HNullConstant* cached_null_constant_;
311 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
312 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000313
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100314 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000315 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000316 DISALLOW_COPY_AND_ASSIGN(HGraph);
317};
318
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700319class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000320 public:
321 HLoopInformation(HBasicBlock* header, HGraph* graph)
322 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100323 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100324 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100325 // Make bit vector growable, as the number of blocks may change.
326 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100327
328 HBasicBlock* GetHeader() const {
329 return header_;
330 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000331
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000332 void SetHeader(HBasicBlock* block) {
333 header_ = block;
334 }
335
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100336 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
337 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
338 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
339
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000340 void AddBackEdge(HBasicBlock* back_edge) {
341 back_edges_.Add(back_edge);
342 }
343
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100344 void RemoveBackEdge(HBasicBlock* back_edge) {
345 back_edges_.Delete(back_edge);
346 }
347
David Brazdil46e2a392015-03-16 17:31:52 +0000348 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100349 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000350 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100351 }
352 return false;
353 }
354
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000355 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000356 return back_edges_.Size();
357 }
358
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100359 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100360
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100361 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
362 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100363 }
364
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100365 void ClearBackEdges() {
366 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100367 }
368
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100369 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
370 // that is the header dominates the back edge.
371 bool Populate();
372
373 // Returns whether this loop information contains `block`.
374 // Note that this loop information *must* be populated before entering this function.
375 bool Contains(const HBasicBlock& block) const;
376
377 // Returns whether this loop information is an inner loop of `other`.
378 // Note that `other` *must* be populated before entering this function.
379 bool IsIn(const HLoopInformation& other) const;
380
381 const ArenaBitVector& GetBlocks() const { return blocks_; }
382
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000383 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000384 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000385
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000386 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100387 // Internal recursive implementation of `Populate`.
388 void PopulateRecursive(HBasicBlock* block);
389
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000390 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100391 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000392 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100393 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000394
395 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
396};
397
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100398static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100399static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100400
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000401// A block in a method. Contains the list of instructions represented
402// as a double linked list. Each block knows its predecessors and
403// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100404
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700405class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000406 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100407 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000408 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000409 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
410 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000411 loop_information_(nullptr),
412 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100413 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100414 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100415 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100416 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000417 lifetime_end_(kNoLifetime),
418 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000419
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100420 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
421 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000422 }
423
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100424 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
425 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000426 }
427
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100428 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
429 return dominated_blocks_;
430 }
431
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100432 bool IsEntryBlock() const {
433 return graph_->GetEntryBlock() == this;
434 }
435
436 bool IsExitBlock() const {
437 return graph_->GetExitBlock() == this;
438 }
439
David Brazdil46e2a392015-03-16 17:31:52 +0000440 bool IsSingleGoto() const;
441
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000442 void AddBackEdge(HBasicBlock* back_edge) {
443 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000444 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000445 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100446 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000447 loop_information_->AddBackEdge(back_edge);
448 }
449
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000450 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000451 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000452
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000453 int GetBlockId() const { return block_id_; }
454 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000455
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000456 HBasicBlock* GetDominator() const { return dominator_; }
457 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100458 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100459 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000460 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
461 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
462 if (dominated_blocks_.Get(i) == existing) {
463 dominated_blocks_.Put(i, new_block);
464 return;
465 }
466 }
467 LOG(FATAL) << "Unreachable";
468 UNREACHABLE();
469 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000470
471 int NumberOfBackEdges() const {
472 return loop_information_ == nullptr
473 ? 0
474 : loop_information_->NumberOfBackEdges();
475 }
476
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100477 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
478 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100479 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100480 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100481 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
482 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000483
484 void AddSuccessor(HBasicBlock* block) {
485 successors_.Add(block);
486 block->predecessors_.Add(this);
487 }
488
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100489 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
490 size_t successor_index = GetSuccessorIndexOf(existing);
491 DCHECK_NE(successor_index, static_cast<size_t>(-1));
492 existing->RemovePredecessor(this);
493 new_block->predecessors_.Add(this);
494 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000495 }
496
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000497 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
498 size_t predecessor_index = GetPredecessorIndexOf(existing);
499 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
500 existing->RemoveSuccessor(this);
501 new_block->successors_.Add(this);
502 predecessors_.Put(predecessor_index, new_block);
503 }
504
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100505 void RemovePredecessor(HBasicBlock* block) {
506 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100507 }
508
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000509 void RemoveSuccessor(HBasicBlock* block) {
510 successors_.Delete(block);
511 }
512
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100513 void ClearAllPredecessors() {
514 predecessors_.Reset();
515 }
516
517 void AddPredecessor(HBasicBlock* block) {
518 predecessors_.Add(block);
519 block->successors_.Add(this);
520 }
521
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100522 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100523 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100524 HBasicBlock* temp = predecessors_.Get(0);
525 predecessors_.Put(0, predecessors_.Get(1));
526 predecessors_.Put(1, temp);
527 }
528
David Brazdil769c9e52015-04-27 13:54:09 +0100529 void SwapSuccessors() {
530 DCHECK_EQ(successors_.Size(), 2u);
531 HBasicBlock* temp = successors_.Get(0);
532 successors_.Put(0, successors_.Get(1));
533 successors_.Put(1, temp);
534 }
535
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100536 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
537 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
538 if (predecessors_.Get(i) == predecessor) {
539 return i;
540 }
541 }
542 return -1;
543 }
544
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100545 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
546 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
547 if (successors_.Get(i) == successor) {
548 return i;
549 }
550 }
551 return -1;
552 }
553
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000554 // Split the block into two blocks just after `cursor`. Returns the newly
555 // created block. Note that this method just updates raw block information,
556 // like predecessors, successors, dominators, and instruction list. It does not
557 // update the graph, reverse post order, loop information, nor make sure the
558 // blocks are consistent (for example ending with a control flow instruction).
559 HBasicBlock* SplitAfter(HInstruction* cursor);
560
561 // Merge `other` at the end of `this`. Successors and dominated blocks of
562 // `other` are changed to be successors and dominated blocks of `this`. Note
563 // that this method does not update the graph, reverse post order, loop
564 // information, nor make sure the blocks are consistent (for example ending
565 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100566 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000567
568 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
569 // of `this` are moved to `other`.
570 // Note that this method does not update the graph, reverse post order, loop
571 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000572 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000573 void ReplaceWith(HBasicBlock* other);
574
David Brazdil2d7352b2015-04-20 14:52:42 +0100575 // Merge `other` at the end of `this`. This method updates loops, reverse post
576 // order, links to predecessors, successors, dominators and deletes the block
577 // from the graph. The two blocks must be successive, i.e. `this` the only
578 // predecessor of `other` and vice versa.
579 void MergeWith(HBasicBlock* other);
580
581 // Disconnects `this` from all its predecessors, successors and dominator,
582 // removes it from all loops it is included in and eventually from the graph.
583 // The block must not dominate any other block. Predecessors and successors
584 // are safely updated.
585 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000586
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000587 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100588 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100589 // Replace instruction `initial` with `replacement` within this block.
590 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
591 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100592 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100593 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000594 // RemoveInstruction and RemovePhi delete a given instruction from the respective
595 // instruction list. With 'ensure_safety' set to true, it verifies that the
596 // instruction is not in use and removes it from the use lists of its inputs.
597 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
598 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100599 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100600
601 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100602 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100603 }
604
Roland Levillain6b879dd2014-09-22 17:13:44 +0100605 bool IsLoopPreHeaderFirstPredecessor() const {
606 DCHECK(IsLoopHeader());
607 DCHECK(!GetPredecessors().IsEmpty());
608 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
609 }
610
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100611 HLoopInformation* GetLoopInformation() const {
612 return loop_information_;
613 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000614
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000615 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100616 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000617 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100618 void SetInLoop(HLoopInformation* info) {
619 if (IsLoopHeader()) {
620 // Nothing to do. This just means `info` is an outer loop.
621 } else if (loop_information_ == nullptr) {
622 loop_information_ = info;
623 } else if (loop_information_->Contains(*info->GetHeader())) {
624 // Block is currently part of an outer loop. Make it part of this inner loop.
625 // Note that a non loop header having a loop information means this loop information
626 // has already been populated
627 loop_information_ = info;
628 } else {
629 // Block is part of an inner loop. Do not update the loop information.
630 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
631 // at this point, because this method is being called while populating `info`.
632 }
633 }
634
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000635 // Raw update of the loop information.
636 void SetLoopInformation(HLoopInformation* info) {
637 loop_information_ = info;
638 }
639
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100640 bool IsInLoop() const { return loop_information_ != nullptr; }
641
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100642 // Returns wheter this block dominates the blocked passed as parameter.
643 bool Dominates(HBasicBlock* block) const;
644
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100645 size_t GetLifetimeStart() const { return lifetime_start_; }
646 size_t GetLifetimeEnd() const { return lifetime_end_; }
647
648 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
649 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
650
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100651 uint32_t GetDexPc() const { return dex_pc_; }
652
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000653 bool IsCatchBlock() const { return is_catch_block_; }
654 void SetIsCatchBlock() { is_catch_block_ = true; }
655
David Brazdil8d5b8b22015-03-24 10:51:52 +0000656 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000657 bool EndsWithIf() const;
658 bool HasSinglePhi() const;
659
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000660 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000661 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000662 GrowableArray<HBasicBlock*> predecessors_;
663 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100664 HInstructionList instructions_;
665 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000666 HLoopInformation* loop_information_;
667 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100668 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000669 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100670 // The dex program counter of the first instruction of this block.
671 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100672 size_t lifetime_start_;
673 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000674 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000675
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000676 friend class HGraph;
677 friend class HInstruction;
678
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000679 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
680};
681
David Brazdilb2bd1c52015-03-25 11:17:37 +0000682// Iterates over the LoopInformation of all loops which contain 'block'
683// from the innermost to the outermost.
684class HLoopInformationOutwardIterator : public ValueObject {
685 public:
686 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
687 : current_(block.GetLoopInformation()) {}
688
689 bool Done() const { return current_ == nullptr; }
690
691 void Advance() {
692 DCHECK(!Done());
693 current_ = current_->GetHeader()->GetDominator()->GetLoopInformation();
694 }
695
696 HLoopInformation* Current() const {
697 DCHECK(!Done());
698 return current_;
699 }
700
701 private:
702 HLoopInformation* current_;
703
704 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
705};
706
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100707#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
708 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000709 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000710 M(ArrayGet, Instruction) \
711 M(ArrayLength, Instruction) \
712 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100713 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000714 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000715 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000716 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100717 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000718 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100719 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700720 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000721 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000722 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000723 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100724 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000725 M(Exit, Instruction) \
726 M(FloatConstant, Constant) \
727 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100728 M(GreaterThan, Condition) \
729 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100730 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000731 M(InstanceFieldGet, Instruction) \
732 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000733 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100734 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000735 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000736 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100737 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000738 M(LessThan, Condition) \
739 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000740 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000741 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100742 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000743 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100744 M(Local, Instruction) \
745 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100746 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000747 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000748 M(Mul, BinaryOperation) \
749 M(Neg, UnaryOperation) \
750 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100751 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100752 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000753 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000754 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000755 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000756 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100757 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000758 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100759 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000760 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100761 M(Return, Instruction) \
762 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000763 M(Shl, BinaryOperation) \
764 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100765 M(StaticFieldGet, Instruction) \
766 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100767 M(StoreLocal, Instruction) \
768 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100769 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000770 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000771 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000772 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000773 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000774 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000775
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100776#define FOR_EACH_INSTRUCTION(M) \
777 FOR_EACH_CONCRETE_INSTRUCTION(M) \
778 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100779 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100780 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100781 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700782
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100783#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000784FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
785#undef FORWARD_DECLARATION
786
Roland Levillainccc07a92014-09-16 14:48:16 +0100787#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000788 InstructionKind GetKind() const OVERRIDE { return k##type; } \
789 const char* DebugName() const OVERRIDE { return #type; } \
790 const H##type* As##type() const OVERRIDE { return this; } \
791 H##type* As##type() OVERRIDE { return this; } \
792 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100793 return other->Is##type(); \
794 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000795 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000796
David Brazdiled596192015-01-23 10:39:45 +0000797template <typename T> class HUseList;
798
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100799template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700800class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000801 public:
David Brazdiled596192015-01-23 10:39:45 +0000802 HUseListNode* GetPrevious() const { return prev_; }
803 HUseListNode* GetNext() const { return next_; }
804 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100805 size_t GetIndex() const { return index_; }
806
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000807 private:
David Brazdiled596192015-01-23 10:39:45 +0000808 HUseListNode(T user, size_t index)
809 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
810
811 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100812 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000813 HUseListNode<T>* prev_;
814 HUseListNode<T>* next_;
815
816 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000817
818 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
819};
820
David Brazdiled596192015-01-23 10:39:45 +0000821template <typename T>
822class HUseList : public ValueObject {
823 public:
824 HUseList() : first_(nullptr) {}
825
826 void Clear() {
827 first_ = nullptr;
828 }
829
830 // Adds a new entry at the beginning of the use list and returns
831 // the newly created node.
832 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000833 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000834 if (IsEmpty()) {
835 first_ = new_node;
836 } else {
837 first_->prev_ = new_node;
838 new_node->next_ = first_;
839 first_ = new_node;
840 }
841 return new_node;
842 }
843
844 HUseListNode<T>* GetFirst() const {
845 return first_;
846 }
847
848 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000849 DCHECK(node != nullptr);
850 DCHECK(Contains(node));
851
David Brazdiled596192015-01-23 10:39:45 +0000852 if (node->prev_ != nullptr) {
853 node->prev_->next_ = node->next_;
854 }
855 if (node->next_ != nullptr) {
856 node->next_->prev_ = node->prev_;
857 }
858 if (node == first_) {
859 first_ = node->next_;
860 }
861 }
862
David Brazdil1abb4192015-02-17 18:33:36 +0000863 bool Contains(const HUseListNode<T>* node) const {
864 if (node == nullptr) {
865 return false;
866 }
867 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
868 if (current == node) {
869 return true;
870 }
871 }
872 return false;
873 }
874
David Brazdiled596192015-01-23 10:39:45 +0000875 bool IsEmpty() const {
876 return first_ == nullptr;
877 }
878
879 bool HasOnlyOneUse() const {
880 return first_ != nullptr && first_->next_ == nullptr;
881 }
882
883 private:
884 HUseListNode<T>* first_;
885};
886
887template<typename T>
888class HUseIterator : public ValueObject {
889 public:
890 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
891
892 bool Done() const { return current_ == nullptr; }
893
894 void Advance() {
895 DCHECK(!Done());
896 current_ = current_->GetNext();
897 }
898
899 HUseListNode<T>* Current() const {
900 DCHECK(!Done());
901 return current_;
902 }
903
904 private:
905 HUseListNode<T>* current_;
906
907 friend class HValue;
908};
909
David Brazdil1abb4192015-02-17 18:33:36 +0000910// This class is used by HEnvironment and HInstruction classes to record the
911// instructions they use and pointers to the corresponding HUseListNodes kept
912// by the used instructions.
913template <typename T>
914class HUserRecord : public ValueObject {
915 public:
916 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
917 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
918
919 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
920 : instruction_(old_record.instruction_), use_node_(use_node) {
921 DCHECK(instruction_ != nullptr);
922 DCHECK(use_node_ != nullptr);
923 DCHECK(old_record.use_node_ == nullptr);
924 }
925
926 HInstruction* GetInstruction() const { return instruction_; }
927 HUseListNode<T>* GetUseNode() const { return use_node_; }
928
929 private:
930 // Instruction used by the user.
931 HInstruction* instruction_;
932
933 // Corresponding entry in the use list kept by 'instruction_'.
934 HUseListNode<T>* use_node_;
935};
936
Calin Juravle27df7582015-04-17 19:12:31 +0100937// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
938// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
939// flag is consider.
940// - DependsOn suggests that there is a real dependency between side effects but it only
941// checks DependendsOnSomething flag.
942//
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100943// Represents the side effects an instruction may have.
944class SideEffects : public ValueObject {
945 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100946 SideEffects() : flags_(0) {}
947
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100948 static SideEffects None() {
949 return SideEffects(0);
950 }
951
952 static SideEffects All() {
953 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
954 }
955
956 static SideEffects ChangesSomething() {
957 return SideEffects((1 << kFlagChangesCount) - 1);
958 }
959
960 static SideEffects DependsOnSomething() {
961 int count = kFlagDependsOnCount - kFlagChangesCount;
962 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
963 }
964
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100965 SideEffects Union(SideEffects other) const {
966 return SideEffects(flags_ | other.flags_);
967 }
968
Roland Levillain72bceff2014-09-15 18:29:00 +0100969 bool HasSideEffects() const {
970 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
971 return (flags_ & all_bits_set) != 0;
972 }
973
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100974 bool HasAllSideEffects() const {
975 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
976 return all_bits_set == (flags_ & all_bits_set);
977 }
978
979 bool DependsOn(SideEffects other) const {
980 size_t depends_flags = other.ComputeDependsFlags();
981 return (flags_ & depends_flags) != 0;
982 }
983
984 bool HasDependencies() const {
985 int count = kFlagDependsOnCount - kFlagChangesCount;
986 size_t all_bits_set = (1 << count) - 1;
987 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
988 }
989
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100990 private:
991 static constexpr int kFlagChangesSomething = 0;
992 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
993
994 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
995 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
996
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100997 explicit SideEffects(size_t flags) : flags_(flags) {}
998
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100999 size_t ComputeDependsFlags() const {
1000 return flags_ << kFlagChangesCount;
1001 }
1002
1003 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001004};
1005
David Brazdiled596192015-01-23 10:39:45 +00001006// A HEnvironment object contains the values of virtual registers at a given location.
1007class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1008 public:
1009 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
1010 : vregs_(arena, number_of_vregs) {
1011 vregs_.SetSize(number_of_vregs);
1012 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001013 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001014 }
1015 }
1016
1017 void CopyFrom(HEnvironment* env);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001018 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1019 // input to the loop phi instead. This is for inserting instructions that
1020 // require an environment (like HDeoptimization) in the loop pre-header.
1021 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001022
1023 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001024 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001025 }
1026
1027 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001028 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001029 }
1030
David Brazdil1abb4192015-02-17 18:33:36 +00001031 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001032
1033 size_t Size() const { return vregs_.Size(); }
1034
1035 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001036 // Record instructions' use entries of this environment for constant-time removal.
1037 // It should only be called by HInstruction when a new environment use is added.
1038 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1039 DCHECK(env_use->GetUser() == this);
1040 size_t index = env_use->GetIndex();
1041 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1042 }
David Brazdiled596192015-01-23 10:39:45 +00001043
David Brazdil1abb4192015-02-17 18:33:36 +00001044 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +00001045
David Brazdil1abb4192015-02-17 18:33:36 +00001046 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001047
1048 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1049};
1050
Calin Juravleacf735c2015-02-12 15:25:22 +00001051class ReferenceTypeInfo : ValueObject {
1052 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001053 typedef Handle<mirror::Class> TypeHandle;
1054
1055 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1056 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1057 if (type_handle->IsObjectClass()) {
1058 // Override the type handle to be consistent with the case when we get to
1059 // Top but don't have the Object class available. It avoids having to guess
1060 // what value the type_handle has when it's Top.
1061 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1062 } else {
1063 return ReferenceTypeInfo(type_handle, is_exact, false);
1064 }
1065 }
1066
1067 static ReferenceTypeInfo CreateTop(bool is_exact) {
1068 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001069 }
1070
1071 bool IsExact() const { return is_exact_; }
1072 bool IsTop() const { return is_top_; }
1073
1074 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1075
Calin Juravleb1498f62015-02-16 13:13:29 +00001076 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001077 if (IsTop()) {
1078 // Top (equivalent for java.lang.Object) is supertype of anything.
1079 return true;
1080 }
1081 if (rti.IsTop()) {
1082 // If we get here `this` is not Top() so it can't be a supertype.
1083 return false;
1084 }
1085 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1086 }
1087
1088 // Returns true if the type information provide the same amount of details.
1089 // Note that it does not mean that the instructions have the same actual type
1090 // (e.g. tops are equal but they can be the result of a merge).
1091 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1092 if (IsExact() != rti.IsExact()) {
1093 return false;
1094 }
1095 if (IsTop() && rti.IsTop()) {
1096 // `Top` means java.lang.Object, so the types are equivalent.
1097 return true;
1098 }
1099 if (IsTop() || rti.IsTop()) {
1100 // If only one is top or object than they are not equivalent.
1101 // NB: We need this extra check because the type_handle of `Top` is invalid
1102 // and we cannot inspect its reference.
1103 return false;
1104 }
1105
1106 // Finally check the types.
1107 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1108 }
1109
1110 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001111 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1112 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1113 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1114
Calin Juravleacf735c2015-02-12 15:25:22 +00001115 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001116 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001117 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001118 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001119 bool is_exact_;
1120 // A true value here means that the object type should be java.lang.Object.
1121 // We don't have access to the corresponding mirror object every time so this
1122 // flag acts as a substitute. When true, the TypeHandle refers to a null
1123 // pointer and should not be used.
1124 bool is_top_;
1125};
1126
1127std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1128
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001129class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001130 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001131 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001132 : previous_(nullptr),
1133 next_(nullptr),
1134 block_(nullptr),
1135 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001136 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001137 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001138 locations_(nullptr),
1139 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001140 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001141 side_effects_(side_effects),
1142 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001143
Dave Allison20dfc792014-06-16 20:44:29 -07001144 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001145
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001146#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001147 enum InstructionKind {
1148 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1149 };
1150#undef DECLARE_KIND
1151
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001152 HInstruction* GetNext() const { return next_; }
1153 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001154
Calin Juravle77520bc2015-01-12 18:45:46 +00001155 HInstruction* GetNextDisregardingMoves() const;
1156 HInstruction* GetPreviousDisregardingMoves() const;
1157
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001158 HBasicBlock* GetBlock() const { return block_; }
1159 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001160 bool IsInBlock() const { return block_ != nullptr; }
1161 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001162 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001163
Roland Levillain6b879dd2014-09-22 17:13:44 +01001164 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001165 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001166
1167 virtual void Accept(HGraphVisitor* visitor) = 0;
1168 virtual const char* DebugName() const = 0;
1169
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001170 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001171 void SetRawInputAt(size_t index, HInstruction* input) {
1172 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1173 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001174
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001175 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001176 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001177 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001178 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001179
Calin Juravle10e244f2015-01-26 18:54:32 +00001180 // Does not apply for all instructions, but having this at top level greatly
1181 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001182 virtual bool CanBeNull() const {
1183 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1184 return true;
1185 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001186
Calin Juravle641547a2015-04-21 22:08:51 +01001187 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1188 UNUSED(obj);
1189 return false;
1190 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001191
Calin Juravleacf735c2015-02-12 15:25:22 +00001192 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001193 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001194 reference_type_info_ = reference_type_info;
1195 }
1196
Calin Juravle61d544b2015-02-23 16:46:57 +00001197 ReferenceTypeInfo GetReferenceTypeInfo() const {
1198 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1199 return reference_type_info_;
1200 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001201
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001202 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001203 DCHECK(user != nullptr);
1204 HUseListNode<HInstruction*>* use =
1205 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1206 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001207 }
1208
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001209 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001210 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001211 HUseListNode<HEnvironment*>* env_use =
1212 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1213 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001214 }
1215
David Brazdil1abb4192015-02-17 18:33:36 +00001216 void RemoveAsUserOfInput(size_t input) {
1217 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1218 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1219 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001220
David Brazdil1abb4192015-02-17 18:33:36 +00001221 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1222 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001223
David Brazdiled596192015-01-23 10:39:45 +00001224 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1225 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001226 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001227 bool HasOnlyOneNonEnvironmentUse() const {
1228 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1229 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001230
Roland Levillain6c82d402014-10-13 16:10:27 +01001231 // Does this instruction strictly dominate `other_instruction`?
1232 // Returns false if this instruction and `other_instruction` are the same.
1233 // Aborts if this instruction and `other_instruction` are both phis.
1234 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001235
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001236 int GetId() const { return id_; }
1237 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001238
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001239 int GetSsaIndex() const { return ssa_index_; }
1240 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1241 bool HasSsaIndex() const { return ssa_index_ != -1; }
1242
1243 bool HasEnvironment() const { return environment_ != nullptr; }
1244 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001245 // Set the `environment_` field. Raw because this method does not
1246 // update the uses lists.
1247 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1248
1249 // Set the environment of this instruction, copying it from `environment`. While
1250 // copying, the uses lists are being updated.
1251 void CopyEnvironmentFrom(HEnvironment* environment) {
1252 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1253 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1254 environment_->CopyFrom(environment);
1255 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001256
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001257 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1258 HBasicBlock* block) {
1259 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1260 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1261 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
1262 }
1263
Nicolas Geoffray39468442014-09-02 15:17:15 +01001264 // Returns the number of entries in the environment. Typically, that is the
1265 // number of dex registers in a method. It could be more in case of inlining.
1266 size_t EnvironmentSize() const;
1267
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001268 LocationSummary* GetLocations() const { return locations_; }
1269 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001270
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001271 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001272 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001273
Alexandre Rames188d4312015-04-09 18:30:21 +01001274 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1275 // uses of this instruction by `other` are *not* updated.
1276 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1277 ReplaceWith(other);
1278 other->ReplaceInput(this, use_index);
1279 }
1280
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001281 // Move `this` instruction before `cursor`.
1282 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001283
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001284#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001285 bool Is##type() const { return (As##type() != nullptr); } \
1286 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001287 virtual H##type* As##type() { return nullptr; }
1288
1289 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1290#undef INSTRUCTION_TYPE_CHECK
1291
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001292 // Returns whether the instruction can be moved within the graph.
1293 virtual bool CanBeMoved() const { return false; }
1294
1295 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001296 virtual bool InstructionTypeEquals(HInstruction* other) const {
1297 UNUSED(other);
1298 return false;
1299 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001300
1301 // Returns whether any data encoded in the two instructions is equal.
1302 // This method does not look at the inputs. Both instructions must be
1303 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001304 virtual bool InstructionDataEquals(HInstruction* other) const {
1305 UNUSED(other);
1306 return false;
1307 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001308
1309 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001310 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001311 // 2) Their inputs are identical.
1312 bool Equals(HInstruction* other) const;
1313
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001314 virtual InstructionKind GetKind() const = 0;
1315
1316 virtual size_t ComputeHashCode() const {
1317 size_t result = GetKind();
1318 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1319 result = (result * 31) + InputAt(i)->GetId();
1320 }
1321 return result;
1322 }
1323
1324 SideEffects GetSideEffects() const { return side_effects_; }
1325
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001326 size_t GetLifetimePosition() const { return lifetime_position_; }
1327 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1328 LiveInterval* GetLiveInterval() const { return live_interval_; }
1329 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1330 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1331
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001332 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1333
1334 // Returns whether the code generation of the instruction will require to have access
1335 // to the current method. Such instructions are:
1336 // (1): Instructions that require an environment, as calling the runtime requires
1337 // to walk the stack and have the current method stored at a specific stack address.
1338 // (2): Object literals like classes and strings, that are loaded from the dex cache
1339 // fields of the current method.
1340 bool NeedsCurrentMethod() const {
1341 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1342 }
1343
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001344 virtual bool NeedsDexCache() const { return false; }
1345
David Brazdil1abb4192015-02-17 18:33:36 +00001346 protected:
1347 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1348 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1349
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001350 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001351 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1352
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001353 HInstruction* previous_;
1354 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001355 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001356
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001357 // An instruction gets an id when it is added to the graph.
1358 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001359 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001360 int id_;
1361
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001362 // When doing liveness analysis, instructions that have uses get an SSA index.
1363 int ssa_index_;
1364
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001365 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001366 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001367
1368 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001369 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001370
Nicolas Geoffray39468442014-09-02 15:17:15 +01001371 // The environment associated with this instruction. Not null if the instruction
1372 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001373 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001374
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001375 // Set by the code generator.
1376 LocationSummary* locations_;
1377
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001378 // Set by the liveness analysis.
1379 LiveInterval* live_interval_;
1380
1381 // Set by the liveness analysis, this is the position in a linear
1382 // order of blocks where this instruction's live interval start.
1383 size_t lifetime_position_;
1384
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001385 const SideEffects side_effects_;
1386
Calin Juravleacf735c2015-02-12 15:25:22 +00001387 // TODO: for primitive types this should be marked as invalid.
1388 ReferenceTypeInfo reference_type_info_;
1389
David Brazdil1abb4192015-02-17 18:33:36 +00001390 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001391 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001392 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001393 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001394 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001395
1396 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1397};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001398std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001399
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001400class HInputIterator : public ValueObject {
1401 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001402 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001403
1404 bool Done() const { return index_ == instruction_->InputCount(); }
1405 HInstruction* Current() const { return instruction_->InputAt(index_); }
1406 void Advance() { index_++; }
1407
1408 private:
1409 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001410 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001411
1412 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1413};
1414
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001415class HInstructionIterator : public ValueObject {
1416 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001417 explicit HInstructionIterator(const HInstructionList& instructions)
1418 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001419 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001420 }
1421
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001422 bool Done() const { return instruction_ == nullptr; }
1423 HInstruction* Current() const { return instruction_; }
1424 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001425 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001426 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001427 }
1428
1429 private:
1430 HInstruction* instruction_;
1431 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001432
1433 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001434};
1435
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001436class HBackwardInstructionIterator : public ValueObject {
1437 public:
1438 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1439 : instruction_(instructions.last_instruction_) {
1440 next_ = Done() ? nullptr : instruction_->GetPrevious();
1441 }
1442
1443 bool Done() const { return instruction_ == nullptr; }
1444 HInstruction* Current() const { return instruction_; }
1445 void Advance() {
1446 instruction_ = next_;
1447 next_ = Done() ? nullptr : instruction_->GetPrevious();
1448 }
1449
1450 private:
1451 HInstruction* instruction_;
1452 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001453
1454 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001455};
1456
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001457// An embedded container with N elements of type T. Used (with partial
1458// specialization for N=0) because embedded arrays cannot have size 0.
1459template<typename T, intptr_t N>
1460class EmbeddedArray {
1461 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001462 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001463
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001464 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001465
1466 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001467 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001468 return elements_[i];
1469 }
1470
1471 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001472 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001473 return elements_[i];
1474 }
1475
1476 const T& At(intptr_t i) const {
1477 return (*this)[i];
1478 }
1479
1480 void SetAt(intptr_t i, const T& val) {
1481 (*this)[i] = val;
1482 }
1483
1484 private:
1485 T elements_[N];
1486};
1487
1488template<typename T>
1489class EmbeddedArray<T, 0> {
1490 public:
1491 intptr_t length() const { return 0; }
1492 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001493 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001494 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001495 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001496 }
1497 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001498 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001499 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001500 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001501 }
1502};
1503
1504template<intptr_t N>
1505class HTemplateInstruction: public HInstruction {
1506 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001507 HTemplateInstruction<N>(SideEffects side_effects)
1508 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001509 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001510
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001511 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001512
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001513 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001514 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1515
1516 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1517 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001518 }
1519
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001520 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001521 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001522
1523 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001524};
1525
Dave Allison20dfc792014-06-16 20:44:29 -07001526template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001527class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001528 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001529 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1530 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001531 virtual ~HExpression() {}
1532
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001533 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001534
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001535 protected:
1536 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001537};
1538
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001539// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1540// instruction that branches to the exit block.
1541class HReturnVoid : public HTemplateInstruction<0> {
1542 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001543 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001544
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001545 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001546
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001547 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001548
1549 private:
1550 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1551};
1552
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001553// Represents dex's RETURN opcodes. A HReturn is a control flow
1554// instruction that branches to the exit block.
1555class HReturn : public HTemplateInstruction<1> {
1556 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001557 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001558 SetRawInputAt(0, value);
1559 }
1560
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001561 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001562
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001563 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001564
1565 private:
1566 DISALLOW_COPY_AND_ASSIGN(HReturn);
1567};
1568
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001569// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001570// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001571// exit block.
1572class HExit : public HTemplateInstruction<0> {
1573 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001574 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001575
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001576 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001577
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001578 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001579
1580 private:
1581 DISALLOW_COPY_AND_ASSIGN(HExit);
1582};
1583
1584// Jumps from one block to another.
1585class HGoto : public HTemplateInstruction<0> {
1586 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001587 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1588
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001589 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001590
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001591 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001592 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001593 }
1594
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001595 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001596
1597 private:
1598 DISALLOW_COPY_AND_ASSIGN(HGoto);
1599};
1600
Dave Allison20dfc792014-06-16 20:44:29 -07001601
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001602// Conditional branch. A block ending with an HIf instruction must have
1603// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001604class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001605 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001606 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001607 SetRawInputAt(0, input);
1608 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001609
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001610 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001611
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001612 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001613 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001614 }
1615
1616 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001617 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001618 }
1619
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001620 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001621
1622 private:
1623 DISALLOW_COPY_AND_ASSIGN(HIf);
1624};
1625
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001626// Deoptimize to interpreter, upon checking a condition.
1627class HDeoptimize : public HTemplateInstruction<1> {
1628 public:
1629 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1630 : HTemplateInstruction(SideEffects::None()),
1631 dex_pc_(dex_pc) {
1632 SetRawInputAt(0, cond);
1633 }
1634
1635 bool NeedsEnvironment() const OVERRIDE { return true; }
1636 bool CanThrow() const OVERRIDE { return true; }
1637 uint32_t GetDexPc() const { return dex_pc_; }
1638
1639 DECLARE_INSTRUCTION(Deoptimize);
1640
1641 private:
1642 uint32_t dex_pc_;
1643
1644 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1645};
1646
Roland Levillain88cb1752014-10-20 16:36:47 +01001647class HUnaryOperation : public HExpression<1> {
1648 public:
1649 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1650 : HExpression(result_type, SideEffects::None()) {
1651 SetRawInputAt(0, input);
1652 }
1653
1654 HInstruction* GetInput() const { return InputAt(0); }
1655 Primitive::Type GetResultType() const { return GetType(); }
1656
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001657 bool CanBeMoved() const OVERRIDE { return true; }
1658 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001659 UNUSED(other);
1660 return true;
1661 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001662
Roland Levillain9240d6a2014-10-20 16:47:04 +01001663 // Try to statically evaluate `operation` and return a HConstant
1664 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001665 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001666 HConstant* TryStaticEvaluation() const;
1667
1668 // Apply this operation to `x`.
1669 virtual int32_t Evaluate(int32_t x) const = 0;
1670 virtual int64_t Evaluate(int64_t x) const = 0;
1671
Roland Levillain88cb1752014-10-20 16:36:47 +01001672 DECLARE_INSTRUCTION(UnaryOperation);
1673
1674 private:
1675 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1676};
1677
Dave Allison20dfc792014-06-16 20:44:29 -07001678class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001679 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001680 HBinaryOperation(Primitive::Type result_type,
1681 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001682 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001683 SetRawInputAt(0, left);
1684 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001685 }
1686
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001687 HInstruction* GetLeft() const { return InputAt(0); }
1688 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001689 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001690
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001691 virtual bool IsCommutative() const { return false; }
1692
1693 // Put constant on the right.
1694 // Returns whether order is changed.
1695 bool OrderInputsWithConstantOnTheRight() {
1696 HInstruction* left = InputAt(0);
1697 HInstruction* right = InputAt(1);
1698 if (left->IsConstant() && !right->IsConstant()) {
1699 ReplaceInput(right, 0);
1700 ReplaceInput(left, 1);
1701 return true;
1702 }
1703 return false;
1704 }
1705
1706 // Order inputs by instruction id, but favor constant on the right side.
1707 // This helps GVN for commutative ops.
1708 void OrderInputs() {
1709 DCHECK(IsCommutative());
1710 HInstruction* left = InputAt(0);
1711 HInstruction* right = InputAt(1);
1712 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1713 return;
1714 }
1715 if (OrderInputsWithConstantOnTheRight()) {
1716 return;
1717 }
1718 // Order according to instruction id.
1719 if (left->GetId() > right->GetId()) {
1720 ReplaceInput(right, 0);
1721 ReplaceInput(left, 1);
1722 }
1723 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001724
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001725 bool CanBeMoved() const OVERRIDE { return true; }
1726 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001727 UNUSED(other);
1728 return true;
1729 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001730
Roland Levillain9240d6a2014-10-20 16:47:04 +01001731 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001732 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001733 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001734 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001735
1736 // Apply this operation to `x` and `y`.
1737 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1738 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1739
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001740 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001741 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001742 HConstant* GetConstantRight() const;
1743
1744 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001745 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001746 HInstruction* GetLeastConstantLeft() const;
1747
Roland Levillainccc07a92014-09-16 14:48:16 +01001748 DECLARE_INSTRUCTION(BinaryOperation);
1749
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001750 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001751 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1752};
1753
Dave Allison20dfc792014-06-16 20:44:29 -07001754class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001755 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001756 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001757 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1758 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001759
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001760 bool NeedsMaterialization() const { return needs_materialization_; }
1761 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001762
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001763 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001764 // `instruction`, and disregard moves in between.
1765 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001766
Dave Allison20dfc792014-06-16 20:44:29 -07001767 DECLARE_INSTRUCTION(Condition);
1768
1769 virtual IfCondition GetCondition() const = 0;
1770
1771 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001772 // For register allocation purposes, returns whether this instruction needs to be
1773 // materialized (that is, not just be in the processor flags).
1774 bool needs_materialization_;
1775
Dave Allison20dfc792014-06-16 20:44:29 -07001776 DISALLOW_COPY_AND_ASSIGN(HCondition);
1777};
1778
1779// Instruction to check if two inputs are equal to each other.
1780class HEqual : public HCondition {
1781 public:
1782 HEqual(HInstruction* first, HInstruction* second)
1783 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001784
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001785 bool IsCommutative() const OVERRIDE { return true; }
1786
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001787 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001788 return x == y ? 1 : 0;
1789 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001790 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001791 return x == y ? 1 : 0;
1792 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001793
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001794 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001795
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001796 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001797 return kCondEQ;
1798 }
1799
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001800 private:
1801 DISALLOW_COPY_AND_ASSIGN(HEqual);
1802};
1803
Dave Allison20dfc792014-06-16 20:44:29 -07001804class HNotEqual : public HCondition {
1805 public:
1806 HNotEqual(HInstruction* first, HInstruction* second)
1807 : HCondition(first, second) {}
1808
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001809 bool IsCommutative() const OVERRIDE { return true; }
1810
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001811 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001812 return x != y ? 1 : 0;
1813 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001814 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001815 return x != y ? 1 : 0;
1816 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001817
Dave Allison20dfc792014-06-16 20:44:29 -07001818 DECLARE_INSTRUCTION(NotEqual);
1819
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001820 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001821 return kCondNE;
1822 }
1823
1824 private:
1825 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1826};
1827
1828class HLessThan : public HCondition {
1829 public:
1830 HLessThan(HInstruction* first, HInstruction* second)
1831 : HCondition(first, second) {}
1832
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001833 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001834 return x < y ? 1 : 0;
1835 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001836 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001837 return x < y ? 1 : 0;
1838 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001839
Dave Allison20dfc792014-06-16 20:44:29 -07001840 DECLARE_INSTRUCTION(LessThan);
1841
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001842 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001843 return kCondLT;
1844 }
1845
1846 private:
1847 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1848};
1849
1850class HLessThanOrEqual : public HCondition {
1851 public:
1852 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1853 : HCondition(first, second) {}
1854
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001855 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001856 return x <= y ? 1 : 0;
1857 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001858 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001859 return x <= y ? 1 : 0;
1860 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001861
Dave Allison20dfc792014-06-16 20:44:29 -07001862 DECLARE_INSTRUCTION(LessThanOrEqual);
1863
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001864 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001865 return kCondLE;
1866 }
1867
1868 private:
1869 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1870};
1871
1872class HGreaterThan : public HCondition {
1873 public:
1874 HGreaterThan(HInstruction* first, HInstruction* second)
1875 : HCondition(first, second) {}
1876
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001877 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001878 return x > y ? 1 : 0;
1879 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001880 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001881 return x > y ? 1 : 0;
1882 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001883
Dave Allison20dfc792014-06-16 20:44:29 -07001884 DECLARE_INSTRUCTION(GreaterThan);
1885
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001886 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001887 return kCondGT;
1888 }
1889
1890 private:
1891 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1892};
1893
1894class HGreaterThanOrEqual : public HCondition {
1895 public:
1896 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1897 : HCondition(first, second) {}
1898
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001899 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001900 return x >= y ? 1 : 0;
1901 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001902 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001903 return x >= y ? 1 : 0;
1904 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001905
Dave Allison20dfc792014-06-16 20:44:29 -07001906 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1907
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001908 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001909 return kCondGE;
1910 }
1911
1912 private:
1913 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1914};
1915
1916
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001917// Instruction to check how two inputs compare to each other.
1918// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1919class HCompare : public HBinaryOperation {
1920 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001921 // The bias applies for floating point operations and indicates how NaN
1922 // comparisons are treated:
1923 enum Bias {
1924 kNoBias, // bias is not applicable (i.e. for long operation)
1925 kGtBias, // return 1 for NaN comparisons
1926 kLtBias, // return -1 for NaN comparisons
1927 };
1928
1929 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1930 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001931 DCHECK_EQ(type, first->GetType());
1932 DCHECK_EQ(type, second->GetType());
1933 }
1934
Calin Juravleddb7df22014-11-25 20:56:51 +00001935 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001936 return
1937 x == y ? 0 :
1938 x > y ? 1 :
1939 -1;
1940 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001941
1942 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001943 return
1944 x == y ? 0 :
1945 x > y ? 1 :
1946 -1;
1947 }
1948
Calin Juravleddb7df22014-11-25 20:56:51 +00001949 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1950 return bias_ == other->AsCompare()->bias_;
1951 }
1952
1953 bool IsGtBias() { return bias_ == kGtBias; }
1954
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001955 DECLARE_INSTRUCTION(Compare);
1956
1957 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001958 const Bias bias_;
1959
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001960 DISALLOW_COPY_AND_ASSIGN(HCompare);
1961};
1962
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001963// A local in the graph. Corresponds to a Dex register.
1964class HLocal : public HTemplateInstruction<0> {
1965 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001966 explicit HLocal(uint16_t reg_number)
1967 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001968
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001969 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001970
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001971 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001972
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001973 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001974 // The Dex register number.
1975 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001976
1977 DISALLOW_COPY_AND_ASSIGN(HLocal);
1978};
1979
1980// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001981class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001982 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001983 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001984 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001985 SetRawInputAt(0, local);
1986 }
1987
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001988 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1989
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001990 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001991
1992 private:
1993 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1994};
1995
1996// Store a value in a given local. This instruction has two inputs: the value
1997// and the local.
1998class HStoreLocal : public HTemplateInstruction<2> {
1999 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002000 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002001 SetRawInputAt(0, local);
2002 SetRawInputAt(1, value);
2003 }
2004
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002005 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2006
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002007 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002008
2009 private:
2010 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2011};
2012
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002013class HConstant : public HExpression<0> {
2014 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002015 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2016
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002017 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002018
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002019 virtual bool IsMinusOne() const { return false; }
2020 virtual bool IsZero() const { return false; }
2021 virtual bool IsOne() const { return false; }
2022
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002023 DECLARE_INSTRUCTION(Constant);
2024
2025 private:
2026 DISALLOW_COPY_AND_ASSIGN(HConstant);
2027};
2028
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002029class HFloatConstant : public HConstant {
2030 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002031 float GetValue() const { return value_; }
2032
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002033 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002034 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2035 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002036 }
2037
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002038 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002039
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002040 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002041 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2042 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002043 }
2044 bool IsZero() const OVERRIDE {
2045 return AsFloatConstant()->GetValue() == 0.0f;
2046 }
2047 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002048 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2049 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002050 }
2051
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002052 DECLARE_INSTRUCTION(FloatConstant);
2053
2054 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002055 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
2056
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002057 const float value_;
2058
Nicolas Geoffray067cae22015-04-26 16:43:00 +00002059 // Only the SsaBuilder can currently create floating-point constants. If we
2060 // ever need to create them later in the pipeline, we will have to handle them
2061 // the same way as integral constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002062 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002063 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2064};
2065
2066class HDoubleConstant : public HConstant {
2067 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002068 double GetValue() const { return value_; }
2069
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002070 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002071 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2072 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002073 }
2074
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002075 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002076
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002077 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002078 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2079 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002080 }
2081 bool IsZero() const OVERRIDE {
2082 return AsDoubleConstant()->GetValue() == 0.0;
2083 }
2084 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002085 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2086 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002087 }
2088
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002089 DECLARE_INSTRUCTION(DoubleConstant);
2090
2091 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002092 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
2093
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002094 const double value_;
2095
Nicolas Geoffray067cae22015-04-26 16:43:00 +00002096 // Only the SsaBuilder can currently create floating-point constants. If we
2097 // ever need to create them later in the pipeline, we will have to handle them
2098 // the same way as integral constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002099 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002100 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2101};
2102
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002103class HNullConstant : public HConstant {
2104 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002105 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2106 return true;
2107 }
2108
2109 size_t ComputeHashCode() const OVERRIDE { return 0; }
2110
2111 DECLARE_INSTRUCTION(NullConstant);
2112
2113 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002114 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2115
2116 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002117 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2118};
2119
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002120// Constants of the type int. Those can be from Dex instructions, or
2121// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002122class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002123 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002124 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002125
Calin Juravle61d544b2015-02-23 16:46:57 +00002126 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002127 return other->AsIntConstant()->value_ == value_;
2128 }
2129
Calin Juravle61d544b2015-02-23 16:46:57 +00002130 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2131
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002132 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2133 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2134 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2135
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002136 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002137
2138 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002139 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2140
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002141 const int32_t value_;
2142
David Brazdil8d5b8b22015-03-24 10:51:52 +00002143 friend class HGraph;
2144 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002145 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002146 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2147};
2148
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002149class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002150 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002151 int64_t GetValue() const { return value_; }
2152
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002153 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002154 return other->AsLongConstant()->value_ == value_;
2155 }
2156
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002157 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002158
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002159 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2160 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2161 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2162
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002163 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002164
2165 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002166 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2167
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002168 const int64_t value_;
2169
David Brazdil8d5b8b22015-03-24 10:51:52 +00002170 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002171 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2172};
2173
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002174enum class Intrinsics {
2175#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2176#include "intrinsics_list.h"
2177 kNone,
2178 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2179#undef INTRINSICS_LIST
2180#undef OPTIMIZING_INTRINSICS
2181};
2182std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2183
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002184class HInvoke : public HInstruction {
2185 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002186 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002187
2188 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2189 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002190 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002191
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002192 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002193 SetRawInputAt(index, argument);
2194 }
2195
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002196 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002197
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002198 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002199
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002200 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2201
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002202 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002203 return intrinsic_;
2204 }
2205
2206 void SetIntrinsic(Intrinsics intrinsic) {
2207 intrinsic_ = intrinsic;
2208 }
2209
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002210 DECLARE_INSTRUCTION(Invoke);
2211
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002212 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002213 HInvoke(ArenaAllocator* arena,
2214 uint32_t number_of_arguments,
2215 Primitive::Type return_type,
2216 uint32_t dex_pc,
2217 uint32_t dex_method_index)
2218 : HInstruction(SideEffects::All()),
2219 inputs_(arena, number_of_arguments),
2220 return_type_(return_type),
2221 dex_pc_(dex_pc),
2222 dex_method_index_(dex_method_index),
2223 intrinsic_(Intrinsics::kNone) {
2224 inputs_.SetSize(number_of_arguments);
2225 }
2226
David Brazdil1abb4192015-02-17 18:33:36 +00002227 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2228 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2229 inputs_.Put(index, input);
2230 }
2231
2232 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002233 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002234 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002235 const uint32_t dex_method_index_;
2236 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002237
2238 private:
2239 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2240};
2241
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002242class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002243 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002244 // Requirements of this method call regarding the class
2245 // initialization (clinit) check of its declaring class.
2246 enum class ClinitCheckRequirement {
2247 kNone, // Class already initialized.
2248 kExplicit, // Static call having explicit clinit check as last input.
2249 kImplicit, // Static call implicitly requiring a clinit check.
2250 };
2251
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002252 HInvokeStaticOrDirect(ArenaAllocator* arena,
2253 uint32_t number_of_arguments,
2254 Primitive::Type return_type,
2255 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002256 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002257 bool is_recursive,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002258 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002259 InvokeType invoke_type,
2260 ClinitCheckRequirement clinit_check_requirement)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002261 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002262 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002263 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002264 is_recursive_(is_recursive),
2265 clinit_check_requirement_(clinit_check_requirement) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002266
Calin Juravle641547a2015-04-21 22:08:51 +01002267 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2268 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002269 // We access the method via the dex cache so we can't do an implicit null check.
2270 // TODO: for intrinsics we can generate implicit null checks.
2271 return false;
2272 }
2273
Nicolas Geoffray79041292015-03-26 10:05:54 +00002274 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002275 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002276 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002277 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002278
Roland Levillain4c0eb422015-04-24 16:43:49 +01002279 // Is this instruction a call to a static method?
2280 bool IsStatic() const {
2281 return GetInvokeType() == kStatic;
2282 }
2283
Roland Levillain0379f822015-04-24 23:01:24 +01002284 // Remove the art::HClinitCheck or art::HLoadClass instruction as
2285 // last input (only relevant for static calls with explicit clinit
2286 // check).
2287 void RemoveClinitCheckOrLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002288 DCHECK(IsStaticWithExplicitClinitCheck());
2289 size_t last_input_index = InputCount() - 1;
2290 HInstruction* last_input = InputAt(last_input_index);
2291 DCHECK(last_input != nullptr);
Roland Levillain0379f822015-04-24 23:01:24 +01002292 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002293 RemoveAsUserOfInput(last_input_index);
2294 inputs_.DeleteAt(last_input_index);
2295 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2296 DCHECK(IsStaticWithImplicitClinitCheck());
2297 }
2298
2299 // Is this a call to a static method whose declaring class has an
2300 // explicit intialization check in the graph?
2301 bool IsStaticWithExplicitClinitCheck() const {
2302 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2303 }
2304
2305 // Is this a call to a static method whose declaring class has an
2306 // implicit intialization check requirement?
2307 bool IsStaticWithImplicitClinitCheck() const {
2308 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2309 }
2310
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002311 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002312
Roland Levillain4c0eb422015-04-24 16:43:49 +01002313 protected:
2314 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2315 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2316 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2317 HInstruction* input = input_record.GetInstruction();
2318 // `input` is the last input of a static invoke marked as having
2319 // an explicit clinit check. It must either be:
2320 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2321 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2322 DCHECK(input != nullptr);
2323 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2324 }
2325 return input_record;
2326 }
2327
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002328 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002329 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002330 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002331 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002332 ClinitCheckRequirement clinit_check_requirement_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002333
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002334 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002335};
2336
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002337class HInvokeVirtual : public HInvoke {
2338 public:
2339 HInvokeVirtual(ArenaAllocator* arena,
2340 uint32_t number_of_arguments,
2341 Primitive::Type return_type,
2342 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002343 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002344 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002345 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002346 vtable_index_(vtable_index) {}
2347
Calin Juravle641547a2015-04-21 22:08:51 +01002348 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002349 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002350 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002351 }
2352
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002353 uint32_t GetVTableIndex() const { return vtable_index_; }
2354
2355 DECLARE_INSTRUCTION(InvokeVirtual);
2356
2357 private:
2358 const uint32_t vtable_index_;
2359
2360 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2361};
2362
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002363class HInvokeInterface : public HInvoke {
2364 public:
2365 HInvokeInterface(ArenaAllocator* arena,
2366 uint32_t number_of_arguments,
2367 Primitive::Type return_type,
2368 uint32_t dex_pc,
2369 uint32_t dex_method_index,
2370 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002371 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002372 imt_index_(imt_index) {}
2373
Calin Juravle641547a2015-04-21 22:08:51 +01002374 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002375 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002376 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002377 }
2378
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002379 uint32_t GetImtIndex() const { return imt_index_; }
2380 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2381
2382 DECLARE_INSTRUCTION(InvokeInterface);
2383
2384 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002385 const uint32_t imt_index_;
2386
2387 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2388};
2389
Dave Allison20dfc792014-06-16 20:44:29 -07002390class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002391 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002392 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002393 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2394 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002395 type_index_(type_index),
2396 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002397
2398 uint32_t GetDexPc() const { return dex_pc_; }
2399 uint16_t GetTypeIndex() const { return type_index_; }
2400
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002401 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002402 bool NeedsEnvironment() const OVERRIDE { return true; }
2403 // It may throw when called on:
2404 // - interfaces
2405 // - abstract/innaccessible/unknown classes
2406 // TODO: optimize when possible.
2407 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002408
Calin Juravle10e244f2015-01-26 18:54:32 +00002409 bool CanBeNull() const OVERRIDE { return false; }
2410
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002411 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2412
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002413 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002414
2415 private:
2416 const uint32_t dex_pc_;
2417 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002418 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002419
2420 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2421};
2422
Roland Levillain88cb1752014-10-20 16:36:47 +01002423class HNeg : public HUnaryOperation {
2424 public:
2425 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2426 : HUnaryOperation(result_type, input) {}
2427
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002428 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2429 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002430
Roland Levillain88cb1752014-10-20 16:36:47 +01002431 DECLARE_INSTRUCTION(Neg);
2432
2433 private:
2434 DISALLOW_COPY_AND_ASSIGN(HNeg);
2435};
2436
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002437class HNewArray : public HExpression<1> {
2438 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002439 HNewArray(HInstruction* length,
2440 uint32_t dex_pc,
2441 uint16_t type_index,
2442 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002443 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2444 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002445 type_index_(type_index),
2446 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002447 SetRawInputAt(0, length);
2448 }
2449
2450 uint32_t GetDexPc() const { return dex_pc_; }
2451 uint16_t GetTypeIndex() const { return type_index_; }
2452
2453 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002454 bool NeedsEnvironment() const OVERRIDE { return true; }
2455
Mingyao Yang0c365e62015-03-31 15:09:29 -07002456 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2457 bool CanThrow() const OVERRIDE { return true; }
2458
Calin Juravle10e244f2015-01-26 18:54:32 +00002459 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002460
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002461 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2462
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002463 DECLARE_INSTRUCTION(NewArray);
2464
2465 private:
2466 const uint32_t dex_pc_;
2467 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002468 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002469
2470 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2471};
2472
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002473class HAdd : public HBinaryOperation {
2474 public:
2475 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2476 : HBinaryOperation(result_type, left, right) {}
2477
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002478 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002479
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 Geoffrayd8ee7372014-03-28 15:43:40 +00002487 DECLARE_INSTRUCTION(Add);
2488
2489 private:
2490 DISALLOW_COPY_AND_ASSIGN(HAdd);
2491};
2492
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002493class HSub : public HBinaryOperation {
2494 public:
2495 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2496 : HBinaryOperation(result_type, left, right) {}
2497
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002498 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002499 return x - y;
2500 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002501 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002502 return x - y;
2503 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002504
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002505 DECLARE_INSTRUCTION(Sub);
2506
2507 private:
2508 DISALLOW_COPY_AND_ASSIGN(HSub);
2509};
2510
Calin Juravle34bacdf2014-10-07 20:23:36 +01002511class HMul : public HBinaryOperation {
2512 public:
2513 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2514 : HBinaryOperation(result_type, left, right) {}
2515
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002516 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002517
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002518 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2519 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002520
2521 DECLARE_INSTRUCTION(Mul);
2522
2523 private:
2524 DISALLOW_COPY_AND_ASSIGN(HMul);
2525};
2526
Calin Juravle7c4954d2014-10-28 16:57:40 +00002527class HDiv : public HBinaryOperation {
2528 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002529 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2530 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002531
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002532 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002533 // Our graph structure ensures we never have 0 for `y` during constant folding.
2534 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002535 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002536 return (y == -1) ? -x : x / y;
2537 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002538
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002539 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002540 DCHECK_NE(y, 0);
2541 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2542 return (y == -1) ? -x : x / y;
2543 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002544
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002545 uint32_t GetDexPc() const { return dex_pc_; }
2546
Calin Juravle7c4954d2014-10-28 16:57:40 +00002547 DECLARE_INSTRUCTION(Div);
2548
2549 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002550 const uint32_t dex_pc_;
2551
Calin Juravle7c4954d2014-10-28 16:57:40 +00002552 DISALLOW_COPY_AND_ASSIGN(HDiv);
2553};
2554
Calin Juravlebacfec32014-11-14 15:54:36 +00002555class HRem : public HBinaryOperation {
2556 public:
2557 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2558 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2559
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002560 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002561 DCHECK_NE(y, 0);
2562 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2563 return (y == -1) ? 0 : x % y;
2564 }
2565
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002566 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002567 DCHECK_NE(y, 0);
2568 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2569 return (y == -1) ? 0 : x % y;
2570 }
2571
2572 uint32_t GetDexPc() const { return dex_pc_; }
2573
2574 DECLARE_INSTRUCTION(Rem);
2575
2576 private:
2577 const uint32_t dex_pc_;
2578
2579 DISALLOW_COPY_AND_ASSIGN(HRem);
2580};
2581
Calin Juravled0d48522014-11-04 16:40:20 +00002582class HDivZeroCheck : public HExpression<1> {
2583 public:
2584 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2585 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2586 SetRawInputAt(0, value);
2587 }
2588
2589 bool CanBeMoved() const OVERRIDE { return true; }
2590
2591 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2592 UNUSED(other);
2593 return true;
2594 }
2595
2596 bool NeedsEnvironment() const OVERRIDE { return true; }
2597 bool CanThrow() const OVERRIDE { return true; }
2598
2599 uint32_t GetDexPc() const { return dex_pc_; }
2600
2601 DECLARE_INSTRUCTION(DivZeroCheck);
2602
2603 private:
2604 const uint32_t dex_pc_;
2605
2606 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2607};
2608
Calin Juravle9aec02f2014-11-18 23:06:35 +00002609class HShl : public HBinaryOperation {
2610 public:
2611 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2612 : HBinaryOperation(result_type, left, right) {}
2613
2614 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2615 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2616
2617 DECLARE_INSTRUCTION(Shl);
2618
2619 private:
2620 DISALLOW_COPY_AND_ASSIGN(HShl);
2621};
2622
2623class HShr : public HBinaryOperation {
2624 public:
2625 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2626 : HBinaryOperation(result_type, left, right) {}
2627
2628 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2629 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2630
2631 DECLARE_INSTRUCTION(Shr);
2632
2633 private:
2634 DISALLOW_COPY_AND_ASSIGN(HShr);
2635};
2636
2637class HUShr : public HBinaryOperation {
2638 public:
2639 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2640 : HBinaryOperation(result_type, left, right) {}
2641
2642 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2643 uint32_t ux = static_cast<uint32_t>(x);
2644 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2645 return static_cast<int32_t>(ux >> uy);
2646 }
2647
2648 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2649 uint64_t ux = static_cast<uint64_t>(x);
2650 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2651 return static_cast<int64_t>(ux >> uy);
2652 }
2653
2654 DECLARE_INSTRUCTION(UShr);
2655
2656 private:
2657 DISALLOW_COPY_AND_ASSIGN(HUShr);
2658};
2659
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002660class HAnd : public HBinaryOperation {
2661 public:
2662 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2663 : HBinaryOperation(result_type, left, right) {}
2664
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002665 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002666
2667 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2668 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2669
2670 DECLARE_INSTRUCTION(And);
2671
2672 private:
2673 DISALLOW_COPY_AND_ASSIGN(HAnd);
2674};
2675
2676class HOr : public HBinaryOperation {
2677 public:
2678 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2679 : HBinaryOperation(result_type, left, right) {}
2680
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002681 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002682
2683 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2684 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2685
2686 DECLARE_INSTRUCTION(Or);
2687
2688 private:
2689 DISALLOW_COPY_AND_ASSIGN(HOr);
2690};
2691
2692class HXor : public HBinaryOperation {
2693 public:
2694 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2695 : HBinaryOperation(result_type, left, right) {}
2696
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002697 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002698
2699 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2700 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2701
2702 DECLARE_INSTRUCTION(Xor);
2703
2704 private:
2705 DISALLOW_COPY_AND_ASSIGN(HXor);
2706};
2707
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002708// The value of a parameter in this method. Its location depends on
2709// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002710class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002711 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002712 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2713 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002714
2715 uint8_t GetIndex() const { return index_; }
2716
Calin Juravle10e244f2015-01-26 18:54:32 +00002717 bool CanBeNull() const OVERRIDE { return !is_this_; }
2718
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002719 DECLARE_INSTRUCTION(ParameterValue);
2720
2721 private:
2722 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002723 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002724 const uint8_t index_;
2725
Calin Juravle10e244f2015-01-26 18:54:32 +00002726 // Whether or not the parameter value corresponds to 'this' argument.
2727 const bool is_this_;
2728
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002729 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2730};
2731
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002732class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002733 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002734 explicit HNot(Primitive::Type result_type, HInstruction* input)
2735 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002736
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002737 bool CanBeMoved() const OVERRIDE { return true; }
2738 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002739 UNUSED(other);
2740 return true;
2741 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002742
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002743 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2744 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002745
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002746 DECLARE_INSTRUCTION(Not);
2747
2748 private:
2749 DISALLOW_COPY_AND_ASSIGN(HNot);
2750};
2751
David Brazdil66d126e2015-04-03 16:02:44 +01002752class HBooleanNot : public HUnaryOperation {
2753 public:
2754 explicit HBooleanNot(HInstruction* input)
2755 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2756
2757 bool CanBeMoved() const OVERRIDE { return true; }
2758 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2759 UNUSED(other);
2760 return true;
2761 }
2762
2763 int32_t Evaluate(int32_t x) const OVERRIDE {
2764 DCHECK(IsUint<1>(x));
2765 return !x;
2766 }
2767
2768 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2769 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2770 UNREACHABLE();
2771 }
2772
2773 DECLARE_INSTRUCTION(BooleanNot);
2774
2775 private:
2776 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2777};
2778
Roland Levillaindff1f282014-11-05 14:15:05 +00002779class HTypeConversion : public HExpression<1> {
2780 public:
2781 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002782 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2783 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002784 SetRawInputAt(0, input);
2785 DCHECK_NE(input->GetType(), result_type);
2786 }
2787
2788 HInstruction* GetInput() const { return InputAt(0); }
2789 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2790 Primitive::Type GetResultType() const { return GetType(); }
2791
Roland Levillain624279f2014-12-04 11:54:28 +00002792 // Required by the x86 and ARM code generators when producing calls
2793 // to the runtime.
2794 uint32_t GetDexPc() const { return dex_pc_; }
2795
Roland Levillaindff1f282014-11-05 14:15:05 +00002796 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002797 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002798
2799 DECLARE_INSTRUCTION(TypeConversion);
2800
2801 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002802 const uint32_t dex_pc_;
2803
Roland Levillaindff1f282014-11-05 14:15:05 +00002804 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2805};
2806
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002807static constexpr uint32_t kNoRegNumber = -1;
2808
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002809class HPhi : public HInstruction {
2810 public:
2811 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002812 : HInstruction(SideEffects::None()),
2813 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002814 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002815 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002816 is_live_(false),
2817 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002818 inputs_.SetSize(number_of_inputs);
2819 }
2820
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002821 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2822 static Primitive::Type ToPhiType(Primitive::Type type) {
2823 switch (type) {
2824 case Primitive::kPrimBoolean:
2825 case Primitive::kPrimByte:
2826 case Primitive::kPrimShort:
2827 case Primitive::kPrimChar:
2828 return Primitive::kPrimInt;
2829 default:
2830 return type;
2831 }
2832 }
2833
Calin Juravle10e244f2015-01-26 18:54:32 +00002834 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002835
2836 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01002837 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002838
Calin Juravle10e244f2015-01-26 18:54:32 +00002839 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002840 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002841
Calin Juravle10e244f2015-01-26 18:54:32 +00002842 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2843 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2844
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002845 uint32_t GetRegNumber() const { return reg_number_; }
2846
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002847 void SetDead() { is_live_ = false; }
2848 void SetLive() { is_live_ = true; }
2849 bool IsDead() const { return !is_live_; }
2850 bool IsLive() const { return is_live_; }
2851
Calin Juravlea4f88312015-04-16 12:57:19 +01002852 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2853 // An equivalent phi is a phi having the same dex register and type.
2854 // It assumes that phis with the same dex register are adjacent.
2855 HPhi* GetNextEquivalentPhiWithSameType() {
2856 HInstruction* next = GetNext();
2857 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2858 if (next->GetType() == GetType()) {
2859 return next->AsPhi();
2860 }
2861 next = next->GetNext();
2862 }
2863 return nullptr;
2864 }
2865
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002866 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002867
David Brazdil1abb4192015-02-17 18:33:36 +00002868 protected:
2869 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2870
2871 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2872 inputs_.Put(index, input);
2873 }
2874
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002875 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002876 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002877 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002878 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002879 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002880 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002881
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002882 DISALLOW_COPY_AND_ASSIGN(HPhi);
2883};
2884
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002885class HNullCheck : public HExpression<1> {
2886 public:
2887 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002888 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002889 SetRawInputAt(0, value);
2890 }
2891
Calin Juravle10e244f2015-01-26 18:54:32 +00002892 bool CanBeMoved() const OVERRIDE { return true; }
2893 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002894 UNUSED(other);
2895 return true;
2896 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002897
Calin Juravle10e244f2015-01-26 18:54:32 +00002898 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002899
Calin Juravle10e244f2015-01-26 18:54:32 +00002900 bool CanThrow() const OVERRIDE { return true; }
2901
2902 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002903
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002904 uint32_t GetDexPc() const { return dex_pc_; }
2905
2906 DECLARE_INSTRUCTION(NullCheck);
2907
2908 private:
2909 const uint32_t dex_pc_;
2910
2911 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2912};
2913
2914class FieldInfo : public ValueObject {
2915 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002916 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2917 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002918
2919 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002920 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002921 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002922
2923 private:
2924 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002925 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002926 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002927};
2928
2929class HInstanceFieldGet : public HExpression<1> {
2930 public:
2931 HInstanceFieldGet(HInstruction* value,
2932 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002933 MemberOffset field_offset,
2934 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002935 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002936 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002937 SetRawInputAt(0, value);
2938 }
2939
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002940 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002941
2942 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2943 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2944 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002945 }
2946
Calin Juravle641547a2015-04-21 22:08:51 +01002947 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2948 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00002949 }
2950
2951 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002952 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2953 }
2954
Calin Juravle52c48962014-12-16 17:02:57 +00002955 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002956 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002957 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002958 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002959
2960 DECLARE_INSTRUCTION(InstanceFieldGet);
2961
2962 private:
2963 const FieldInfo field_info_;
2964
2965 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2966};
2967
2968class HInstanceFieldSet : public HTemplateInstruction<2> {
2969 public:
2970 HInstanceFieldSet(HInstruction* object,
2971 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002972 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002973 MemberOffset field_offset,
2974 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002975 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002976 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002977 SetRawInputAt(0, object);
2978 SetRawInputAt(1, value);
2979 }
2980
Calin Juravle641547a2015-04-21 22:08:51 +01002981 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2982 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00002983 }
2984
Calin Juravle52c48962014-12-16 17:02:57 +00002985 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002986 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002987 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002988 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002989 HInstruction* GetValue() const { return InputAt(1); }
2990
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002991 DECLARE_INSTRUCTION(InstanceFieldSet);
2992
2993 private:
2994 const FieldInfo field_info_;
2995
2996 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2997};
2998
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002999class HArrayGet : public HExpression<2> {
3000 public:
3001 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003002 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003003 SetRawInputAt(0, array);
3004 SetRawInputAt(1, index);
3005 }
3006
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003007 bool CanBeMoved() const OVERRIDE { return true; }
3008 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003009 UNUSED(other);
3010 return true;
3011 }
Calin Juravle641547a2015-04-21 22:08:51 +01003012 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3013 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003014 // TODO: We can be smarter here.
3015 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3016 // which generates the implicit null check. There are cases when these can be removed
3017 // to produce better code. If we ever add optimizations to do so we should allow an
3018 // implicit check here (as long as the address falls in the first page).
3019 return false;
3020 }
3021
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003022 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003023
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003024 HInstruction* GetArray() const { return InputAt(0); }
3025 HInstruction* GetIndex() const { return InputAt(1); }
3026
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003027 DECLARE_INSTRUCTION(ArrayGet);
3028
3029 private:
3030 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3031};
3032
3033class HArraySet : public HTemplateInstruction<3> {
3034 public:
3035 HArraySet(HInstruction* array,
3036 HInstruction* index,
3037 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003038 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003039 uint32_t dex_pc)
3040 : HTemplateInstruction(SideEffects::ChangesSomething()),
3041 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003042 expected_component_type_(expected_component_type),
3043 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003044 SetRawInputAt(0, array);
3045 SetRawInputAt(1, index);
3046 SetRawInputAt(2, value);
3047 }
3048
Calin Juravle77520bc2015-01-12 18:45:46 +00003049 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003050 // We currently always call a runtime method to catch array store
3051 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003052 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003053 }
3054
Calin Juravle641547a2015-04-21 22:08:51 +01003055 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3056 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003057 // TODO: Same as for ArrayGet.
3058 return false;
3059 }
3060
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003061 void ClearNeedsTypeCheck() {
3062 needs_type_check_ = false;
3063 }
3064
3065 bool NeedsTypeCheck() const { return needs_type_check_; }
3066
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003067 uint32_t GetDexPc() const { return dex_pc_; }
3068
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003069 HInstruction* GetArray() const { return InputAt(0); }
3070 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003071 HInstruction* GetValue() const { return InputAt(2); }
3072
3073 Primitive::Type GetComponentType() const {
3074 // The Dex format does not type floating point index operations. Since the
3075 // `expected_component_type_` is set during building and can therefore not
3076 // be correct, we also check what is the value type. If it is a floating
3077 // point type, we must use that type.
3078 Primitive::Type value_type = GetValue()->GetType();
3079 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3080 ? value_type
3081 : expected_component_type_;
3082 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003083
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003084 DECLARE_INSTRUCTION(ArraySet);
3085
3086 private:
3087 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003088 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003089 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003090
3091 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3092};
3093
3094class HArrayLength : public HExpression<1> {
3095 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003096 explicit HArrayLength(HInstruction* array)
3097 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3098 // Note that arrays do not change length, so the instruction does not
3099 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003100 SetRawInputAt(0, array);
3101 }
3102
Calin Juravle77520bc2015-01-12 18:45:46 +00003103 bool CanBeMoved() const OVERRIDE { return true; }
3104 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003105 UNUSED(other);
3106 return true;
3107 }
Calin Juravle641547a2015-04-21 22:08:51 +01003108 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3109 return obj == InputAt(0);
3110 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003111
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003112 DECLARE_INSTRUCTION(ArrayLength);
3113
3114 private:
3115 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3116};
3117
3118class HBoundsCheck : public HExpression<2> {
3119 public:
3120 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003121 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003122 DCHECK(index->GetType() == Primitive::kPrimInt);
3123 SetRawInputAt(0, index);
3124 SetRawInputAt(1, length);
3125 }
3126
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003127 bool CanBeMoved() const OVERRIDE { return true; }
3128 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003129 UNUSED(other);
3130 return true;
3131 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003132
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003133 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003134
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003135 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003136
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003137 uint32_t GetDexPc() const { return dex_pc_; }
3138
3139 DECLARE_INSTRUCTION(BoundsCheck);
3140
3141 private:
3142 const uint32_t dex_pc_;
3143
3144 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3145};
3146
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003147/**
3148 * Some DEX instructions are folded into multiple HInstructions that need
3149 * to stay live until the last HInstruction. This class
3150 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003151 * HInstruction stays live. `index` represents the stack location index of the
3152 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003153 */
3154class HTemporary : public HTemplateInstruction<0> {
3155 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003156 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003157
3158 size_t GetIndex() const { return index_; }
3159
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003160 Primitive::Type GetType() const OVERRIDE {
3161 // The previous instruction is the one that will be stored in the temporary location.
3162 DCHECK(GetPrevious() != nullptr);
3163 return GetPrevious()->GetType();
3164 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003165
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003166 DECLARE_INSTRUCTION(Temporary);
3167
3168 private:
3169 const size_t index_;
3170
3171 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3172};
3173
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003174class HSuspendCheck : public HTemplateInstruction<0> {
3175 public:
3176 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01003177 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003178
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003179 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003180 return true;
3181 }
3182
3183 uint32_t GetDexPc() const { return dex_pc_; }
3184
3185 DECLARE_INSTRUCTION(SuspendCheck);
3186
3187 private:
3188 const uint32_t dex_pc_;
3189
3190 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3191};
3192
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003193/**
3194 * Instruction to load a Class object.
3195 */
3196class HLoadClass : public HExpression<0> {
3197 public:
3198 HLoadClass(uint16_t type_index,
3199 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003200 uint32_t dex_pc)
3201 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3202 type_index_(type_index),
3203 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003204 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003205 generate_clinit_check_(false),
3206 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003207
3208 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003209
3210 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3211 return other->AsLoadClass()->type_index_ == type_index_;
3212 }
3213
3214 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3215
3216 uint32_t GetDexPc() const { return dex_pc_; }
3217 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003218 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003219
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003220 bool NeedsEnvironment() const OVERRIDE {
3221 // Will call runtime and load the class if the class is not loaded yet.
3222 // TODO: finer grain decision.
3223 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003224 }
3225
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003226 bool MustGenerateClinitCheck() const {
3227 return generate_clinit_check_;
3228 }
3229
3230 void SetMustGenerateClinitCheck() {
3231 generate_clinit_check_ = true;
3232 }
3233
3234 bool CanCallRuntime() const {
3235 return MustGenerateClinitCheck() || !is_referrers_class_;
3236 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003237
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003238 bool CanThrow() const OVERRIDE {
3239 // May call runtime and and therefore can throw.
3240 // TODO: finer grain decision.
3241 return !is_referrers_class_;
3242 }
3243
Calin Juravleacf735c2015-02-12 15:25:22 +00003244 ReferenceTypeInfo GetLoadedClassRTI() {
3245 return loaded_class_rti_;
3246 }
3247
3248 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3249 // Make sure we only set exact types (the loaded class should never be merged).
3250 DCHECK(rti.IsExact());
3251 loaded_class_rti_ = rti;
3252 }
3253
3254 bool IsResolved() {
3255 return loaded_class_rti_.IsExact();
3256 }
3257
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003258 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3259
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003260 DECLARE_INSTRUCTION(LoadClass);
3261
3262 private:
3263 const uint16_t type_index_;
3264 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003265 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003266 // Whether this instruction must generate the initialization check.
3267 // Used for code generation.
3268 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003269
Calin Juravleacf735c2015-02-12 15:25:22 +00003270 ReferenceTypeInfo loaded_class_rti_;
3271
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003272 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3273};
3274
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003275class HLoadString : public HExpression<0> {
3276 public:
3277 HLoadString(uint32_t string_index, uint32_t dex_pc)
3278 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3279 string_index_(string_index),
3280 dex_pc_(dex_pc) {}
3281
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003282 bool CanBeMoved() const OVERRIDE { return true; }
3283
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003284 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3285 return other->AsLoadString()->string_index_ == string_index_;
3286 }
3287
3288 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3289
3290 uint32_t GetDexPc() const { return dex_pc_; }
3291 uint32_t GetStringIndex() const { return string_index_; }
3292
3293 // TODO: Can we deopt or debug when we resolve a string?
3294 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003295 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003296
3297 DECLARE_INSTRUCTION(LoadString);
3298
3299 private:
3300 const uint32_t string_index_;
3301 const uint32_t dex_pc_;
3302
3303 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3304};
3305
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003306/**
3307 * Performs an initialization check on its Class object input.
3308 */
3309class HClinitCheck : public HExpression<1> {
3310 public:
3311 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003312 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003313 dex_pc_(dex_pc) {
3314 SetRawInputAt(0, constant);
3315 }
3316
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003317 bool CanBeMoved() const OVERRIDE { return true; }
3318 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3319 UNUSED(other);
3320 return true;
3321 }
3322
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003323 bool NeedsEnvironment() const OVERRIDE {
3324 // May call runtime to initialize the class.
3325 return true;
3326 }
3327
3328 uint32_t GetDexPc() const { return dex_pc_; }
3329
3330 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3331
3332 DECLARE_INSTRUCTION(ClinitCheck);
3333
3334 private:
3335 const uint32_t dex_pc_;
3336
3337 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3338};
3339
3340class HStaticFieldGet : public HExpression<1> {
3341 public:
3342 HStaticFieldGet(HInstruction* cls,
3343 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003344 MemberOffset field_offset,
3345 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003346 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003347 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003348 SetRawInputAt(0, cls);
3349 }
3350
Calin Juravle52c48962014-12-16 17:02:57 +00003351
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003352 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003353
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003354 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003355 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3356 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003357 }
3358
3359 size_t ComputeHashCode() const OVERRIDE {
3360 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3361 }
3362
Calin Juravle52c48962014-12-16 17:02:57 +00003363 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003364 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3365 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003366 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003367
3368 DECLARE_INSTRUCTION(StaticFieldGet);
3369
3370 private:
3371 const FieldInfo field_info_;
3372
3373 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3374};
3375
3376class HStaticFieldSet : public HTemplateInstruction<2> {
3377 public:
3378 HStaticFieldSet(HInstruction* cls,
3379 HInstruction* value,
3380 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003381 MemberOffset field_offset,
3382 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003383 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003384 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003385 SetRawInputAt(0, cls);
3386 SetRawInputAt(1, value);
3387 }
3388
Calin Juravle52c48962014-12-16 17:02:57 +00003389 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003390 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3391 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003392 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003393
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003394 HInstruction* GetValue() const { return InputAt(1); }
3395
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003396 DECLARE_INSTRUCTION(StaticFieldSet);
3397
3398 private:
3399 const FieldInfo field_info_;
3400
3401 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3402};
3403
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003404// Implement the move-exception DEX instruction.
3405class HLoadException : public HExpression<0> {
3406 public:
3407 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3408
3409 DECLARE_INSTRUCTION(LoadException);
3410
3411 private:
3412 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3413};
3414
3415class HThrow : public HTemplateInstruction<1> {
3416 public:
3417 HThrow(HInstruction* exception, uint32_t dex_pc)
3418 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3419 SetRawInputAt(0, exception);
3420 }
3421
3422 bool IsControlFlow() const OVERRIDE { return true; }
3423
3424 bool NeedsEnvironment() const OVERRIDE { return true; }
3425
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003426 bool CanThrow() const OVERRIDE { return true; }
3427
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003428 uint32_t GetDexPc() const { return dex_pc_; }
3429
3430 DECLARE_INSTRUCTION(Throw);
3431
3432 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003433 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003434
3435 DISALLOW_COPY_AND_ASSIGN(HThrow);
3436};
3437
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003438class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003439 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003440 HInstanceOf(HInstruction* object,
3441 HLoadClass* constant,
3442 bool class_is_final,
3443 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003444 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3445 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003446 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003447 dex_pc_(dex_pc) {
3448 SetRawInputAt(0, object);
3449 SetRawInputAt(1, constant);
3450 }
3451
3452 bool CanBeMoved() const OVERRIDE { return true; }
3453
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003454 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003455 return true;
3456 }
3457
3458 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003459 return false;
3460 }
3461
3462 uint32_t GetDexPc() const { return dex_pc_; }
3463
3464 bool IsClassFinal() const { return class_is_final_; }
3465
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003466 // Used only in code generation.
3467 bool MustDoNullCheck() const { return must_do_null_check_; }
3468 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3469
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003470 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003471
3472 private:
3473 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003474 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003475 const uint32_t dex_pc_;
3476
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003477 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3478};
3479
Calin Juravleb1498f62015-02-16 13:13:29 +00003480class HBoundType : public HExpression<1> {
3481 public:
3482 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3483 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3484 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003485 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003486 SetRawInputAt(0, input);
3487 }
3488
3489 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3490
3491 bool CanBeNull() const OVERRIDE {
3492 // `null instanceof ClassX` always return false so we can't be null.
3493 return false;
3494 }
3495
3496 DECLARE_INSTRUCTION(BoundType);
3497
3498 private:
3499 // Encodes the most upper class that this instruction can have. In other words
3500 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3501 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3502 const ReferenceTypeInfo bound_type_;
3503
3504 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3505};
3506
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003507class HCheckCast : public HTemplateInstruction<2> {
3508 public:
3509 HCheckCast(HInstruction* object,
3510 HLoadClass* constant,
3511 bool class_is_final,
3512 uint32_t dex_pc)
3513 : HTemplateInstruction(SideEffects::None()),
3514 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003515 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003516 dex_pc_(dex_pc) {
3517 SetRawInputAt(0, object);
3518 SetRawInputAt(1, constant);
3519 }
3520
3521 bool CanBeMoved() const OVERRIDE { return true; }
3522
3523 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3524 return true;
3525 }
3526
3527 bool NeedsEnvironment() const OVERRIDE {
3528 // Instruction may throw a CheckCastError.
3529 return true;
3530 }
3531
3532 bool CanThrow() const OVERRIDE { return true; }
3533
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003534 bool MustDoNullCheck() const { return must_do_null_check_; }
3535 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3536
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003537 uint32_t GetDexPc() const { return dex_pc_; }
3538
3539 bool IsClassFinal() const { return class_is_final_; }
3540
3541 DECLARE_INSTRUCTION(CheckCast);
3542
3543 private:
3544 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003545 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003546 const uint32_t dex_pc_;
3547
3548 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003549};
3550
Calin Juravle27df7582015-04-17 19:12:31 +01003551class HMemoryBarrier : public HTemplateInstruction<0> {
3552 public:
3553 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3554 : HTemplateInstruction(SideEffects::None()),
3555 barrier_kind_(barrier_kind) {}
3556
3557 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3558
3559 DECLARE_INSTRUCTION(MemoryBarrier);
3560
3561 private:
3562 const MemBarrierKind barrier_kind_;
3563
3564 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3565};
3566
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003567class HMonitorOperation : public HTemplateInstruction<1> {
3568 public:
3569 enum OperationKind {
3570 kEnter,
3571 kExit,
3572 };
3573
3574 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3575 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3576 SetRawInputAt(0, object);
3577 }
3578
3579 // Instruction may throw a Java exception, so we need an environment.
3580 bool NeedsEnvironment() const OVERRIDE { return true; }
3581 bool CanThrow() const OVERRIDE { return true; }
3582
3583 uint32_t GetDexPc() const { return dex_pc_; }
3584
3585 bool IsEnter() const { return kind_ == kEnter; }
3586
3587 DECLARE_INSTRUCTION(MonitorOperation);
3588
Calin Juravle52c48962014-12-16 17:02:57 +00003589 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003590 const OperationKind kind_;
3591 const uint32_t dex_pc_;
3592
3593 private:
3594 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3595};
3596
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003597class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003598 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003599 MoveOperands(Location source,
3600 Location destination,
3601 Primitive::Type type,
3602 HInstruction* instruction)
3603 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003604
3605 Location GetSource() const { return source_; }
3606 Location GetDestination() const { return destination_; }
3607
3608 void SetSource(Location value) { source_ = value; }
3609 void SetDestination(Location value) { destination_ = value; }
3610
3611 // The parallel move resolver marks moves as "in-progress" by clearing the
3612 // destination (but not the source).
3613 Location MarkPending() {
3614 DCHECK(!IsPending());
3615 Location dest = destination_;
3616 destination_ = Location::NoLocation();
3617 return dest;
3618 }
3619
3620 void ClearPending(Location dest) {
3621 DCHECK(IsPending());
3622 destination_ = dest;
3623 }
3624
3625 bool IsPending() const {
3626 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3627 return destination_.IsInvalid() && !source_.IsInvalid();
3628 }
3629
3630 // True if this blocks a move from the given location.
3631 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003632 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003633 }
3634
3635 // A move is redundant if it's been eliminated, if its source and
3636 // destination are the same, or if its destination is unneeded.
3637 bool IsRedundant() const {
3638 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3639 }
3640
3641 // We clear both operands to indicate move that's been eliminated.
3642 void Eliminate() {
3643 source_ = destination_ = Location::NoLocation();
3644 }
3645
3646 bool IsEliminated() const {
3647 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3648 return source_.IsInvalid();
3649 }
3650
Nicolas Geoffray90218252015-04-15 11:56:51 +01003651 bool Is64BitMove() const {
3652 return Primitive::Is64BitType(type_);
3653 }
3654
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003655 HInstruction* GetInstruction() const { return instruction_; }
3656
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003657 private:
3658 Location source_;
3659 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003660 // The type this move is for.
3661 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003662 // The instruction this move is assocatied with. Null when this move is
3663 // for moving an input in the expected locations of user (including a phi user).
3664 // This is only used in debug mode, to ensure we do not connect interval siblings
3665 // in the same parallel move.
3666 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003667};
3668
3669static constexpr size_t kDefaultNumberOfMoves = 4;
3670
3671class HParallelMove : public HTemplateInstruction<0> {
3672 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003673 explicit HParallelMove(ArenaAllocator* arena)
3674 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003675
Nicolas Geoffray90218252015-04-15 11:56:51 +01003676 void AddMove(Location source,
3677 Location destination,
3678 Primitive::Type type,
3679 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003680 DCHECK(source.IsValid());
3681 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003682 if (kIsDebugBuild) {
3683 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003684 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003685 if (moves_.Get(i).GetInstruction() == instruction) {
3686 // Special case the situation where the move is for the spill slot
3687 // of the instruction.
3688 if ((GetPrevious() == instruction)
3689 || ((GetPrevious() == nullptr)
3690 && instruction->IsPhi()
3691 && instruction->GetBlock() == GetBlock())) {
3692 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3693 << "Doing parallel moves for the same instruction.";
3694 } else {
3695 DCHECK(false) << "Doing parallel moves for the same instruction.";
3696 }
3697 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003698 }
3699 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003700 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003701 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3702 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003703 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003704 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003705 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003706 }
3707
3708 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003709 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003710 }
3711
3712 size_t NumMoves() const { return moves_.Size(); }
3713
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003714 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003715
3716 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003717 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003718
3719 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3720};
3721
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003722class HGraphVisitor : public ValueObject {
3723 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003724 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3725 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003726
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003727 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003728 virtual void VisitBasicBlock(HBasicBlock* block);
3729
Roland Levillain633021e2014-10-01 14:12:25 +01003730 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003731 void VisitInsertionOrder();
3732
Roland Levillain633021e2014-10-01 14:12:25 +01003733 // Visit the graph following dominator tree reverse post-order.
3734 void VisitReversePostOrder();
3735
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003736 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003737
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003738 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003739#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003740 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3741
3742 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3743
3744#undef DECLARE_VISIT_INSTRUCTION
3745
3746 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003747 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003748
3749 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3750};
3751
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003752class HGraphDelegateVisitor : public HGraphVisitor {
3753 public:
3754 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3755 virtual ~HGraphDelegateVisitor() {}
3756
3757 // Visit functions that delegate to to super class.
3758#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003759 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003760
3761 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3762
3763#undef DECLARE_VISIT_INSTRUCTION
3764
3765 private:
3766 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3767};
3768
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003769class HInsertionOrderIterator : public ValueObject {
3770 public:
3771 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3772
3773 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3774 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3775 void Advance() { ++index_; }
3776
3777 private:
3778 const HGraph& graph_;
3779 size_t index_;
3780
3781 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3782};
3783
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003784class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003785 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003786 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3787 // Check that reverse post order of the graph has been built.
3788 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3789 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003790
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003791 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3792 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003793 void Advance() { ++index_; }
3794
3795 private:
3796 const HGraph& graph_;
3797 size_t index_;
3798
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003799 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003800};
3801
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003802class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003803 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003804 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003805 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3806 // Check that reverse post order of the graph has been built.
3807 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3808 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003809
3810 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003811 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003812 void Advance() { --index_; }
3813
3814 private:
3815 const HGraph& graph_;
3816 size_t index_;
3817
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003818 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003819};
3820
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003821class HLinearPostOrderIterator : public ValueObject {
3822 public:
3823 explicit HLinearPostOrderIterator(const HGraph& graph)
3824 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3825
3826 bool Done() const { return index_ == 0; }
3827
3828 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3829
3830 void Advance() {
3831 --index_;
3832 DCHECK_GE(index_, 0U);
3833 }
3834
3835 private:
3836 const GrowableArray<HBasicBlock*>& order_;
3837 size_t index_;
3838
3839 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3840};
3841
3842class HLinearOrderIterator : public ValueObject {
3843 public:
3844 explicit HLinearOrderIterator(const HGraph& graph)
3845 : order_(graph.GetLinearOrder()), index_(0) {}
3846
3847 bool Done() const { return index_ == order_.Size(); }
3848 HBasicBlock* Current() const { return order_.Get(index_); }
3849 void Advance() { ++index_; }
3850
3851 private:
3852 const GrowableArray<HBasicBlock*>& order_;
3853 size_t index_;
3854
3855 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3856};
3857
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003858// Iterator over the blocks that art part of the loop. Includes blocks part
3859// of an inner loop. The order in which the blocks are iterated is on their
3860// block id.
3861class HBlocksInLoopIterator : public ValueObject {
3862 public:
3863 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3864 : blocks_in_loop_(info.GetBlocks()),
3865 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3866 index_(0) {
3867 if (!blocks_in_loop_.IsBitSet(index_)) {
3868 Advance();
3869 }
3870 }
3871
3872 bool Done() const { return index_ == blocks_.Size(); }
3873 HBasicBlock* Current() const { return blocks_.Get(index_); }
3874 void Advance() {
3875 ++index_;
3876 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3877 if (blocks_in_loop_.IsBitSet(index_)) {
3878 break;
3879 }
3880 }
3881 }
3882
3883 private:
3884 const BitVector& blocks_in_loop_;
3885 const GrowableArray<HBasicBlock*>& blocks_;
3886 size_t index_;
3887
3888 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3889};
3890
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003891inline int64_t Int64FromConstant(HConstant* constant) {
3892 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3893 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3894 : constant->AsLongConstant()->GetValue();
3895}
3896
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003897} // namespace art
3898
3899#endif // ART_COMPILER_OPTIMIZING_NODES_H_