blob: 4409a4e8c3ecb0a54cdf508b75551c27ab5d281e [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 Geoffraye53798a2014-12-01 10:31:54 +0000106 HGraph(ArenaAllocator* arena, 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 Geoffraye53798a2014-12-01 10:31:54 +0000117 current_instruction_id_(start_instruction_id) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000118
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000119 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100120 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100121 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000122
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000123 HBasicBlock* GetEntryBlock() const { return entry_block_; }
124 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000125
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000126 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
127 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000128
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000129 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100130
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000131 // Try building the SSA form of this graph, with dominance computation and loop
132 // recognition. Returns whether it was successful in doing all these steps.
133 bool TryBuildingSsa() {
134 BuildDominatorTree();
135 TransformToSsa();
136 return AnalyzeNaturalLoops();
137 }
138
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000139 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000140 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100141 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000142
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000143 // Analyze all natural loops in this graph. Returns false if one
144 // loop is not natural, that is the header does not dominate the
145 // back edge.
146 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100147
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000148 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
149 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
150
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100151 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
152 void SimplifyLoop(HBasicBlock* header);
153
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000154 int32_t GetNextInstructionId() {
155 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000156 return current_instruction_id_++;
157 }
158
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000159 int32_t GetCurrentInstructionId() const {
160 return current_instruction_id_;
161 }
162
163 void SetCurrentInstructionId(int32_t id) {
164 current_instruction_id_ = id;
165 }
166
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100167 uint16_t GetMaximumNumberOfOutVRegs() const {
168 return maximum_number_of_out_vregs_;
169 }
170
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000171 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
172 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100173 }
174
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000175 void UpdateTemporariesVRegSlots(size_t slots) {
176 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100177 }
178
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000179 size_t GetTemporariesVRegSlots() const {
180 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100181 }
182
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100183 void SetNumberOfVRegs(uint16_t number_of_vregs) {
184 number_of_vregs_ = number_of_vregs;
185 }
186
187 uint16_t GetNumberOfVRegs() const {
188 return number_of_vregs_;
189 }
190
191 void SetNumberOfInVRegs(uint16_t value) {
192 number_of_in_vregs_ = value;
193 }
194
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100195 uint16_t GetNumberOfLocalVRegs() const {
196 return number_of_vregs_ - number_of_in_vregs_;
197 }
198
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100199 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
200 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100201 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100202
Mingyao Yange4335eb2015-03-02 15:14:13 -0800203 bool HasArrayAccesses() const {
204 return has_array_accesses_;
205 }
206
207 void SetHasArrayAccesses(bool value) {
208 has_array_accesses_ = value;
209 }
210
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000211 HNullConstant* GetNullConstant();
212
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000213 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000214 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
215 void VisitBlockForDominatorTree(HBasicBlock* block,
216 HBasicBlock* predecessor,
217 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100218 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000219 void VisitBlockForBackEdges(HBasicBlock* block,
220 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100221 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000222 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000223 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
224
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000225 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000226
227 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000228 GrowableArray<HBasicBlock*> blocks_;
229
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100230 // List of blocks to perform a reverse post order tree traversal.
231 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000232
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000233 HBasicBlock* entry_block_;
234 HBasicBlock* exit_block_;
235
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100236 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100237 uint16_t maximum_number_of_out_vregs_;
238
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100239 // The number of virtual registers in this method. Contains the parameters.
240 uint16_t number_of_vregs_;
241
242 // The number of virtual registers used by parameters of this method.
243 uint16_t number_of_in_vregs_;
244
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000245 // Number of vreg size slots that the temporaries use (used in baseline compiler).
246 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100247
Mingyao Yange4335eb2015-03-02 15:14:13 -0800248 // Has array accesses. We can totally skip BCE if it's false.
249 bool has_array_accesses_;
250
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000251 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000252 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000253
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000254 // Cached null constant that might be created when building SSA form.
255 HNullConstant* cached_null_constant_;
256
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000257 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000258 DISALLOW_COPY_AND_ASSIGN(HGraph);
259};
260
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700261class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000262 public:
263 HLoopInformation(HBasicBlock* header, HGraph* graph)
264 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100265 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100266 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100267 // Make bit vector growable, as the number of blocks may change.
268 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100269
270 HBasicBlock* GetHeader() const {
271 return header_;
272 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000273
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000274 void SetHeader(HBasicBlock* block) {
275 header_ = block;
276 }
277
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100278 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
279 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
280 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
281
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000282 void AddBackEdge(HBasicBlock* back_edge) {
283 back_edges_.Add(back_edge);
284 }
285
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100286 void RemoveBackEdge(HBasicBlock* back_edge) {
287 back_edges_.Delete(back_edge);
288 }
289
290 bool IsBackEdge(HBasicBlock* block) {
291 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
292 if (back_edges_.Get(i) == block) return true;
293 }
294 return false;
295 }
296
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000297 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000298 return back_edges_.Size();
299 }
300
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100301 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100302
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100303 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
304 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100305 }
306
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100307 void ClearBackEdges() {
308 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100309 }
310
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100311 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
312 // that is the header dominates the back edge.
313 bool Populate();
314
315 // Returns whether this loop information contains `block`.
316 // Note that this loop information *must* be populated before entering this function.
317 bool Contains(const HBasicBlock& block) const;
318
319 // Returns whether this loop information is an inner loop of `other`.
320 // Note that `other` *must* be populated before entering this function.
321 bool IsIn(const HLoopInformation& other) const;
322
323 const ArenaBitVector& GetBlocks() const { return blocks_; }
324
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000325 void Add(HBasicBlock* block);
326
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000327 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100328 // Internal recursive implementation of `Populate`.
329 void PopulateRecursive(HBasicBlock* block);
330
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000331 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100332 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000333 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100334 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000335
336 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
337};
338
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100339static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100340static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100341
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000342// A block in a method. Contains the list of instructions represented
343// as a double linked list. Each block knows its predecessors and
344// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100345
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700346class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000347 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100348 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000349 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000350 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
351 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000352 loop_information_(nullptr),
353 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100354 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100355 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100356 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100357 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000358 lifetime_end_(kNoLifetime),
359 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000360
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100361 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
362 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000363 }
364
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100365 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
366 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000367 }
368
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100369 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
370 return dominated_blocks_;
371 }
372
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100373 bool IsEntryBlock() const {
374 return graph_->GetEntryBlock() == this;
375 }
376
377 bool IsExitBlock() const {
378 return graph_->GetExitBlock() == this;
379 }
380
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000381 void AddBackEdge(HBasicBlock* back_edge) {
382 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000383 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000384 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100385 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000386 loop_information_->AddBackEdge(back_edge);
387 }
388
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000389 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000390 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000391
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000392 int GetBlockId() const { return block_id_; }
393 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000394
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000395 HBasicBlock* GetDominator() const { return dominator_; }
396 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100397 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000398 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
399 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
400 if (dominated_blocks_.Get(i) == existing) {
401 dominated_blocks_.Put(i, new_block);
402 return;
403 }
404 }
405 LOG(FATAL) << "Unreachable";
406 UNREACHABLE();
407 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000408
409 int NumberOfBackEdges() const {
410 return loop_information_ == nullptr
411 ? 0
412 : loop_information_->NumberOfBackEdges();
413 }
414
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100415 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
416 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100417 const HInstructionList& GetInstructions() const { return instructions_; }
418 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100419 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000420
421 void AddSuccessor(HBasicBlock* block) {
422 successors_.Add(block);
423 block->predecessors_.Add(this);
424 }
425
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100426 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
427 size_t successor_index = GetSuccessorIndexOf(existing);
428 DCHECK_NE(successor_index, static_cast<size_t>(-1));
429 existing->RemovePredecessor(this);
430 new_block->predecessors_.Add(this);
431 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000432 }
433
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000434 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
435 size_t predecessor_index = GetPredecessorIndexOf(existing);
436 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
437 existing->RemoveSuccessor(this);
438 new_block->successors_.Add(this);
439 predecessors_.Put(predecessor_index, new_block);
440 }
441
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100442 void RemovePredecessor(HBasicBlock* block) {
443 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100444 }
445
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000446 void RemoveSuccessor(HBasicBlock* block) {
447 successors_.Delete(block);
448 }
449
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100450 void ClearAllPredecessors() {
451 predecessors_.Reset();
452 }
453
454 void AddPredecessor(HBasicBlock* block) {
455 predecessors_.Add(block);
456 block->successors_.Add(this);
457 }
458
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100459 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100460 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100461 HBasicBlock* temp = predecessors_.Get(0);
462 predecessors_.Put(0, predecessors_.Get(1));
463 predecessors_.Put(1, temp);
464 }
465
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100466 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
467 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
468 if (predecessors_.Get(i) == predecessor) {
469 return i;
470 }
471 }
472 return -1;
473 }
474
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100475 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
476 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
477 if (successors_.Get(i) == successor) {
478 return i;
479 }
480 }
481 return -1;
482 }
483
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000484 // Split the block into two blocks just after `cursor`. Returns the newly
485 // created block. Note that this method just updates raw block information,
486 // like predecessors, successors, dominators, and instruction list. It does not
487 // update the graph, reverse post order, loop information, nor make sure the
488 // blocks are consistent (for example ending with a control flow instruction).
489 HBasicBlock* SplitAfter(HInstruction* cursor);
490
491 // Merge `other` at the end of `this`. Successors and dominated blocks of
492 // `other` are changed to be successors and dominated blocks of `this`. Note
493 // that this method does not update the graph, reverse post order, loop
494 // information, nor make sure the blocks are consistent (for example ending
495 // with a control flow instruction).
496 void MergeWith(HBasicBlock* other);
497
498 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
499 // of `this` are moved to `other`.
500 // Note that this method does not update the graph, reverse post order, loop
501 // information, nor make sure the blocks are consistent (for example ending
502 void ReplaceWith(HBasicBlock* other);
503
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000504 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100505 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100506 // Replace instruction `initial` with `replacement` within this block.
507 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
508 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100509 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100510 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000511 // RemoveInstruction and RemovePhi delete a given instruction from the respective
512 // instruction list. With 'ensure_safety' set to true, it verifies that the
513 // instruction is not in use and removes it from the use lists of its inputs.
514 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
515 void RemovePhi(HPhi* phi, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100516
517 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100518 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100519 }
520
Roland Levillain6b879dd2014-09-22 17:13:44 +0100521 bool IsLoopPreHeaderFirstPredecessor() const {
522 DCHECK(IsLoopHeader());
523 DCHECK(!GetPredecessors().IsEmpty());
524 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
525 }
526
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100527 HLoopInformation* GetLoopInformation() const {
528 return loop_information_;
529 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000530
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000531 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100532 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000533 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100534 void SetInLoop(HLoopInformation* info) {
535 if (IsLoopHeader()) {
536 // Nothing to do. This just means `info` is an outer loop.
537 } else if (loop_information_ == nullptr) {
538 loop_information_ = info;
539 } else if (loop_information_->Contains(*info->GetHeader())) {
540 // Block is currently part of an outer loop. Make it part of this inner loop.
541 // Note that a non loop header having a loop information means this loop information
542 // has already been populated
543 loop_information_ = info;
544 } else {
545 // Block is part of an inner loop. Do not update the loop information.
546 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
547 // at this point, because this method is being called while populating `info`.
548 }
549 }
550
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000551 // Raw update of the loop information.
552 void SetLoopInformation(HLoopInformation* info) {
553 loop_information_ = info;
554 }
555
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100556 bool IsInLoop() const { return loop_information_ != nullptr; }
557
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100558 // Returns wheter this block dominates the blocked passed as parameter.
559 bool Dominates(HBasicBlock* block) const;
560
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100561 size_t GetLifetimeStart() const { return lifetime_start_; }
562 size_t GetLifetimeEnd() const { return lifetime_end_; }
563
564 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
565 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
566
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100567 uint32_t GetDexPc() const { return dex_pc_; }
568
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000569 bool IsCatchBlock() const { return is_catch_block_; }
570 void SetIsCatchBlock() { is_catch_block_ = true; }
571
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000572 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000573 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000574 GrowableArray<HBasicBlock*> predecessors_;
575 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100576 HInstructionList instructions_;
577 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000578 HLoopInformation* loop_information_;
579 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100580 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000581 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100582 // The dex program counter of the first instruction of this block.
583 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100584 size_t lifetime_start_;
585 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000586 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000587
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000588 friend class HGraph;
589 friend class HInstruction;
590
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000591 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
592};
593
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100594#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
595 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000596 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000597 M(ArrayGet, Instruction) \
598 M(ArrayLength, Instruction) \
599 M(ArraySet, Instruction) \
600 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000601 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000602 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100603 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000604 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100605 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000606 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000607 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000608 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100609 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000610 M(Exit, Instruction) \
611 M(FloatConstant, Constant) \
612 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100613 M(GreaterThan, Condition) \
614 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100615 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000616 M(InstanceFieldGet, Instruction) \
617 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000618 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100619 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000620 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000621 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100622 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000623 M(LessThan, Condition) \
624 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000625 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000626 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100627 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000628 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100629 M(Local, Instruction) \
630 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000631 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000632 M(Mul, BinaryOperation) \
633 M(Neg, UnaryOperation) \
634 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100635 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100636 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000637 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000638 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000639 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000640 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100641 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000642 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100643 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000644 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100645 M(Return, Instruction) \
646 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000647 M(Shl, BinaryOperation) \
648 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100649 M(StaticFieldGet, Instruction) \
650 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100651 M(StoreLocal, Instruction) \
652 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100653 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000654 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000655 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000656 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000657 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000658 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000659
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100660#define FOR_EACH_INSTRUCTION(M) \
661 FOR_EACH_CONCRETE_INSTRUCTION(M) \
662 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100663 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100664 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100665 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700666
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100667#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000668FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
669#undef FORWARD_DECLARATION
670
Roland Levillainccc07a92014-09-16 14:48:16 +0100671#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000672 InstructionKind GetKind() const OVERRIDE { return k##type; } \
673 const char* DebugName() const OVERRIDE { return #type; } \
674 const H##type* As##type() const OVERRIDE { return this; } \
675 H##type* As##type() OVERRIDE { return this; } \
676 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100677 return other->Is##type(); \
678 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000679 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000680
David Brazdiled596192015-01-23 10:39:45 +0000681template <typename T> class HUseList;
682
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100683template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700684class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000685 public:
David Brazdiled596192015-01-23 10:39:45 +0000686 HUseListNode* GetPrevious() const { return prev_; }
687 HUseListNode* GetNext() const { return next_; }
688 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100689 size_t GetIndex() const { return index_; }
690
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000691 private:
David Brazdiled596192015-01-23 10:39:45 +0000692 HUseListNode(T user, size_t index)
693 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
694
695 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100696 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000697 HUseListNode<T>* prev_;
698 HUseListNode<T>* next_;
699
700 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000701
702 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
703};
704
David Brazdiled596192015-01-23 10:39:45 +0000705template <typename T>
706class HUseList : public ValueObject {
707 public:
708 HUseList() : first_(nullptr) {}
709
710 void Clear() {
711 first_ = nullptr;
712 }
713
714 // Adds a new entry at the beginning of the use list and returns
715 // the newly created node.
716 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000717 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000718 if (IsEmpty()) {
719 first_ = new_node;
720 } else {
721 first_->prev_ = new_node;
722 new_node->next_ = first_;
723 first_ = new_node;
724 }
725 return new_node;
726 }
727
728 HUseListNode<T>* GetFirst() const {
729 return first_;
730 }
731
732 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000733 DCHECK(node != nullptr);
734 DCHECK(Contains(node));
735
David Brazdiled596192015-01-23 10:39:45 +0000736 if (node->prev_ != nullptr) {
737 node->prev_->next_ = node->next_;
738 }
739 if (node->next_ != nullptr) {
740 node->next_->prev_ = node->prev_;
741 }
742 if (node == first_) {
743 first_ = node->next_;
744 }
745 }
746
David Brazdil1abb4192015-02-17 18:33:36 +0000747 bool Contains(const HUseListNode<T>* node) const {
748 if (node == nullptr) {
749 return false;
750 }
751 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
752 if (current == node) {
753 return true;
754 }
755 }
756 return false;
757 }
758
David Brazdiled596192015-01-23 10:39:45 +0000759 bool IsEmpty() const {
760 return first_ == nullptr;
761 }
762
763 bool HasOnlyOneUse() const {
764 return first_ != nullptr && first_->next_ == nullptr;
765 }
766
767 private:
768 HUseListNode<T>* first_;
769};
770
771template<typename T>
772class HUseIterator : public ValueObject {
773 public:
774 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
775
776 bool Done() const { return current_ == nullptr; }
777
778 void Advance() {
779 DCHECK(!Done());
780 current_ = current_->GetNext();
781 }
782
783 HUseListNode<T>* Current() const {
784 DCHECK(!Done());
785 return current_;
786 }
787
788 private:
789 HUseListNode<T>* current_;
790
791 friend class HValue;
792};
793
David Brazdil1abb4192015-02-17 18:33:36 +0000794// This class is used by HEnvironment and HInstruction classes to record the
795// instructions they use and pointers to the corresponding HUseListNodes kept
796// by the used instructions.
797template <typename T>
798class HUserRecord : public ValueObject {
799 public:
800 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
801 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
802
803 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
804 : instruction_(old_record.instruction_), use_node_(use_node) {
805 DCHECK(instruction_ != nullptr);
806 DCHECK(use_node_ != nullptr);
807 DCHECK(old_record.use_node_ == nullptr);
808 }
809
810 HInstruction* GetInstruction() const { return instruction_; }
811 HUseListNode<T>* GetUseNode() const { return use_node_; }
812
813 private:
814 // Instruction used by the user.
815 HInstruction* instruction_;
816
817 // Corresponding entry in the use list kept by 'instruction_'.
818 HUseListNode<T>* use_node_;
819};
820
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100821// Represents the side effects an instruction may have.
822class SideEffects : public ValueObject {
823 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100824 SideEffects() : flags_(0) {}
825
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100826 static SideEffects None() {
827 return SideEffects(0);
828 }
829
830 static SideEffects All() {
831 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
832 }
833
834 static SideEffects ChangesSomething() {
835 return SideEffects((1 << kFlagChangesCount) - 1);
836 }
837
838 static SideEffects DependsOnSomething() {
839 int count = kFlagDependsOnCount - kFlagChangesCount;
840 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
841 }
842
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100843 SideEffects Union(SideEffects other) const {
844 return SideEffects(flags_ | other.flags_);
845 }
846
Roland Levillain72bceff2014-09-15 18:29:00 +0100847 bool HasSideEffects() const {
848 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
849 return (flags_ & all_bits_set) != 0;
850 }
851
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100852 bool HasAllSideEffects() const {
853 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
854 return all_bits_set == (flags_ & all_bits_set);
855 }
856
857 bool DependsOn(SideEffects other) const {
858 size_t depends_flags = other.ComputeDependsFlags();
859 return (flags_ & depends_flags) != 0;
860 }
861
862 bool HasDependencies() const {
863 int count = kFlagDependsOnCount - kFlagChangesCount;
864 size_t all_bits_set = (1 << count) - 1;
865 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
866 }
867
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100868 private:
869 static constexpr int kFlagChangesSomething = 0;
870 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
871
872 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
873 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
874
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100875 explicit SideEffects(size_t flags) : flags_(flags) {}
876
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100877 size_t ComputeDependsFlags() const {
878 return flags_ << kFlagChangesCount;
879 }
880
881 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100882};
883
David Brazdiled596192015-01-23 10:39:45 +0000884// A HEnvironment object contains the values of virtual registers at a given location.
885class HEnvironment : public ArenaObject<kArenaAllocMisc> {
886 public:
887 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
888 : vregs_(arena, number_of_vregs) {
889 vregs_.SetSize(number_of_vregs);
890 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000891 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000892 }
893 }
894
895 void CopyFrom(HEnvironment* env);
896
897 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +0000898 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +0000899 }
900
901 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +0000902 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +0000903 }
904
David Brazdil1abb4192015-02-17 18:33:36 +0000905 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +0000906
907 size_t Size() const { return vregs_.Size(); }
908
909 private:
David Brazdil1abb4192015-02-17 18:33:36 +0000910 // Record instructions' use entries of this environment for constant-time removal.
911 // It should only be called by HInstruction when a new environment use is added.
912 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
913 DCHECK(env_use->GetUser() == this);
914 size_t index = env_use->GetIndex();
915 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
916 }
David Brazdiled596192015-01-23 10:39:45 +0000917
David Brazdil1abb4192015-02-17 18:33:36 +0000918 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +0000919
David Brazdil1abb4192015-02-17 18:33:36 +0000920 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +0000921
922 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
923};
924
Calin Juravleacf735c2015-02-12 15:25:22 +0000925class ReferenceTypeInfo : ValueObject {
926 public:
Calin Juravleb1498f62015-02-16 13:13:29 +0000927 typedef Handle<mirror::Class> TypeHandle;
928
929 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
930 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
931 if (type_handle->IsObjectClass()) {
932 // Override the type handle to be consistent with the case when we get to
933 // Top but don't have the Object class available. It avoids having to guess
934 // what value the type_handle has when it's Top.
935 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
936 } else {
937 return ReferenceTypeInfo(type_handle, is_exact, false);
938 }
939 }
940
941 static ReferenceTypeInfo CreateTop(bool is_exact) {
942 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +0000943 }
944
945 bool IsExact() const { return is_exact_; }
946 bool IsTop() const { return is_top_; }
947
948 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
949
Calin Juravleb1498f62015-02-16 13:13:29 +0000950 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000951 if (IsTop()) {
952 // Top (equivalent for java.lang.Object) is supertype of anything.
953 return true;
954 }
955 if (rti.IsTop()) {
956 // If we get here `this` is not Top() so it can't be a supertype.
957 return false;
958 }
959 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
960 }
961
962 // Returns true if the type information provide the same amount of details.
963 // Note that it does not mean that the instructions have the same actual type
964 // (e.g. tops are equal but they can be the result of a merge).
965 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
966 if (IsExact() != rti.IsExact()) {
967 return false;
968 }
969 if (IsTop() && rti.IsTop()) {
970 // `Top` means java.lang.Object, so the types are equivalent.
971 return true;
972 }
973 if (IsTop() || rti.IsTop()) {
974 // If only one is top or object than they are not equivalent.
975 // NB: We need this extra check because the type_handle of `Top` is invalid
976 // and we cannot inspect its reference.
977 return false;
978 }
979
980 // Finally check the types.
981 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
982 }
983
984 private:
Calin Juravleb1498f62015-02-16 13:13:29 +0000985 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
986 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
987 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
988
Calin Juravleacf735c2015-02-12 15:25:22 +0000989 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +0000990 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +0000991 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +0000992 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +0000993 bool is_exact_;
994 // A true value here means that the object type should be java.lang.Object.
995 // We don't have access to the corresponding mirror object every time so this
996 // flag acts as a substitute. When true, the TypeHandle refers to a null
997 // pointer and should not be used.
998 bool is_top_;
999};
1000
1001std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1002
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001003class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001004 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001005 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001006 : previous_(nullptr),
1007 next_(nullptr),
1008 block_(nullptr),
1009 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001010 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001011 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001012 locations_(nullptr),
1013 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001014 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001015 side_effects_(side_effects),
1016 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001017
Dave Allison20dfc792014-06-16 20:44:29 -07001018 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001019
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001020#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001021 enum InstructionKind {
1022 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1023 };
1024#undef DECLARE_KIND
1025
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001026 HInstruction* GetNext() const { return next_; }
1027 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001028
Calin Juravle77520bc2015-01-12 18:45:46 +00001029 HInstruction* GetNextDisregardingMoves() const;
1030 HInstruction* GetPreviousDisregardingMoves() const;
1031
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001032 HBasicBlock* GetBlock() const { return block_; }
1033 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001034 bool IsInBlock() const { return block_ != nullptr; }
1035 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001036 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001037
Roland Levillain6b879dd2014-09-22 17:13:44 +01001038 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001039 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001040
1041 virtual void Accept(HGraphVisitor* visitor) = 0;
1042 virtual const char* DebugName() const = 0;
1043
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001044 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001045 void SetRawInputAt(size_t index, HInstruction* input) {
1046 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1047 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001048
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001049 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001050 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001051 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001052 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001053
Calin Juravle61d544b2015-02-23 16:46:57 +00001054 virtual bool ActAsNullConstant() const { return false; }
1055
Calin Juravle10e244f2015-01-26 18:54:32 +00001056 // Does not apply for all instructions, but having this at top level greatly
1057 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001058 virtual bool CanBeNull() const {
1059 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1060 return true;
1061 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001062
Calin Juravle77520bc2015-01-12 18:45:46 +00001063 virtual bool CanDoImplicitNullCheck() const { return false; }
1064
Calin Juravleacf735c2015-02-12 15:25:22 +00001065 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001066 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001067 reference_type_info_ = reference_type_info;
1068 }
1069
Calin Juravle61d544b2015-02-23 16:46:57 +00001070 ReferenceTypeInfo GetReferenceTypeInfo() const {
1071 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1072 return reference_type_info_;
1073 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001074
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001075 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001076 DCHECK(user != nullptr);
1077 HUseListNode<HInstruction*>* use =
1078 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1079 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001080 }
1081
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001082 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001083 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001084 HUseListNode<HEnvironment*>* env_use =
1085 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1086 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001087 }
1088
David Brazdil1abb4192015-02-17 18:33:36 +00001089 void RemoveAsUserOfInput(size_t input) {
1090 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1091 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1092 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001093
David Brazdil1abb4192015-02-17 18:33:36 +00001094 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1095 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001096
David Brazdiled596192015-01-23 10:39:45 +00001097 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1098 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001099
Roland Levillain6c82d402014-10-13 16:10:27 +01001100 // Does this instruction strictly dominate `other_instruction`?
1101 // Returns false if this instruction and `other_instruction` are the same.
1102 // Aborts if this instruction and `other_instruction` are both phis.
1103 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001104
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001105 int GetId() const { return id_; }
1106 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001107
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001108 int GetSsaIndex() const { return ssa_index_; }
1109 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1110 bool HasSsaIndex() const { return ssa_index_ != -1; }
1111
1112 bool HasEnvironment() const { return environment_ != nullptr; }
1113 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001114 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
1115
Nicolas Geoffray39468442014-09-02 15:17:15 +01001116 // Returns the number of entries in the environment. Typically, that is the
1117 // number of dex registers in a method. It could be more in case of inlining.
1118 size_t EnvironmentSize() const;
1119
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001120 LocationSummary* GetLocations() const { return locations_; }
1121 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001122
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001123 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001124 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001125
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001126 // Move `this` instruction before `cursor`.
1127 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001128
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001129#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001130 bool Is##type() const { return (As##type() != nullptr); } \
1131 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001132 virtual H##type* As##type() { return nullptr; }
1133
1134 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1135#undef INSTRUCTION_TYPE_CHECK
1136
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001137 // Returns whether the instruction can be moved within the graph.
1138 virtual bool CanBeMoved() const { return false; }
1139
1140 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001141 virtual bool InstructionTypeEquals(HInstruction* other) const {
1142 UNUSED(other);
1143 return false;
1144 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001145
1146 // Returns whether any data encoded in the two instructions is equal.
1147 // This method does not look at the inputs. Both instructions must be
1148 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001149 virtual bool InstructionDataEquals(HInstruction* other) const {
1150 UNUSED(other);
1151 return false;
1152 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001153
1154 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001155 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001156 // 2) Their inputs are identical.
1157 bool Equals(HInstruction* other) const;
1158
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001159 virtual InstructionKind GetKind() const = 0;
1160
1161 virtual size_t ComputeHashCode() const {
1162 size_t result = GetKind();
1163 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1164 result = (result * 31) + InputAt(i)->GetId();
1165 }
1166 return result;
1167 }
1168
1169 SideEffects GetSideEffects() const { return side_effects_; }
1170
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001171 size_t GetLifetimePosition() const { return lifetime_position_; }
1172 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1173 LiveInterval* GetLiveInterval() const { return live_interval_; }
1174 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1175 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1176
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001177 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1178
1179 // Returns whether the code generation of the instruction will require to have access
1180 // to the current method. Such instructions are:
1181 // (1): Instructions that require an environment, as calling the runtime requires
1182 // to walk the stack and have the current method stored at a specific stack address.
1183 // (2): Object literals like classes and strings, that are loaded from the dex cache
1184 // fields of the current method.
1185 bool NeedsCurrentMethod() const {
1186 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1187 }
1188
David Brazdil1abb4192015-02-17 18:33:36 +00001189 protected:
1190 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1191 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1192
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001193 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001194 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1195
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001196 HInstruction* previous_;
1197 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001198 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001199
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001200 // An instruction gets an id when it is added to the graph.
1201 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001202 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001203 int id_;
1204
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001205 // When doing liveness analysis, instructions that have uses get an SSA index.
1206 int ssa_index_;
1207
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001208 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001209 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001210
1211 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001212 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001213
Nicolas Geoffray39468442014-09-02 15:17:15 +01001214 // The environment associated with this instruction. Not null if the instruction
1215 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001216 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001217
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001218 // Set by the code generator.
1219 LocationSummary* locations_;
1220
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001221 // Set by the liveness analysis.
1222 LiveInterval* live_interval_;
1223
1224 // Set by the liveness analysis, this is the position in a linear
1225 // order of blocks where this instruction's live interval start.
1226 size_t lifetime_position_;
1227
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001228 const SideEffects side_effects_;
1229
Calin Juravleacf735c2015-02-12 15:25:22 +00001230 // TODO: for primitive types this should be marked as invalid.
1231 ReferenceTypeInfo reference_type_info_;
1232
David Brazdil1abb4192015-02-17 18:33:36 +00001233 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001234 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001235 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001236 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001237 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001238
1239 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1240};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001241std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001242
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001243class HInputIterator : public ValueObject {
1244 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001245 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001246
1247 bool Done() const { return index_ == instruction_->InputCount(); }
1248 HInstruction* Current() const { return instruction_->InputAt(index_); }
1249 void Advance() { index_++; }
1250
1251 private:
1252 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001253 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001254
1255 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1256};
1257
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001258class HInstructionIterator : public ValueObject {
1259 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001260 explicit HInstructionIterator(const HInstructionList& instructions)
1261 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001262 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001263 }
1264
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001265 bool Done() const { return instruction_ == nullptr; }
1266 HInstruction* Current() const { return instruction_; }
1267 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001268 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001269 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001270 }
1271
1272 private:
1273 HInstruction* instruction_;
1274 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001275
1276 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001277};
1278
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001279class HBackwardInstructionIterator : public ValueObject {
1280 public:
1281 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1282 : instruction_(instructions.last_instruction_) {
1283 next_ = Done() ? nullptr : instruction_->GetPrevious();
1284 }
1285
1286 bool Done() const { return instruction_ == nullptr; }
1287 HInstruction* Current() const { return instruction_; }
1288 void Advance() {
1289 instruction_ = next_;
1290 next_ = Done() ? nullptr : instruction_->GetPrevious();
1291 }
1292
1293 private:
1294 HInstruction* instruction_;
1295 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001296
1297 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001298};
1299
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001300// An embedded container with N elements of type T. Used (with partial
1301// specialization for N=0) because embedded arrays cannot have size 0.
1302template<typename T, intptr_t N>
1303class EmbeddedArray {
1304 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001305 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001306
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001307 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001308
1309 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001310 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001311 return elements_[i];
1312 }
1313
1314 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001315 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001316 return elements_[i];
1317 }
1318
1319 const T& At(intptr_t i) const {
1320 return (*this)[i];
1321 }
1322
1323 void SetAt(intptr_t i, const T& val) {
1324 (*this)[i] = val;
1325 }
1326
1327 private:
1328 T elements_[N];
1329};
1330
1331template<typename T>
1332class EmbeddedArray<T, 0> {
1333 public:
1334 intptr_t length() const { return 0; }
1335 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001336 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001337 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001338 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001339 }
1340 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001341 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001342 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001343 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001344 }
1345};
1346
1347template<intptr_t N>
1348class HTemplateInstruction: public HInstruction {
1349 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001350 HTemplateInstruction<N>(SideEffects side_effects)
1351 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001352 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001353
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001354 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001355
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001356 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001357 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1358
1359 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1360 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001361 }
1362
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001363 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001364 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001365
1366 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001367};
1368
Dave Allison20dfc792014-06-16 20:44:29 -07001369template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001370class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001371 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001372 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1373 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001374 virtual ~HExpression() {}
1375
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001376 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001377
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001378 protected:
1379 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001380};
1381
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001382// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1383// instruction that branches to the exit block.
1384class HReturnVoid : public HTemplateInstruction<0> {
1385 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001386 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001387
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001388 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001389
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001390 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001391
1392 private:
1393 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1394};
1395
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001396// Represents dex's RETURN opcodes. A HReturn is a control flow
1397// instruction that branches to the exit block.
1398class HReturn : public HTemplateInstruction<1> {
1399 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001400 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001401 SetRawInputAt(0, value);
1402 }
1403
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001404 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001405
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001406 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001407
1408 private:
1409 DISALLOW_COPY_AND_ASSIGN(HReturn);
1410};
1411
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001412// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001413// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001414// exit block.
1415class HExit : public HTemplateInstruction<0> {
1416 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001417 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001418
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001419 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001420
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001421 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001422
1423 private:
1424 DISALLOW_COPY_AND_ASSIGN(HExit);
1425};
1426
1427// Jumps from one block to another.
1428class HGoto : public HTemplateInstruction<0> {
1429 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001430 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1431
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001432 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001433
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001434 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001435 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001436 }
1437
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001438 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001439
1440 private:
1441 DISALLOW_COPY_AND_ASSIGN(HGoto);
1442};
1443
Dave Allison20dfc792014-06-16 20:44:29 -07001444
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001445// Conditional branch. A block ending with an HIf instruction must have
1446// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001447class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001448 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001449 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001450 SetRawInputAt(0, input);
1451 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001452
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001453 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001454
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001455 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001456 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001457 }
1458
1459 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001460 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001461 }
1462
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001463 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001464
Dave Allison20dfc792014-06-16 20:44:29 -07001465 virtual bool IsIfInstruction() const { return true; }
1466
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001467 private:
1468 DISALLOW_COPY_AND_ASSIGN(HIf);
1469};
1470
Roland Levillain88cb1752014-10-20 16:36:47 +01001471class HUnaryOperation : public HExpression<1> {
1472 public:
1473 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1474 : HExpression(result_type, SideEffects::None()) {
1475 SetRawInputAt(0, input);
1476 }
1477
1478 HInstruction* GetInput() const { return InputAt(0); }
1479 Primitive::Type GetResultType() const { return GetType(); }
1480
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001481 bool CanBeMoved() const OVERRIDE { return true; }
1482 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001483 UNUSED(other);
1484 return true;
1485 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001486
Roland Levillain9240d6a2014-10-20 16:47:04 +01001487 // Try to statically evaluate `operation` and return a HConstant
1488 // containing the result of this evaluation. If `operation` cannot
1489 // be evaluated as a constant, return nullptr.
1490 HConstant* TryStaticEvaluation() const;
1491
1492 // Apply this operation to `x`.
1493 virtual int32_t Evaluate(int32_t x) const = 0;
1494 virtual int64_t Evaluate(int64_t x) const = 0;
1495
Roland Levillain88cb1752014-10-20 16:36:47 +01001496 DECLARE_INSTRUCTION(UnaryOperation);
1497
1498 private:
1499 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1500};
1501
Dave Allison20dfc792014-06-16 20:44:29 -07001502class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001503 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001504 HBinaryOperation(Primitive::Type result_type,
1505 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001506 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001507 SetRawInputAt(0, left);
1508 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001509 }
1510
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001511 HInstruction* GetLeft() const { return InputAt(0); }
1512 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001513 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001514
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001515 virtual bool IsCommutative() const { return false; }
1516
1517 // Put constant on the right.
1518 // Returns whether order is changed.
1519 bool OrderInputsWithConstantOnTheRight() {
1520 HInstruction* left = InputAt(0);
1521 HInstruction* right = InputAt(1);
1522 if (left->IsConstant() && !right->IsConstant()) {
1523 ReplaceInput(right, 0);
1524 ReplaceInput(left, 1);
1525 return true;
1526 }
1527 return false;
1528 }
1529
1530 // Order inputs by instruction id, but favor constant on the right side.
1531 // This helps GVN for commutative ops.
1532 void OrderInputs() {
1533 DCHECK(IsCommutative());
1534 HInstruction* left = InputAt(0);
1535 HInstruction* right = InputAt(1);
1536 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1537 return;
1538 }
1539 if (OrderInputsWithConstantOnTheRight()) {
1540 return;
1541 }
1542 // Order according to instruction id.
1543 if (left->GetId() > right->GetId()) {
1544 ReplaceInput(right, 0);
1545 ReplaceInput(left, 1);
1546 }
1547 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001548
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001549 bool CanBeMoved() const OVERRIDE { return true; }
1550 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001551 UNUSED(other);
1552 return true;
1553 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001554
Roland Levillain9240d6a2014-10-20 16:47:04 +01001555 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001556 // containing the result of this evaluation. If `operation` cannot
1557 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001558 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001559
1560 // Apply this operation to `x` and `y`.
1561 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1562 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1563
Roland Levillainccc07a92014-09-16 14:48:16 +01001564 DECLARE_INSTRUCTION(BinaryOperation);
1565
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001566 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001567 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1568};
1569
Dave Allison20dfc792014-06-16 20:44:29 -07001570class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001571 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001572 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001573 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1574 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001575
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001576 bool NeedsMaterialization() const { return needs_materialization_; }
1577 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001578
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001579 // For code generation purposes, returns whether this instruction is just before
1580 // `if_`, and disregard moves in between.
1581 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1582
Dave Allison20dfc792014-06-16 20:44:29 -07001583 DECLARE_INSTRUCTION(Condition);
1584
1585 virtual IfCondition GetCondition() const = 0;
1586
1587 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001588 // For register allocation purposes, returns whether this instruction needs to be
1589 // materialized (that is, not just be in the processor flags).
1590 bool needs_materialization_;
1591
Dave Allison20dfc792014-06-16 20:44:29 -07001592 DISALLOW_COPY_AND_ASSIGN(HCondition);
1593};
1594
1595// Instruction to check if two inputs are equal to each other.
1596class HEqual : public HCondition {
1597 public:
1598 HEqual(HInstruction* first, HInstruction* second)
1599 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001600
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001601 bool IsCommutative() const OVERRIDE { return true; }
1602
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001603 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001604 return x == y ? 1 : 0;
1605 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001606 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001607 return x == y ? 1 : 0;
1608 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001609
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001610 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001611
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001612 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001613 return kCondEQ;
1614 }
1615
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001616 private:
1617 DISALLOW_COPY_AND_ASSIGN(HEqual);
1618};
1619
Dave Allison20dfc792014-06-16 20:44:29 -07001620class HNotEqual : public HCondition {
1621 public:
1622 HNotEqual(HInstruction* first, HInstruction* second)
1623 : HCondition(first, second) {}
1624
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001625 bool IsCommutative() const OVERRIDE { return true; }
1626
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001627 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001628 return x != y ? 1 : 0;
1629 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001630 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001631 return x != y ? 1 : 0;
1632 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001633
Dave Allison20dfc792014-06-16 20:44:29 -07001634 DECLARE_INSTRUCTION(NotEqual);
1635
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001636 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001637 return kCondNE;
1638 }
1639
1640 private:
1641 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1642};
1643
1644class HLessThan : public HCondition {
1645 public:
1646 HLessThan(HInstruction* first, HInstruction* second)
1647 : HCondition(first, second) {}
1648
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001649 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001650 return x < y ? 1 : 0;
1651 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001652 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001653 return x < y ? 1 : 0;
1654 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001655
Dave Allison20dfc792014-06-16 20:44:29 -07001656 DECLARE_INSTRUCTION(LessThan);
1657
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001658 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001659 return kCondLT;
1660 }
1661
1662 private:
1663 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1664};
1665
1666class HLessThanOrEqual : public HCondition {
1667 public:
1668 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1669 : HCondition(first, second) {}
1670
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001671 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001672 return x <= y ? 1 : 0;
1673 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001674 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001675 return x <= y ? 1 : 0;
1676 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001677
Dave Allison20dfc792014-06-16 20:44:29 -07001678 DECLARE_INSTRUCTION(LessThanOrEqual);
1679
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001680 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001681 return kCondLE;
1682 }
1683
1684 private:
1685 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1686};
1687
1688class HGreaterThan : public HCondition {
1689 public:
1690 HGreaterThan(HInstruction* first, HInstruction* second)
1691 : HCondition(first, second) {}
1692
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001693 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001694 return x > y ? 1 : 0;
1695 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001696 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001697 return x > y ? 1 : 0;
1698 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001699
Dave Allison20dfc792014-06-16 20:44:29 -07001700 DECLARE_INSTRUCTION(GreaterThan);
1701
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001702 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001703 return kCondGT;
1704 }
1705
1706 private:
1707 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1708};
1709
1710class HGreaterThanOrEqual : public HCondition {
1711 public:
1712 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1713 : HCondition(first, second) {}
1714
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001715 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001716 return x >= y ? 1 : 0;
1717 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001718 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001719 return x >= y ? 1 : 0;
1720 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001721
Dave Allison20dfc792014-06-16 20:44:29 -07001722 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1723
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001724 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001725 return kCondGE;
1726 }
1727
1728 private:
1729 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1730};
1731
1732
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001733// Instruction to check how two inputs compare to each other.
1734// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1735class HCompare : public HBinaryOperation {
1736 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001737 // The bias applies for floating point operations and indicates how NaN
1738 // comparisons are treated:
1739 enum Bias {
1740 kNoBias, // bias is not applicable (i.e. for long operation)
1741 kGtBias, // return 1 for NaN comparisons
1742 kLtBias, // return -1 for NaN comparisons
1743 };
1744
1745 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1746 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001747 DCHECK_EQ(type, first->GetType());
1748 DCHECK_EQ(type, second->GetType());
1749 }
1750
Calin Juravleddb7df22014-11-25 20:56:51 +00001751 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001752 return
1753 x == y ? 0 :
1754 x > y ? 1 :
1755 -1;
1756 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001757
1758 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001759 return
1760 x == y ? 0 :
1761 x > y ? 1 :
1762 -1;
1763 }
1764
Calin Juravleddb7df22014-11-25 20:56:51 +00001765 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1766 return bias_ == other->AsCompare()->bias_;
1767 }
1768
1769 bool IsGtBias() { return bias_ == kGtBias; }
1770
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001771 DECLARE_INSTRUCTION(Compare);
1772
1773 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001774 const Bias bias_;
1775
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001776 DISALLOW_COPY_AND_ASSIGN(HCompare);
1777};
1778
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001779// A local in the graph. Corresponds to a Dex register.
1780class HLocal : public HTemplateInstruction<0> {
1781 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001782 explicit HLocal(uint16_t reg_number)
1783 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001784
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001785 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001786
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001787 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001788
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001789 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001790 // The Dex register number.
1791 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001792
1793 DISALLOW_COPY_AND_ASSIGN(HLocal);
1794};
1795
1796// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001797class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001798 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001799 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001800 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001801 SetRawInputAt(0, local);
1802 }
1803
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001804 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1805
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001806 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001807
1808 private:
1809 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1810};
1811
1812// Store a value in a given local. This instruction has two inputs: the value
1813// and the local.
1814class HStoreLocal : public HTemplateInstruction<2> {
1815 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001816 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001817 SetRawInputAt(0, local);
1818 SetRawInputAt(1, value);
1819 }
1820
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001821 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1822
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001823 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001824
1825 private:
1826 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1827};
1828
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001829class HConstant : public HExpression<0> {
1830 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001831 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1832
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001833 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001834
1835 DECLARE_INSTRUCTION(Constant);
1836
1837 private:
1838 DISALLOW_COPY_AND_ASSIGN(HConstant);
1839};
1840
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001841class HFloatConstant : public HConstant {
1842 public:
1843 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1844
1845 float GetValue() const { return value_; }
1846
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001847 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001848 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1849 bit_cast<float, int32_t>(value_);
1850 }
1851
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001852 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001853
1854 DECLARE_INSTRUCTION(FloatConstant);
1855
1856 private:
1857 const float value_;
1858
1859 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1860};
1861
1862class HDoubleConstant : public HConstant {
1863 public:
1864 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1865
1866 double GetValue() const { return value_; }
1867
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001868 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001869 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1870 bit_cast<double, int64_t>(value_);
1871 }
1872
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001873 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001874
1875 DECLARE_INSTRUCTION(DoubleConstant);
1876
1877 private:
1878 const double value_;
1879
1880 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1881};
1882
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001883class HNullConstant : public HConstant {
1884 public:
1885 HNullConstant() : HConstant(Primitive::kPrimNot) {}
1886
1887 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
1888 return true;
1889 }
1890
1891 size_t ComputeHashCode() const OVERRIDE { return 0; }
1892
Calin Juravle61d544b2015-02-23 16:46:57 +00001893 bool ActAsNullConstant() const OVERRIDE { return true; }
1894
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001895 DECLARE_INSTRUCTION(NullConstant);
1896
1897 private:
1898 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
1899};
1900
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001901// Constants of the type int. Those can be from Dex instructions, or
1902// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001903class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001904 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001905 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001906
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001907 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001908
Calin Juravle61d544b2015-02-23 16:46:57 +00001909 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001910 return other->AsIntConstant()->value_ == value_;
1911 }
1912
Calin Juravle61d544b2015-02-23 16:46:57 +00001913 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
1914
1915 // TODO: Null is represented by the `0` constant. In most cases we replace it
1916 // with a HNullConstant but we don't do it when comparing (a != null). This
1917 // method is an workaround until we fix the above.
1918 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001919
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001920 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001921
1922 private:
1923 const int32_t value_;
1924
1925 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1926};
1927
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001928class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001929 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001930 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001931
1932 int64_t GetValue() const { return value_; }
1933
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001934 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001935 return other->AsLongConstant()->value_ == value_;
1936 }
1937
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001938 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001939
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001940 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001941
1942 private:
1943 const int64_t value_;
1944
1945 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1946};
1947
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001948enum class Intrinsics {
1949#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
1950#include "intrinsics_list.h"
1951 kNone,
1952 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
1953#undef INTRINSICS_LIST
1954#undef OPTIMIZING_INTRINSICS
1955};
1956std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
1957
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001958class HInvoke : public HInstruction {
1959 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001960 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001961
1962 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1963 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00001964 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001965
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001966 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001967 SetRawInputAt(index, argument);
1968 }
1969
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001970 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001971
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001972 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001973
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001974 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1975
1976 Intrinsics GetIntrinsic() {
1977 return intrinsic_;
1978 }
1979
1980 void SetIntrinsic(Intrinsics intrinsic) {
1981 intrinsic_ = intrinsic;
1982 }
1983
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001984 DECLARE_INSTRUCTION(Invoke);
1985
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001986 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001987 HInvoke(ArenaAllocator* arena,
1988 uint32_t number_of_arguments,
1989 Primitive::Type return_type,
1990 uint32_t dex_pc,
1991 uint32_t dex_method_index)
1992 : HInstruction(SideEffects::All()),
1993 inputs_(arena, number_of_arguments),
1994 return_type_(return_type),
1995 dex_pc_(dex_pc),
1996 dex_method_index_(dex_method_index),
1997 intrinsic_(Intrinsics::kNone) {
1998 inputs_.SetSize(number_of_arguments);
1999 }
2000
David Brazdil1abb4192015-02-17 18:33:36 +00002001 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2002 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2003 inputs_.Put(index, input);
2004 }
2005
2006 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002007 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002008 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002009 const uint32_t dex_method_index_;
2010 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002011
2012 private:
2013 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2014};
2015
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002016class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002017 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002018 HInvokeStaticOrDirect(ArenaAllocator* arena,
2019 uint32_t number_of_arguments,
2020 Primitive::Type return_type,
2021 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002022 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002023 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002024 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002025 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002026 invoke_type_(invoke_type),
2027 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002028
Calin Juravle77520bc2015-01-12 18:45:46 +00002029 bool CanDoImplicitNullCheck() const OVERRIDE {
2030 // We access the method via the dex cache so we can't do an implicit null check.
2031 // TODO: for intrinsics we can generate implicit null checks.
2032 return false;
2033 }
2034
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002035 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002036 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002037
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002038 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002039
2040 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002041 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002042 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002043
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002044 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002045};
2046
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002047class HInvokeVirtual : public HInvoke {
2048 public:
2049 HInvokeVirtual(ArenaAllocator* arena,
2050 uint32_t number_of_arguments,
2051 Primitive::Type return_type,
2052 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002053 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002054 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002055 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002056 vtable_index_(vtable_index) {}
2057
Calin Juravle77520bc2015-01-12 18:45:46 +00002058 bool CanDoImplicitNullCheck() const OVERRIDE {
2059 // TODO: Add implicit null checks in intrinsics.
2060 return !GetLocations()->Intrinsified();
2061 }
2062
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002063 uint32_t GetVTableIndex() const { return vtable_index_; }
2064
2065 DECLARE_INSTRUCTION(InvokeVirtual);
2066
2067 private:
2068 const uint32_t vtable_index_;
2069
2070 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2071};
2072
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002073class HInvokeInterface : public HInvoke {
2074 public:
2075 HInvokeInterface(ArenaAllocator* arena,
2076 uint32_t number_of_arguments,
2077 Primitive::Type return_type,
2078 uint32_t dex_pc,
2079 uint32_t dex_method_index,
2080 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002081 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002082 imt_index_(imt_index) {}
2083
Calin Juravle77520bc2015-01-12 18:45:46 +00002084 bool CanDoImplicitNullCheck() const OVERRIDE {
2085 // TODO: Add implicit null checks in intrinsics.
2086 return !GetLocations()->Intrinsified();
2087 }
2088
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002089 uint32_t GetImtIndex() const { return imt_index_; }
2090 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2091
2092 DECLARE_INSTRUCTION(InvokeInterface);
2093
2094 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002095 const uint32_t imt_index_;
2096
2097 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2098};
2099
Dave Allison20dfc792014-06-16 20:44:29 -07002100class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002101 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002102 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002103 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2104 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002105 type_index_(type_index),
2106 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002107
2108 uint32_t GetDexPc() const { return dex_pc_; }
2109 uint16_t GetTypeIndex() const { return type_index_; }
2110
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002111 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002112 bool NeedsEnvironment() const OVERRIDE { return true; }
2113 // It may throw when called on:
2114 // - interfaces
2115 // - abstract/innaccessible/unknown classes
2116 // TODO: optimize when possible.
2117 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002118
Calin Juravle10e244f2015-01-26 18:54:32 +00002119 bool CanBeNull() const OVERRIDE { return false; }
2120
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002121 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2122
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002123 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002124
2125 private:
2126 const uint32_t dex_pc_;
2127 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002128 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002129
2130 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2131};
2132
Roland Levillain88cb1752014-10-20 16:36:47 +01002133class HNeg : public HUnaryOperation {
2134 public:
2135 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2136 : HUnaryOperation(result_type, input) {}
2137
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002138 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2139 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002140
Roland Levillain88cb1752014-10-20 16:36:47 +01002141 DECLARE_INSTRUCTION(Neg);
2142
2143 private:
2144 DISALLOW_COPY_AND_ASSIGN(HNeg);
2145};
2146
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002147class HNewArray : public HExpression<1> {
2148 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002149 HNewArray(HInstruction* length,
2150 uint32_t dex_pc,
2151 uint16_t type_index,
2152 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002153 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2154 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002155 type_index_(type_index),
2156 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002157 SetRawInputAt(0, length);
2158 }
2159
2160 uint32_t GetDexPc() const { return dex_pc_; }
2161 uint16_t GetTypeIndex() const { return type_index_; }
2162
2163 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002164 bool NeedsEnvironment() const OVERRIDE { return true; }
2165
2166 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002167
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002168 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2169
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002170 DECLARE_INSTRUCTION(NewArray);
2171
2172 private:
2173 const uint32_t dex_pc_;
2174 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002175 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002176
2177 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2178};
2179
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002180class HAdd : public HBinaryOperation {
2181 public:
2182 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2183 : HBinaryOperation(result_type, left, right) {}
2184
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002185 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002186
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002187 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002188 return x + y;
2189 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002190 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002191 return x + y;
2192 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002193
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002194 DECLARE_INSTRUCTION(Add);
2195
2196 private:
2197 DISALLOW_COPY_AND_ASSIGN(HAdd);
2198};
2199
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002200class HSub : public HBinaryOperation {
2201 public:
2202 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2203 : HBinaryOperation(result_type, left, right) {}
2204
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002205 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002206 return x - y;
2207 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002208 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002209 return x - y;
2210 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002211
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002212 DECLARE_INSTRUCTION(Sub);
2213
2214 private:
2215 DISALLOW_COPY_AND_ASSIGN(HSub);
2216};
2217
Calin Juravle34bacdf2014-10-07 20:23:36 +01002218class HMul : public HBinaryOperation {
2219 public:
2220 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2221 : HBinaryOperation(result_type, left, right) {}
2222
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002223 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002224
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002225 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2226 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002227
2228 DECLARE_INSTRUCTION(Mul);
2229
2230 private:
2231 DISALLOW_COPY_AND_ASSIGN(HMul);
2232};
2233
Calin Juravle7c4954d2014-10-28 16:57:40 +00002234class HDiv : public HBinaryOperation {
2235 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002236 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2237 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002238
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002239 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002240 // Our graph structure ensures we never have 0 for `y` during constant folding.
2241 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002242 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002243 return (y == -1) ? -x : x / y;
2244 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002245
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002246 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002247 DCHECK_NE(y, 0);
2248 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2249 return (y == -1) ? -x : x / y;
2250 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002251
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002252 uint32_t GetDexPc() const { return dex_pc_; }
2253
Calin Juravle7c4954d2014-10-28 16:57:40 +00002254 DECLARE_INSTRUCTION(Div);
2255
2256 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002257 const uint32_t dex_pc_;
2258
Calin Juravle7c4954d2014-10-28 16:57:40 +00002259 DISALLOW_COPY_AND_ASSIGN(HDiv);
2260};
2261
Calin Juravlebacfec32014-11-14 15:54:36 +00002262class HRem : public HBinaryOperation {
2263 public:
2264 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2265 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2266
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002267 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002268 DCHECK_NE(y, 0);
2269 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2270 return (y == -1) ? 0 : x % y;
2271 }
2272
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002273 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002274 DCHECK_NE(y, 0);
2275 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2276 return (y == -1) ? 0 : x % y;
2277 }
2278
2279 uint32_t GetDexPc() const { return dex_pc_; }
2280
2281 DECLARE_INSTRUCTION(Rem);
2282
2283 private:
2284 const uint32_t dex_pc_;
2285
2286 DISALLOW_COPY_AND_ASSIGN(HRem);
2287};
2288
Calin Juravled0d48522014-11-04 16:40:20 +00002289class HDivZeroCheck : public HExpression<1> {
2290 public:
2291 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2292 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2293 SetRawInputAt(0, value);
2294 }
2295
2296 bool CanBeMoved() const OVERRIDE { return true; }
2297
2298 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2299 UNUSED(other);
2300 return true;
2301 }
2302
2303 bool NeedsEnvironment() const OVERRIDE { return true; }
2304 bool CanThrow() const OVERRIDE { return true; }
2305
2306 uint32_t GetDexPc() const { return dex_pc_; }
2307
2308 DECLARE_INSTRUCTION(DivZeroCheck);
2309
2310 private:
2311 const uint32_t dex_pc_;
2312
2313 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2314};
2315
Calin Juravle9aec02f2014-11-18 23:06:35 +00002316class HShl : public HBinaryOperation {
2317 public:
2318 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2319 : HBinaryOperation(result_type, left, right) {}
2320
2321 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2322 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2323
2324 DECLARE_INSTRUCTION(Shl);
2325
2326 private:
2327 DISALLOW_COPY_AND_ASSIGN(HShl);
2328};
2329
2330class HShr : public HBinaryOperation {
2331 public:
2332 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2333 : HBinaryOperation(result_type, left, right) {}
2334
2335 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2336 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2337
2338 DECLARE_INSTRUCTION(Shr);
2339
2340 private:
2341 DISALLOW_COPY_AND_ASSIGN(HShr);
2342};
2343
2344class HUShr : public HBinaryOperation {
2345 public:
2346 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2347 : HBinaryOperation(result_type, left, right) {}
2348
2349 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2350 uint32_t ux = static_cast<uint32_t>(x);
2351 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2352 return static_cast<int32_t>(ux >> uy);
2353 }
2354
2355 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2356 uint64_t ux = static_cast<uint64_t>(x);
2357 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2358 return static_cast<int64_t>(ux >> uy);
2359 }
2360
2361 DECLARE_INSTRUCTION(UShr);
2362
2363 private:
2364 DISALLOW_COPY_AND_ASSIGN(HUShr);
2365};
2366
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002367class HAnd : public HBinaryOperation {
2368 public:
2369 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2370 : HBinaryOperation(result_type, left, right) {}
2371
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002372 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002373
2374 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2375 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2376
2377 DECLARE_INSTRUCTION(And);
2378
2379 private:
2380 DISALLOW_COPY_AND_ASSIGN(HAnd);
2381};
2382
2383class HOr : public HBinaryOperation {
2384 public:
2385 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2386 : HBinaryOperation(result_type, left, right) {}
2387
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002388 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002389
2390 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2391 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2392
2393 DECLARE_INSTRUCTION(Or);
2394
2395 private:
2396 DISALLOW_COPY_AND_ASSIGN(HOr);
2397};
2398
2399class HXor : public HBinaryOperation {
2400 public:
2401 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2402 : HBinaryOperation(result_type, left, right) {}
2403
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002404 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002405
2406 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2407 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2408
2409 DECLARE_INSTRUCTION(Xor);
2410
2411 private:
2412 DISALLOW_COPY_AND_ASSIGN(HXor);
2413};
2414
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002415// The value of a parameter in this method. Its location depends on
2416// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002417class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002418 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002419 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2420 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002421
2422 uint8_t GetIndex() const { return index_; }
2423
Calin Juravle10e244f2015-01-26 18:54:32 +00002424 bool CanBeNull() const OVERRIDE { return !is_this_; }
2425
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002426 DECLARE_INSTRUCTION(ParameterValue);
2427
2428 private:
2429 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002430 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002431 const uint8_t index_;
2432
Calin Juravle10e244f2015-01-26 18:54:32 +00002433 // Whether or not the parameter value corresponds to 'this' argument.
2434 const bool is_this_;
2435
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002436 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2437};
2438
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002439class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002440 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002441 explicit HNot(Primitive::Type result_type, HInstruction* input)
2442 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002443
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002444 bool CanBeMoved() const OVERRIDE { return true; }
2445 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002446 UNUSED(other);
2447 return true;
2448 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002449
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002450 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2451 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002452
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002453 DECLARE_INSTRUCTION(Not);
2454
2455 private:
2456 DISALLOW_COPY_AND_ASSIGN(HNot);
2457};
2458
Roland Levillaindff1f282014-11-05 14:15:05 +00002459class HTypeConversion : public HExpression<1> {
2460 public:
2461 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002462 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2463 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002464 SetRawInputAt(0, input);
2465 DCHECK_NE(input->GetType(), result_type);
2466 }
2467
2468 HInstruction* GetInput() const { return InputAt(0); }
2469 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2470 Primitive::Type GetResultType() const { return GetType(); }
2471
Roland Levillain624279f2014-12-04 11:54:28 +00002472 // Required by the x86 and ARM code generators when producing calls
2473 // to the runtime.
2474 uint32_t GetDexPc() const { return dex_pc_; }
2475
Roland Levillaindff1f282014-11-05 14:15:05 +00002476 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002477 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002478
2479 DECLARE_INSTRUCTION(TypeConversion);
2480
2481 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002482 const uint32_t dex_pc_;
2483
Roland Levillaindff1f282014-11-05 14:15:05 +00002484 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2485};
2486
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002487static constexpr uint32_t kNoRegNumber = -1;
2488
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002489class HPhi : public HInstruction {
2490 public:
2491 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002492 : HInstruction(SideEffects::None()),
2493 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002494 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002495 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002496 is_live_(false),
2497 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002498 inputs_.SetSize(number_of_inputs);
2499 }
2500
Calin Juravle10e244f2015-01-26 18:54:32 +00002501 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002502
2503 void AddInput(HInstruction* input);
2504
Calin Juravle10e244f2015-01-26 18:54:32 +00002505 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002506 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002507
Calin Juravle10e244f2015-01-26 18:54:32 +00002508 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2509 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2510
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002511 uint32_t GetRegNumber() const { return reg_number_; }
2512
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002513 void SetDead() { is_live_ = false; }
2514 void SetLive() { is_live_ = true; }
2515 bool IsDead() const { return !is_live_; }
2516 bool IsLive() const { return is_live_; }
2517
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002518 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002519
David Brazdil1abb4192015-02-17 18:33:36 +00002520 protected:
2521 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2522
2523 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2524 inputs_.Put(index, input);
2525 }
2526
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002527 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002528 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002529 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002530 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002531 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002532 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002533
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002534 DISALLOW_COPY_AND_ASSIGN(HPhi);
2535};
2536
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002537class HNullCheck : public HExpression<1> {
2538 public:
2539 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002540 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002541 SetRawInputAt(0, value);
2542 }
2543
Calin Juravle10e244f2015-01-26 18:54:32 +00002544 bool CanBeMoved() const OVERRIDE { return true; }
2545 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002546 UNUSED(other);
2547 return true;
2548 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002549
Calin Juravle10e244f2015-01-26 18:54:32 +00002550 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002551
Calin Juravle10e244f2015-01-26 18:54:32 +00002552 bool CanThrow() const OVERRIDE { return true; }
2553
2554 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002555
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002556 uint32_t GetDexPc() const { return dex_pc_; }
2557
2558 DECLARE_INSTRUCTION(NullCheck);
2559
2560 private:
2561 const uint32_t dex_pc_;
2562
2563 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2564};
2565
2566class FieldInfo : public ValueObject {
2567 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002568 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2569 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002570
2571 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002572 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002573 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002574
2575 private:
2576 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002577 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002578 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002579};
2580
2581class HInstanceFieldGet : public HExpression<1> {
2582 public:
2583 HInstanceFieldGet(HInstruction* value,
2584 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002585 MemberOffset field_offset,
2586 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002587 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002588 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002589 SetRawInputAt(0, value);
2590 }
2591
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002592 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002593
2594 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2595 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2596 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002597 }
2598
Calin Juravle77520bc2015-01-12 18:45:46 +00002599 bool CanDoImplicitNullCheck() const OVERRIDE {
2600 return GetFieldOffset().Uint32Value() < kPageSize;
2601 }
2602
2603 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002604 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2605 }
2606
Calin Juravle52c48962014-12-16 17:02:57 +00002607 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002608 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002609 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002610 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002611
2612 DECLARE_INSTRUCTION(InstanceFieldGet);
2613
2614 private:
2615 const FieldInfo field_info_;
2616
2617 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2618};
2619
2620class HInstanceFieldSet : public HTemplateInstruction<2> {
2621 public:
2622 HInstanceFieldSet(HInstruction* object,
2623 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002624 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002625 MemberOffset field_offset,
2626 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002627 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002628 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002629 SetRawInputAt(0, object);
2630 SetRawInputAt(1, value);
2631 }
2632
Calin Juravle77520bc2015-01-12 18:45:46 +00002633 bool CanDoImplicitNullCheck() const OVERRIDE {
2634 return GetFieldOffset().Uint32Value() < kPageSize;
2635 }
2636
Calin Juravle52c48962014-12-16 17:02:57 +00002637 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002638 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002639 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002640 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002641 HInstruction* GetValue() const { return InputAt(1); }
2642
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002643 DECLARE_INSTRUCTION(InstanceFieldSet);
2644
2645 private:
2646 const FieldInfo field_info_;
2647
2648 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2649};
2650
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002651class HArrayGet : public HExpression<2> {
2652 public:
2653 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002654 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002655 SetRawInputAt(0, array);
2656 SetRawInputAt(1, index);
2657 }
2658
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002659 bool CanBeMoved() const OVERRIDE { return true; }
2660 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002661 UNUSED(other);
2662 return true;
2663 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002664 bool CanDoImplicitNullCheck() const OVERRIDE {
2665 // TODO: We can be smarter here.
2666 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2667 // which generates the implicit null check. There are cases when these can be removed
2668 // to produce better code. If we ever add optimizations to do so we should allow an
2669 // implicit check here (as long as the address falls in the first page).
2670 return false;
2671 }
2672
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002673 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002674
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002675 HInstruction* GetArray() const { return InputAt(0); }
2676 HInstruction* GetIndex() const { return InputAt(1); }
2677
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002678 DECLARE_INSTRUCTION(ArrayGet);
2679
2680 private:
2681 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2682};
2683
2684class HArraySet : public HTemplateInstruction<3> {
2685 public:
2686 HArraySet(HInstruction* array,
2687 HInstruction* index,
2688 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002689 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002690 uint32_t dex_pc)
2691 : HTemplateInstruction(SideEffects::ChangesSomething()),
2692 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002693 expected_component_type_(expected_component_type),
2694 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002695 SetRawInputAt(0, array);
2696 SetRawInputAt(1, index);
2697 SetRawInputAt(2, value);
2698 }
2699
Calin Juravle77520bc2015-01-12 18:45:46 +00002700 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002701 // We currently always call a runtime method to catch array store
2702 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002703 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002704 }
2705
Calin Juravle77520bc2015-01-12 18:45:46 +00002706 bool CanDoImplicitNullCheck() const OVERRIDE {
2707 // TODO: Same as for ArrayGet.
2708 return false;
2709 }
2710
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002711 void ClearNeedsTypeCheck() {
2712 needs_type_check_ = false;
2713 }
2714
2715 bool NeedsTypeCheck() const { return needs_type_check_; }
2716
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002717 uint32_t GetDexPc() const { return dex_pc_; }
2718
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002719 HInstruction* GetArray() const { return InputAt(0); }
2720 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002721 HInstruction* GetValue() const { return InputAt(2); }
2722
2723 Primitive::Type GetComponentType() const {
2724 // The Dex format does not type floating point index operations. Since the
2725 // `expected_component_type_` is set during building and can therefore not
2726 // be correct, we also check what is the value type. If it is a floating
2727 // point type, we must use that type.
2728 Primitive::Type value_type = GetValue()->GetType();
2729 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2730 ? value_type
2731 : expected_component_type_;
2732 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002733
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002734 DECLARE_INSTRUCTION(ArraySet);
2735
2736 private:
2737 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002738 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002739 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002740
2741 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2742};
2743
2744class HArrayLength : public HExpression<1> {
2745 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002746 explicit HArrayLength(HInstruction* array)
2747 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2748 // Note that arrays do not change length, so the instruction does not
2749 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002750 SetRawInputAt(0, array);
2751 }
2752
Calin Juravle77520bc2015-01-12 18:45:46 +00002753 bool CanBeMoved() const OVERRIDE { return true; }
2754 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002755 UNUSED(other);
2756 return true;
2757 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002758 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002759
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002760 DECLARE_INSTRUCTION(ArrayLength);
2761
2762 private:
2763 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2764};
2765
2766class HBoundsCheck : public HExpression<2> {
2767 public:
2768 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002769 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002770 DCHECK(index->GetType() == Primitive::kPrimInt);
2771 SetRawInputAt(0, index);
2772 SetRawInputAt(1, length);
2773 }
2774
Alexandre Rames2ed20af2015-03-06 13:55:35 +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 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002780
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002781 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002782
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002783 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002784
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002785 uint32_t GetDexPc() const { return dex_pc_; }
2786
2787 DECLARE_INSTRUCTION(BoundsCheck);
2788
2789 private:
2790 const uint32_t dex_pc_;
2791
2792 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2793};
2794
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002795/**
2796 * Some DEX instructions are folded into multiple HInstructions that need
2797 * to stay live until the last HInstruction. This class
2798 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002799 * HInstruction stays live. `index` represents the stack location index of the
2800 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002801 */
2802class HTemporary : public HTemplateInstruction<0> {
2803 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002804 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002805
2806 size_t GetIndex() const { return index_; }
2807
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002808 Primitive::Type GetType() const OVERRIDE {
2809 // The previous instruction is the one that will be stored in the temporary location.
2810 DCHECK(GetPrevious() != nullptr);
2811 return GetPrevious()->GetType();
2812 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002813
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002814 DECLARE_INSTRUCTION(Temporary);
2815
2816 private:
2817 const size_t index_;
2818
2819 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2820};
2821
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002822class HSuspendCheck : public HTemplateInstruction<0> {
2823 public:
2824 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002825 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002826
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002827 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002828 return true;
2829 }
2830
2831 uint32_t GetDexPc() const { return dex_pc_; }
2832
2833 DECLARE_INSTRUCTION(SuspendCheck);
2834
2835 private:
2836 const uint32_t dex_pc_;
2837
2838 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2839};
2840
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002841/**
2842 * Instruction to load a Class object.
2843 */
2844class HLoadClass : public HExpression<0> {
2845 public:
2846 HLoadClass(uint16_t type_index,
2847 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002848 uint32_t dex_pc)
2849 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2850 type_index_(type_index),
2851 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002852 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00002853 generate_clinit_check_(false),
2854 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002855
2856 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002857
2858 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2859 return other->AsLoadClass()->type_index_ == type_index_;
2860 }
2861
2862 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2863
2864 uint32_t GetDexPc() const { return dex_pc_; }
2865 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002866 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002867
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002868 bool NeedsEnvironment() const OVERRIDE {
2869 // Will call runtime and load the class if the class is not loaded yet.
2870 // TODO: finer grain decision.
2871 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002872 }
2873
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002874 bool MustGenerateClinitCheck() const {
2875 return generate_clinit_check_;
2876 }
2877
2878 void SetMustGenerateClinitCheck() {
2879 generate_clinit_check_ = true;
2880 }
2881
2882 bool CanCallRuntime() const {
2883 return MustGenerateClinitCheck() || !is_referrers_class_;
2884 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002885
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002886 bool CanThrow() const OVERRIDE {
2887 // May call runtime and and therefore can throw.
2888 // TODO: finer grain decision.
2889 return !is_referrers_class_;
2890 }
2891
Calin Juravleacf735c2015-02-12 15:25:22 +00002892 ReferenceTypeInfo GetLoadedClassRTI() {
2893 return loaded_class_rti_;
2894 }
2895
2896 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
2897 // Make sure we only set exact types (the loaded class should never be merged).
2898 DCHECK(rti.IsExact());
2899 loaded_class_rti_ = rti;
2900 }
2901
2902 bool IsResolved() {
2903 return loaded_class_rti_.IsExact();
2904 }
2905
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002906 DECLARE_INSTRUCTION(LoadClass);
2907
2908 private:
2909 const uint16_t type_index_;
2910 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002911 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002912 // Whether this instruction must generate the initialization check.
2913 // Used for code generation.
2914 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002915
Calin Juravleacf735c2015-02-12 15:25:22 +00002916 ReferenceTypeInfo loaded_class_rti_;
2917
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002918 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2919};
2920
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002921class HLoadString : public HExpression<0> {
2922 public:
2923 HLoadString(uint32_t string_index, uint32_t dex_pc)
2924 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2925 string_index_(string_index),
2926 dex_pc_(dex_pc) {}
2927
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002928 bool CanBeMoved() const OVERRIDE { return true; }
2929
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002930 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2931 return other->AsLoadString()->string_index_ == string_index_;
2932 }
2933
2934 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2935
2936 uint32_t GetDexPc() const { return dex_pc_; }
2937 uint32_t GetStringIndex() const { return string_index_; }
2938
2939 // TODO: Can we deopt or debug when we resolve a string?
2940 bool NeedsEnvironment() const OVERRIDE { return false; }
2941
2942 DECLARE_INSTRUCTION(LoadString);
2943
2944 private:
2945 const uint32_t string_index_;
2946 const uint32_t dex_pc_;
2947
2948 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2949};
2950
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002951// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002952/**
2953 * Performs an initialization check on its Class object input.
2954 */
2955class HClinitCheck : public HExpression<1> {
2956 public:
2957 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2958 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2959 dex_pc_(dex_pc) {
2960 SetRawInputAt(0, constant);
2961 }
2962
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002963 bool CanBeMoved() const OVERRIDE { return true; }
2964 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2965 UNUSED(other);
2966 return true;
2967 }
2968
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002969 bool NeedsEnvironment() const OVERRIDE {
2970 // May call runtime to initialize the class.
2971 return true;
2972 }
2973
2974 uint32_t GetDexPc() const { return dex_pc_; }
2975
2976 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2977
2978 DECLARE_INSTRUCTION(ClinitCheck);
2979
2980 private:
2981 const uint32_t dex_pc_;
2982
2983 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2984};
2985
2986class HStaticFieldGet : public HExpression<1> {
2987 public:
2988 HStaticFieldGet(HInstruction* cls,
2989 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002990 MemberOffset field_offset,
2991 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002992 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002993 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002994 SetRawInputAt(0, cls);
2995 }
2996
Calin Juravle52c48962014-12-16 17:02:57 +00002997
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002998 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002999
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003000 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003001 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3002 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003003 }
3004
3005 size_t ComputeHashCode() const OVERRIDE {
3006 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3007 }
3008
Calin Juravle52c48962014-12-16 17:02:57 +00003009 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003010 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3011 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003012 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003013
3014 DECLARE_INSTRUCTION(StaticFieldGet);
3015
3016 private:
3017 const FieldInfo field_info_;
3018
3019 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3020};
3021
3022class HStaticFieldSet : public HTemplateInstruction<2> {
3023 public:
3024 HStaticFieldSet(HInstruction* cls,
3025 HInstruction* value,
3026 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003027 MemberOffset field_offset,
3028 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003029 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003030 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003031 SetRawInputAt(0, cls);
3032 SetRawInputAt(1, value);
3033 }
3034
Calin Juravle52c48962014-12-16 17:02:57 +00003035 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003036 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3037 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003038 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003039
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003040 HInstruction* GetValue() const { return InputAt(1); }
3041
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003042 DECLARE_INSTRUCTION(StaticFieldSet);
3043
3044 private:
3045 const FieldInfo field_info_;
3046
3047 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3048};
3049
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003050// Implement the move-exception DEX instruction.
3051class HLoadException : public HExpression<0> {
3052 public:
3053 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3054
3055 DECLARE_INSTRUCTION(LoadException);
3056
3057 private:
3058 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3059};
3060
3061class HThrow : public HTemplateInstruction<1> {
3062 public:
3063 HThrow(HInstruction* exception, uint32_t dex_pc)
3064 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3065 SetRawInputAt(0, exception);
3066 }
3067
3068 bool IsControlFlow() const OVERRIDE { return true; }
3069
3070 bool NeedsEnvironment() const OVERRIDE { return true; }
3071
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003072 bool CanThrow() const OVERRIDE { return true; }
3073
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003074 uint32_t GetDexPc() const { return dex_pc_; }
3075
3076 DECLARE_INSTRUCTION(Throw);
3077
3078 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003079 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003080
3081 DISALLOW_COPY_AND_ASSIGN(HThrow);
3082};
3083
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003084class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003085 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003086 HInstanceOf(HInstruction* object,
3087 HLoadClass* constant,
3088 bool class_is_final,
3089 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003090 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3091 class_is_final_(class_is_final),
3092 dex_pc_(dex_pc) {
3093 SetRawInputAt(0, object);
3094 SetRawInputAt(1, constant);
3095 }
3096
3097 bool CanBeMoved() const OVERRIDE { return true; }
3098
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003099 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003100 return true;
3101 }
3102
3103 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003104 return false;
3105 }
3106
3107 uint32_t GetDexPc() const { return dex_pc_; }
3108
3109 bool IsClassFinal() const { return class_is_final_; }
3110
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003111 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003112
3113 private:
3114 const bool class_is_final_;
3115 const uint32_t dex_pc_;
3116
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003117 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3118};
3119
Calin Juravleb1498f62015-02-16 13:13:29 +00003120class HBoundType : public HExpression<1> {
3121 public:
3122 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3123 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3124 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003125 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003126 SetRawInputAt(0, input);
3127 }
3128
3129 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3130
3131 bool CanBeNull() const OVERRIDE {
3132 // `null instanceof ClassX` always return false so we can't be null.
3133 return false;
3134 }
3135
3136 DECLARE_INSTRUCTION(BoundType);
3137
3138 private:
3139 // Encodes the most upper class that this instruction can have. In other words
3140 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3141 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3142 const ReferenceTypeInfo bound_type_;
3143
3144 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3145};
3146
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003147class HCheckCast : public HTemplateInstruction<2> {
3148 public:
3149 HCheckCast(HInstruction* object,
3150 HLoadClass* constant,
3151 bool class_is_final,
3152 uint32_t dex_pc)
3153 : HTemplateInstruction(SideEffects::None()),
3154 class_is_final_(class_is_final),
3155 dex_pc_(dex_pc) {
3156 SetRawInputAt(0, object);
3157 SetRawInputAt(1, constant);
3158 }
3159
3160 bool CanBeMoved() const OVERRIDE { return true; }
3161
3162 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3163 return true;
3164 }
3165
3166 bool NeedsEnvironment() const OVERRIDE {
3167 // Instruction may throw a CheckCastError.
3168 return true;
3169 }
3170
3171 bool CanThrow() const OVERRIDE { return true; }
3172
3173 uint32_t GetDexPc() const { return dex_pc_; }
3174
3175 bool IsClassFinal() const { return class_is_final_; }
3176
3177 DECLARE_INSTRUCTION(CheckCast);
3178
3179 private:
3180 const bool class_is_final_;
3181 const uint32_t dex_pc_;
3182
3183 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003184};
3185
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003186class HMonitorOperation : public HTemplateInstruction<1> {
3187 public:
3188 enum OperationKind {
3189 kEnter,
3190 kExit,
3191 };
3192
3193 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3194 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3195 SetRawInputAt(0, object);
3196 }
3197
3198 // Instruction may throw a Java exception, so we need an environment.
3199 bool NeedsEnvironment() const OVERRIDE { return true; }
3200 bool CanThrow() const OVERRIDE { return true; }
3201
3202 uint32_t GetDexPc() const { return dex_pc_; }
3203
3204 bool IsEnter() const { return kind_ == kEnter; }
3205
3206 DECLARE_INSTRUCTION(MonitorOperation);
3207
Calin Juravle52c48962014-12-16 17:02:57 +00003208 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003209 const OperationKind kind_;
3210 const uint32_t dex_pc_;
3211
3212 private:
3213 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3214};
3215
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003216class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003217 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003218 MoveOperands(Location source, Location destination, HInstruction* instruction)
3219 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003220
3221 Location GetSource() const { return source_; }
3222 Location GetDestination() const { return destination_; }
3223
3224 void SetSource(Location value) { source_ = value; }
3225 void SetDestination(Location value) { destination_ = value; }
3226
3227 // The parallel move resolver marks moves as "in-progress" by clearing the
3228 // destination (but not the source).
3229 Location MarkPending() {
3230 DCHECK(!IsPending());
3231 Location dest = destination_;
3232 destination_ = Location::NoLocation();
3233 return dest;
3234 }
3235
3236 void ClearPending(Location dest) {
3237 DCHECK(IsPending());
3238 destination_ = dest;
3239 }
3240
3241 bool IsPending() const {
3242 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3243 return destination_.IsInvalid() && !source_.IsInvalid();
3244 }
3245
3246 // True if this blocks a move from the given location.
3247 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003248 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003249 }
3250
3251 // A move is redundant if it's been eliminated, if its source and
3252 // destination are the same, or if its destination is unneeded.
3253 bool IsRedundant() const {
3254 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3255 }
3256
3257 // We clear both operands to indicate move that's been eliminated.
3258 void Eliminate() {
3259 source_ = destination_ = Location::NoLocation();
3260 }
3261
3262 bool IsEliminated() const {
3263 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3264 return source_.IsInvalid();
3265 }
3266
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003267 HInstruction* GetInstruction() const { return instruction_; }
3268
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003269 private:
3270 Location source_;
3271 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003272 // The instruction this move is assocatied with. Null when this move is
3273 // for moving an input in the expected locations of user (including a phi user).
3274 // This is only used in debug mode, to ensure we do not connect interval siblings
3275 // in the same parallel move.
3276 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003277};
3278
3279static constexpr size_t kDefaultNumberOfMoves = 4;
3280
3281class HParallelMove : public HTemplateInstruction<0> {
3282 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003283 explicit HParallelMove(ArenaAllocator* arena)
3284 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003285
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003286 void AddMove(Location source, Location destination, HInstruction* instruction) {
3287 DCHECK(source.IsValid());
3288 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003289 if (kIsDebugBuild) {
3290 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003291 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffrayb4ba3542015-03-05 11:28:58 +00003292 if (moves_.Get(i).GetInstruction() == instruction) {
3293 // Special case the situation where the move is for the spill slot
3294 // of the instruction.
3295 if ((GetPrevious() == instruction)
3296 || ((GetPrevious() == nullptr)
3297 && instruction->IsPhi()
3298 && instruction->GetBlock() == GetBlock())) {
3299 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3300 << "Doing parallel moves for the same instruction.";
3301 } else {
3302 DCHECK(false) << "Doing parallel moves for the same instruction.";
3303 }
3304 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003305 }
3306 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003307 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3308 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3309 << "Same destination for two moves in a parallel move.";
3310 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003311 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003312 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003313 }
3314
3315 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003316 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003317 }
3318
3319 size_t NumMoves() const { return moves_.Size(); }
3320
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003321 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003322
3323 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003324 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003325
3326 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3327};
3328
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003329class HGraphVisitor : public ValueObject {
3330 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003331 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3332 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003333
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003334 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003335 virtual void VisitBasicBlock(HBasicBlock* block);
3336
Roland Levillain633021e2014-10-01 14:12:25 +01003337 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003338 void VisitInsertionOrder();
3339
Roland Levillain633021e2014-10-01 14:12:25 +01003340 // Visit the graph following dominator tree reverse post-order.
3341 void VisitReversePostOrder();
3342
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003343 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003344
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003345 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003346#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003347 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3348
3349 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3350
3351#undef DECLARE_VISIT_INSTRUCTION
3352
3353 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003354 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003355
3356 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3357};
3358
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003359class HGraphDelegateVisitor : public HGraphVisitor {
3360 public:
3361 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3362 virtual ~HGraphDelegateVisitor() {}
3363
3364 // Visit functions that delegate to to super class.
3365#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003366 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003367
3368 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3369
3370#undef DECLARE_VISIT_INSTRUCTION
3371
3372 private:
3373 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3374};
3375
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003376class HInsertionOrderIterator : public ValueObject {
3377 public:
3378 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3379
3380 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3381 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3382 void Advance() { ++index_; }
3383
3384 private:
3385 const HGraph& graph_;
3386 size_t index_;
3387
3388 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3389};
3390
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003391class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003392 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003393 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003394
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003395 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3396 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003397 void Advance() { ++index_; }
3398
3399 private:
3400 const HGraph& graph_;
3401 size_t index_;
3402
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003403 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003404};
3405
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003406class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003407 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003408 explicit HPostOrderIterator(const HGraph& graph)
3409 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003410
3411 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003412 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003413 void Advance() { --index_; }
3414
3415 private:
3416 const HGraph& graph_;
3417 size_t index_;
3418
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003419 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003420};
3421
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003422// Iterator over the blocks that art part of the loop. Includes blocks part
3423// of an inner loop. The order in which the blocks are iterated is on their
3424// block id.
3425class HBlocksInLoopIterator : public ValueObject {
3426 public:
3427 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3428 : blocks_in_loop_(info.GetBlocks()),
3429 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3430 index_(0) {
3431 if (!blocks_in_loop_.IsBitSet(index_)) {
3432 Advance();
3433 }
3434 }
3435
3436 bool Done() const { return index_ == blocks_.Size(); }
3437 HBasicBlock* Current() const { return blocks_.Get(index_); }
3438 void Advance() {
3439 ++index_;
3440 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3441 if (blocks_in_loop_.IsBitSet(index_)) {
3442 break;
3443 }
3444 }
3445 }
3446
3447 private:
3448 const BitVector& blocks_in_loop_;
3449 const GrowableArray<HBasicBlock*>& blocks_;
3450 size_t index_;
3451
3452 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3453};
3454
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003455} // namespace art
3456
3457#endif // ART_COMPILER_OPTIMIZING_NODES_H_