blob: 55639d43766f7e44778260a9d69f2f1ca409b09d [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
Mathieu Chartierb666f482015-02-18 14:33:14 -080020#include "base/arena_object.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000021#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000022#include "handle.h"
23#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000024#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010025#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000026#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010027#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000029#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "utils/growable_array.h"
31
32namespace art {
33
David Brazdil1abb4192015-02-17 18:33:36 +000034class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010036class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000038class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000039class HInvoke;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040class HGraphVisitor;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000041class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010042class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010043class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010044class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000045class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046
47static const int kDefaultNumberOfBlocks = 8;
48static const int kDefaultNumberOfSuccessors = 2;
49static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010050static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000051static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
Calin Juravle9aec02f2014-11-18 23:06:35 +000053static constexpr uint32_t kMaxIntShiftValue = 0x1f;
54static constexpr uint64_t kMaxLongShiftValue = 0x3f;
55
Dave Allison20dfc792014-06-16 20:44:29 -070056enum IfCondition {
57 kCondEQ,
58 kCondNE,
59 kCondLT,
60 kCondLE,
61 kCondGT,
62 kCondGE,
63};
64
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010065class HInstructionList {
66 public:
67 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
68
69 void AddInstruction(HInstruction* instruction);
70 void RemoveInstruction(HInstruction* instruction);
71
Roland Levillain6b469232014-09-25 10:10:38 +010072 // Return true if this list contains `instruction`.
73 bool Contains(HInstruction* instruction) const;
74
Roland Levillainccc07a92014-09-16 14:48:16 +010075 // Return true if `instruction1` is found before `instruction2` in
76 // this instruction list and false otherwise. Abort if none
77 // of these instructions is found.
78 bool FoundBefore(const HInstruction* instruction1,
79 const HInstruction* instruction2) const;
80
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000081 bool IsEmpty() const { return first_instruction_ == nullptr; }
82 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
83
84 // Update the block of all instructions to be `block`.
85 void SetBlockOfInstructions(HBasicBlock* block) const;
86
87 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
88 void Add(const HInstructionList& instruction_list);
89
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010090 private:
91 HInstruction* first_instruction_;
92 HInstruction* last_instruction_;
93
94 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000095 friend class HGraph;
96 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010097 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010098 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010099
100 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
101};
102
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000103// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700104class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000105 public:
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000106 HGraph(ArenaAllocator* arena, bool debuggable = false, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000107 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000108 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100109 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700110 entry_block_(nullptr),
111 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100112 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100113 number_of_vregs_(0),
114 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000115 temporaries_vreg_slots_(0),
Mingyao Yange4335eb2015-03-02 15:14:13 -0800116 has_array_accesses_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000117 debuggable_(debuggable),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000118 current_instruction_id_(start_instruction_id) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000119
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000120 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100121 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100122 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000123
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000124 HBasicBlock* GetEntryBlock() const { return entry_block_; }
125 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000126
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000127 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
128 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000129
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000130 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100131
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000132 // Try building the SSA form of this graph, with dominance computation and loop
133 // recognition. Returns whether it was successful in doing all these steps.
134 bool TryBuildingSsa() {
135 BuildDominatorTree();
136 TransformToSsa();
137 return AnalyzeNaturalLoops();
138 }
139
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000140 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000141 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100142 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000143
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000144 // Analyze all natural loops in this graph. Returns false if one
145 // loop is not natural, that is the header does not dominate the
146 // back edge.
147 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100148
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000149 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
150 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
151
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100152 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
153 void SimplifyLoop(HBasicBlock* header);
154
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000155 int32_t GetNextInstructionId() {
156 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000157 return current_instruction_id_++;
158 }
159
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 int32_t GetCurrentInstructionId() const {
161 return current_instruction_id_;
162 }
163
164 void SetCurrentInstructionId(int32_t id) {
165 current_instruction_id_ = id;
166 }
167
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100168 uint16_t GetMaximumNumberOfOutVRegs() const {
169 return maximum_number_of_out_vregs_;
170 }
171
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000172 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
173 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100174 }
175
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000176 void UpdateTemporariesVRegSlots(size_t slots) {
177 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100178 }
179
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000180 size_t GetTemporariesVRegSlots() const {
181 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100182 }
183
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100184 void SetNumberOfVRegs(uint16_t number_of_vregs) {
185 number_of_vregs_ = number_of_vregs;
186 }
187
188 uint16_t GetNumberOfVRegs() const {
189 return number_of_vregs_;
190 }
191
192 void SetNumberOfInVRegs(uint16_t value) {
193 number_of_in_vregs_ = value;
194 }
195
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100196 uint16_t GetNumberOfLocalVRegs() const {
197 return number_of_vregs_ - number_of_in_vregs_;
198 }
199
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100200 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
201 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100202 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100203
Mingyao Yange4335eb2015-03-02 15:14:13 -0800204 bool HasArrayAccesses() const {
205 return has_array_accesses_;
206 }
207
208 void SetHasArrayAccesses(bool value) {
209 has_array_accesses_ = value;
210 }
211
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000212 bool IsDebuggable() const { return debuggable_; }
213
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000214 HNullConstant* GetNullConstant();
215
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000216 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000217 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
218 void VisitBlockForDominatorTree(HBasicBlock* block,
219 HBasicBlock* predecessor,
220 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100221 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000222 void VisitBlockForBackEdges(HBasicBlock* block,
223 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100224 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000225 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000226 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
227
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000228 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000229
230 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000231 GrowableArray<HBasicBlock*> blocks_;
232
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100233 // List of blocks to perform a reverse post order tree traversal.
234 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000235
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000236 HBasicBlock* entry_block_;
237 HBasicBlock* exit_block_;
238
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100239 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100240 uint16_t maximum_number_of_out_vregs_;
241
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100242 // The number of virtual registers in this method. Contains the parameters.
243 uint16_t number_of_vregs_;
244
245 // The number of virtual registers used by parameters of this method.
246 uint16_t number_of_in_vregs_;
247
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000248 // Number of vreg size slots that the temporaries use (used in baseline compiler).
249 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100250
Mingyao Yange4335eb2015-03-02 15:14:13 -0800251 // Has array accesses. We can totally skip BCE if it's false.
252 bool has_array_accesses_;
253
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000254 // Indicates whether the graph should be compiled in a way that
255 // ensures full debuggability. If false, we can apply more
256 // aggressive optimizations that may limit the level of debugging.
257 const bool debuggable_;
258
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000259 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000260 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000261
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000262 // Cached null constant that might be created when building SSA form.
263 HNullConstant* cached_null_constant_;
264
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000265 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000266 DISALLOW_COPY_AND_ASSIGN(HGraph);
267};
268
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700269class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270 public:
271 HLoopInformation(HBasicBlock* header, HGraph* graph)
272 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100273 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100274 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100275 // Make bit vector growable, as the number of blocks may change.
276 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100277
278 HBasicBlock* GetHeader() const {
279 return header_;
280 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000281
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000282 void SetHeader(HBasicBlock* block) {
283 header_ = block;
284 }
285
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100286 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
287 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
288 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
289
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000290 void AddBackEdge(HBasicBlock* back_edge) {
291 back_edges_.Add(back_edge);
292 }
293
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100294 void RemoveBackEdge(HBasicBlock* back_edge) {
295 back_edges_.Delete(back_edge);
296 }
297
298 bool IsBackEdge(HBasicBlock* block) {
299 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
300 if (back_edges_.Get(i) == block) return true;
301 }
302 return false;
303 }
304
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000305 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000306 return back_edges_.Size();
307 }
308
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100309 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100310
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100311 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
312 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100313 }
314
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100315 void ClearBackEdges() {
316 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100317 }
318
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100319 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
320 // that is the header dominates the back edge.
321 bool Populate();
322
323 // Returns whether this loop information contains `block`.
324 // Note that this loop information *must* be populated before entering this function.
325 bool Contains(const HBasicBlock& block) const;
326
327 // Returns whether this loop information is an inner loop of `other`.
328 // Note that `other` *must* be populated before entering this function.
329 bool IsIn(const HLoopInformation& other) const;
330
331 const ArenaBitVector& GetBlocks() const { return blocks_; }
332
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000333 void Add(HBasicBlock* block);
334
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000335 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100336 // Internal recursive implementation of `Populate`.
337 void PopulateRecursive(HBasicBlock* block);
338
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000339 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100340 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000341 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100342 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000343
344 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
345};
346
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100347static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100348static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100349
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000350// A block in a method. Contains the list of instructions represented
351// as a double linked list. Each block knows its predecessors and
352// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100353
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700354class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100356 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000357 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000358 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
359 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000360 loop_information_(nullptr),
361 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100362 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100363 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100364 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100365 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000366 lifetime_end_(kNoLifetime),
367 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000368
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100369 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
370 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000371 }
372
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100373 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
374 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000375 }
376
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100377 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
378 return dominated_blocks_;
379 }
380
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100381 bool IsEntryBlock() const {
382 return graph_->GetEntryBlock() == this;
383 }
384
385 bool IsExitBlock() const {
386 return graph_->GetExitBlock() == this;
387 }
388
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000389 void AddBackEdge(HBasicBlock* back_edge) {
390 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000391 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000392 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100393 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000394 loop_information_->AddBackEdge(back_edge);
395 }
396
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000397 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000398 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000399
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000400 int GetBlockId() const { return block_id_; }
401 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000402
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000403 HBasicBlock* GetDominator() const { return dominator_; }
404 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100405 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000406 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
407 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
408 if (dominated_blocks_.Get(i) == existing) {
409 dominated_blocks_.Put(i, new_block);
410 return;
411 }
412 }
413 LOG(FATAL) << "Unreachable";
414 UNREACHABLE();
415 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000416
417 int NumberOfBackEdges() const {
418 return loop_information_ == nullptr
419 ? 0
420 : loop_information_->NumberOfBackEdges();
421 }
422
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100423 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
424 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100425 const HInstructionList& GetInstructions() const { return instructions_; }
426 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100427 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000428
429 void AddSuccessor(HBasicBlock* block) {
430 successors_.Add(block);
431 block->predecessors_.Add(this);
432 }
433
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100434 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
435 size_t successor_index = GetSuccessorIndexOf(existing);
436 DCHECK_NE(successor_index, static_cast<size_t>(-1));
437 existing->RemovePredecessor(this);
438 new_block->predecessors_.Add(this);
439 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000440 }
441
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000442 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
443 size_t predecessor_index = GetPredecessorIndexOf(existing);
444 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
445 existing->RemoveSuccessor(this);
446 new_block->successors_.Add(this);
447 predecessors_.Put(predecessor_index, new_block);
448 }
449
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100450 void RemovePredecessor(HBasicBlock* block) {
451 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100452 }
453
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000454 void RemoveSuccessor(HBasicBlock* block) {
455 successors_.Delete(block);
456 }
457
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100458 void ClearAllPredecessors() {
459 predecessors_.Reset();
460 }
461
462 void AddPredecessor(HBasicBlock* block) {
463 predecessors_.Add(block);
464 block->successors_.Add(this);
465 }
466
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100467 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100468 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100469 HBasicBlock* temp = predecessors_.Get(0);
470 predecessors_.Put(0, predecessors_.Get(1));
471 predecessors_.Put(1, temp);
472 }
473
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100474 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
475 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
476 if (predecessors_.Get(i) == predecessor) {
477 return i;
478 }
479 }
480 return -1;
481 }
482
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100483 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
484 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
485 if (successors_.Get(i) == successor) {
486 return i;
487 }
488 }
489 return -1;
490 }
491
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000492 // Split the block into two blocks just after `cursor`. Returns the newly
493 // created block. Note that this method just updates raw block information,
494 // like predecessors, successors, dominators, and instruction list. It does not
495 // update the graph, reverse post order, loop information, nor make sure the
496 // blocks are consistent (for example ending with a control flow instruction).
497 HBasicBlock* SplitAfter(HInstruction* cursor);
498
499 // Merge `other` at the end of `this`. Successors and dominated blocks of
500 // `other` are changed to be successors and dominated blocks of `this`. Note
501 // that this method does not update the graph, reverse post order, loop
502 // information, nor make sure the blocks are consistent (for example ending
503 // with a control flow instruction).
504 void MergeWith(HBasicBlock* other);
505
506 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
507 // of `this` are moved to `other`.
508 // Note that this method does not update the graph, reverse post order, loop
509 // information, nor make sure the blocks are consistent (for example ending
510 void ReplaceWith(HBasicBlock* other);
511
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000512 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100513 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100514 // Replace instruction `initial` with `replacement` within this block.
515 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
516 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100517 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100518 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000519 // RemoveInstruction and RemovePhi delete a given instruction from the respective
520 // instruction list. With 'ensure_safety' set to true, it verifies that the
521 // instruction is not in use and removes it from the use lists of its inputs.
522 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
523 void RemovePhi(HPhi* phi, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100524
525 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100526 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100527 }
528
Roland Levillain6b879dd2014-09-22 17:13:44 +0100529 bool IsLoopPreHeaderFirstPredecessor() const {
530 DCHECK(IsLoopHeader());
531 DCHECK(!GetPredecessors().IsEmpty());
532 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
533 }
534
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100535 HLoopInformation* GetLoopInformation() const {
536 return loop_information_;
537 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000538
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000539 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100540 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000541 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100542 void SetInLoop(HLoopInformation* info) {
543 if (IsLoopHeader()) {
544 // Nothing to do. This just means `info` is an outer loop.
545 } else if (loop_information_ == nullptr) {
546 loop_information_ = info;
547 } else if (loop_information_->Contains(*info->GetHeader())) {
548 // Block is currently part of an outer loop. Make it part of this inner loop.
549 // Note that a non loop header having a loop information means this loop information
550 // has already been populated
551 loop_information_ = info;
552 } else {
553 // Block is part of an inner loop. Do not update the loop information.
554 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
555 // at this point, because this method is being called while populating `info`.
556 }
557 }
558
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000559 // Raw update of the loop information.
560 void SetLoopInformation(HLoopInformation* info) {
561 loop_information_ = info;
562 }
563
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100564 bool IsInLoop() const { return loop_information_ != nullptr; }
565
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100566 // Returns wheter this block dominates the blocked passed as parameter.
567 bool Dominates(HBasicBlock* block) const;
568
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100569 size_t GetLifetimeStart() const { return lifetime_start_; }
570 size_t GetLifetimeEnd() const { return lifetime_end_; }
571
572 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
573 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
574
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100575 uint32_t GetDexPc() const { return dex_pc_; }
576
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000577 bool IsCatchBlock() const { return is_catch_block_; }
578 void SetIsCatchBlock() { is_catch_block_ = true; }
579
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000580 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000581 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000582 GrowableArray<HBasicBlock*> predecessors_;
583 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100584 HInstructionList instructions_;
585 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000586 HLoopInformation* loop_information_;
587 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100588 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000589 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100590 // The dex program counter of the first instruction of this block.
591 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100592 size_t lifetime_start_;
593 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000594 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000595
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000596 friend class HGraph;
597 friend class HInstruction;
598
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000599 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
600};
601
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100602#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
603 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000604 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000605 M(ArrayGet, Instruction) \
606 M(ArrayLength, Instruction) \
607 M(ArraySet, Instruction) \
608 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000609 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000610 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100611 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000612 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100613 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000614 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000615 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000616 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100617 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000618 M(Exit, Instruction) \
619 M(FloatConstant, Constant) \
620 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100621 M(GreaterThan, Condition) \
622 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100623 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000624 M(InstanceFieldGet, Instruction) \
625 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000626 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100627 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000628 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000629 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100630 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000631 M(LessThan, Condition) \
632 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000633 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000634 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100635 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000636 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100637 M(Local, Instruction) \
638 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000639 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000640 M(Mul, BinaryOperation) \
641 M(Neg, UnaryOperation) \
642 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100643 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100644 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000645 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000646 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000647 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000648 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100649 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000650 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100651 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000652 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100653 M(Return, Instruction) \
654 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000655 M(Shl, BinaryOperation) \
656 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100657 M(StaticFieldGet, Instruction) \
658 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100659 M(StoreLocal, Instruction) \
660 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100661 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000662 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000663 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000664 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000665 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000666 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000667
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100668#define FOR_EACH_INSTRUCTION(M) \
669 FOR_EACH_CONCRETE_INSTRUCTION(M) \
670 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100671 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100672 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100673 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700674
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100675#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000676FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
677#undef FORWARD_DECLARATION
678
Roland Levillainccc07a92014-09-16 14:48:16 +0100679#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000680 InstructionKind GetKind() const OVERRIDE { return k##type; } \
681 const char* DebugName() const OVERRIDE { return #type; } \
682 const H##type* As##type() const OVERRIDE { return this; } \
683 H##type* As##type() OVERRIDE { return this; } \
684 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100685 return other->Is##type(); \
686 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000687 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000688
David Brazdiled596192015-01-23 10:39:45 +0000689template <typename T> class HUseList;
690
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100691template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700692class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000693 public:
David Brazdiled596192015-01-23 10:39:45 +0000694 HUseListNode* GetPrevious() const { return prev_; }
695 HUseListNode* GetNext() const { return next_; }
696 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100697 size_t GetIndex() const { return index_; }
698
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000699 private:
David Brazdiled596192015-01-23 10:39:45 +0000700 HUseListNode(T user, size_t index)
701 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
702
703 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100704 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000705 HUseListNode<T>* prev_;
706 HUseListNode<T>* next_;
707
708 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000709
710 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
711};
712
David Brazdiled596192015-01-23 10:39:45 +0000713template <typename T>
714class HUseList : public ValueObject {
715 public:
716 HUseList() : first_(nullptr) {}
717
718 void Clear() {
719 first_ = nullptr;
720 }
721
722 // Adds a new entry at the beginning of the use list and returns
723 // the newly created node.
724 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000725 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000726 if (IsEmpty()) {
727 first_ = new_node;
728 } else {
729 first_->prev_ = new_node;
730 new_node->next_ = first_;
731 first_ = new_node;
732 }
733 return new_node;
734 }
735
736 HUseListNode<T>* GetFirst() const {
737 return first_;
738 }
739
740 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000741 DCHECK(node != nullptr);
742 DCHECK(Contains(node));
743
David Brazdiled596192015-01-23 10:39:45 +0000744 if (node->prev_ != nullptr) {
745 node->prev_->next_ = node->next_;
746 }
747 if (node->next_ != nullptr) {
748 node->next_->prev_ = node->prev_;
749 }
750 if (node == first_) {
751 first_ = node->next_;
752 }
753 }
754
David Brazdil1abb4192015-02-17 18:33:36 +0000755 bool Contains(const HUseListNode<T>* node) const {
756 if (node == nullptr) {
757 return false;
758 }
759 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
760 if (current == node) {
761 return true;
762 }
763 }
764 return false;
765 }
766
David Brazdiled596192015-01-23 10:39:45 +0000767 bool IsEmpty() const {
768 return first_ == nullptr;
769 }
770
771 bool HasOnlyOneUse() const {
772 return first_ != nullptr && first_->next_ == nullptr;
773 }
774
775 private:
776 HUseListNode<T>* first_;
777};
778
779template<typename T>
780class HUseIterator : public ValueObject {
781 public:
782 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
783
784 bool Done() const { return current_ == nullptr; }
785
786 void Advance() {
787 DCHECK(!Done());
788 current_ = current_->GetNext();
789 }
790
791 HUseListNode<T>* Current() const {
792 DCHECK(!Done());
793 return current_;
794 }
795
796 private:
797 HUseListNode<T>* current_;
798
799 friend class HValue;
800};
801
David Brazdil1abb4192015-02-17 18:33:36 +0000802// This class is used by HEnvironment and HInstruction classes to record the
803// instructions they use and pointers to the corresponding HUseListNodes kept
804// by the used instructions.
805template <typename T>
806class HUserRecord : public ValueObject {
807 public:
808 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
809 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
810
811 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
812 : instruction_(old_record.instruction_), use_node_(use_node) {
813 DCHECK(instruction_ != nullptr);
814 DCHECK(use_node_ != nullptr);
815 DCHECK(old_record.use_node_ == nullptr);
816 }
817
818 HInstruction* GetInstruction() const { return instruction_; }
819 HUseListNode<T>* GetUseNode() const { return use_node_; }
820
821 private:
822 // Instruction used by the user.
823 HInstruction* instruction_;
824
825 // Corresponding entry in the use list kept by 'instruction_'.
826 HUseListNode<T>* use_node_;
827};
828
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100829// Represents the side effects an instruction may have.
830class SideEffects : public ValueObject {
831 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100832 SideEffects() : flags_(0) {}
833
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100834 static SideEffects None() {
835 return SideEffects(0);
836 }
837
838 static SideEffects All() {
839 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
840 }
841
842 static SideEffects ChangesSomething() {
843 return SideEffects((1 << kFlagChangesCount) - 1);
844 }
845
846 static SideEffects DependsOnSomething() {
847 int count = kFlagDependsOnCount - kFlagChangesCount;
848 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
849 }
850
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100851 SideEffects Union(SideEffects other) const {
852 return SideEffects(flags_ | other.flags_);
853 }
854
Roland Levillain72bceff2014-09-15 18:29:00 +0100855 bool HasSideEffects() const {
856 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
857 return (flags_ & all_bits_set) != 0;
858 }
859
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100860 bool HasAllSideEffects() const {
861 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
862 return all_bits_set == (flags_ & all_bits_set);
863 }
864
865 bool DependsOn(SideEffects other) const {
866 size_t depends_flags = other.ComputeDependsFlags();
867 return (flags_ & depends_flags) != 0;
868 }
869
870 bool HasDependencies() const {
871 int count = kFlagDependsOnCount - kFlagChangesCount;
872 size_t all_bits_set = (1 << count) - 1;
873 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
874 }
875
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100876 private:
877 static constexpr int kFlagChangesSomething = 0;
878 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
879
880 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
881 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
882
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100883 explicit SideEffects(size_t flags) : flags_(flags) {}
884
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100885 size_t ComputeDependsFlags() const {
886 return flags_ << kFlagChangesCount;
887 }
888
889 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100890};
891
David Brazdiled596192015-01-23 10:39:45 +0000892// A HEnvironment object contains the values of virtual registers at a given location.
893class HEnvironment : public ArenaObject<kArenaAllocMisc> {
894 public:
895 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
896 : vregs_(arena, number_of_vregs) {
897 vregs_.SetSize(number_of_vregs);
898 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000899 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000900 }
901 }
902
903 void CopyFrom(HEnvironment* env);
904
905 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +0000906 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +0000907 }
908
909 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +0000910 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +0000911 }
912
David Brazdil1abb4192015-02-17 18:33:36 +0000913 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +0000914
915 size_t Size() const { return vregs_.Size(); }
916
917 private:
David Brazdil1abb4192015-02-17 18:33:36 +0000918 // Record instructions' use entries of this environment for constant-time removal.
919 // It should only be called by HInstruction when a new environment use is added.
920 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
921 DCHECK(env_use->GetUser() == this);
922 size_t index = env_use->GetIndex();
923 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
924 }
David Brazdiled596192015-01-23 10:39:45 +0000925
David Brazdil1abb4192015-02-17 18:33:36 +0000926 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +0000927
David Brazdil1abb4192015-02-17 18:33:36 +0000928 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +0000929
930 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
931};
932
Calin Juravleacf735c2015-02-12 15:25:22 +0000933class ReferenceTypeInfo : ValueObject {
934 public:
Calin Juravleb1498f62015-02-16 13:13:29 +0000935 typedef Handle<mirror::Class> TypeHandle;
936
937 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
938 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
939 if (type_handle->IsObjectClass()) {
940 // Override the type handle to be consistent with the case when we get to
941 // Top but don't have the Object class available. It avoids having to guess
942 // what value the type_handle has when it's Top.
943 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
944 } else {
945 return ReferenceTypeInfo(type_handle, is_exact, false);
946 }
947 }
948
949 static ReferenceTypeInfo CreateTop(bool is_exact) {
950 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +0000951 }
952
953 bool IsExact() const { return is_exact_; }
954 bool IsTop() const { return is_top_; }
955
956 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
957
Calin Juravleb1498f62015-02-16 13:13:29 +0000958 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000959 if (IsTop()) {
960 // Top (equivalent for java.lang.Object) is supertype of anything.
961 return true;
962 }
963 if (rti.IsTop()) {
964 // If we get here `this` is not Top() so it can't be a supertype.
965 return false;
966 }
967 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
968 }
969
970 // Returns true if the type information provide the same amount of details.
971 // Note that it does not mean that the instructions have the same actual type
972 // (e.g. tops are equal but they can be the result of a merge).
973 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
974 if (IsExact() != rti.IsExact()) {
975 return false;
976 }
977 if (IsTop() && rti.IsTop()) {
978 // `Top` means java.lang.Object, so the types are equivalent.
979 return true;
980 }
981 if (IsTop() || rti.IsTop()) {
982 // If only one is top or object than they are not equivalent.
983 // NB: We need this extra check because the type_handle of `Top` is invalid
984 // and we cannot inspect its reference.
985 return false;
986 }
987
988 // Finally check the types.
989 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
990 }
991
992 private:
Calin Juravleb1498f62015-02-16 13:13:29 +0000993 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
994 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
995 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
996
Calin Juravleacf735c2015-02-12 15:25:22 +0000997 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +0000998 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +0000999 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001000 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001001 bool is_exact_;
1002 // A true value here means that the object type should be java.lang.Object.
1003 // We don't have access to the corresponding mirror object every time so this
1004 // flag acts as a substitute. When true, the TypeHandle refers to a null
1005 // pointer and should not be used.
1006 bool is_top_;
1007};
1008
1009std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1010
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001011class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001012 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001013 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001014 : previous_(nullptr),
1015 next_(nullptr),
1016 block_(nullptr),
1017 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001018 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001019 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001020 locations_(nullptr),
1021 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001022 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001023 side_effects_(side_effects),
1024 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001025
Dave Allison20dfc792014-06-16 20:44:29 -07001026 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001027
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001028#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001029 enum InstructionKind {
1030 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1031 };
1032#undef DECLARE_KIND
1033
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001034 HInstruction* GetNext() const { return next_; }
1035 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001036
Calin Juravle77520bc2015-01-12 18:45:46 +00001037 HInstruction* GetNextDisregardingMoves() const;
1038 HInstruction* GetPreviousDisregardingMoves() const;
1039
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001040 HBasicBlock* GetBlock() const { return block_; }
1041 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001042 bool IsInBlock() const { return block_ != nullptr; }
1043 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001044 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001045
Roland Levillain6b879dd2014-09-22 17:13:44 +01001046 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001047 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001048
1049 virtual void Accept(HGraphVisitor* visitor) = 0;
1050 virtual const char* DebugName() const = 0;
1051
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001052 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001053 void SetRawInputAt(size_t index, HInstruction* input) {
1054 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1055 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001056
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001057 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001058 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001059 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001060 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001061
Calin Juravle61d544b2015-02-23 16:46:57 +00001062 virtual bool ActAsNullConstant() const { return false; }
1063
Calin Juravle10e244f2015-01-26 18:54:32 +00001064 // Does not apply for all instructions, but having this at top level greatly
1065 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001066 virtual bool CanBeNull() const {
1067 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1068 return true;
1069 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001070
Calin Juravle77520bc2015-01-12 18:45:46 +00001071 virtual bool CanDoImplicitNullCheck() const { return false; }
1072
Calin Juravleacf735c2015-02-12 15:25:22 +00001073 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001074 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001075 reference_type_info_ = reference_type_info;
1076 }
1077
Calin Juravle61d544b2015-02-23 16:46:57 +00001078 ReferenceTypeInfo GetReferenceTypeInfo() const {
1079 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1080 return reference_type_info_;
1081 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001082
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001083 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001084 DCHECK(user != nullptr);
1085 HUseListNode<HInstruction*>* use =
1086 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1087 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001088 }
1089
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001090 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001091 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001092 HUseListNode<HEnvironment*>* env_use =
1093 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1094 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001095 }
1096
David Brazdil1abb4192015-02-17 18:33:36 +00001097 void RemoveAsUserOfInput(size_t input) {
1098 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1099 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1100 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001101
David Brazdil1abb4192015-02-17 18:33:36 +00001102 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1103 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001104
David Brazdiled596192015-01-23 10:39:45 +00001105 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1106 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001107 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001108
Roland Levillain6c82d402014-10-13 16:10:27 +01001109 // Does this instruction strictly dominate `other_instruction`?
1110 // Returns false if this instruction and `other_instruction` are the same.
1111 // Aborts if this instruction and `other_instruction` are both phis.
1112 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001113
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001114 int GetId() const { return id_; }
1115 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001116
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001117 int GetSsaIndex() const { return ssa_index_; }
1118 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1119 bool HasSsaIndex() const { return ssa_index_ != -1; }
1120
1121 bool HasEnvironment() const { return environment_ != nullptr; }
1122 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001123 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
1124
Nicolas Geoffray39468442014-09-02 15:17:15 +01001125 // Returns the number of entries in the environment. Typically, that is the
1126 // number of dex registers in a method. It could be more in case of inlining.
1127 size_t EnvironmentSize() const;
1128
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001129 LocationSummary* GetLocations() const { return locations_; }
1130 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001131
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001132 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001133 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001134
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001135 // Move `this` instruction before `cursor`.
1136 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001137
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001138#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001139 bool Is##type() const { return (As##type() != nullptr); } \
1140 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001141 virtual H##type* As##type() { return nullptr; }
1142
1143 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1144#undef INSTRUCTION_TYPE_CHECK
1145
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001146 // Returns whether the instruction can be moved within the graph.
1147 virtual bool CanBeMoved() const { return false; }
1148
1149 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001150 virtual bool InstructionTypeEquals(HInstruction* other) const {
1151 UNUSED(other);
1152 return false;
1153 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001154
1155 // Returns whether any data encoded in the two instructions is equal.
1156 // This method does not look at the inputs. Both instructions must be
1157 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001158 virtual bool InstructionDataEquals(HInstruction* other) const {
1159 UNUSED(other);
1160 return false;
1161 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001162
1163 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001164 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001165 // 2) Their inputs are identical.
1166 bool Equals(HInstruction* other) const;
1167
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001168 virtual InstructionKind GetKind() const = 0;
1169
1170 virtual size_t ComputeHashCode() const {
1171 size_t result = GetKind();
1172 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1173 result = (result * 31) + InputAt(i)->GetId();
1174 }
1175 return result;
1176 }
1177
1178 SideEffects GetSideEffects() const { return side_effects_; }
1179
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001180 size_t GetLifetimePosition() const { return lifetime_position_; }
1181 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1182 LiveInterval* GetLiveInterval() const { return live_interval_; }
1183 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1184 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1185
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001186 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1187
1188 // Returns whether the code generation of the instruction will require to have access
1189 // to the current method. Such instructions are:
1190 // (1): Instructions that require an environment, as calling the runtime requires
1191 // to walk the stack and have the current method stored at a specific stack address.
1192 // (2): Object literals like classes and strings, that are loaded from the dex cache
1193 // fields of the current method.
1194 bool NeedsCurrentMethod() const {
1195 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1196 }
1197
David Brazdil1abb4192015-02-17 18:33:36 +00001198 protected:
1199 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1200 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1201
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001202 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001203 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1204
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001205 HInstruction* previous_;
1206 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001207 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001208
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001209 // An instruction gets an id when it is added to the graph.
1210 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001211 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001212 int id_;
1213
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001214 // When doing liveness analysis, instructions that have uses get an SSA index.
1215 int ssa_index_;
1216
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001217 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001218 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001219
1220 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001221 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001222
Nicolas Geoffray39468442014-09-02 15:17:15 +01001223 // The environment associated with this instruction. Not null if the instruction
1224 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001225 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001226
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001227 // Set by the code generator.
1228 LocationSummary* locations_;
1229
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001230 // Set by the liveness analysis.
1231 LiveInterval* live_interval_;
1232
1233 // Set by the liveness analysis, this is the position in a linear
1234 // order of blocks where this instruction's live interval start.
1235 size_t lifetime_position_;
1236
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001237 const SideEffects side_effects_;
1238
Calin Juravleacf735c2015-02-12 15:25:22 +00001239 // TODO: for primitive types this should be marked as invalid.
1240 ReferenceTypeInfo reference_type_info_;
1241
David Brazdil1abb4192015-02-17 18:33:36 +00001242 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001243 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001244 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001245 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001246 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001247
1248 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1249};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001250std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001251
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001252class HInputIterator : public ValueObject {
1253 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001254 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001255
1256 bool Done() const { return index_ == instruction_->InputCount(); }
1257 HInstruction* Current() const { return instruction_->InputAt(index_); }
1258 void Advance() { index_++; }
1259
1260 private:
1261 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001262 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001263
1264 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1265};
1266
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001267class HInstructionIterator : public ValueObject {
1268 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001269 explicit HInstructionIterator(const HInstructionList& instructions)
1270 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001271 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001272 }
1273
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001274 bool Done() const { return instruction_ == nullptr; }
1275 HInstruction* Current() const { return instruction_; }
1276 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001277 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001278 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001279 }
1280
1281 private:
1282 HInstruction* instruction_;
1283 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001284
1285 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001286};
1287
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001288class HBackwardInstructionIterator : public ValueObject {
1289 public:
1290 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1291 : instruction_(instructions.last_instruction_) {
1292 next_ = Done() ? nullptr : instruction_->GetPrevious();
1293 }
1294
1295 bool Done() const { return instruction_ == nullptr; }
1296 HInstruction* Current() const { return instruction_; }
1297 void Advance() {
1298 instruction_ = next_;
1299 next_ = Done() ? nullptr : instruction_->GetPrevious();
1300 }
1301
1302 private:
1303 HInstruction* instruction_;
1304 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001305
1306 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001307};
1308
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001309// An embedded container with N elements of type T. Used (with partial
1310// specialization for N=0) because embedded arrays cannot have size 0.
1311template<typename T, intptr_t N>
1312class EmbeddedArray {
1313 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001314 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001315
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001316 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001317
1318 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001319 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001320 return elements_[i];
1321 }
1322
1323 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001324 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001325 return elements_[i];
1326 }
1327
1328 const T& At(intptr_t i) const {
1329 return (*this)[i];
1330 }
1331
1332 void SetAt(intptr_t i, const T& val) {
1333 (*this)[i] = val;
1334 }
1335
1336 private:
1337 T elements_[N];
1338};
1339
1340template<typename T>
1341class EmbeddedArray<T, 0> {
1342 public:
1343 intptr_t length() const { return 0; }
1344 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001345 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001346 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001347 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001348 }
1349 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001350 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001351 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001352 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001353 }
1354};
1355
1356template<intptr_t N>
1357class HTemplateInstruction: public HInstruction {
1358 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001359 HTemplateInstruction<N>(SideEffects side_effects)
1360 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001361 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001362
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001363 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001364
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001365 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001366 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1367
1368 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1369 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001370 }
1371
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001372 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001373 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001374
1375 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001376};
1377
Dave Allison20dfc792014-06-16 20:44:29 -07001378template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001379class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001380 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001381 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1382 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001383 virtual ~HExpression() {}
1384
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001385 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001386
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001387 protected:
1388 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001389};
1390
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001391// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1392// instruction that branches to the exit block.
1393class HReturnVoid : public HTemplateInstruction<0> {
1394 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001395 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001396
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001397 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001398
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001399 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001400
1401 private:
1402 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1403};
1404
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001405// Represents dex's RETURN opcodes. A HReturn is a control flow
1406// instruction that branches to the exit block.
1407class HReturn : public HTemplateInstruction<1> {
1408 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001409 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001410 SetRawInputAt(0, value);
1411 }
1412
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001413 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001414
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001415 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001416
1417 private:
1418 DISALLOW_COPY_AND_ASSIGN(HReturn);
1419};
1420
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001421// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001422// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001423// exit block.
1424class HExit : public HTemplateInstruction<0> {
1425 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001426 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001427
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001428 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001429
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001430 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001431
1432 private:
1433 DISALLOW_COPY_AND_ASSIGN(HExit);
1434};
1435
1436// Jumps from one block to another.
1437class HGoto : public HTemplateInstruction<0> {
1438 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001439 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1440
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001441 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001442
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001443 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001444 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001445 }
1446
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001447 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001448
1449 private:
1450 DISALLOW_COPY_AND_ASSIGN(HGoto);
1451};
1452
Dave Allison20dfc792014-06-16 20:44:29 -07001453
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001454// Conditional branch. A block ending with an HIf instruction must have
1455// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001456class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001457 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001458 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001459 SetRawInputAt(0, input);
1460 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001461
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001462 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001463
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001464 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001465 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001466 }
1467
1468 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001469 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001470 }
1471
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001472 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001473
Dave Allison20dfc792014-06-16 20:44:29 -07001474 virtual bool IsIfInstruction() const { return true; }
1475
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001476 private:
1477 DISALLOW_COPY_AND_ASSIGN(HIf);
1478};
1479
Roland Levillain88cb1752014-10-20 16:36:47 +01001480class HUnaryOperation : public HExpression<1> {
1481 public:
1482 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1483 : HExpression(result_type, SideEffects::None()) {
1484 SetRawInputAt(0, input);
1485 }
1486
1487 HInstruction* GetInput() const { return InputAt(0); }
1488 Primitive::Type GetResultType() const { return GetType(); }
1489
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001490 bool CanBeMoved() const OVERRIDE { return true; }
1491 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001492 UNUSED(other);
1493 return true;
1494 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001495
Roland Levillain9240d6a2014-10-20 16:47:04 +01001496 // Try to statically evaluate `operation` and return a HConstant
1497 // containing the result of this evaluation. If `operation` cannot
1498 // be evaluated as a constant, return nullptr.
1499 HConstant* TryStaticEvaluation() const;
1500
1501 // Apply this operation to `x`.
1502 virtual int32_t Evaluate(int32_t x) const = 0;
1503 virtual int64_t Evaluate(int64_t x) const = 0;
1504
Roland Levillain88cb1752014-10-20 16:36:47 +01001505 DECLARE_INSTRUCTION(UnaryOperation);
1506
1507 private:
1508 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1509};
1510
Dave Allison20dfc792014-06-16 20:44:29 -07001511class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001512 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001513 HBinaryOperation(Primitive::Type result_type,
1514 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001515 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001516 SetRawInputAt(0, left);
1517 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001518 }
1519
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001520 HInstruction* GetLeft() const { return InputAt(0); }
1521 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001522 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001523
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001524 virtual bool IsCommutative() const { return false; }
1525
1526 // Put constant on the right.
1527 // Returns whether order is changed.
1528 bool OrderInputsWithConstantOnTheRight() {
1529 HInstruction* left = InputAt(0);
1530 HInstruction* right = InputAt(1);
1531 if (left->IsConstant() && !right->IsConstant()) {
1532 ReplaceInput(right, 0);
1533 ReplaceInput(left, 1);
1534 return true;
1535 }
1536 return false;
1537 }
1538
1539 // Order inputs by instruction id, but favor constant on the right side.
1540 // This helps GVN for commutative ops.
1541 void OrderInputs() {
1542 DCHECK(IsCommutative());
1543 HInstruction* left = InputAt(0);
1544 HInstruction* right = InputAt(1);
1545 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1546 return;
1547 }
1548 if (OrderInputsWithConstantOnTheRight()) {
1549 return;
1550 }
1551 // Order according to instruction id.
1552 if (left->GetId() > right->GetId()) {
1553 ReplaceInput(right, 0);
1554 ReplaceInput(left, 1);
1555 }
1556 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001557
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001558 bool CanBeMoved() const OVERRIDE { return true; }
1559 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001560 UNUSED(other);
1561 return true;
1562 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001563
Roland Levillain9240d6a2014-10-20 16:47:04 +01001564 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001565 // containing the result of this evaluation. If `operation` cannot
1566 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001567 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001568
1569 // Apply this operation to `x` and `y`.
1570 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1571 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1572
Roland Levillainccc07a92014-09-16 14:48:16 +01001573 DECLARE_INSTRUCTION(BinaryOperation);
1574
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001575 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001576 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1577};
1578
Dave Allison20dfc792014-06-16 20:44:29 -07001579class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001580 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001581 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001582 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1583 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001584
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001585 bool NeedsMaterialization() const { return needs_materialization_; }
1586 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001587
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001588 // For code generation purposes, returns whether this instruction is just before
1589 // `if_`, and disregard moves in between.
1590 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1591
Dave Allison20dfc792014-06-16 20:44:29 -07001592 DECLARE_INSTRUCTION(Condition);
1593
1594 virtual IfCondition GetCondition() const = 0;
1595
1596 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001597 // For register allocation purposes, returns whether this instruction needs to be
1598 // materialized (that is, not just be in the processor flags).
1599 bool needs_materialization_;
1600
Dave Allison20dfc792014-06-16 20:44:29 -07001601 DISALLOW_COPY_AND_ASSIGN(HCondition);
1602};
1603
1604// Instruction to check if two inputs are equal to each other.
1605class HEqual : public HCondition {
1606 public:
1607 HEqual(HInstruction* first, HInstruction* second)
1608 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001609
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001610 bool IsCommutative() const OVERRIDE { return true; }
1611
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001612 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001613 return x == y ? 1 : 0;
1614 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001615 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001616 return x == y ? 1 : 0;
1617 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001618
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001619 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001620
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001621 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001622 return kCondEQ;
1623 }
1624
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001625 private:
1626 DISALLOW_COPY_AND_ASSIGN(HEqual);
1627};
1628
Dave Allison20dfc792014-06-16 20:44:29 -07001629class HNotEqual : public HCondition {
1630 public:
1631 HNotEqual(HInstruction* first, HInstruction* second)
1632 : HCondition(first, second) {}
1633
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001634 bool IsCommutative() const OVERRIDE { return true; }
1635
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001636 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001637 return x != y ? 1 : 0;
1638 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001639 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001640 return x != y ? 1 : 0;
1641 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001642
Dave Allison20dfc792014-06-16 20:44:29 -07001643 DECLARE_INSTRUCTION(NotEqual);
1644
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001645 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001646 return kCondNE;
1647 }
1648
1649 private:
1650 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1651};
1652
1653class HLessThan : public HCondition {
1654 public:
1655 HLessThan(HInstruction* first, HInstruction* second)
1656 : HCondition(first, second) {}
1657
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001658 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001659 return x < y ? 1 : 0;
1660 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001661 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001662 return x < y ? 1 : 0;
1663 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001664
Dave Allison20dfc792014-06-16 20:44:29 -07001665 DECLARE_INSTRUCTION(LessThan);
1666
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001667 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001668 return kCondLT;
1669 }
1670
1671 private:
1672 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1673};
1674
1675class HLessThanOrEqual : public HCondition {
1676 public:
1677 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1678 : HCondition(first, second) {}
1679
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001680 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001681 return x <= y ? 1 : 0;
1682 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001683 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001684 return x <= y ? 1 : 0;
1685 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001686
Dave Allison20dfc792014-06-16 20:44:29 -07001687 DECLARE_INSTRUCTION(LessThanOrEqual);
1688
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001689 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001690 return kCondLE;
1691 }
1692
1693 private:
1694 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1695};
1696
1697class HGreaterThan : public HCondition {
1698 public:
1699 HGreaterThan(HInstruction* first, HInstruction* second)
1700 : HCondition(first, second) {}
1701
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001702 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001703 return x > y ? 1 : 0;
1704 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001705 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001706 return x > y ? 1 : 0;
1707 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001708
Dave Allison20dfc792014-06-16 20:44:29 -07001709 DECLARE_INSTRUCTION(GreaterThan);
1710
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001711 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001712 return kCondGT;
1713 }
1714
1715 private:
1716 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1717};
1718
1719class HGreaterThanOrEqual : public HCondition {
1720 public:
1721 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1722 : HCondition(first, second) {}
1723
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001724 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001725 return x >= y ? 1 : 0;
1726 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001727 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001728 return x >= y ? 1 : 0;
1729 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001730
Dave Allison20dfc792014-06-16 20:44:29 -07001731 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1732
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001733 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001734 return kCondGE;
1735 }
1736
1737 private:
1738 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1739};
1740
1741
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001742// Instruction to check how two inputs compare to each other.
1743// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1744class HCompare : public HBinaryOperation {
1745 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001746 // The bias applies for floating point operations and indicates how NaN
1747 // comparisons are treated:
1748 enum Bias {
1749 kNoBias, // bias is not applicable (i.e. for long operation)
1750 kGtBias, // return 1 for NaN comparisons
1751 kLtBias, // return -1 for NaN comparisons
1752 };
1753
1754 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1755 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001756 DCHECK_EQ(type, first->GetType());
1757 DCHECK_EQ(type, second->GetType());
1758 }
1759
Calin Juravleddb7df22014-11-25 20:56:51 +00001760 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001761 return
1762 x == y ? 0 :
1763 x > y ? 1 :
1764 -1;
1765 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001766
1767 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001768 return
1769 x == y ? 0 :
1770 x > y ? 1 :
1771 -1;
1772 }
1773
Calin Juravleddb7df22014-11-25 20:56:51 +00001774 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1775 return bias_ == other->AsCompare()->bias_;
1776 }
1777
1778 bool IsGtBias() { return bias_ == kGtBias; }
1779
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001780 DECLARE_INSTRUCTION(Compare);
1781
1782 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001783 const Bias bias_;
1784
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001785 DISALLOW_COPY_AND_ASSIGN(HCompare);
1786};
1787
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001788// A local in the graph. Corresponds to a Dex register.
1789class HLocal : public HTemplateInstruction<0> {
1790 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001791 explicit HLocal(uint16_t reg_number)
1792 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001793
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001794 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001795
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001796 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001797
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001798 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001799 // The Dex register number.
1800 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001801
1802 DISALLOW_COPY_AND_ASSIGN(HLocal);
1803};
1804
1805// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001806class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001807 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001808 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001809 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001810 SetRawInputAt(0, local);
1811 }
1812
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001813 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1814
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001815 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001816
1817 private:
1818 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1819};
1820
1821// Store a value in a given local. This instruction has two inputs: the value
1822// and the local.
1823class HStoreLocal : public HTemplateInstruction<2> {
1824 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001825 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001826 SetRawInputAt(0, local);
1827 SetRawInputAt(1, value);
1828 }
1829
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001830 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1831
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001832 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001833
1834 private:
1835 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1836};
1837
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001838class HConstant : public HExpression<0> {
1839 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001840 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1841
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001842 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001843
1844 DECLARE_INSTRUCTION(Constant);
1845
1846 private:
1847 DISALLOW_COPY_AND_ASSIGN(HConstant);
1848};
1849
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001850class HFloatConstant : public HConstant {
1851 public:
1852 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1853
1854 float GetValue() const { return value_; }
1855
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001856 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001857 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1858 bit_cast<float, int32_t>(value_);
1859 }
1860
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001861 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001862
1863 DECLARE_INSTRUCTION(FloatConstant);
1864
1865 private:
1866 const float value_;
1867
1868 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1869};
1870
1871class HDoubleConstant : public HConstant {
1872 public:
1873 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1874
1875 double GetValue() const { return value_; }
1876
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001877 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001878 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1879 bit_cast<double, int64_t>(value_);
1880 }
1881
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001882 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001883
1884 DECLARE_INSTRUCTION(DoubleConstant);
1885
1886 private:
1887 const double value_;
1888
1889 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1890};
1891
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001892class HNullConstant : public HConstant {
1893 public:
1894 HNullConstant() : HConstant(Primitive::kPrimNot) {}
1895
1896 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
1897 return true;
1898 }
1899
1900 size_t ComputeHashCode() const OVERRIDE { return 0; }
1901
Calin Juravle61d544b2015-02-23 16:46:57 +00001902 bool ActAsNullConstant() const OVERRIDE { return true; }
1903
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001904 DECLARE_INSTRUCTION(NullConstant);
1905
1906 private:
1907 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
1908};
1909
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001910// Constants of the type int. Those can be from Dex instructions, or
1911// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001912class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001913 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001914 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001915
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001916 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001917
Calin Juravle61d544b2015-02-23 16:46:57 +00001918 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001919 return other->AsIntConstant()->value_ == value_;
1920 }
1921
Calin Juravle61d544b2015-02-23 16:46:57 +00001922 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
1923
1924 // TODO: Null is represented by the `0` constant. In most cases we replace it
1925 // with a HNullConstant but we don't do it when comparing (a != null). This
1926 // method is an workaround until we fix the above.
1927 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001928
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001929 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001930
1931 private:
1932 const int32_t value_;
1933
1934 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1935};
1936
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001937class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001938 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001939 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001940
1941 int64_t GetValue() const { return value_; }
1942
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001943 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001944 return other->AsLongConstant()->value_ == value_;
1945 }
1946
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001947 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001948
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001949 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001950
1951 private:
1952 const int64_t value_;
1953
1954 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1955};
1956
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001957enum class Intrinsics {
1958#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
1959#include "intrinsics_list.h"
1960 kNone,
1961 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
1962#undef INTRINSICS_LIST
1963#undef OPTIMIZING_INTRINSICS
1964};
1965std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
1966
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001967class HInvoke : public HInstruction {
1968 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001969 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001970
1971 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1972 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00001973 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001974
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001975 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001976 SetRawInputAt(index, argument);
1977 }
1978
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001979 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001980
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001981 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001982
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001983 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1984
1985 Intrinsics GetIntrinsic() {
1986 return intrinsic_;
1987 }
1988
1989 void SetIntrinsic(Intrinsics intrinsic) {
1990 intrinsic_ = intrinsic;
1991 }
1992
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001993 DECLARE_INSTRUCTION(Invoke);
1994
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001995 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001996 HInvoke(ArenaAllocator* arena,
1997 uint32_t number_of_arguments,
1998 Primitive::Type return_type,
1999 uint32_t dex_pc,
2000 uint32_t dex_method_index)
2001 : HInstruction(SideEffects::All()),
2002 inputs_(arena, number_of_arguments),
2003 return_type_(return_type),
2004 dex_pc_(dex_pc),
2005 dex_method_index_(dex_method_index),
2006 intrinsic_(Intrinsics::kNone) {
2007 inputs_.SetSize(number_of_arguments);
2008 }
2009
David Brazdil1abb4192015-02-17 18:33:36 +00002010 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2011 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2012 inputs_.Put(index, input);
2013 }
2014
2015 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002016 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002017 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002018 const uint32_t dex_method_index_;
2019 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002020
2021 private:
2022 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2023};
2024
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002025class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002026 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002027 HInvokeStaticOrDirect(ArenaAllocator* arena,
2028 uint32_t number_of_arguments,
2029 Primitive::Type return_type,
2030 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002031 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002032 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002033 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002034 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002035 invoke_type_(invoke_type),
2036 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002037
Calin Juravle77520bc2015-01-12 18:45:46 +00002038 bool CanDoImplicitNullCheck() const OVERRIDE {
2039 // We access the method via the dex cache so we can't do an implicit null check.
2040 // TODO: for intrinsics we can generate implicit null checks.
2041 return false;
2042 }
2043
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002044 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002045 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002046
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002047 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002048
2049 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002050 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002051 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002052
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002053 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002054};
2055
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002056class HInvokeVirtual : public HInvoke {
2057 public:
2058 HInvokeVirtual(ArenaAllocator* arena,
2059 uint32_t number_of_arguments,
2060 Primitive::Type return_type,
2061 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002062 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002063 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002064 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002065 vtable_index_(vtable_index) {}
2066
Calin Juravle77520bc2015-01-12 18:45:46 +00002067 bool CanDoImplicitNullCheck() const OVERRIDE {
2068 // TODO: Add implicit null checks in intrinsics.
2069 return !GetLocations()->Intrinsified();
2070 }
2071
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002072 uint32_t GetVTableIndex() const { return vtable_index_; }
2073
2074 DECLARE_INSTRUCTION(InvokeVirtual);
2075
2076 private:
2077 const uint32_t vtable_index_;
2078
2079 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2080};
2081
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002082class HInvokeInterface : public HInvoke {
2083 public:
2084 HInvokeInterface(ArenaAllocator* arena,
2085 uint32_t number_of_arguments,
2086 Primitive::Type return_type,
2087 uint32_t dex_pc,
2088 uint32_t dex_method_index,
2089 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002090 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002091 imt_index_(imt_index) {}
2092
Calin Juravle77520bc2015-01-12 18:45:46 +00002093 bool CanDoImplicitNullCheck() const OVERRIDE {
2094 // TODO: Add implicit null checks in intrinsics.
2095 return !GetLocations()->Intrinsified();
2096 }
2097
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002098 uint32_t GetImtIndex() const { return imt_index_; }
2099 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2100
2101 DECLARE_INSTRUCTION(InvokeInterface);
2102
2103 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002104 const uint32_t imt_index_;
2105
2106 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2107};
2108
Dave Allison20dfc792014-06-16 20:44:29 -07002109class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002110 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002111 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002112 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2113 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002114 type_index_(type_index),
2115 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002116
2117 uint32_t GetDexPc() const { return dex_pc_; }
2118 uint16_t GetTypeIndex() const { return type_index_; }
2119
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002120 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002121 bool NeedsEnvironment() const OVERRIDE { return true; }
2122 // It may throw when called on:
2123 // - interfaces
2124 // - abstract/innaccessible/unknown classes
2125 // TODO: optimize when possible.
2126 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002127
Calin Juravle10e244f2015-01-26 18:54:32 +00002128 bool CanBeNull() const OVERRIDE { return false; }
2129
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002130 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2131
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002132 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002133
2134 private:
2135 const uint32_t dex_pc_;
2136 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002137 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002138
2139 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2140};
2141
Roland Levillain88cb1752014-10-20 16:36:47 +01002142class HNeg : public HUnaryOperation {
2143 public:
2144 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2145 : HUnaryOperation(result_type, input) {}
2146
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002147 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2148 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002149
Roland Levillain88cb1752014-10-20 16:36:47 +01002150 DECLARE_INSTRUCTION(Neg);
2151
2152 private:
2153 DISALLOW_COPY_AND_ASSIGN(HNeg);
2154};
2155
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002156class HNewArray : public HExpression<1> {
2157 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002158 HNewArray(HInstruction* length,
2159 uint32_t dex_pc,
2160 uint16_t type_index,
2161 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002162 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2163 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002164 type_index_(type_index),
2165 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002166 SetRawInputAt(0, length);
2167 }
2168
2169 uint32_t GetDexPc() const { return dex_pc_; }
2170 uint16_t GetTypeIndex() const { return type_index_; }
2171
2172 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002173 bool NeedsEnvironment() const OVERRIDE { return true; }
2174
2175 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002176
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002177 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2178
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002179 DECLARE_INSTRUCTION(NewArray);
2180
2181 private:
2182 const uint32_t dex_pc_;
2183 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002184 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002185
2186 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2187};
2188
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002189class HAdd : public HBinaryOperation {
2190 public:
2191 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2192 : HBinaryOperation(result_type, left, right) {}
2193
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002194 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002195
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002196 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002197 return x + y;
2198 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002199 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002200 return x + y;
2201 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002202
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002203 DECLARE_INSTRUCTION(Add);
2204
2205 private:
2206 DISALLOW_COPY_AND_ASSIGN(HAdd);
2207};
2208
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002209class HSub : public HBinaryOperation {
2210 public:
2211 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2212 : HBinaryOperation(result_type, left, right) {}
2213
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002214 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002215 return x - y;
2216 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002217 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002218 return x - y;
2219 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002220
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002221 DECLARE_INSTRUCTION(Sub);
2222
2223 private:
2224 DISALLOW_COPY_AND_ASSIGN(HSub);
2225};
2226
Calin Juravle34bacdf2014-10-07 20:23:36 +01002227class HMul : public HBinaryOperation {
2228 public:
2229 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2230 : HBinaryOperation(result_type, left, right) {}
2231
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002232 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002233
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002234 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2235 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002236
2237 DECLARE_INSTRUCTION(Mul);
2238
2239 private:
2240 DISALLOW_COPY_AND_ASSIGN(HMul);
2241};
2242
Calin Juravle7c4954d2014-10-28 16:57:40 +00002243class HDiv : public HBinaryOperation {
2244 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002245 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2246 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002247
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002248 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002249 // Our graph structure ensures we never have 0 for `y` during constant folding.
2250 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002251 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002252 return (y == -1) ? -x : x / y;
2253 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002254
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002255 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002256 DCHECK_NE(y, 0);
2257 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2258 return (y == -1) ? -x : x / y;
2259 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002260
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002261 uint32_t GetDexPc() const { return dex_pc_; }
2262
Calin Juravle7c4954d2014-10-28 16:57:40 +00002263 DECLARE_INSTRUCTION(Div);
2264
2265 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002266 const uint32_t dex_pc_;
2267
Calin Juravle7c4954d2014-10-28 16:57:40 +00002268 DISALLOW_COPY_AND_ASSIGN(HDiv);
2269};
2270
Calin Juravlebacfec32014-11-14 15:54:36 +00002271class HRem : public HBinaryOperation {
2272 public:
2273 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2274 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2275
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002276 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002277 DCHECK_NE(y, 0);
2278 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2279 return (y == -1) ? 0 : x % y;
2280 }
2281
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002282 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002283 DCHECK_NE(y, 0);
2284 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2285 return (y == -1) ? 0 : x % y;
2286 }
2287
2288 uint32_t GetDexPc() const { return dex_pc_; }
2289
2290 DECLARE_INSTRUCTION(Rem);
2291
2292 private:
2293 const uint32_t dex_pc_;
2294
2295 DISALLOW_COPY_AND_ASSIGN(HRem);
2296};
2297
Calin Juravled0d48522014-11-04 16:40:20 +00002298class HDivZeroCheck : public HExpression<1> {
2299 public:
2300 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2301 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2302 SetRawInputAt(0, value);
2303 }
2304
2305 bool CanBeMoved() const OVERRIDE { return true; }
2306
2307 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2308 UNUSED(other);
2309 return true;
2310 }
2311
2312 bool NeedsEnvironment() const OVERRIDE { return true; }
2313 bool CanThrow() const OVERRIDE { return true; }
2314
2315 uint32_t GetDexPc() const { return dex_pc_; }
2316
2317 DECLARE_INSTRUCTION(DivZeroCheck);
2318
2319 private:
2320 const uint32_t dex_pc_;
2321
2322 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2323};
2324
Calin Juravle9aec02f2014-11-18 23:06:35 +00002325class HShl : public HBinaryOperation {
2326 public:
2327 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2328 : HBinaryOperation(result_type, left, right) {}
2329
2330 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2331 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2332
2333 DECLARE_INSTRUCTION(Shl);
2334
2335 private:
2336 DISALLOW_COPY_AND_ASSIGN(HShl);
2337};
2338
2339class HShr : public HBinaryOperation {
2340 public:
2341 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2342 : HBinaryOperation(result_type, left, right) {}
2343
2344 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2345 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2346
2347 DECLARE_INSTRUCTION(Shr);
2348
2349 private:
2350 DISALLOW_COPY_AND_ASSIGN(HShr);
2351};
2352
2353class HUShr : public HBinaryOperation {
2354 public:
2355 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2356 : HBinaryOperation(result_type, left, right) {}
2357
2358 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2359 uint32_t ux = static_cast<uint32_t>(x);
2360 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2361 return static_cast<int32_t>(ux >> uy);
2362 }
2363
2364 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2365 uint64_t ux = static_cast<uint64_t>(x);
2366 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2367 return static_cast<int64_t>(ux >> uy);
2368 }
2369
2370 DECLARE_INSTRUCTION(UShr);
2371
2372 private:
2373 DISALLOW_COPY_AND_ASSIGN(HUShr);
2374};
2375
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002376class HAnd : public HBinaryOperation {
2377 public:
2378 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2379 : HBinaryOperation(result_type, left, right) {}
2380
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002381 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002382
2383 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2384 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2385
2386 DECLARE_INSTRUCTION(And);
2387
2388 private:
2389 DISALLOW_COPY_AND_ASSIGN(HAnd);
2390};
2391
2392class HOr : public HBinaryOperation {
2393 public:
2394 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2395 : HBinaryOperation(result_type, left, right) {}
2396
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002397 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002398
2399 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2400 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2401
2402 DECLARE_INSTRUCTION(Or);
2403
2404 private:
2405 DISALLOW_COPY_AND_ASSIGN(HOr);
2406};
2407
2408class HXor : public HBinaryOperation {
2409 public:
2410 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2411 : HBinaryOperation(result_type, left, right) {}
2412
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002413 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002414
2415 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2416 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2417
2418 DECLARE_INSTRUCTION(Xor);
2419
2420 private:
2421 DISALLOW_COPY_AND_ASSIGN(HXor);
2422};
2423
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002424// The value of a parameter in this method. Its location depends on
2425// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002426class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002427 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002428 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2429 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002430
2431 uint8_t GetIndex() const { return index_; }
2432
Calin Juravle10e244f2015-01-26 18:54:32 +00002433 bool CanBeNull() const OVERRIDE { return !is_this_; }
2434
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002435 DECLARE_INSTRUCTION(ParameterValue);
2436
2437 private:
2438 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002439 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002440 const uint8_t index_;
2441
Calin Juravle10e244f2015-01-26 18:54:32 +00002442 // Whether or not the parameter value corresponds to 'this' argument.
2443 const bool is_this_;
2444
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002445 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2446};
2447
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002448class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002449 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002450 explicit HNot(Primitive::Type result_type, HInstruction* input)
2451 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002452
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002453 bool CanBeMoved() const OVERRIDE { return true; }
2454 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002455 UNUSED(other);
2456 return true;
2457 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002458
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002459 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2460 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002461
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002462 DECLARE_INSTRUCTION(Not);
2463
2464 private:
2465 DISALLOW_COPY_AND_ASSIGN(HNot);
2466};
2467
Roland Levillaindff1f282014-11-05 14:15:05 +00002468class HTypeConversion : public HExpression<1> {
2469 public:
2470 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002471 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2472 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002473 SetRawInputAt(0, input);
2474 DCHECK_NE(input->GetType(), result_type);
2475 }
2476
2477 HInstruction* GetInput() const { return InputAt(0); }
2478 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2479 Primitive::Type GetResultType() const { return GetType(); }
2480
Roland Levillain624279f2014-12-04 11:54:28 +00002481 // Required by the x86 and ARM code generators when producing calls
2482 // to the runtime.
2483 uint32_t GetDexPc() const { return dex_pc_; }
2484
Roland Levillaindff1f282014-11-05 14:15:05 +00002485 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002486 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002487
2488 DECLARE_INSTRUCTION(TypeConversion);
2489
2490 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002491 const uint32_t dex_pc_;
2492
Roland Levillaindff1f282014-11-05 14:15:05 +00002493 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2494};
2495
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002496static constexpr uint32_t kNoRegNumber = -1;
2497
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002498class HPhi : public HInstruction {
2499 public:
2500 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002501 : HInstruction(SideEffects::None()),
2502 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002503 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002504 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002505 is_live_(false),
2506 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002507 inputs_.SetSize(number_of_inputs);
2508 }
2509
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002510 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2511 static Primitive::Type ToPhiType(Primitive::Type type) {
2512 switch (type) {
2513 case Primitive::kPrimBoolean:
2514 case Primitive::kPrimByte:
2515 case Primitive::kPrimShort:
2516 case Primitive::kPrimChar:
2517 return Primitive::kPrimInt;
2518 default:
2519 return type;
2520 }
2521 }
2522
Calin Juravle10e244f2015-01-26 18:54:32 +00002523 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002524
2525 void AddInput(HInstruction* input);
2526
Calin Juravle10e244f2015-01-26 18:54:32 +00002527 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002528 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002529
Calin Juravle10e244f2015-01-26 18:54:32 +00002530 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2531 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2532
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002533 uint32_t GetRegNumber() const { return reg_number_; }
2534
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002535 void SetDead() { is_live_ = false; }
2536 void SetLive() { is_live_ = true; }
2537 bool IsDead() const { return !is_live_; }
2538 bool IsLive() const { return is_live_; }
2539
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002540 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002541
David Brazdil1abb4192015-02-17 18:33:36 +00002542 protected:
2543 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2544
2545 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2546 inputs_.Put(index, input);
2547 }
2548
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002549 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002550 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002551 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002552 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002553 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002554 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002555
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002556 DISALLOW_COPY_AND_ASSIGN(HPhi);
2557};
2558
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002559class HNullCheck : public HExpression<1> {
2560 public:
2561 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002562 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002563 SetRawInputAt(0, value);
2564 }
2565
Calin Juravle10e244f2015-01-26 18:54:32 +00002566 bool CanBeMoved() const OVERRIDE { return true; }
2567 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002568 UNUSED(other);
2569 return true;
2570 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002571
Calin Juravle10e244f2015-01-26 18:54:32 +00002572 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002573
Calin Juravle10e244f2015-01-26 18:54:32 +00002574 bool CanThrow() const OVERRIDE { return true; }
2575
2576 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002577
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002578 uint32_t GetDexPc() const { return dex_pc_; }
2579
2580 DECLARE_INSTRUCTION(NullCheck);
2581
2582 private:
2583 const uint32_t dex_pc_;
2584
2585 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2586};
2587
2588class FieldInfo : public ValueObject {
2589 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002590 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2591 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002592
2593 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002594 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002595 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002596
2597 private:
2598 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002599 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002600 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002601};
2602
2603class HInstanceFieldGet : public HExpression<1> {
2604 public:
2605 HInstanceFieldGet(HInstruction* value,
2606 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002607 MemberOffset field_offset,
2608 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002609 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002610 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002611 SetRawInputAt(0, value);
2612 }
2613
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002614 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002615
2616 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2617 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2618 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002619 }
2620
Calin Juravle77520bc2015-01-12 18:45:46 +00002621 bool CanDoImplicitNullCheck() const OVERRIDE {
2622 return GetFieldOffset().Uint32Value() < kPageSize;
2623 }
2624
2625 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002626 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2627 }
2628
Calin Juravle52c48962014-12-16 17:02:57 +00002629 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002630 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002631 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002632 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002633
2634 DECLARE_INSTRUCTION(InstanceFieldGet);
2635
2636 private:
2637 const FieldInfo field_info_;
2638
2639 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2640};
2641
2642class HInstanceFieldSet : public HTemplateInstruction<2> {
2643 public:
2644 HInstanceFieldSet(HInstruction* object,
2645 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002646 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002647 MemberOffset field_offset,
2648 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002649 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002650 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002651 SetRawInputAt(0, object);
2652 SetRawInputAt(1, value);
2653 }
2654
Calin Juravle77520bc2015-01-12 18:45:46 +00002655 bool CanDoImplicitNullCheck() const OVERRIDE {
2656 return GetFieldOffset().Uint32Value() < kPageSize;
2657 }
2658
Calin Juravle52c48962014-12-16 17:02:57 +00002659 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002660 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002661 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002662 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002663 HInstruction* GetValue() const { return InputAt(1); }
2664
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002665 DECLARE_INSTRUCTION(InstanceFieldSet);
2666
2667 private:
2668 const FieldInfo field_info_;
2669
2670 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2671};
2672
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002673class HArrayGet : public HExpression<2> {
2674 public:
2675 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002676 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002677 SetRawInputAt(0, array);
2678 SetRawInputAt(1, index);
2679 }
2680
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002681 bool CanBeMoved() const OVERRIDE { return true; }
2682 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002683 UNUSED(other);
2684 return true;
2685 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002686 bool CanDoImplicitNullCheck() const OVERRIDE {
2687 // TODO: We can be smarter here.
2688 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2689 // which generates the implicit null check. There are cases when these can be removed
2690 // to produce better code. If we ever add optimizations to do so we should allow an
2691 // implicit check here (as long as the address falls in the first page).
2692 return false;
2693 }
2694
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002695 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002696
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002697 HInstruction* GetArray() const { return InputAt(0); }
2698 HInstruction* GetIndex() const { return InputAt(1); }
2699
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002700 DECLARE_INSTRUCTION(ArrayGet);
2701
2702 private:
2703 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2704};
2705
2706class HArraySet : public HTemplateInstruction<3> {
2707 public:
2708 HArraySet(HInstruction* array,
2709 HInstruction* index,
2710 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002711 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002712 uint32_t dex_pc)
2713 : HTemplateInstruction(SideEffects::ChangesSomething()),
2714 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002715 expected_component_type_(expected_component_type),
2716 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002717 SetRawInputAt(0, array);
2718 SetRawInputAt(1, index);
2719 SetRawInputAt(2, value);
2720 }
2721
Calin Juravle77520bc2015-01-12 18:45:46 +00002722 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002723 // We currently always call a runtime method to catch array store
2724 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002725 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002726 }
2727
Calin Juravle77520bc2015-01-12 18:45:46 +00002728 bool CanDoImplicitNullCheck() const OVERRIDE {
2729 // TODO: Same as for ArrayGet.
2730 return false;
2731 }
2732
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002733 void ClearNeedsTypeCheck() {
2734 needs_type_check_ = false;
2735 }
2736
2737 bool NeedsTypeCheck() const { return needs_type_check_; }
2738
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002739 uint32_t GetDexPc() const { return dex_pc_; }
2740
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002741 HInstruction* GetArray() const { return InputAt(0); }
2742 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002743 HInstruction* GetValue() const { return InputAt(2); }
2744
2745 Primitive::Type GetComponentType() const {
2746 // The Dex format does not type floating point index operations. Since the
2747 // `expected_component_type_` is set during building and can therefore not
2748 // be correct, we also check what is the value type. If it is a floating
2749 // point type, we must use that type.
2750 Primitive::Type value_type = GetValue()->GetType();
2751 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2752 ? value_type
2753 : expected_component_type_;
2754 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002755
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002756 DECLARE_INSTRUCTION(ArraySet);
2757
2758 private:
2759 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002760 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002761 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002762
2763 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2764};
2765
2766class HArrayLength : public HExpression<1> {
2767 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002768 explicit HArrayLength(HInstruction* array)
2769 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2770 // Note that arrays do not change length, so the instruction does not
2771 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002772 SetRawInputAt(0, array);
2773 }
2774
Calin Juravle77520bc2015-01-12 18:45:46 +00002775 bool CanBeMoved() const OVERRIDE { return true; }
2776 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002777 UNUSED(other);
2778 return true;
2779 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002780 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002781
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002782 DECLARE_INSTRUCTION(ArrayLength);
2783
2784 private:
2785 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2786};
2787
2788class HBoundsCheck : public HExpression<2> {
2789 public:
2790 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002791 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002792 DCHECK(index->GetType() == Primitive::kPrimInt);
2793 SetRawInputAt(0, index);
2794 SetRawInputAt(1, length);
2795 }
2796
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002797 bool CanBeMoved() const OVERRIDE { return true; }
2798 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002799 UNUSED(other);
2800 return true;
2801 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002802
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002803 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002804
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002805 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002806
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002807 uint32_t GetDexPc() const { return dex_pc_; }
2808
2809 DECLARE_INSTRUCTION(BoundsCheck);
2810
2811 private:
2812 const uint32_t dex_pc_;
2813
2814 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2815};
2816
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002817/**
2818 * Some DEX instructions are folded into multiple HInstructions that need
2819 * to stay live until the last HInstruction. This class
2820 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002821 * HInstruction stays live. `index` represents the stack location index of the
2822 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002823 */
2824class HTemporary : public HTemplateInstruction<0> {
2825 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002826 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002827
2828 size_t GetIndex() const { return index_; }
2829
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002830 Primitive::Type GetType() const OVERRIDE {
2831 // The previous instruction is the one that will be stored in the temporary location.
2832 DCHECK(GetPrevious() != nullptr);
2833 return GetPrevious()->GetType();
2834 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002835
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002836 DECLARE_INSTRUCTION(Temporary);
2837
2838 private:
2839 const size_t index_;
2840
2841 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2842};
2843
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002844class HSuspendCheck : public HTemplateInstruction<0> {
2845 public:
2846 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002847 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002848
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002849 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002850 return true;
2851 }
2852
2853 uint32_t GetDexPc() const { return dex_pc_; }
2854
2855 DECLARE_INSTRUCTION(SuspendCheck);
2856
2857 private:
2858 const uint32_t dex_pc_;
2859
2860 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2861};
2862
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002863/**
2864 * Instruction to load a Class object.
2865 */
2866class HLoadClass : public HExpression<0> {
2867 public:
2868 HLoadClass(uint16_t type_index,
2869 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002870 uint32_t dex_pc)
2871 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2872 type_index_(type_index),
2873 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002874 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00002875 generate_clinit_check_(false),
2876 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002877
2878 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002879
2880 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2881 return other->AsLoadClass()->type_index_ == type_index_;
2882 }
2883
2884 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2885
2886 uint32_t GetDexPc() const { return dex_pc_; }
2887 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002888 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002889
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002890 bool NeedsEnvironment() const OVERRIDE {
2891 // Will call runtime and load the class if the class is not loaded yet.
2892 // TODO: finer grain decision.
2893 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002894 }
2895
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002896 bool MustGenerateClinitCheck() const {
2897 return generate_clinit_check_;
2898 }
2899
2900 void SetMustGenerateClinitCheck() {
2901 generate_clinit_check_ = true;
2902 }
2903
2904 bool CanCallRuntime() const {
2905 return MustGenerateClinitCheck() || !is_referrers_class_;
2906 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002907
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002908 bool CanThrow() const OVERRIDE {
2909 // May call runtime and and therefore can throw.
2910 // TODO: finer grain decision.
2911 return !is_referrers_class_;
2912 }
2913
Calin Juravleacf735c2015-02-12 15:25:22 +00002914 ReferenceTypeInfo GetLoadedClassRTI() {
2915 return loaded_class_rti_;
2916 }
2917
2918 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
2919 // Make sure we only set exact types (the loaded class should never be merged).
2920 DCHECK(rti.IsExact());
2921 loaded_class_rti_ = rti;
2922 }
2923
2924 bool IsResolved() {
2925 return loaded_class_rti_.IsExact();
2926 }
2927
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002928 DECLARE_INSTRUCTION(LoadClass);
2929
2930 private:
2931 const uint16_t type_index_;
2932 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002933 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002934 // Whether this instruction must generate the initialization check.
2935 // Used for code generation.
2936 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002937
Calin Juravleacf735c2015-02-12 15:25:22 +00002938 ReferenceTypeInfo loaded_class_rti_;
2939
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002940 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2941};
2942
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002943class HLoadString : public HExpression<0> {
2944 public:
2945 HLoadString(uint32_t string_index, uint32_t dex_pc)
2946 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2947 string_index_(string_index),
2948 dex_pc_(dex_pc) {}
2949
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002950 bool CanBeMoved() const OVERRIDE { return true; }
2951
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002952 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2953 return other->AsLoadString()->string_index_ == string_index_;
2954 }
2955
2956 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2957
2958 uint32_t GetDexPc() const { return dex_pc_; }
2959 uint32_t GetStringIndex() const { return string_index_; }
2960
2961 // TODO: Can we deopt or debug when we resolve a string?
2962 bool NeedsEnvironment() const OVERRIDE { return false; }
2963
2964 DECLARE_INSTRUCTION(LoadString);
2965
2966 private:
2967 const uint32_t string_index_;
2968 const uint32_t dex_pc_;
2969
2970 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2971};
2972
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002973// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002974/**
2975 * Performs an initialization check on its Class object input.
2976 */
2977class HClinitCheck : public HExpression<1> {
2978 public:
2979 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2980 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2981 dex_pc_(dex_pc) {
2982 SetRawInputAt(0, constant);
2983 }
2984
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002985 bool CanBeMoved() const OVERRIDE { return true; }
2986 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2987 UNUSED(other);
2988 return true;
2989 }
2990
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002991 bool NeedsEnvironment() const OVERRIDE {
2992 // May call runtime to initialize the class.
2993 return true;
2994 }
2995
2996 uint32_t GetDexPc() const { return dex_pc_; }
2997
2998 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2999
3000 DECLARE_INSTRUCTION(ClinitCheck);
3001
3002 private:
3003 const uint32_t dex_pc_;
3004
3005 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3006};
3007
3008class HStaticFieldGet : public HExpression<1> {
3009 public:
3010 HStaticFieldGet(HInstruction* cls,
3011 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003012 MemberOffset field_offset,
3013 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003014 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003015 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003016 SetRawInputAt(0, cls);
3017 }
3018
Calin Juravle52c48962014-12-16 17:02:57 +00003019
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003020 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003021
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003022 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003023 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3024 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003025 }
3026
3027 size_t ComputeHashCode() const OVERRIDE {
3028 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3029 }
3030
Calin Juravle52c48962014-12-16 17:02:57 +00003031 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003032 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3033 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003034 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003035
3036 DECLARE_INSTRUCTION(StaticFieldGet);
3037
3038 private:
3039 const FieldInfo field_info_;
3040
3041 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3042};
3043
3044class HStaticFieldSet : public HTemplateInstruction<2> {
3045 public:
3046 HStaticFieldSet(HInstruction* cls,
3047 HInstruction* value,
3048 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003049 MemberOffset field_offset,
3050 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003051 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003052 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003053 SetRawInputAt(0, cls);
3054 SetRawInputAt(1, value);
3055 }
3056
Calin Juravle52c48962014-12-16 17:02:57 +00003057 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003058 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3059 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003060 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003061
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003062 HInstruction* GetValue() const { return InputAt(1); }
3063
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003064 DECLARE_INSTRUCTION(StaticFieldSet);
3065
3066 private:
3067 const FieldInfo field_info_;
3068
3069 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3070};
3071
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003072// Implement the move-exception DEX instruction.
3073class HLoadException : public HExpression<0> {
3074 public:
3075 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3076
3077 DECLARE_INSTRUCTION(LoadException);
3078
3079 private:
3080 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3081};
3082
3083class HThrow : public HTemplateInstruction<1> {
3084 public:
3085 HThrow(HInstruction* exception, uint32_t dex_pc)
3086 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3087 SetRawInputAt(0, exception);
3088 }
3089
3090 bool IsControlFlow() const OVERRIDE { return true; }
3091
3092 bool NeedsEnvironment() const OVERRIDE { return true; }
3093
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003094 bool CanThrow() const OVERRIDE { return true; }
3095
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003096 uint32_t GetDexPc() const { return dex_pc_; }
3097
3098 DECLARE_INSTRUCTION(Throw);
3099
3100 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003101 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003102
3103 DISALLOW_COPY_AND_ASSIGN(HThrow);
3104};
3105
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003106class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003107 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003108 HInstanceOf(HInstruction* object,
3109 HLoadClass* constant,
3110 bool class_is_final,
3111 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003112 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3113 class_is_final_(class_is_final),
3114 dex_pc_(dex_pc) {
3115 SetRawInputAt(0, object);
3116 SetRawInputAt(1, constant);
3117 }
3118
3119 bool CanBeMoved() const OVERRIDE { return true; }
3120
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003121 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003122 return true;
3123 }
3124
3125 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003126 return false;
3127 }
3128
3129 uint32_t GetDexPc() const { return dex_pc_; }
3130
3131 bool IsClassFinal() const { return class_is_final_; }
3132
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003133 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003134
3135 private:
3136 const bool class_is_final_;
3137 const uint32_t dex_pc_;
3138
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003139 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3140};
3141
Calin Juravleb1498f62015-02-16 13:13:29 +00003142class HBoundType : public HExpression<1> {
3143 public:
3144 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3145 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3146 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003147 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003148 SetRawInputAt(0, input);
3149 }
3150
3151 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3152
3153 bool CanBeNull() const OVERRIDE {
3154 // `null instanceof ClassX` always return false so we can't be null.
3155 return false;
3156 }
3157
3158 DECLARE_INSTRUCTION(BoundType);
3159
3160 private:
3161 // Encodes the most upper class that this instruction can have. In other words
3162 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3163 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3164 const ReferenceTypeInfo bound_type_;
3165
3166 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3167};
3168
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003169class HCheckCast : public HTemplateInstruction<2> {
3170 public:
3171 HCheckCast(HInstruction* object,
3172 HLoadClass* constant,
3173 bool class_is_final,
3174 uint32_t dex_pc)
3175 : HTemplateInstruction(SideEffects::None()),
3176 class_is_final_(class_is_final),
3177 dex_pc_(dex_pc) {
3178 SetRawInputAt(0, object);
3179 SetRawInputAt(1, constant);
3180 }
3181
3182 bool CanBeMoved() const OVERRIDE { return true; }
3183
3184 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3185 return true;
3186 }
3187
3188 bool NeedsEnvironment() const OVERRIDE {
3189 // Instruction may throw a CheckCastError.
3190 return true;
3191 }
3192
3193 bool CanThrow() const OVERRIDE { return true; }
3194
3195 uint32_t GetDexPc() const { return dex_pc_; }
3196
3197 bool IsClassFinal() const { return class_is_final_; }
3198
3199 DECLARE_INSTRUCTION(CheckCast);
3200
3201 private:
3202 const bool class_is_final_;
3203 const uint32_t dex_pc_;
3204
3205 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003206};
3207
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003208class HMonitorOperation : public HTemplateInstruction<1> {
3209 public:
3210 enum OperationKind {
3211 kEnter,
3212 kExit,
3213 };
3214
3215 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3216 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3217 SetRawInputAt(0, object);
3218 }
3219
3220 // Instruction may throw a Java exception, so we need an environment.
3221 bool NeedsEnvironment() const OVERRIDE { return true; }
3222 bool CanThrow() const OVERRIDE { return true; }
3223
3224 uint32_t GetDexPc() const { return dex_pc_; }
3225
3226 bool IsEnter() const { return kind_ == kEnter; }
3227
3228 DECLARE_INSTRUCTION(MonitorOperation);
3229
Calin Juravle52c48962014-12-16 17:02:57 +00003230 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003231 const OperationKind kind_;
3232 const uint32_t dex_pc_;
3233
3234 private:
3235 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3236};
3237
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003238class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003239 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003240 MoveOperands(Location source, Location destination, HInstruction* instruction)
3241 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003242
3243 Location GetSource() const { return source_; }
3244 Location GetDestination() const { return destination_; }
3245
3246 void SetSource(Location value) { source_ = value; }
3247 void SetDestination(Location value) { destination_ = value; }
3248
3249 // The parallel move resolver marks moves as "in-progress" by clearing the
3250 // destination (but not the source).
3251 Location MarkPending() {
3252 DCHECK(!IsPending());
3253 Location dest = destination_;
3254 destination_ = Location::NoLocation();
3255 return dest;
3256 }
3257
3258 void ClearPending(Location dest) {
3259 DCHECK(IsPending());
3260 destination_ = dest;
3261 }
3262
3263 bool IsPending() const {
3264 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3265 return destination_.IsInvalid() && !source_.IsInvalid();
3266 }
3267
3268 // True if this blocks a move from the given location.
3269 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003270 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003271 }
3272
3273 // A move is redundant if it's been eliminated, if its source and
3274 // destination are the same, or if its destination is unneeded.
3275 bool IsRedundant() const {
3276 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3277 }
3278
3279 // We clear both operands to indicate move that's been eliminated.
3280 void Eliminate() {
3281 source_ = destination_ = Location::NoLocation();
3282 }
3283
3284 bool IsEliminated() const {
3285 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3286 return source_.IsInvalid();
3287 }
3288
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003289 HInstruction* GetInstruction() const { return instruction_; }
3290
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003291 private:
3292 Location source_;
3293 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003294 // The instruction this move is assocatied with. Null when this move is
3295 // for moving an input in the expected locations of user (including a phi user).
3296 // This is only used in debug mode, to ensure we do not connect interval siblings
3297 // in the same parallel move.
3298 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003299};
3300
3301static constexpr size_t kDefaultNumberOfMoves = 4;
3302
3303class HParallelMove : public HTemplateInstruction<0> {
3304 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003305 explicit HParallelMove(ArenaAllocator* arena)
3306 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003307
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003308 void AddMove(Location source, Location destination, HInstruction* instruction) {
3309 DCHECK(source.IsValid());
3310 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003311 if (kIsDebugBuild) {
3312 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003313 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003314 if (moves_.Get(i).GetInstruction() == instruction) {
3315 // Special case the situation where the move is for the spill slot
3316 // of the instruction.
3317 if ((GetPrevious() == instruction)
3318 || ((GetPrevious() == nullptr)
3319 && instruction->IsPhi()
3320 && instruction->GetBlock() == GetBlock())) {
3321 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3322 << "Doing parallel moves for the same instruction.";
3323 } else {
3324 DCHECK(false) << "Doing parallel moves for the same instruction.";
3325 }
3326 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003327 }
3328 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003329 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3330 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3331 << "Same destination for two moves in a parallel move.";
3332 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003333 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003334 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003335 }
3336
3337 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003338 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003339 }
3340
3341 size_t NumMoves() const { return moves_.Size(); }
3342
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003343 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003344
3345 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003346 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003347
3348 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3349};
3350
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003351class HGraphVisitor : public ValueObject {
3352 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003353 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3354 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003355
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003356 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003357 virtual void VisitBasicBlock(HBasicBlock* block);
3358
Roland Levillain633021e2014-10-01 14:12:25 +01003359 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003360 void VisitInsertionOrder();
3361
Roland Levillain633021e2014-10-01 14:12:25 +01003362 // Visit the graph following dominator tree reverse post-order.
3363 void VisitReversePostOrder();
3364
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003365 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003366
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003367 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003368#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003369 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3370
3371 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3372
3373#undef DECLARE_VISIT_INSTRUCTION
3374
3375 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003376 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003377
3378 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3379};
3380
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003381class HGraphDelegateVisitor : public HGraphVisitor {
3382 public:
3383 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3384 virtual ~HGraphDelegateVisitor() {}
3385
3386 // Visit functions that delegate to to super class.
3387#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003388 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003389
3390 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3391
3392#undef DECLARE_VISIT_INSTRUCTION
3393
3394 private:
3395 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3396};
3397
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003398class HInsertionOrderIterator : public ValueObject {
3399 public:
3400 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3401
3402 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3403 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3404 void Advance() { ++index_; }
3405
3406 private:
3407 const HGraph& graph_;
3408 size_t index_;
3409
3410 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3411};
3412
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003413class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003414 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003415 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003416
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003417 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3418 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003419 void Advance() { ++index_; }
3420
3421 private:
3422 const HGraph& graph_;
3423 size_t index_;
3424
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003425 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003426};
3427
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003428class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003429 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003430 explicit HPostOrderIterator(const HGraph& graph)
3431 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003432
3433 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003434 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003435 void Advance() { --index_; }
3436
3437 private:
3438 const HGraph& graph_;
3439 size_t index_;
3440
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003441 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003442};
3443
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003444// Iterator over the blocks that art part of the loop. Includes blocks part
3445// of an inner loop. The order in which the blocks are iterated is on their
3446// block id.
3447class HBlocksInLoopIterator : public ValueObject {
3448 public:
3449 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3450 : blocks_in_loop_(info.GetBlocks()),
3451 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3452 index_(0) {
3453 if (!blocks_in_loop_.IsBitSet(index_)) {
3454 Advance();
3455 }
3456 }
3457
3458 bool Done() const { return index_ == blocks_.Size(); }
3459 HBasicBlock* Current() const { return blocks_.Get(index_); }
3460 void Advance() {
3461 ++index_;
3462 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3463 if (blocks_in_loop_.IsBitSet(index_)) {
3464 break;
3465 }
3466 }
3467 }
3468
3469 private:
3470 const BitVector& blocks_in_loop_;
3471 const GrowableArray<HBasicBlock*>& blocks_;
3472 size_t index_;
3473
3474 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3475};
3476
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003477} // namespace art
3478
3479#endif // ART_COMPILER_OPTIMIZING_NODES_H_