blob: 0ef174dcd5e03909f432c3afeda6d609ce338a71 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Mathieu Chartierb666f482015-02-18 14:33:14 -080020#include "base/arena_object.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000021#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000022#include "handle.h"
23#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000024#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010025#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000026#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010027#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000029#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "utils/growable_array.h"
31
32namespace art {
33
David Brazdil1abb4192015-02-17 18:33:36 +000034class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010036class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000038class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000039class HInvoke;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040class HGraphVisitor;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000041class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010042class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010043class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010044class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000045class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046
47static const int kDefaultNumberOfBlocks = 8;
48static const int kDefaultNumberOfSuccessors = 2;
49static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010050static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000051static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
Calin Juravle9aec02f2014-11-18 23:06:35 +000053static constexpr uint32_t kMaxIntShiftValue = 0x1f;
54static constexpr uint64_t kMaxLongShiftValue = 0x3f;
55
Dave Allison20dfc792014-06-16 20:44:29 -070056enum IfCondition {
57 kCondEQ,
58 kCondNE,
59 kCondLT,
60 kCondLE,
61 kCondGT,
62 kCondGE,
63};
64
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010065class HInstructionList {
66 public:
67 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
68
69 void AddInstruction(HInstruction* instruction);
70 void RemoveInstruction(HInstruction* instruction);
71
Roland Levillain6b469232014-09-25 10:10:38 +010072 // Return true if this list contains `instruction`.
73 bool Contains(HInstruction* instruction) const;
74
Roland Levillainccc07a92014-09-16 14:48:16 +010075 // Return true if `instruction1` is found before `instruction2` in
76 // this instruction list and false otherwise. Abort if none
77 // of these instructions is found.
78 bool FoundBefore(const HInstruction* instruction1,
79 const HInstruction* instruction2) const;
80
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000081 bool IsEmpty() const { return first_instruction_ == nullptr; }
82 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
83
84 // Update the block of all instructions to be `block`.
85 void SetBlockOfInstructions(HBasicBlock* block) const;
86
87 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
88 void Add(const HInstructionList& instruction_list);
89
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010090 private:
91 HInstruction* first_instruction_;
92 HInstruction* last_instruction_;
93
94 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000095 friend class HGraph;
96 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010097 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010098 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010099
100 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
101};
102
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000103// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700104class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000105 public:
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000106 HGraph(ArenaAllocator* arena, bool debuggable = false, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000107 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000108 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100109 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700110 entry_block_(nullptr),
111 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100112 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100113 number_of_vregs_(0),
114 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000115 temporaries_vreg_slots_(0),
Mingyao Yange4335eb2015-03-02 15:14:13 -0800116 has_array_accesses_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000117 debuggable_(debuggable),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000118 current_instruction_id_(start_instruction_id) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000119
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000120 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100121 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100122 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000123
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000124 HBasicBlock* GetEntryBlock() const { return entry_block_; }
125 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000126
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000127 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
128 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000129
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000130 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100131
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000132 // Try building the SSA form of this graph, with dominance computation and loop
133 // recognition. Returns whether it was successful in doing all these steps.
134 bool TryBuildingSsa() {
135 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000136 // The SSA builder requires loops to all be natural. Specifically, the dead phi
137 // elimination phase checks the consistency of the graph when doing a post-order
138 // visit for eliminating dead phis: a dead phi can only have loop header phi
139 // users remaining when being visited.
140 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000141 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000142 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000143 }
144
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000145 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000146 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100147 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000148
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000149 // Analyze all natural loops in this graph. Returns false if one
150 // loop is not natural, that is the header does not dominate the
151 // back edge.
152 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100153
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000154 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
155 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
156
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100157 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
158 void SimplifyLoop(HBasicBlock* header);
159
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 int32_t GetNextInstructionId() {
161 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000162 return current_instruction_id_++;
163 }
164
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000165 int32_t GetCurrentInstructionId() const {
166 return current_instruction_id_;
167 }
168
169 void SetCurrentInstructionId(int32_t id) {
170 current_instruction_id_ = id;
171 }
172
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100173 uint16_t GetMaximumNumberOfOutVRegs() const {
174 return maximum_number_of_out_vregs_;
175 }
176
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
178 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100179 }
180
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000181 void UpdateTemporariesVRegSlots(size_t slots) {
182 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100183 }
184
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000185 size_t GetTemporariesVRegSlots() const {
186 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100187 }
188
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100189 void SetNumberOfVRegs(uint16_t number_of_vregs) {
190 number_of_vregs_ = number_of_vregs;
191 }
192
193 uint16_t GetNumberOfVRegs() const {
194 return number_of_vregs_;
195 }
196
197 void SetNumberOfInVRegs(uint16_t value) {
198 number_of_in_vregs_ = value;
199 }
200
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100201 uint16_t GetNumberOfLocalVRegs() const {
202 return number_of_vregs_ - number_of_in_vregs_;
203 }
204
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100205 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
206 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100207 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100208
Mingyao Yange4335eb2015-03-02 15:14:13 -0800209 bool HasArrayAccesses() const {
210 return has_array_accesses_;
211 }
212
213 void SetHasArrayAccesses(bool value) {
214 has_array_accesses_ = value;
215 }
216
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000217 bool IsDebuggable() const { return debuggable_; }
218
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000219 HNullConstant* GetNullConstant();
220
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000221 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000222 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
223 void VisitBlockForDominatorTree(HBasicBlock* block,
224 HBasicBlock* predecessor,
225 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100226 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000227 void VisitBlockForBackEdges(HBasicBlock* block,
228 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100229 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000230 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000231 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
232
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000233 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000234
235 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000236 GrowableArray<HBasicBlock*> blocks_;
237
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100238 // List of blocks to perform a reverse post order tree traversal.
239 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000240
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000241 HBasicBlock* entry_block_;
242 HBasicBlock* exit_block_;
243
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100244 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100245 uint16_t maximum_number_of_out_vregs_;
246
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100247 // The number of virtual registers in this method. Contains the parameters.
248 uint16_t number_of_vregs_;
249
250 // The number of virtual registers used by parameters of this method.
251 uint16_t number_of_in_vregs_;
252
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000253 // Number of vreg size slots that the temporaries use (used in baseline compiler).
254 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100255
Mingyao Yange4335eb2015-03-02 15:14:13 -0800256 // Has array accesses. We can totally skip BCE if it's false.
257 bool has_array_accesses_;
258
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000259 // Indicates whether the graph should be compiled in a way that
260 // ensures full debuggability. If false, we can apply more
261 // aggressive optimizations that may limit the level of debugging.
262 const bool debuggable_;
263
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000264 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000265 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000266
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000267 // Cached null constant that might be created when building SSA form.
268 HNullConstant* cached_null_constant_;
269
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000270 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000271 DISALLOW_COPY_AND_ASSIGN(HGraph);
272};
273
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700274class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000275 public:
276 HLoopInformation(HBasicBlock* header, HGraph* graph)
277 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100278 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100279 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100280 // Make bit vector growable, as the number of blocks may change.
281 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100282
283 HBasicBlock* GetHeader() const {
284 return header_;
285 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000286
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000287 void SetHeader(HBasicBlock* block) {
288 header_ = block;
289 }
290
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100291 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
292 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
293 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
294
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000295 void AddBackEdge(HBasicBlock* back_edge) {
296 back_edges_.Add(back_edge);
297 }
298
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100299 void RemoveBackEdge(HBasicBlock* back_edge) {
300 back_edges_.Delete(back_edge);
301 }
302
303 bool IsBackEdge(HBasicBlock* block) {
304 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
305 if (back_edges_.Get(i) == block) return true;
306 }
307 return false;
308 }
309
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000310 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000311 return back_edges_.Size();
312 }
313
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100314 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100315
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100316 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
317 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100318 }
319
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100320 void ClearBackEdges() {
321 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100322 }
323
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100324 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
325 // that is the header dominates the back edge.
326 bool Populate();
327
328 // Returns whether this loop information contains `block`.
329 // Note that this loop information *must* be populated before entering this function.
330 bool Contains(const HBasicBlock& block) const;
331
332 // Returns whether this loop information is an inner loop of `other`.
333 // Note that `other` *must* be populated before entering this function.
334 bool IsIn(const HLoopInformation& other) const;
335
336 const ArenaBitVector& GetBlocks() const { return blocks_; }
337
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000338 void Add(HBasicBlock* block);
339
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000340 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100341 // Internal recursive implementation of `Populate`.
342 void PopulateRecursive(HBasicBlock* block);
343
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000344 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100345 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000346 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100347 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000348
349 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
350};
351
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100352static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100353static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100354
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355// A block in a method. Contains the list of instructions represented
356// as a double linked list. Each block knows its predecessors and
357// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100358
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700359class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000360 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100361 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000362 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000363 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
364 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000365 loop_information_(nullptr),
366 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100367 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100368 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100369 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100370 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000371 lifetime_end_(kNoLifetime),
372 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100374 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
375 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000376 }
377
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100378 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
379 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000380 }
381
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100382 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
383 return dominated_blocks_;
384 }
385
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100386 bool IsEntryBlock() const {
387 return graph_->GetEntryBlock() == this;
388 }
389
390 bool IsExitBlock() const {
391 return graph_->GetExitBlock() == this;
392 }
393
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000394 void AddBackEdge(HBasicBlock* back_edge) {
395 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000396 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000397 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100398 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000399 loop_information_->AddBackEdge(back_edge);
400 }
401
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000402 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000403 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000404
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000405 int GetBlockId() const { return block_id_; }
406 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000407
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000408 HBasicBlock* GetDominator() const { return dominator_; }
409 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100410 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000411 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
412 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
413 if (dominated_blocks_.Get(i) == existing) {
414 dominated_blocks_.Put(i, new_block);
415 return;
416 }
417 }
418 LOG(FATAL) << "Unreachable";
419 UNREACHABLE();
420 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000421
422 int NumberOfBackEdges() const {
423 return loop_information_ == nullptr
424 ? 0
425 : loop_information_->NumberOfBackEdges();
426 }
427
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100428 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
429 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100430 const HInstructionList& GetInstructions() const { return instructions_; }
431 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100432 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000433
434 void AddSuccessor(HBasicBlock* block) {
435 successors_.Add(block);
436 block->predecessors_.Add(this);
437 }
438
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100439 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
440 size_t successor_index = GetSuccessorIndexOf(existing);
441 DCHECK_NE(successor_index, static_cast<size_t>(-1));
442 existing->RemovePredecessor(this);
443 new_block->predecessors_.Add(this);
444 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000445 }
446
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000447 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
448 size_t predecessor_index = GetPredecessorIndexOf(existing);
449 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
450 existing->RemoveSuccessor(this);
451 new_block->successors_.Add(this);
452 predecessors_.Put(predecessor_index, new_block);
453 }
454
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100455 void RemovePredecessor(HBasicBlock* block) {
456 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100457 }
458
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000459 void RemoveSuccessor(HBasicBlock* block) {
460 successors_.Delete(block);
461 }
462
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100463 void ClearAllPredecessors() {
464 predecessors_.Reset();
465 }
466
467 void AddPredecessor(HBasicBlock* block) {
468 predecessors_.Add(block);
469 block->successors_.Add(this);
470 }
471
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100472 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100473 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100474 HBasicBlock* temp = predecessors_.Get(0);
475 predecessors_.Put(0, predecessors_.Get(1));
476 predecessors_.Put(1, temp);
477 }
478
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100479 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
480 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
481 if (predecessors_.Get(i) == predecessor) {
482 return i;
483 }
484 }
485 return -1;
486 }
487
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100488 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
489 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
490 if (successors_.Get(i) == successor) {
491 return i;
492 }
493 }
494 return -1;
495 }
496
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000497 // Split the block into two blocks just after `cursor`. Returns the newly
498 // created block. Note that this method just updates raw block information,
499 // like predecessors, successors, dominators, and instruction list. It does not
500 // update the graph, reverse post order, loop information, nor make sure the
501 // blocks are consistent (for example ending with a control flow instruction).
502 HBasicBlock* SplitAfter(HInstruction* cursor);
503
504 // Merge `other` at the end of `this`. Successors and dominated blocks of
505 // `other` are changed to be successors and dominated blocks of `this`. Note
506 // that this method does not update the graph, reverse post order, loop
507 // information, nor make sure the blocks are consistent (for example ending
508 // with a control flow instruction).
509 void MergeWith(HBasicBlock* other);
510
511 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
512 // of `this` are moved to `other`.
513 // Note that this method does not update the graph, reverse post order, loop
514 // information, nor make sure the blocks are consistent (for example ending
515 void ReplaceWith(HBasicBlock* other);
516
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000517 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100518 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100519 // Replace instruction `initial` with `replacement` within this block.
520 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
521 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100522 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100523 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000524 // RemoveInstruction and RemovePhi delete a given instruction from the respective
525 // instruction list. With 'ensure_safety' set to true, it verifies that the
526 // instruction is not in use and removes it from the use lists of its inputs.
527 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
528 void RemovePhi(HPhi* phi, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100529
530 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100531 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100532 }
533
Roland Levillain6b879dd2014-09-22 17:13:44 +0100534 bool IsLoopPreHeaderFirstPredecessor() const {
535 DCHECK(IsLoopHeader());
536 DCHECK(!GetPredecessors().IsEmpty());
537 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
538 }
539
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100540 HLoopInformation* GetLoopInformation() const {
541 return loop_information_;
542 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000543
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000544 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100545 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000546 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100547 void SetInLoop(HLoopInformation* info) {
548 if (IsLoopHeader()) {
549 // Nothing to do. This just means `info` is an outer loop.
550 } else if (loop_information_ == nullptr) {
551 loop_information_ = info;
552 } else if (loop_information_->Contains(*info->GetHeader())) {
553 // Block is currently part of an outer loop. Make it part of this inner loop.
554 // Note that a non loop header having a loop information means this loop information
555 // has already been populated
556 loop_information_ = info;
557 } else {
558 // Block is part of an inner loop. Do not update the loop information.
559 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
560 // at this point, because this method is being called while populating `info`.
561 }
562 }
563
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000564 // Raw update of the loop information.
565 void SetLoopInformation(HLoopInformation* info) {
566 loop_information_ = info;
567 }
568
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100569 bool IsInLoop() const { return loop_information_ != nullptr; }
570
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100571 // Returns wheter this block dominates the blocked passed as parameter.
572 bool Dominates(HBasicBlock* block) const;
573
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100574 size_t GetLifetimeStart() const { return lifetime_start_; }
575 size_t GetLifetimeEnd() const { return lifetime_end_; }
576
577 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
578 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
579
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100580 uint32_t GetDexPc() const { return dex_pc_; }
581
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000582 bool IsCatchBlock() const { return is_catch_block_; }
583 void SetIsCatchBlock() { is_catch_block_ = true; }
584
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000585 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000586 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000587 GrowableArray<HBasicBlock*> predecessors_;
588 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100589 HInstructionList instructions_;
590 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000591 HLoopInformation* loop_information_;
592 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100593 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000594 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100595 // The dex program counter of the first instruction of this block.
596 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100597 size_t lifetime_start_;
598 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000599 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000600
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000601 friend class HGraph;
602 friend class HInstruction;
603
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000604 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
605};
606
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100607#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
608 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000609 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000610 M(ArrayGet, Instruction) \
611 M(ArrayLength, Instruction) \
612 M(ArraySet, Instruction) \
613 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000614 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000615 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100616 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000617 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100618 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000619 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000620 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000621 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100622 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000623 M(Exit, Instruction) \
624 M(FloatConstant, Constant) \
625 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100626 M(GreaterThan, Condition) \
627 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100628 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000629 M(InstanceFieldGet, Instruction) \
630 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000631 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100632 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000633 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000634 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100635 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000636 M(LessThan, Condition) \
637 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000638 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000639 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100640 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000641 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100642 M(Local, Instruction) \
643 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000644 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000645 M(Mul, BinaryOperation) \
646 M(Neg, UnaryOperation) \
647 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100648 M(NewInstance, Instruction) \
Roland Levillain1cc5f252014-10-22 18:06:21 +0100649 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000650 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000651 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000652 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000653 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100654 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000655 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100656 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000657 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100658 M(Return, Instruction) \
659 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000660 M(Shl, BinaryOperation) \
661 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100662 M(StaticFieldGet, Instruction) \
663 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100664 M(StoreLocal, Instruction) \
665 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100666 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000667 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000668 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000669 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000670 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000671 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000672
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100673#define FOR_EACH_INSTRUCTION(M) \
674 FOR_EACH_CONCRETE_INSTRUCTION(M) \
675 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100676 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100677 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100678 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700679
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100680#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000681FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
682#undef FORWARD_DECLARATION
683
Roland Levillainccc07a92014-09-16 14:48:16 +0100684#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000685 InstructionKind GetKind() const OVERRIDE { return k##type; } \
686 const char* DebugName() const OVERRIDE { return #type; } \
687 const H##type* As##type() const OVERRIDE { return this; } \
688 H##type* As##type() OVERRIDE { return this; } \
689 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100690 return other->Is##type(); \
691 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000692 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000693
David Brazdiled596192015-01-23 10:39:45 +0000694template <typename T> class HUseList;
695
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100696template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700697class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000698 public:
David Brazdiled596192015-01-23 10:39:45 +0000699 HUseListNode* GetPrevious() const { return prev_; }
700 HUseListNode* GetNext() const { return next_; }
701 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100702 size_t GetIndex() const { return index_; }
703
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000704 private:
David Brazdiled596192015-01-23 10:39:45 +0000705 HUseListNode(T user, size_t index)
706 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
707
708 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100709 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000710 HUseListNode<T>* prev_;
711 HUseListNode<T>* next_;
712
713 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000714
715 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
716};
717
David Brazdiled596192015-01-23 10:39:45 +0000718template <typename T>
719class HUseList : public ValueObject {
720 public:
721 HUseList() : first_(nullptr) {}
722
723 void Clear() {
724 first_ = nullptr;
725 }
726
727 // Adds a new entry at the beginning of the use list and returns
728 // the newly created node.
729 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000730 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000731 if (IsEmpty()) {
732 first_ = new_node;
733 } else {
734 first_->prev_ = new_node;
735 new_node->next_ = first_;
736 first_ = new_node;
737 }
738 return new_node;
739 }
740
741 HUseListNode<T>* GetFirst() const {
742 return first_;
743 }
744
745 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000746 DCHECK(node != nullptr);
747 DCHECK(Contains(node));
748
David Brazdiled596192015-01-23 10:39:45 +0000749 if (node->prev_ != nullptr) {
750 node->prev_->next_ = node->next_;
751 }
752 if (node->next_ != nullptr) {
753 node->next_->prev_ = node->prev_;
754 }
755 if (node == first_) {
756 first_ = node->next_;
757 }
758 }
759
David Brazdil1abb4192015-02-17 18:33:36 +0000760 bool Contains(const HUseListNode<T>* node) const {
761 if (node == nullptr) {
762 return false;
763 }
764 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
765 if (current == node) {
766 return true;
767 }
768 }
769 return false;
770 }
771
David Brazdiled596192015-01-23 10:39:45 +0000772 bool IsEmpty() const {
773 return first_ == nullptr;
774 }
775
776 bool HasOnlyOneUse() const {
777 return first_ != nullptr && first_->next_ == nullptr;
778 }
779
780 private:
781 HUseListNode<T>* first_;
782};
783
784template<typename T>
785class HUseIterator : public ValueObject {
786 public:
787 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
788
789 bool Done() const { return current_ == nullptr; }
790
791 void Advance() {
792 DCHECK(!Done());
793 current_ = current_->GetNext();
794 }
795
796 HUseListNode<T>* Current() const {
797 DCHECK(!Done());
798 return current_;
799 }
800
801 private:
802 HUseListNode<T>* current_;
803
804 friend class HValue;
805};
806
David Brazdil1abb4192015-02-17 18:33:36 +0000807// This class is used by HEnvironment and HInstruction classes to record the
808// instructions they use and pointers to the corresponding HUseListNodes kept
809// by the used instructions.
810template <typename T>
811class HUserRecord : public ValueObject {
812 public:
813 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
814 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
815
816 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
817 : instruction_(old_record.instruction_), use_node_(use_node) {
818 DCHECK(instruction_ != nullptr);
819 DCHECK(use_node_ != nullptr);
820 DCHECK(old_record.use_node_ == nullptr);
821 }
822
823 HInstruction* GetInstruction() const { return instruction_; }
824 HUseListNode<T>* GetUseNode() const { return use_node_; }
825
826 private:
827 // Instruction used by the user.
828 HInstruction* instruction_;
829
830 // Corresponding entry in the use list kept by 'instruction_'.
831 HUseListNode<T>* use_node_;
832};
833
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100834// Represents the side effects an instruction may have.
835class SideEffects : public ValueObject {
836 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100837 SideEffects() : flags_(0) {}
838
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100839 static SideEffects None() {
840 return SideEffects(0);
841 }
842
843 static SideEffects All() {
844 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
845 }
846
847 static SideEffects ChangesSomething() {
848 return SideEffects((1 << kFlagChangesCount) - 1);
849 }
850
851 static SideEffects DependsOnSomething() {
852 int count = kFlagDependsOnCount - kFlagChangesCount;
853 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
854 }
855
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100856 SideEffects Union(SideEffects other) const {
857 return SideEffects(flags_ | other.flags_);
858 }
859
Roland Levillain72bceff2014-09-15 18:29:00 +0100860 bool HasSideEffects() const {
861 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
862 return (flags_ & all_bits_set) != 0;
863 }
864
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100865 bool HasAllSideEffects() const {
866 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
867 return all_bits_set == (flags_ & all_bits_set);
868 }
869
870 bool DependsOn(SideEffects other) const {
871 size_t depends_flags = other.ComputeDependsFlags();
872 return (flags_ & depends_flags) != 0;
873 }
874
875 bool HasDependencies() const {
876 int count = kFlagDependsOnCount - kFlagChangesCount;
877 size_t all_bits_set = (1 << count) - 1;
878 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
879 }
880
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100881 private:
882 static constexpr int kFlagChangesSomething = 0;
883 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
884
885 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
886 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
887
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100888 explicit SideEffects(size_t flags) : flags_(flags) {}
889
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100890 size_t ComputeDependsFlags() const {
891 return flags_ << kFlagChangesCount;
892 }
893
894 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100895};
896
David Brazdiled596192015-01-23 10:39:45 +0000897// A HEnvironment object contains the values of virtual registers at a given location.
898class HEnvironment : public ArenaObject<kArenaAllocMisc> {
899 public:
900 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
901 : vregs_(arena, number_of_vregs) {
902 vregs_.SetSize(number_of_vregs);
903 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000904 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000905 }
906 }
907
908 void CopyFrom(HEnvironment* env);
909
910 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +0000911 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +0000912 }
913
914 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +0000915 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +0000916 }
917
David Brazdil1abb4192015-02-17 18:33:36 +0000918 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +0000919
920 size_t Size() const { return vregs_.Size(); }
921
922 private:
David Brazdil1abb4192015-02-17 18:33:36 +0000923 // Record instructions' use entries of this environment for constant-time removal.
924 // It should only be called by HInstruction when a new environment use is added.
925 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
926 DCHECK(env_use->GetUser() == this);
927 size_t index = env_use->GetIndex();
928 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
929 }
David Brazdiled596192015-01-23 10:39:45 +0000930
David Brazdil1abb4192015-02-17 18:33:36 +0000931 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +0000932
David Brazdil1abb4192015-02-17 18:33:36 +0000933 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +0000934
935 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
936};
937
Calin Juravleacf735c2015-02-12 15:25:22 +0000938class ReferenceTypeInfo : ValueObject {
939 public:
Calin Juravleb1498f62015-02-16 13:13:29 +0000940 typedef Handle<mirror::Class> TypeHandle;
941
942 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
943 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
944 if (type_handle->IsObjectClass()) {
945 // Override the type handle to be consistent with the case when we get to
946 // Top but don't have the Object class available. It avoids having to guess
947 // what value the type_handle has when it's Top.
948 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
949 } else {
950 return ReferenceTypeInfo(type_handle, is_exact, false);
951 }
952 }
953
954 static ReferenceTypeInfo CreateTop(bool is_exact) {
955 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +0000956 }
957
958 bool IsExact() const { return is_exact_; }
959 bool IsTop() const { return is_top_; }
960
961 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
962
Calin Juravleb1498f62015-02-16 13:13:29 +0000963 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000964 if (IsTop()) {
965 // Top (equivalent for java.lang.Object) is supertype of anything.
966 return true;
967 }
968 if (rti.IsTop()) {
969 // If we get here `this` is not Top() so it can't be a supertype.
970 return false;
971 }
972 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
973 }
974
975 // Returns true if the type information provide the same amount of details.
976 // Note that it does not mean that the instructions have the same actual type
977 // (e.g. tops are equal but they can be the result of a merge).
978 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
979 if (IsExact() != rti.IsExact()) {
980 return false;
981 }
982 if (IsTop() && rti.IsTop()) {
983 // `Top` means java.lang.Object, so the types are equivalent.
984 return true;
985 }
986 if (IsTop() || rti.IsTop()) {
987 // If only one is top or object than they are not equivalent.
988 // NB: We need this extra check because the type_handle of `Top` is invalid
989 // and we cannot inspect its reference.
990 return false;
991 }
992
993 // Finally check the types.
994 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
995 }
996
997 private:
Calin Juravleb1498f62015-02-16 13:13:29 +0000998 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
999 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1000 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1001
Calin Juravleacf735c2015-02-12 15:25:22 +00001002 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001003 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001004 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001005 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001006 bool is_exact_;
1007 // A true value here means that the object type should be java.lang.Object.
1008 // We don't have access to the corresponding mirror object every time so this
1009 // flag acts as a substitute. When true, the TypeHandle refers to a null
1010 // pointer and should not be used.
1011 bool is_top_;
1012};
1013
1014std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1015
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001016class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001017 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001018 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001019 : previous_(nullptr),
1020 next_(nullptr),
1021 block_(nullptr),
1022 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001023 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001024 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001025 locations_(nullptr),
1026 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001027 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001028 side_effects_(side_effects),
1029 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001030
Dave Allison20dfc792014-06-16 20:44:29 -07001031 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001032
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001033#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001034 enum InstructionKind {
1035 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1036 };
1037#undef DECLARE_KIND
1038
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001039 HInstruction* GetNext() const { return next_; }
1040 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001041
Calin Juravle77520bc2015-01-12 18:45:46 +00001042 HInstruction* GetNextDisregardingMoves() const;
1043 HInstruction* GetPreviousDisregardingMoves() const;
1044
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001045 HBasicBlock* GetBlock() const { return block_; }
1046 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001047 bool IsInBlock() const { return block_ != nullptr; }
1048 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001049 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001050
Roland Levillain6b879dd2014-09-22 17:13:44 +01001051 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001052 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001053
1054 virtual void Accept(HGraphVisitor* visitor) = 0;
1055 virtual const char* DebugName() const = 0;
1056
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001057 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001058 void SetRawInputAt(size_t index, HInstruction* input) {
1059 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1060 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001061
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001062 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001063 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001064 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001065 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001066
Calin Juravle61d544b2015-02-23 16:46:57 +00001067 virtual bool ActAsNullConstant() const { return false; }
1068
Calin Juravle10e244f2015-01-26 18:54:32 +00001069 // Does not apply for all instructions, but having this at top level greatly
1070 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001071 virtual bool CanBeNull() const {
1072 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1073 return true;
1074 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001075
Calin Juravle77520bc2015-01-12 18:45:46 +00001076 virtual bool CanDoImplicitNullCheck() const { return false; }
1077
Calin Juravleacf735c2015-02-12 15:25:22 +00001078 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001079 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001080 reference_type_info_ = reference_type_info;
1081 }
1082
Calin Juravle61d544b2015-02-23 16:46:57 +00001083 ReferenceTypeInfo GetReferenceTypeInfo() const {
1084 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1085 return reference_type_info_;
1086 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001087
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001088 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001089 DCHECK(user != nullptr);
1090 HUseListNode<HInstruction*>* use =
1091 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1092 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001093 }
1094
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001095 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001096 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001097 HUseListNode<HEnvironment*>* env_use =
1098 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1099 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001100 }
1101
David Brazdil1abb4192015-02-17 18:33:36 +00001102 void RemoveAsUserOfInput(size_t input) {
1103 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1104 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1105 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001106
David Brazdil1abb4192015-02-17 18:33:36 +00001107 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1108 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001109
David Brazdiled596192015-01-23 10:39:45 +00001110 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1111 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001112
Roland Levillain6c82d402014-10-13 16:10:27 +01001113 // Does this instruction strictly dominate `other_instruction`?
1114 // Returns false if this instruction and `other_instruction` are the same.
1115 // Aborts if this instruction and `other_instruction` are both phis.
1116 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001117
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001118 int GetId() const { return id_; }
1119 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001120
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001121 int GetSsaIndex() const { return ssa_index_; }
1122 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1123 bool HasSsaIndex() const { return ssa_index_ != -1; }
1124
1125 bool HasEnvironment() const { return environment_ != nullptr; }
1126 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001127 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
1128
Nicolas Geoffray39468442014-09-02 15:17:15 +01001129 // Returns the number of entries in the environment. Typically, that is the
1130 // number of dex registers in a method. It could be more in case of inlining.
1131 size_t EnvironmentSize() const;
1132
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001133 LocationSummary* GetLocations() const { return locations_; }
1134 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001135
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001136 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001137 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001138
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001139 // Move `this` instruction before `cursor`.
1140 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001141
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001142#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001143 bool Is##type() const { return (As##type() != nullptr); } \
1144 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001145 virtual H##type* As##type() { return nullptr; }
1146
1147 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1148#undef INSTRUCTION_TYPE_CHECK
1149
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001150 // Returns whether the instruction can be moved within the graph.
1151 virtual bool CanBeMoved() const { return false; }
1152
1153 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001154 virtual bool InstructionTypeEquals(HInstruction* other) const {
1155 UNUSED(other);
1156 return false;
1157 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001158
1159 // Returns whether any data encoded in the two instructions is equal.
1160 // This method does not look at the inputs. Both instructions must be
1161 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001162 virtual bool InstructionDataEquals(HInstruction* other) const {
1163 UNUSED(other);
1164 return false;
1165 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001166
1167 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001168 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001169 // 2) Their inputs are identical.
1170 bool Equals(HInstruction* other) const;
1171
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001172 virtual InstructionKind GetKind() const = 0;
1173
1174 virtual size_t ComputeHashCode() const {
1175 size_t result = GetKind();
1176 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1177 result = (result * 31) + InputAt(i)->GetId();
1178 }
1179 return result;
1180 }
1181
1182 SideEffects GetSideEffects() const { return side_effects_; }
1183
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001184 size_t GetLifetimePosition() const { return lifetime_position_; }
1185 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1186 LiveInterval* GetLiveInterval() const { return live_interval_; }
1187 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1188 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1189
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001190 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1191
1192 // Returns whether the code generation of the instruction will require to have access
1193 // to the current method. Such instructions are:
1194 // (1): Instructions that require an environment, as calling the runtime requires
1195 // to walk the stack and have the current method stored at a specific stack address.
1196 // (2): Object literals like classes and strings, that are loaded from the dex cache
1197 // fields of the current method.
1198 bool NeedsCurrentMethod() const {
1199 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1200 }
1201
David Brazdil1abb4192015-02-17 18:33:36 +00001202 protected:
1203 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1204 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1205
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001206 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001207 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1208
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001209 HInstruction* previous_;
1210 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001211 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001212
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001213 // An instruction gets an id when it is added to the graph.
1214 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001215 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001216 int id_;
1217
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001218 // When doing liveness analysis, instructions that have uses get an SSA index.
1219 int ssa_index_;
1220
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001221 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001222 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001223
1224 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001225 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001226
Nicolas Geoffray39468442014-09-02 15:17:15 +01001227 // The environment associated with this instruction. Not null if the instruction
1228 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001229 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001230
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001231 // Set by the code generator.
1232 LocationSummary* locations_;
1233
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001234 // Set by the liveness analysis.
1235 LiveInterval* live_interval_;
1236
1237 // Set by the liveness analysis, this is the position in a linear
1238 // order of blocks where this instruction's live interval start.
1239 size_t lifetime_position_;
1240
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001241 const SideEffects side_effects_;
1242
Calin Juravleacf735c2015-02-12 15:25:22 +00001243 // TODO: for primitive types this should be marked as invalid.
1244 ReferenceTypeInfo reference_type_info_;
1245
David Brazdil1abb4192015-02-17 18:33:36 +00001246 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001247 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001248 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001249 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001250 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001251
1252 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1253};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001254std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001255
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001256class HInputIterator : public ValueObject {
1257 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001258 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001259
1260 bool Done() const { return index_ == instruction_->InputCount(); }
1261 HInstruction* Current() const { return instruction_->InputAt(index_); }
1262 void Advance() { index_++; }
1263
1264 private:
1265 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001266 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001267
1268 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1269};
1270
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001271class HInstructionIterator : public ValueObject {
1272 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001273 explicit HInstructionIterator(const HInstructionList& instructions)
1274 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001275 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001276 }
1277
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001278 bool Done() const { return instruction_ == nullptr; }
1279 HInstruction* Current() const { return instruction_; }
1280 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001281 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001282 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001283 }
1284
1285 private:
1286 HInstruction* instruction_;
1287 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001288
1289 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001290};
1291
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001292class HBackwardInstructionIterator : public ValueObject {
1293 public:
1294 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1295 : instruction_(instructions.last_instruction_) {
1296 next_ = Done() ? nullptr : instruction_->GetPrevious();
1297 }
1298
1299 bool Done() const { return instruction_ == nullptr; }
1300 HInstruction* Current() const { return instruction_; }
1301 void Advance() {
1302 instruction_ = next_;
1303 next_ = Done() ? nullptr : instruction_->GetPrevious();
1304 }
1305
1306 private:
1307 HInstruction* instruction_;
1308 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001309
1310 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001311};
1312
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001313// An embedded container with N elements of type T. Used (with partial
1314// specialization for N=0) because embedded arrays cannot have size 0.
1315template<typename T, intptr_t N>
1316class EmbeddedArray {
1317 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001318 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001319
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001320 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001321
1322 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001323 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001324 return elements_[i];
1325 }
1326
1327 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001328 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001329 return elements_[i];
1330 }
1331
1332 const T& At(intptr_t i) const {
1333 return (*this)[i];
1334 }
1335
1336 void SetAt(intptr_t i, const T& val) {
1337 (*this)[i] = val;
1338 }
1339
1340 private:
1341 T elements_[N];
1342};
1343
1344template<typename T>
1345class EmbeddedArray<T, 0> {
1346 public:
1347 intptr_t length() const { return 0; }
1348 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001349 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001350 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001351 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001352 }
1353 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001354 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001355 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001356 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001357 }
1358};
1359
1360template<intptr_t N>
1361class HTemplateInstruction: public HInstruction {
1362 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001363 HTemplateInstruction<N>(SideEffects side_effects)
1364 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001365 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001366
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001367 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001368
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001369 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001370 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1371
1372 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1373 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001374 }
1375
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001376 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001377 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001378
1379 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001380};
1381
Dave Allison20dfc792014-06-16 20:44:29 -07001382template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001383class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001384 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001385 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1386 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001387 virtual ~HExpression() {}
1388
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001389 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001390
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001391 protected:
1392 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001393};
1394
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001395// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1396// instruction that branches to the exit block.
1397class HReturnVoid : public HTemplateInstruction<0> {
1398 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001399 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001400
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001401 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001402
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001403 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001404
1405 private:
1406 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1407};
1408
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001409// Represents dex's RETURN opcodes. A HReturn is a control flow
1410// instruction that branches to the exit block.
1411class HReturn : public HTemplateInstruction<1> {
1412 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001413 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001414 SetRawInputAt(0, value);
1415 }
1416
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001417 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001418
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001419 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001420
1421 private:
1422 DISALLOW_COPY_AND_ASSIGN(HReturn);
1423};
1424
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001425// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001426// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001427// exit block.
1428class HExit : public HTemplateInstruction<0> {
1429 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001430 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001431
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001432 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001433
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001434 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001435
1436 private:
1437 DISALLOW_COPY_AND_ASSIGN(HExit);
1438};
1439
1440// Jumps from one block to another.
1441class HGoto : public HTemplateInstruction<0> {
1442 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001443 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1444
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001445 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001446
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001447 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001448 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001449 }
1450
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001451 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001452
1453 private:
1454 DISALLOW_COPY_AND_ASSIGN(HGoto);
1455};
1456
Dave Allison20dfc792014-06-16 20:44:29 -07001457
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001458// Conditional branch. A block ending with an HIf instruction must have
1459// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001460class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001461 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001462 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001463 SetRawInputAt(0, input);
1464 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001465
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001466 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001467
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001468 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001469 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001470 }
1471
1472 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001473 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001474 }
1475
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001476 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001477
Dave Allison20dfc792014-06-16 20:44:29 -07001478 virtual bool IsIfInstruction() const { return true; }
1479
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001480 private:
1481 DISALLOW_COPY_AND_ASSIGN(HIf);
1482};
1483
Roland Levillain88cb1752014-10-20 16:36:47 +01001484class HUnaryOperation : public HExpression<1> {
1485 public:
1486 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1487 : HExpression(result_type, SideEffects::None()) {
1488 SetRawInputAt(0, input);
1489 }
1490
1491 HInstruction* GetInput() const { return InputAt(0); }
1492 Primitive::Type GetResultType() const { return GetType(); }
1493
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001494 bool CanBeMoved() const OVERRIDE { return true; }
1495 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001496 UNUSED(other);
1497 return true;
1498 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001499
Roland Levillain9240d6a2014-10-20 16:47:04 +01001500 // Try to statically evaluate `operation` and return a HConstant
1501 // containing the result of this evaluation. If `operation` cannot
1502 // be evaluated as a constant, return nullptr.
1503 HConstant* TryStaticEvaluation() const;
1504
1505 // Apply this operation to `x`.
1506 virtual int32_t Evaluate(int32_t x) const = 0;
1507 virtual int64_t Evaluate(int64_t x) const = 0;
1508
Roland Levillain88cb1752014-10-20 16:36:47 +01001509 DECLARE_INSTRUCTION(UnaryOperation);
1510
1511 private:
1512 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1513};
1514
Dave Allison20dfc792014-06-16 20:44:29 -07001515class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001516 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001517 HBinaryOperation(Primitive::Type result_type,
1518 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001519 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001520 SetRawInputAt(0, left);
1521 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001522 }
1523
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001524 HInstruction* GetLeft() const { return InputAt(0); }
1525 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001526 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001527
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001528 virtual bool IsCommutative() const { return false; }
1529
1530 // Put constant on the right.
1531 // Returns whether order is changed.
1532 bool OrderInputsWithConstantOnTheRight() {
1533 HInstruction* left = InputAt(0);
1534 HInstruction* right = InputAt(1);
1535 if (left->IsConstant() && !right->IsConstant()) {
1536 ReplaceInput(right, 0);
1537 ReplaceInput(left, 1);
1538 return true;
1539 }
1540 return false;
1541 }
1542
1543 // Order inputs by instruction id, but favor constant on the right side.
1544 // This helps GVN for commutative ops.
1545 void OrderInputs() {
1546 DCHECK(IsCommutative());
1547 HInstruction* left = InputAt(0);
1548 HInstruction* right = InputAt(1);
1549 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1550 return;
1551 }
1552 if (OrderInputsWithConstantOnTheRight()) {
1553 return;
1554 }
1555 // Order according to instruction id.
1556 if (left->GetId() > right->GetId()) {
1557 ReplaceInput(right, 0);
1558 ReplaceInput(left, 1);
1559 }
1560 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001561
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001562 bool CanBeMoved() const OVERRIDE { return true; }
1563 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001564 UNUSED(other);
1565 return true;
1566 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001567
Roland Levillain9240d6a2014-10-20 16:47:04 +01001568 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001569 // containing the result of this evaluation. If `operation` cannot
1570 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001571 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001572
1573 // Apply this operation to `x` and `y`.
1574 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1575 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1576
Roland Levillainccc07a92014-09-16 14:48:16 +01001577 DECLARE_INSTRUCTION(BinaryOperation);
1578
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001579 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001580 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1581};
1582
Dave Allison20dfc792014-06-16 20:44:29 -07001583class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001584 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001585 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001586 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1587 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001588
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001589 bool NeedsMaterialization() const { return needs_materialization_; }
1590 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001591
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001592 // For code generation purposes, returns whether this instruction is just before
1593 // `if_`, and disregard moves in between.
1594 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1595
Dave Allison20dfc792014-06-16 20:44:29 -07001596 DECLARE_INSTRUCTION(Condition);
1597
1598 virtual IfCondition GetCondition() const = 0;
1599
1600 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001601 // For register allocation purposes, returns whether this instruction needs to be
1602 // materialized (that is, not just be in the processor flags).
1603 bool needs_materialization_;
1604
Dave Allison20dfc792014-06-16 20:44:29 -07001605 DISALLOW_COPY_AND_ASSIGN(HCondition);
1606};
1607
1608// Instruction to check if two inputs are equal to each other.
1609class HEqual : public HCondition {
1610 public:
1611 HEqual(HInstruction* first, HInstruction* second)
1612 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001613
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001614 bool IsCommutative() const OVERRIDE { return true; }
1615
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001616 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001617 return x == y ? 1 : 0;
1618 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001619 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001620 return x == y ? 1 : 0;
1621 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001622
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001623 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001624
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001625 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001626 return kCondEQ;
1627 }
1628
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001629 private:
1630 DISALLOW_COPY_AND_ASSIGN(HEqual);
1631};
1632
Dave Allison20dfc792014-06-16 20:44:29 -07001633class HNotEqual : public HCondition {
1634 public:
1635 HNotEqual(HInstruction* first, HInstruction* second)
1636 : HCondition(first, second) {}
1637
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001638 bool IsCommutative() const OVERRIDE { return true; }
1639
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001640 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001641 return x != y ? 1 : 0;
1642 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001643 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001644 return x != y ? 1 : 0;
1645 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001646
Dave Allison20dfc792014-06-16 20:44:29 -07001647 DECLARE_INSTRUCTION(NotEqual);
1648
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001649 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001650 return kCondNE;
1651 }
1652
1653 private:
1654 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1655};
1656
1657class HLessThan : public HCondition {
1658 public:
1659 HLessThan(HInstruction* first, HInstruction* second)
1660 : HCondition(first, second) {}
1661
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001662 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001663 return x < y ? 1 : 0;
1664 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001665 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001666 return x < y ? 1 : 0;
1667 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001668
Dave Allison20dfc792014-06-16 20:44:29 -07001669 DECLARE_INSTRUCTION(LessThan);
1670
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001671 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001672 return kCondLT;
1673 }
1674
1675 private:
1676 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1677};
1678
1679class HLessThanOrEqual : public HCondition {
1680 public:
1681 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1682 : HCondition(first, second) {}
1683
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001684 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001685 return x <= y ? 1 : 0;
1686 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001687 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001688 return x <= y ? 1 : 0;
1689 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001690
Dave Allison20dfc792014-06-16 20:44:29 -07001691 DECLARE_INSTRUCTION(LessThanOrEqual);
1692
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001693 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001694 return kCondLE;
1695 }
1696
1697 private:
1698 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1699};
1700
1701class HGreaterThan : public HCondition {
1702 public:
1703 HGreaterThan(HInstruction* first, HInstruction* second)
1704 : HCondition(first, second) {}
1705
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001706 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001707 return x > y ? 1 : 0;
1708 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001709 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001710 return x > y ? 1 : 0;
1711 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001712
Dave Allison20dfc792014-06-16 20:44:29 -07001713 DECLARE_INSTRUCTION(GreaterThan);
1714
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001715 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001716 return kCondGT;
1717 }
1718
1719 private:
1720 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1721};
1722
1723class HGreaterThanOrEqual : public HCondition {
1724 public:
1725 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1726 : HCondition(first, second) {}
1727
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001728 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001729 return x >= y ? 1 : 0;
1730 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001731 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001732 return x >= y ? 1 : 0;
1733 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001734
Dave Allison20dfc792014-06-16 20:44:29 -07001735 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1736
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001737 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001738 return kCondGE;
1739 }
1740
1741 private:
1742 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1743};
1744
1745
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001746// Instruction to check how two inputs compare to each other.
1747// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1748class HCompare : public HBinaryOperation {
1749 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001750 // The bias applies for floating point operations and indicates how NaN
1751 // comparisons are treated:
1752 enum Bias {
1753 kNoBias, // bias is not applicable (i.e. for long operation)
1754 kGtBias, // return 1 for NaN comparisons
1755 kLtBias, // return -1 for NaN comparisons
1756 };
1757
1758 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1759 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001760 DCHECK_EQ(type, first->GetType());
1761 DCHECK_EQ(type, second->GetType());
1762 }
1763
Calin Juravleddb7df22014-11-25 20:56:51 +00001764 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001765 return
1766 x == y ? 0 :
1767 x > y ? 1 :
1768 -1;
1769 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001770
1771 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001772 return
1773 x == y ? 0 :
1774 x > y ? 1 :
1775 -1;
1776 }
1777
Calin Juravleddb7df22014-11-25 20:56:51 +00001778 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1779 return bias_ == other->AsCompare()->bias_;
1780 }
1781
1782 bool IsGtBias() { return bias_ == kGtBias; }
1783
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001784 DECLARE_INSTRUCTION(Compare);
1785
1786 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001787 const Bias bias_;
1788
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001789 DISALLOW_COPY_AND_ASSIGN(HCompare);
1790};
1791
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001792// A local in the graph. Corresponds to a Dex register.
1793class HLocal : public HTemplateInstruction<0> {
1794 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001795 explicit HLocal(uint16_t reg_number)
1796 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001797
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001798 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001799
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001800 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001801
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001802 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001803 // The Dex register number.
1804 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001805
1806 DISALLOW_COPY_AND_ASSIGN(HLocal);
1807};
1808
1809// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001810class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001811 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001812 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001813 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001814 SetRawInputAt(0, local);
1815 }
1816
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001817 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1818
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001819 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001820
1821 private:
1822 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1823};
1824
1825// Store a value in a given local. This instruction has two inputs: the value
1826// and the local.
1827class HStoreLocal : public HTemplateInstruction<2> {
1828 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001829 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001830 SetRawInputAt(0, local);
1831 SetRawInputAt(1, value);
1832 }
1833
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001834 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1835
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001836 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001837
1838 private:
1839 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1840};
1841
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001842class HConstant : public HExpression<0> {
1843 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001844 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1845
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001846 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001847
1848 DECLARE_INSTRUCTION(Constant);
1849
1850 private:
1851 DISALLOW_COPY_AND_ASSIGN(HConstant);
1852};
1853
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001854class HFloatConstant : public HConstant {
1855 public:
1856 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1857
1858 float GetValue() const { return value_; }
1859
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001860 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001861 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1862 bit_cast<float, int32_t>(value_);
1863 }
1864
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001865 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001866
1867 DECLARE_INSTRUCTION(FloatConstant);
1868
1869 private:
1870 const float value_;
1871
1872 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1873};
1874
1875class HDoubleConstant : public HConstant {
1876 public:
1877 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1878
1879 double GetValue() const { return value_; }
1880
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001881 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001882 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1883 bit_cast<double, int64_t>(value_);
1884 }
1885
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001886 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001887
1888 DECLARE_INSTRUCTION(DoubleConstant);
1889
1890 private:
1891 const double value_;
1892
1893 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1894};
1895
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001896class HNullConstant : public HConstant {
1897 public:
1898 HNullConstant() : HConstant(Primitive::kPrimNot) {}
1899
1900 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
1901 return true;
1902 }
1903
1904 size_t ComputeHashCode() const OVERRIDE { return 0; }
1905
Calin Juravle61d544b2015-02-23 16:46:57 +00001906 bool ActAsNullConstant() const OVERRIDE { return true; }
1907
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001908 DECLARE_INSTRUCTION(NullConstant);
1909
1910 private:
1911 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
1912};
1913
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001914// Constants of the type int. Those can be from Dex instructions, or
1915// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001916class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001917 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001918 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001919
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001920 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001921
Calin Juravle61d544b2015-02-23 16:46:57 +00001922 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001923 return other->AsIntConstant()->value_ == value_;
1924 }
1925
Calin Juravle61d544b2015-02-23 16:46:57 +00001926 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
1927
1928 // TODO: Null is represented by the `0` constant. In most cases we replace it
1929 // with a HNullConstant but we don't do it when comparing (a != null). This
1930 // method is an workaround until we fix the above.
1931 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001932
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001933 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001934
1935 private:
1936 const int32_t value_;
1937
1938 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1939};
1940
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001941class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001942 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001943 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001944
1945 int64_t GetValue() const { return value_; }
1946
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001947 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001948 return other->AsLongConstant()->value_ == value_;
1949 }
1950
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001951 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001952
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001953 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001954
1955 private:
1956 const int64_t value_;
1957
1958 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1959};
1960
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001961enum class Intrinsics {
1962#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
1963#include "intrinsics_list.h"
1964 kNone,
1965 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
1966#undef INTRINSICS_LIST
1967#undef OPTIMIZING_INTRINSICS
1968};
1969std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
1970
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001971class HInvoke : public HInstruction {
1972 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001973 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001974
1975 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1976 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00001977 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001978
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001979 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001980 SetRawInputAt(index, argument);
1981 }
1982
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001983 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001984
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001985 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001986
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001987 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1988
1989 Intrinsics GetIntrinsic() {
1990 return intrinsic_;
1991 }
1992
1993 void SetIntrinsic(Intrinsics intrinsic) {
1994 intrinsic_ = intrinsic;
1995 }
1996
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001997 DECLARE_INSTRUCTION(Invoke);
1998
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001999 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002000 HInvoke(ArenaAllocator* arena,
2001 uint32_t number_of_arguments,
2002 Primitive::Type return_type,
2003 uint32_t dex_pc,
2004 uint32_t dex_method_index)
2005 : HInstruction(SideEffects::All()),
2006 inputs_(arena, number_of_arguments),
2007 return_type_(return_type),
2008 dex_pc_(dex_pc),
2009 dex_method_index_(dex_method_index),
2010 intrinsic_(Intrinsics::kNone) {
2011 inputs_.SetSize(number_of_arguments);
2012 }
2013
David Brazdil1abb4192015-02-17 18:33:36 +00002014 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2015 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2016 inputs_.Put(index, input);
2017 }
2018
2019 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002020 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002021 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002022 const uint32_t dex_method_index_;
2023 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002024
2025 private:
2026 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2027};
2028
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002029class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002030 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002031 HInvokeStaticOrDirect(ArenaAllocator* arena,
2032 uint32_t number_of_arguments,
2033 Primitive::Type return_type,
2034 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002035 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002036 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002037 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002038 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002039 invoke_type_(invoke_type),
2040 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002041
Calin Juravle77520bc2015-01-12 18:45:46 +00002042 bool CanDoImplicitNullCheck() const OVERRIDE {
2043 // We access the method via the dex cache so we can't do an implicit null check.
2044 // TODO: for intrinsics we can generate implicit null checks.
2045 return false;
2046 }
2047
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002048 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002049 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002050
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002051 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002052
2053 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002054 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002055 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002056
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002057 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002058};
2059
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002060class HInvokeVirtual : public HInvoke {
2061 public:
2062 HInvokeVirtual(ArenaAllocator* arena,
2063 uint32_t number_of_arguments,
2064 Primitive::Type return_type,
2065 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002066 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002067 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002068 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002069 vtable_index_(vtable_index) {}
2070
Calin Juravle77520bc2015-01-12 18:45:46 +00002071 bool CanDoImplicitNullCheck() const OVERRIDE {
2072 // TODO: Add implicit null checks in intrinsics.
2073 return !GetLocations()->Intrinsified();
2074 }
2075
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002076 uint32_t GetVTableIndex() const { return vtable_index_; }
2077
2078 DECLARE_INSTRUCTION(InvokeVirtual);
2079
2080 private:
2081 const uint32_t vtable_index_;
2082
2083 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2084};
2085
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002086class HInvokeInterface : public HInvoke {
2087 public:
2088 HInvokeInterface(ArenaAllocator* arena,
2089 uint32_t number_of_arguments,
2090 Primitive::Type return_type,
2091 uint32_t dex_pc,
2092 uint32_t dex_method_index,
2093 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002094 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002095 imt_index_(imt_index) {}
2096
Calin Juravle77520bc2015-01-12 18:45:46 +00002097 bool CanDoImplicitNullCheck() const OVERRIDE {
2098 // TODO: Add implicit null checks in intrinsics.
2099 return !GetLocations()->Intrinsified();
2100 }
2101
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002102 uint32_t GetImtIndex() const { return imt_index_; }
2103 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2104
2105 DECLARE_INSTRUCTION(InvokeInterface);
2106
2107 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002108 const uint32_t imt_index_;
2109
2110 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2111};
2112
Dave Allison20dfc792014-06-16 20:44:29 -07002113class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002114 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002115 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002116 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2117 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002118 type_index_(type_index),
2119 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002120
2121 uint32_t GetDexPc() const { return dex_pc_; }
2122 uint16_t GetTypeIndex() const { return type_index_; }
2123
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002124 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002125 bool NeedsEnvironment() const OVERRIDE { return true; }
2126 // It may throw when called on:
2127 // - interfaces
2128 // - abstract/innaccessible/unknown classes
2129 // TODO: optimize when possible.
2130 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002131
Calin Juravle10e244f2015-01-26 18:54:32 +00002132 bool CanBeNull() const OVERRIDE { return false; }
2133
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002134 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2135
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002136 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002137
2138 private:
2139 const uint32_t dex_pc_;
2140 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002141 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002142
2143 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2144};
2145
Roland Levillain88cb1752014-10-20 16:36:47 +01002146class HNeg : public HUnaryOperation {
2147 public:
2148 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2149 : HUnaryOperation(result_type, input) {}
2150
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002151 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2152 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002153
Roland Levillain88cb1752014-10-20 16:36:47 +01002154 DECLARE_INSTRUCTION(Neg);
2155
2156 private:
2157 DISALLOW_COPY_AND_ASSIGN(HNeg);
2158};
2159
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002160class HNewArray : public HExpression<1> {
2161 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002162 HNewArray(HInstruction* length,
2163 uint32_t dex_pc,
2164 uint16_t type_index,
2165 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002166 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2167 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002168 type_index_(type_index),
2169 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002170 SetRawInputAt(0, length);
2171 }
2172
2173 uint32_t GetDexPc() const { return dex_pc_; }
2174 uint16_t GetTypeIndex() const { return type_index_; }
2175
2176 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002177 bool NeedsEnvironment() const OVERRIDE { return true; }
2178
2179 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002180
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002181 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2182
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002183 DECLARE_INSTRUCTION(NewArray);
2184
2185 private:
2186 const uint32_t dex_pc_;
2187 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002188 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002189
2190 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2191};
2192
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002193class HAdd : public HBinaryOperation {
2194 public:
2195 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2196 : HBinaryOperation(result_type, left, right) {}
2197
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002198 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002199
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002200 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002201 return x + y;
2202 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002203 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002204 return x + y;
2205 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002206
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002207 DECLARE_INSTRUCTION(Add);
2208
2209 private:
2210 DISALLOW_COPY_AND_ASSIGN(HAdd);
2211};
2212
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002213class HSub : public HBinaryOperation {
2214 public:
2215 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2216 : HBinaryOperation(result_type, left, right) {}
2217
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002218 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002219 return x - y;
2220 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002221 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002222 return x - y;
2223 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002224
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002225 DECLARE_INSTRUCTION(Sub);
2226
2227 private:
2228 DISALLOW_COPY_AND_ASSIGN(HSub);
2229};
2230
Calin Juravle34bacdf2014-10-07 20:23:36 +01002231class HMul : public HBinaryOperation {
2232 public:
2233 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2234 : HBinaryOperation(result_type, left, right) {}
2235
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002236 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002237
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002238 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2239 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002240
2241 DECLARE_INSTRUCTION(Mul);
2242
2243 private:
2244 DISALLOW_COPY_AND_ASSIGN(HMul);
2245};
2246
Calin Juravle7c4954d2014-10-28 16:57:40 +00002247class HDiv : public HBinaryOperation {
2248 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002249 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2250 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002251
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002252 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002253 // Our graph structure ensures we never have 0 for `y` during constant folding.
2254 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002255 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002256 return (y == -1) ? -x : x / y;
2257 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002258
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002259 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002260 DCHECK_NE(y, 0);
2261 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2262 return (y == -1) ? -x : x / y;
2263 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002264
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002265 uint32_t GetDexPc() const { return dex_pc_; }
2266
Calin Juravle7c4954d2014-10-28 16:57:40 +00002267 DECLARE_INSTRUCTION(Div);
2268
2269 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002270 const uint32_t dex_pc_;
2271
Calin Juravle7c4954d2014-10-28 16:57:40 +00002272 DISALLOW_COPY_AND_ASSIGN(HDiv);
2273};
2274
Calin Juravlebacfec32014-11-14 15:54:36 +00002275class HRem : public HBinaryOperation {
2276 public:
2277 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2278 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2279
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002280 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002281 DCHECK_NE(y, 0);
2282 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2283 return (y == -1) ? 0 : x % y;
2284 }
2285
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002286 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002287 DCHECK_NE(y, 0);
2288 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2289 return (y == -1) ? 0 : x % y;
2290 }
2291
2292 uint32_t GetDexPc() const { return dex_pc_; }
2293
2294 DECLARE_INSTRUCTION(Rem);
2295
2296 private:
2297 const uint32_t dex_pc_;
2298
2299 DISALLOW_COPY_AND_ASSIGN(HRem);
2300};
2301
Calin Juravled0d48522014-11-04 16:40:20 +00002302class HDivZeroCheck : public HExpression<1> {
2303 public:
2304 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2305 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2306 SetRawInputAt(0, value);
2307 }
2308
2309 bool CanBeMoved() const OVERRIDE { return true; }
2310
2311 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2312 UNUSED(other);
2313 return true;
2314 }
2315
2316 bool NeedsEnvironment() const OVERRIDE { return true; }
2317 bool CanThrow() const OVERRIDE { return true; }
2318
2319 uint32_t GetDexPc() const { return dex_pc_; }
2320
2321 DECLARE_INSTRUCTION(DivZeroCheck);
2322
2323 private:
2324 const uint32_t dex_pc_;
2325
2326 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2327};
2328
Calin Juravle9aec02f2014-11-18 23:06:35 +00002329class HShl : public HBinaryOperation {
2330 public:
2331 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2332 : HBinaryOperation(result_type, left, right) {}
2333
2334 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2335 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2336
2337 DECLARE_INSTRUCTION(Shl);
2338
2339 private:
2340 DISALLOW_COPY_AND_ASSIGN(HShl);
2341};
2342
2343class HShr : public HBinaryOperation {
2344 public:
2345 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2346 : HBinaryOperation(result_type, left, right) {}
2347
2348 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2349 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2350
2351 DECLARE_INSTRUCTION(Shr);
2352
2353 private:
2354 DISALLOW_COPY_AND_ASSIGN(HShr);
2355};
2356
2357class HUShr : public HBinaryOperation {
2358 public:
2359 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2360 : HBinaryOperation(result_type, left, right) {}
2361
2362 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2363 uint32_t ux = static_cast<uint32_t>(x);
2364 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2365 return static_cast<int32_t>(ux >> uy);
2366 }
2367
2368 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2369 uint64_t ux = static_cast<uint64_t>(x);
2370 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2371 return static_cast<int64_t>(ux >> uy);
2372 }
2373
2374 DECLARE_INSTRUCTION(UShr);
2375
2376 private:
2377 DISALLOW_COPY_AND_ASSIGN(HUShr);
2378};
2379
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002380class HAnd : public HBinaryOperation {
2381 public:
2382 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2383 : HBinaryOperation(result_type, left, right) {}
2384
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002385 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002386
2387 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2388 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2389
2390 DECLARE_INSTRUCTION(And);
2391
2392 private:
2393 DISALLOW_COPY_AND_ASSIGN(HAnd);
2394};
2395
2396class HOr : public HBinaryOperation {
2397 public:
2398 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2399 : HBinaryOperation(result_type, left, right) {}
2400
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002401 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002402
2403 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2404 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2405
2406 DECLARE_INSTRUCTION(Or);
2407
2408 private:
2409 DISALLOW_COPY_AND_ASSIGN(HOr);
2410};
2411
2412class HXor : public HBinaryOperation {
2413 public:
2414 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2415 : HBinaryOperation(result_type, left, right) {}
2416
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002417 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002418
2419 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2420 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2421
2422 DECLARE_INSTRUCTION(Xor);
2423
2424 private:
2425 DISALLOW_COPY_AND_ASSIGN(HXor);
2426};
2427
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002428// The value of a parameter in this method. Its location depends on
2429// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002430class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002431 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002432 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2433 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002434
2435 uint8_t GetIndex() const { return index_; }
2436
Calin Juravle10e244f2015-01-26 18:54:32 +00002437 bool CanBeNull() const OVERRIDE { return !is_this_; }
2438
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002439 DECLARE_INSTRUCTION(ParameterValue);
2440
2441 private:
2442 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002443 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002444 const uint8_t index_;
2445
Calin Juravle10e244f2015-01-26 18:54:32 +00002446 // Whether or not the parameter value corresponds to 'this' argument.
2447 const bool is_this_;
2448
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002449 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2450};
2451
Roland Levillain1cc5f252014-10-22 18:06:21 +01002452class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002453 public:
Roland Levillain1cc5f252014-10-22 18:06:21 +01002454 explicit HNot(Primitive::Type result_type, HInstruction* input)
2455 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002456
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002457 bool CanBeMoved() const OVERRIDE { return true; }
2458 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002459 UNUSED(other);
2460 return true;
2461 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002462
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002463 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2464 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f252014-10-22 18:06:21 +01002465
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002466 DECLARE_INSTRUCTION(Not);
2467
2468 private:
2469 DISALLOW_COPY_AND_ASSIGN(HNot);
2470};
2471
Roland Levillaindff1f282014-11-05 14:15:05 +00002472class HTypeConversion : public HExpression<1> {
2473 public:
2474 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002475 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2476 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002477 SetRawInputAt(0, input);
2478 DCHECK_NE(input->GetType(), result_type);
2479 }
2480
2481 HInstruction* GetInput() const { return InputAt(0); }
2482 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2483 Primitive::Type GetResultType() const { return GetType(); }
2484
Roland Levillain624279f2014-12-04 11:54:28 +00002485 // Required by the x86 and ARM code generators when producing calls
2486 // to the runtime.
2487 uint32_t GetDexPc() const { return dex_pc_; }
2488
Roland Levillaindff1f282014-11-05 14:15:05 +00002489 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002490 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002491
2492 DECLARE_INSTRUCTION(TypeConversion);
2493
2494 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002495 const uint32_t dex_pc_;
2496
Roland Levillaindff1f282014-11-05 14:15:05 +00002497 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2498};
2499
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002500static constexpr uint32_t kNoRegNumber = -1;
2501
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002502class HPhi : public HInstruction {
2503 public:
2504 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002505 : HInstruction(SideEffects::None()),
2506 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002507 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002508 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002509 is_live_(false),
2510 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002511 inputs_.SetSize(number_of_inputs);
2512 }
2513
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002514 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2515 static Primitive::Type ToPhiType(Primitive::Type type) {
2516 switch (type) {
2517 case Primitive::kPrimBoolean:
2518 case Primitive::kPrimByte:
2519 case Primitive::kPrimShort:
2520 case Primitive::kPrimChar:
2521 return Primitive::kPrimInt;
2522 default:
2523 return type;
2524 }
2525 }
2526
Calin Juravle10e244f2015-01-26 18:54:32 +00002527 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002528
2529 void AddInput(HInstruction* input);
2530
Calin Juravle10e244f2015-01-26 18:54:32 +00002531 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002532 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002533
Calin Juravle10e244f2015-01-26 18:54:32 +00002534 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2535 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2536
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002537 uint32_t GetRegNumber() const { return reg_number_; }
2538
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002539 void SetDead() { is_live_ = false; }
2540 void SetLive() { is_live_ = true; }
2541 bool IsDead() const { return !is_live_; }
2542 bool IsLive() const { return is_live_; }
2543
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002544 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002545
David Brazdil1abb4192015-02-17 18:33:36 +00002546 protected:
2547 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2548
2549 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2550 inputs_.Put(index, input);
2551 }
2552
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002553 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002554 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002555 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002556 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002557 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002558 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002559
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002560 DISALLOW_COPY_AND_ASSIGN(HPhi);
2561};
2562
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002563class HNullCheck : public HExpression<1> {
2564 public:
2565 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002566 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002567 SetRawInputAt(0, value);
2568 }
2569
Calin Juravle10e244f2015-01-26 18:54:32 +00002570 bool CanBeMoved() const OVERRIDE { return true; }
2571 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002572 UNUSED(other);
2573 return true;
2574 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002575
Calin Juravle10e244f2015-01-26 18:54:32 +00002576 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002577
Calin Juravle10e244f2015-01-26 18:54:32 +00002578 bool CanThrow() const OVERRIDE { return true; }
2579
2580 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002581
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002582 uint32_t GetDexPc() const { return dex_pc_; }
2583
2584 DECLARE_INSTRUCTION(NullCheck);
2585
2586 private:
2587 const uint32_t dex_pc_;
2588
2589 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2590};
2591
2592class FieldInfo : public ValueObject {
2593 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002594 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2595 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002596
2597 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002598 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002599 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002600
2601 private:
2602 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002603 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002604 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002605};
2606
2607class HInstanceFieldGet : public HExpression<1> {
2608 public:
2609 HInstanceFieldGet(HInstruction* value,
2610 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002611 MemberOffset field_offset,
2612 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002613 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002614 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002615 SetRawInputAt(0, value);
2616 }
2617
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002618 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002619
2620 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2621 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2622 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002623 }
2624
Calin Juravle77520bc2015-01-12 18:45:46 +00002625 bool CanDoImplicitNullCheck() const OVERRIDE {
2626 return GetFieldOffset().Uint32Value() < kPageSize;
2627 }
2628
2629 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002630 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2631 }
2632
Calin Juravle52c48962014-12-16 17:02:57 +00002633 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002634 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002635 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002636 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002637
2638 DECLARE_INSTRUCTION(InstanceFieldGet);
2639
2640 private:
2641 const FieldInfo field_info_;
2642
2643 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2644};
2645
2646class HInstanceFieldSet : public HTemplateInstruction<2> {
2647 public:
2648 HInstanceFieldSet(HInstruction* object,
2649 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002650 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002651 MemberOffset field_offset,
2652 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002653 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002654 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002655 SetRawInputAt(0, object);
2656 SetRawInputAt(1, value);
2657 }
2658
Calin Juravle77520bc2015-01-12 18:45:46 +00002659 bool CanDoImplicitNullCheck() const OVERRIDE {
2660 return GetFieldOffset().Uint32Value() < kPageSize;
2661 }
2662
Calin Juravle52c48962014-12-16 17:02:57 +00002663 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002664 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002665 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002666 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002667 HInstruction* GetValue() const { return InputAt(1); }
2668
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002669 DECLARE_INSTRUCTION(InstanceFieldSet);
2670
2671 private:
2672 const FieldInfo field_info_;
2673
2674 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2675};
2676
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002677class HArrayGet : public HExpression<2> {
2678 public:
2679 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002680 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002681 SetRawInputAt(0, array);
2682 SetRawInputAt(1, index);
2683 }
2684
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002685 bool CanBeMoved() const OVERRIDE { return true; }
2686 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002687 UNUSED(other);
2688 return true;
2689 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002690 bool CanDoImplicitNullCheck() const OVERRIDE {
2691 // TODO: We can be smarter here.
2692 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2693 // which generates the implicit null check. There are cases when these can be removed
2694 // to produce better code. If we ever add optimizations to do so we should allow an
2695 // implicit check here (as long as the address falls in the first page).
2696 return false;
2697 }
2698
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002699 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002700
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002701 HInstruction* GetArray() const { return InputAt(0); }
2702 HInstruction* GetIndex() const { return InputAt(1); }
2703
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002704 DECLARE_INSTRUCTION(ArrayGet);
2705
2706 private:
2707 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2708};
2709
2710class HArraySet : public HTemplateInstruction<3> {
2711 public:
2712 HArraySet(HInstruction* array,
2713 HInstruction* index,
2714 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002715 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002716 uint32_t dex_pc)
2717 : HTemplateInstruction(SideEffects::ChangesSomething()),
2718 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002719 expected_component_type_(expected_component_type),
2720 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002721 SetRawInputAt(0, array);
2722 SetRawInputAt(1, index);
2723 SetRawInputAt(2, value);
2724 }
2725
Calin Juravle77520bc2015-01-12 18:45:46 +00002726 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002727 // We currently always call a runtime method to catch array store
2728 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002729 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002730 }
2731
Calin Juravle77520bc2015-01-12 18:45:46 +00002732 bool CanDoImplicitNullCheck() const OVERRIDE {
2733 // TODO: Same as for ArrayGet.
2734 return false;
2735 }
2736
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002737 void ClearNeedsTypeCheck() {
2738 needs_type_check_ = false;
2739 }
2740
2741 bool NeedsTypeCheck() const { return needs_type_check_; }
2742
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002743 uint32_t GetDexPc() const { return dex_pc_; }
2744
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002745 HInstruction* GetArray() const { return InputAt(0); }
2746 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002747 HInstruction* GetValue() const { return InputAt(2); }
2748
2749 Primitive::Type GetComponentType() const {
2750 // The Dex format does not type floating point index operations. Since the
2751 // `expected_component_type_` is set during building and can therefore not
2752 // be correct, we also check what is the value type. If it is a floating
2753 // point type, we must use that type.
2754 Primitive::Type value_type = GetValue()->GetType();
2755 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2756 ? value_type
2757 : expected_component_type_;
2758 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002759
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002760 DECLARE_INSTRUCTION(ArraySet);
2761
2762 private:
2763 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002764 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002765 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002766
2767 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2768};
2769
2770class HArrayLength : public HExpression<1> {
2771 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002772 explicit HArrayLength(HInstruction* array)
2773 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2774 // Note that arrays do not change length, so the instruction does not
2775 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002776 SetRawInputAt(0, array);
2777 }
2778
Calin Juravle77520bc2015-01-12 18:45:46 +00002779 bool CanBeMoved() const OVERRIDE { return true; }
2780 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002781 UNUSED(other);
2782 return true;
2783 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002784 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002785
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002786 DECLARE_INSTRUCTION(ArrayLength);
2787
2788 private:
2789 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2790};
2791
2792class HBoundsCheck : public HExpression<2> {
2793 public:
2794 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002795 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002796 DCHECK(index->GetType() == Primitive::kPrimInt);
2797 SetRawInputAt(0, index);
2798 SetRawInputAt(1, length);
2799 }
2800
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002801 bool CanBeMoved() const OVERRIDE { return true; }
2802 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002803 UNUSED(other);
2804 return true;
2805 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002806
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002807 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002808
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002809 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002810
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002811 uint32_t GetDexPc() const { return dex_pc_; }
2812
2813 DECLARE_INSTRUCTION(BoundsCheck);
2814
2815 private:
2816 const uint32_t dex_pc_;
2817
2818 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2819};
2820
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002821/**
2822 * Some DEX instructions are folded into multiple HInstructions that need
2823 * to stay live until the last HInstruction. This class
2824 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002825 * HInstruction stays live. `index` represents the stack location index of the
2826 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002827 */
2828class HTemporary : public HTemplateInstruction<0> {
2829 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002830 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002831
2832 size_t GetIndex() const { return index_; }
2833
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002834 Primitive::Type GetType() const OVERRIDE {
2835 // The previous instruction is the one that will be stored in the temporary location.
2836 DCHECK(GetPrevious() != nullptr);
2837 return GetPrevious()->GetType();
2838 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002839
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002840 DECLARE_INSTRUCTION(Temporary);
2841
2842 private:
2843 const size_t index_;
2844
2845 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2846};
2847
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002848class HSuspendCheck : public HTemplateInstruction<0> {
2849 public:
2850 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002851 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002852
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002853 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002854 return true;
2855 }
2856
2857 uint32_t GetDexPc() const { return dex_pc_; }
2858
2859 DECLARE_INSTRUCTION(SuspendCheck);
2860
2861 private:
2862 const uint32_t dex_pc_;
2863
2864 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2865};
2866
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002867/**
2868 * Instruction to load a Class object.
2869 */
2870class HLoadClass : public HExpression<0> {
2871 public:
2872 HLoadClass(uint16_t type_index,
2873 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002874 uint32_t dex_pc)
2875 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2876 type_index_(type_index),
2877 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002878 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00002879 generate_clinit_check_(false),
2880 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002881
2882 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002883
2884 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2885 return other->AsLoadClass()->type_index_ == type_index_;
2886 }
2887
2888 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2889
2890 uint32_t GetDexPc() const { return dex_pc_; }
2891 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002892 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002893
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002894 bool NeedsEnvironment() const OVERRIDE {
2895 // Will call runtime and load the class if the class is not loaded yet.
2896 // TODO: finer grain decision.
2897 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002898 }
2899
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002900 bool MustGenerateClinitCheck() const {
2901 return generate_clinit_check_;
2902 }
2903
2904 void SetMustGenerateClinitCheck() {
2905 generate_clinit_check_ = true;
2906 }
2907
2908 bool CanCallRuntime() const {
2909 return MustGenerateClinitCheck() || !is_referrers_class_;
2910 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002911
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002912 bool CanThrow() const OVERRIDE {
2913 // May call runtime and and therefore can throw.
2914 // TODO: finer grain decision.
2915 return !is_referrers_class_;
2916 }
2917
Calin Juravleacf735c2015-02-12 15:25:22 +00002918 ReferenceTypeInfo GetLoadedClassRTI() {
2919 return loaded_class_rti_;
2920 }
2921
2922 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
2923 // Make sure we only set exact types (the loaded class should never be merged).
2924 DCHECK(rti.IsExact());
2925 loaded_class_rti_ = rti;
2926 }
2927
2928 bool IsResolved() {
2929 return loaded_class_rti_.IsExact();
2930 }
2931
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002932 DECLARE_INSTRUCTION(LoadClass);
2933
2934 private:
2935 const uint16_t type_index_;
2936 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002937 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002938 // Whether this instruction must generate the initialization check.
2939 // Used for code generation.
2940 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002941
Calin Juravleacf735c2015-02-12 15:25:22 +00002942 ReferenceTypeInfo loaded_class_rti_;
2943
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002944 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2945};
2946
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002947class HLoadString : public HExpression<0> {
2948 public:
2949 HLoadString(uint32_t string_index, uint32_t dex_pc)
2950 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2951 string_index_(string_index),
2952 dex_pc_(dex_pc) {}
2953
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002954 bool CanBeMoved() const OVERRIDE { return true; }
2955
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002956 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2957 return other->AsLoadString()->string_index_ == string_index_;
2958 }
2959
2960 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2961
2962 uint32_t GetDexPc() const { return dex_pc_; }
2963 uint32_t GetStringIndex() const { return string_index_; }
2964
2965 // TODO: Can we deopt or debug when we resolve a string?
2966 bool NeedsEnvironment() const OVERRIDE { return false; }
2967
2968 DECLARE_INSTRUCTION(LoadString);
2969
2970 private:
2971 const uint32_t string_index_;
2972 const uint32_t dex_pc_;
2973
2974 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2975};
2976
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002977// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002978/**
2979 * Performs an initialization check on its Class object input.
2980 */
2981class HClinitCheck : public HExpression<1> {
2982 public:
2983 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2984 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2985 dex_pc_(dex_pc) {
2986 SetRawInputAt(0, constant);
2987 }
2988
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002989 bool CanBeMoved() const OVERRIDE { return true; }
2990 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2991 UNUSED(other);
2992 return true;
2993 }
2994
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002995 bool NeedsEnvironment() const OVERRIDE {
2996 // May call runtime to initialize the class.
2997 return true;
2998 }
2999
3000 uint32_t GetDexPc() const { return dex_pc_; }
3001
3002 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3003
3004 DECLARE_INSTRUCTION(ClinitCheck);
3005
3006 private:
3007 const uint32_t dex_pc_;
3008
3009 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3010};
3011
3012class HStaticFieldGet : public HExpression<1> {
3013 public:
3014 HStaticFieldGet(HInstruction* cls,
3015 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003016 MemberOffset field_offset,
3017 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003018 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003019 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003020 SetRawInputAt(0, cls);
3021 }
3022
Calin Juravle52c48962014-12-16 17:02:57 +00003023
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003024 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003025
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003026 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003027 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3028 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003029 }
3030
3031 size_t ComputeHashCode() const OVERRIDE {
3032 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
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
3040 DECLARE_INSTRUCTION(StaticFieldGet);
3041
3042 private:
3043 const FieldInfo field_info_;
3044
3045 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3046};
3047
3048class HStaticFieldSet : public HTemplateInstruction<2> {
3049 public:
3050 HStaticFieldSet(HInstruction* cls,
3051 HInstruction* value,
3052 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003053 MemberOffset field_offset,
3054 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003055 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003056 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003057 SetRawInputAt(0, cls);
3058 SetRawInputAt(1, value);
3059 }
3060
Calin Juravle52c48962014-12-16 17:02:57 +00003061 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003062 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3063 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003064 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003065
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003066 HInstruction* GetValue() const { return InputAt(1); }
3067
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003068 DECLARE_INSTRUCTION(StaticFieldSet);
3069
3070 private:
3071 const FieldInfo field_info_;
3072
3073 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3074};
3075
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003076// Implement the move-exception DEX instruction.
3077class HLoadException : public HExpression<0> {
3078 public:
3079 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3080
3081 DECLARE_INSTRUCTION(LoadException);
3082
3083 private:
3084 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3085};
3086
3087class HThrow : public HTemplateInstruction<1> {
3088 public:
3089 HThrow(HInstruction* exception, uint32_t dex_pc)
3090 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3091 SetRawInputAt(0, exception);
3092 }
3093
3094 bool IsControlFlow() const OVERRIDE { return true; }
3095
3096 bool NeedsEnvironment() const OVERRIDE { return true; }
3097
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003098 bool CanThrow() const OVERRIDE { return true; }
3099
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003100 uint32_t GetDexPc() const { return dex_pc_; }
3101
3102 DECLARE_INSTRUCTION(Throw);
3103
3104 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003105 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003106
3107 DISALLOW_COPY_AND_ASSIGN(HThrow);
3108};
3109
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003110class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003111 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003112 HInstanceOf(HInstruction* object,
3113 HLoadClass* constant,
3114 bool class_is_final,
3115 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003116 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3117 class_is_final_(class_is_final),
3118 dex_pc_(dex_pc) {
3119 SetRawInputAt(0, object);
3120 SetRawInputAt(1, constant);
3121 }
3122
3123 bool CanBeMoved() const OVERRIDE { return true; }
3124
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003125 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003126 return true;
3127 }
3128
3129 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003130 return false;
3131 }
3132
3133 uint32_t GetDexPc() const { return dex_pc_; }
3134
3135 bool IsClassFinal() const { return class_is_final_; }
3136
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003137 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003138
3139 private:
3140 const bool class_is_final_;
3141 const uint32_t dex_pc_;
3142
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003143 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3144};
3145
Calin Juravleb1498f62015-02-16 13:13:29 +00003146class HBoundType : public HExpression<1> {
3147 public:
3148 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3149 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3150 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003151 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003152 SetRawInputAt(0, input);
3153 }
3154
3155 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3156
3157 bool CanBeNull() const OVERRIDE {
3158 // `null instanceof ClassX` always return false so we can't be null.
3159 return false;
3160 }
3161
3162 DECLARE_INSTRUCTION(BoundType);
3163
3164 private:
3165 // Encodes the most upper class that this instruction can have. In other words
3166 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3167 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3168 const ReferenceTypeInfo bound_type_;
3169
3170 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3171};
3172
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003173class HCheckCast : public HTemplateInstruction<2> {
3174 public:
3175 HCheckCast(HInstruction* object,
3176 HLoadClass* constant,
3177 bool class_is_final,
3178 uint32_t dex_pc)
3179 : HTemplateInstruction(SideEffects::None()),
3180 class_is_final_(class_is_final),
3181 dex_pc_(dex_pc) {
3182 SetRawInputAt(0, object);
3183 SetRawInputAt(1, constant);
3184 }
3185
3186 bool CanBeMoved() const OVERRIDE { return true; }
3187
3188 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3189 return true;
3190 }
3191
3192 bool NeedsEnvironment() const OVERRIDE {
3193 // Instruction may throw a CheckCastError.
3194 return true;
3195 }
3196
3197 bool CanThrow() const OVERRIDE { return true; }
3198
3199 uint32_t GetDexPc() const { return dex_pc_; }
3200
3201 bool IsClassFinal() const { return class_is_final_; }
3202
3203 DECLARE_INSTRUCTION(CheckCast);
3204
3205 private:
3206 const bool class_is_final_;
3207 const uint32_t dex_pc_;
3208
3209 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003210};
3211
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003212class HMonitorOperation : public HTemplateInstruction<1> {
3213 public:
3214 enum OperationKind {
3215 kEnter,
3216 kExit,
3217 };
3218
3219 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3220 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3221 SetRawInputAt(0, object);
3222 }
3223
3224 // Instruction may throw a Java exception, so we need an environment.
3225 bool NeedsEnvironment() const OVERRIDE { return true; }
3226 bool CanThrow() const OVERRIDE { return true; }
3227
3228 uint32_t GetDexPc() const { return dex_pc_; }
3229
3230 bool IsEnter() const { return kind_ == kEnter; }
3231
3232 DECLARE_INSTRUCTION(MonitorOperation);
3233
Calin Juravle52c48962014-12-16 17:02:57 +00003234 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003235 const OperationKind kind_;
3236 const uint32_t dex_pc_;
3237
3238 private:
3239 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3240};
3241
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003242class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003243 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003244 MoveOperands(Location source, Location destination, HInstruction* instruction)
3245 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003246
3247 Location GetSource() const { return source_; }
3248 Location GetDestination() const { return destination_; }
3249
3250 void SetSource(Location value) { source_ = value; }
3251 void SetDestination(Location value) { destination_ = value; }
3252
3253 // The parallel move resolver marks moves as "in-progress" by clearing the
3254 // destination (but not the source).
3255 Location MarkPending() {
3256 DCHECK(!IsPending());
3257 Location dest = destination_;
3258 destination_ = Location::NoLocation();
3259 return dest;
3260 }
3261
3262 void ClearPending(Location dest) {
3263 DCHECK(IsPending());
3264 destination_ = dest;
3265 }
3266
3267 bool IsPending() const {
3268 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3269 return destination_.IsInvalid() && !source_.IsInvalid();
3270 }
3271
3272 // True if this blocks a move from the given location.
3273 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003274 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003275 }
3276
3277 // A move is redundant if it's been eliminated, if its source and
3278 // destination are the same, or if its destination is unneeded.
3279 bool IsRedundant() const {
3280 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3281 }
3282
3283 // We clear both operands to indicate move that's been eliminated.
3284 void Eliminate() {
3285 source_ = destination_ = Location::NoLocation();
3286 }
3287
3288 bool IsEliminated() const {
3289 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3290 return source_.IsInvalid();
3291 }
3292
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003293 HInstruction* GetInstruction() const { return instruction_; }
3294
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003295 private:
3296 Location source_;
3297 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003298 // The instruction this move is assocatied with. Null when this move is
3299 // for moving an input in the expected locations of user (including a phi user).
3300 // This is only used in debug mode, to ensure we do not connect interval siblings
3301 // in the same parallel move.
3302 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003303};
3304
3305static constexpr size_t kDefaultNumberOfMoves = 4;
3306
3307class HParallelMove : public HTemplateInstruction<0> {
3308 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003309 explicit HParallelMove(ArenaAllocator* arena)
3310 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003311
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003312 void AddMove(Location source, Location destination, HInstruction* instruction) {
3313 DCHECK(source.IsValid());
3314 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003315 if (kIsDebugBuild) {
3316 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003317 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003318 if (moves_.Get(i).GetInstruction() == instruction) {
3319 // Special case the situation where the move is for the spill slot
3320 // of the instruction.
3321 if ((GetPrevious() == instruction)
3322 || ((GetPrevious() == nullptr)
3323 && instruction->IsPhi()
3324 && instruction->GetBlock() == GetBlock())) {
3325 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3326 << "Doing parallel moves for the same instruction.";
3327 } else {
3328 DCHECK(false) << "Doing parallel moves for the same instruction.";
3329 }
3330 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003331 }
3332 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003333 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3334 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3335 << "Same destination for two moves in a parallel move.";
3336 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003337 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003338 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003339 }
3340
3341 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003342 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003343 }
3344
3345 size_t NumMoves() const { return moves_.Size(); }
3346
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003347 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003348
3349 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003350 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003351
3352 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3353};
3354
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003355class HGraphVisitor : public ValueObject {
3356 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003357 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3358 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003359
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003360 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003361 virtual void VisitBasicBlock(HBasicBlock* block);
3362
Roland Levillain633021e2014-10-01 14:12:25 +01003363 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003364 void VisitInsertionOrder();
3365
Roland Levillain633021e2014-10-01 14:12:25 +01003366 // Visit the graph following dominator tree reverse post-order.
3367 void VisitReversePostOrder();
3368
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003369 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003370
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003371 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003372#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003373 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3374
3375 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3376
3377#undef DECLARE_VISIT_INSTRUCTION
3378
3379 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003380 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003381
3382 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3383};
3384
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003385class HGraphDelegateVisitor : public HGraphVisitor {
3386 public:
3387 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3388 virtual ~HGraphDelegateVisitor() {}
3389
3390 // Visit functions that delegate to to super class.
3391#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003392 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003393
3394 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3395
3396#undef DECLARE_VISIT_INSTRUCTION
3397
3398 private:
3399 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3400};
3401
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003402class HInsertionOrderIterator : public ValueObject {
3403 public:
3404 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3405
3406 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3407 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3408 void Advance() { ++index_; }
3409
3410 private:
3411 const HGraph& graph_;
3412 size_t index_;
3413
3414 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3415};
3416
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003417class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003418 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003419 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003420
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003421 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3422 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003423 void Advance() { ++index_; }
3424
3425 private:
3426 const HGraph& graph_;
3427 size_t index_;
3428
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003429 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003430};
3431
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003432class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003433 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003434 explicit HPostOrderIterator(const HGraph& graph)
3435 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003436
3437 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003438 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003439 void Advance() { --index_; }
3440
3441 private:
3442 const HGraph& graph_;
3443 size_t index_;
3444
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003445 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003446};
3447
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003448// Iterator over the blocks that art part of the loop. Includes blocks part
3449// of an inner loop. The order in which the blocks are iterated is on their
3450// block id.
3451class HBlocksInLoopIterator : public ValueObject {
3452 public:
3453 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3454 : blocks_in_loop_(info.GetBlocks()),
3455 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3456 index_(0) {
3457 if (!blocks_in_loop_.IsBitSet(index_)) {
3458 Advance();
3459 }
3460 }
3461
3462 bool Done() const { return index_ == blocks_.Size(); }
3463 HBasicBlock* Current() const { return blocks_.Get(index_); }
3464 void Advance() {
3465 ++index_;
3466 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3467 if (blocks_in_loop_.IsBitSet(index_)) {
3468 break;
3469 }
3470 }
3471 }
3472
3473 private:
3474 const BitVector& blocks_in_loop_;
3475 const GrowableArray<HBasicBlock*>& blocks_;
3476 size_t index_;
3477
3478 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3479};
3480
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003481} // namespace art
3482
3483#endif // ART_COMPILER_OPTIMIZING_NODES_H_