blob: df847aaab2ba4bd78b3731037730314d07514c3e [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 Geoffray915b9d02015-03-11 15:11:19 +00001112 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001113
Roland Levillain6c82d402014-10-13 16:10:27 +01001114 // Does this instruction strictly dominate `other_instruction`?
1115 // Returns false if this instruction and `other_instruction` are the same.
1116 // Aborts if this instruction and `other_instruction` are both phis.
1117 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001118
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001119 int GetId() const { return id_; }
1120 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001121
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001122 int GetSsaIndex() const { return ssa_index_; }
1123 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1124 bool HasSsaIndex() const { return ssa_index_ != -1; }
1125
1126 bool HasEnvironment() const { return environment_ != nullptr; }
1127 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001128 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
1129
Nicolas Geoffray39468442014-09-02 15:17:15 +01001130 // Returns the number of entries in the environment. Typically, that is the
1131 // number of dex registers in a method. It could be more in case of inlining.
1132 size_t EnvironmentSize() const;
1133
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001134 LocationSummary* GetLocations() const { return locations_; }
1135 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001136
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001137 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001138 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001139
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001140 // Move `this` instruction before `cursor`.
1141 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001142
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001143#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001144 bool Is##type() const { return (As##type() != nullptr); } \
1145 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001146 virtual H##type* As##type() { return nullptr; }
1147
1148 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1149#undef INSTRUCTION_TYPE_CHECK
1150
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001151 // Returns whether the instruction can be moved within the graph.
1152 virtual bool CanBeMoved() const { return false; }
1153
1154 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001155 virtual bool InstructionTypeEquals(HInstruction* other) const {
1156 UNUSED(other);
1157 return false;
1158 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001159
1160 // Returns whether any data encoded in the two instructions is equal.
1161 // This method does not look at the inputs. Both instructions must be
1162 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001163 virtual bool InstructionDataEquals(HInstruction* other) const {
1164 UNUSED(other);
1165 return false;
1166 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001167
1168 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001169 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001170 // 2) Their inputs are identical.
1171 bool Equals(HInstruction* other) const;
1172
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001173 virtual InstructionKind GetKind() const = 0;
1174
1175 virtual size_t ComputeHashCode() const {
1176 size_t result = GetKind();
1177 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1178 result = (result * 31) + InputAt(i)->GetId();
1179 }
1180 return result;
1181 }
1182
1183 SideEffects GetSideEffects() const { return side_effects_; }
1184
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001185 size_t GetLifetimePosition() const { return lifetime_position_; }
1186 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1187 LiveInterval* GetLiveInterval() const { return live_interval_; }
1188 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1189 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1190
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001191 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1192
1193 // Returns whether the code generation of the instruction will require to have access
1194 // to the current method. Such instructions are:
1195 // (1): Instructions that require an environment, as calling the runtime requires
1196 // to walk the stack and have the current method stored at a specific stack address.
1197 // (2): Object literals like classes and strings, that are loaded from the dex cache
1198 // fields of the current method.
1199 bool NeedsCurrentMethod() const {
1200 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1201 }
1202
Nicolas Geoffray7e4c3502015-03-18 11:00:52 +00001203 virtual bool NeedsDexCache() const { return false; }
1204
David Brazdil1abb4192015-02-17 18:33:36 +00001205 protected:
1206 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1207 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1208
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001209 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001210 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1211
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001212 HInstruction* previous_;
1213 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001214 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001215
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001216 // An instruction gets an id when it is added to the graph.
1217 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001218 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001219 int id_;
1220
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001221 // When doing liveness analysis, instructions that have uses get an SSA index.
1222 int ssa_index_;
1223
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001224 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001225 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001226
1227 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001228 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001229
Nicolas Geoffray39468442014-09-02 15:17:15 +01001230 // The environment associated with this instruction. Not null if the instruction
1231 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001232 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001233
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001234 // Set by the code generator.
1235 LocationSummary* locations_;
1236
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001237 // Set by the liveness analysis.
1238 LiveInterval* live_interval_;
1239
1240 // Set by the liveness analysis, this is the position in a linear
1241 // order of blocks where this instruction's live interval start.
1242 size_t lifetime_position_;
1243
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001244 const SideEffects side_effects_;
1245
Calin Juravleacf735c2015-02-12 15:25:22 +00001246 // TODO: for primitive types this should be marked as invalid.
1247 ReferenceTypeInfo reference_type_info_;
1248
David Brazdil1abb4192015-02-17 18:33:36 +00001249 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001250 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001251 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001252 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001253 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001254
1255 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1256};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001257std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001258
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001259class HInputIterator : public ValueObject {
1260 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001261 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001262
1263 bool Done() const { return index_ == instruction_->InputCount(); }
1264 HInstruction* Current() const { return instruction_->InputAt(index_); }
1265 void Advance() { index_++; }
1266
1267 private:
1268 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001269 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001270
1271 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1272};
1273
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001274class HInstructionIterator : public ValueObject {
1275 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001276 explicit HInstructionIterator(const HInstructionList& instructions)
1277 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001278 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001279 }
1280
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001281 bool Done() const { return instruction_ == nullptr; }
1282 HInstruction* Current() const { return instruction_; }
1283 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001284 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001285 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001286 }
1287
1288 private:
1289 HInstruction* instruction_;
1290 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001291
1292 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001293};
1294
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001295class HBackwardInstructionIterator : public ValueObject {
1296 public:
1297 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1298 : instruction_(instructions.last_instruction_) {
1299 next_ = Done() ? nullptr : instruction_->GetPrevious();
1300 }
1301
1302 bool Done() const { return instruction_ == nullptr; }
1303 HInstruction* Current() const { return instruction_; }
1304 void Advance() {
1305 instruction_ = next_;
1306 next_ = Done() ? nullptr : instruction_->GetPrevious();
1307 }
1308
1309 private:
1310 HInstruction* instruction_;
1311 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001312
1313 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001314};
1315
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001316// An embedded container with N elements of type T. Used (with partial
1317// specialization for N=0) because embedded arrays cannot have size 0.
1318template<typename T, intptr_t N>
1319class EmbeddedArray {
1320 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001321 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001322
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001323 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001324
1325 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001326 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001327 return elements_[i];
1328 }
1329
1330 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001331 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001332 return elements_[i];
1333 }
1334
1335 const T& At(intptr_t i) const {
1336 return (*this)[i];
1337 }
1338
1339 void SetAt(intptr_t i, const T& val) {
1340 (*this)[i] = val;
1341 }
1342
1343 private:
1344 T elements_[N];
1345};
1346
1347template<typename T>
1348class EmbeddedArray<T, 0> {
1349 public:
1350 intptr_t length() const { return 0; }
1351 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001352 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001353 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001354 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001355 }
1356 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001357 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001358 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001359 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001360 }
1361};
1362
1363template<intptr_t N>
1364class HTemplateInstruction: public HInstruction {
1365 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001366 HTemplateInstruction<N>(SideEffects side_effects)
1367 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001368 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001369
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001370 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001371
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001372 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001373 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1374
1375 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1376 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001377 }
1378
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001379 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001380 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001381
1382 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001383};
1384
Dave Allison20dfc792014-06-16 20:44:29 -07001385template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001386class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001387 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001388 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1389 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001390 virtual ~HExpression() {}
1391
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001392 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001393
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001394 protected:
1395 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001396};
1397
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001398// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1399// instruction that branches to the exit block.
1400class HReturnVoid : public HTemplateInstruction<0> {
1401 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001402 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001403
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001404 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001405
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001406 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001407
1408 private:
1409 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1410};
1411
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001412// Represents dex's RETURN opcodes. A HReturn is a control flow
1413// instruction that branches to the exit block.
1414class HReturn : public HTemplateInstruction<1> {
1415 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001416 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001417 SetRawInputAt(0, value);
1418 }
1419
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001420 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001421
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001422 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001423
1424 private:
1425 DISALLOW_COPY_AND_ASSIGN(HReturn);
1426};
1427
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001428// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001429// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001430// exit block.
1431class HExit : public HTemplateInstruction<0> {
1432 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001433 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001434
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001435 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001436
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001437 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001438
1439 private:
1440 DISALLOW_COPY_AND_ASSIGN(HExit);
1441};
1442
1443// Jumps from one block to another.
1444class HGoto : public HTemplateInstruction<0> {
1445 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001446 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1447
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001448 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001449
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001450 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001451 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001452 }
1453
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001454 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001455
1456 private:
1457 DISALLOW_COPY_AND_ASSIGN(HGoto);
1458};
1459
Dave Allison20dfc792014-06-16 20:44:29 -07001460
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001461// Conditional branch. A block ending with an HIf instruction must have
1462// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001463class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001464 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001465 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001466 SetRawInputAt(0, input);
1467 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001468
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001469 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001470
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001471 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001472 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001473 }
1474
1475 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001476 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001477 }
1478
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001479 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001480
Andreas Gampe0ba62732015-03-24 02:39:46 +00001481 virtual bool IsIfInstruction() const { return true; }
1482
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001483 private:
1484 DISALLOW_COPY_AND_ASSIGN(HIf);
1485};
1486
Roland Levillain88cb1752014-10-20 16:36:47 +01001487class HUnaryOperation : public HExpression<1> {
1488 public:
1489 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1490 : HExpression(result_type, SideEffects::None()) {
1491 SetRawInputAt(0, input);
1492 }
1493
1494 HInstruction* GetInput() const { return InputAt(0); }
1495 Primitive::Type GetResultType() const { return GetType(); }
1496
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001497 bool CanBeMoved() const OVERRIDE { return true; }
1498 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001499 UNUSED(other);
1500 return true;
1501 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001502
Roland Levillain9240d6a2014-10-20 16:47:04 +01001503 // Try to statically evaluate `operation` and return a HConstant
1504 // containing the result of this evaluation. If `operation` cannot
1505 // be evaluated as a constant, return nullptr.
1506 HConstant* TryStaticEvaluation() const;
1507
1508 // Apply this operation to `x`.
1509 virtual int32_t Evaluate(int32_t x) const = 0;
1510 virtual int64_t Evaluate(int64_t x) const = 0;
1511
Roland Levillain88cb1752014-10-20 16:36:47 +01001512 DECLARE_INSTRUCTION(UnaryOperation);
1513
1514 private:
1515 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1516};
1517
Dave Allison20dfc792014-06-16 20:44:29 -07001518class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001519 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001520 HBinaryOperation(Primitive::Type result_type,
1521 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001522 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001523 SetRawInputAt(0, left);
1524 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001525 }
1526
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001527 HInstruction* GetLeft() const { return InputAt(0); }
1528 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001529 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001530
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001531 virtual bool IsCommutative() const { return false; }
1532
1533 // Put constant on the right.
1534 // Returns whether order is changed.
1535 bool OrderInputsWithConstantOnTheRight() {
1536 HInstruction* left = InputAt(0);
1537 HInstruction* right = InputAt(1);
1538 if (left->IsConstant() && !right->IsConstant()) {
1539 ReplaceInput(right, 0);
1540 ReplaceInput(left, 1);
1541 return true;
1542 }
1543 return false;
1544 }
1545
1546 // Order inputs by instruction id, but favor constant on the right side.
1547 // This helps GVN for commutative ops.
1548 void OrderInputs() {
1549 DCHECK(IsCommutative());
1550 HInstruction* left = InputAt(0);
1551 HInstruction* right = InputAt(1);
1552 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1553 return;
1554 }
1555 if (OrderInputsWithConstantOnTheRight()) {
1556 return;
1557 }
1558 // Order according to instruction id.
1559 if (left->GetId() > right->GetId()) {
1560 ReplaceInput(right, 0);
1561 ReplaceInput(left, 1);
1562 }
1563 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001564
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001565 bool CanBeMoved() const OVERRIDE { return true; }
1566 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001567 UNUSED(other);
1568 return true;
1569 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001570
Roland Levillain9240d6a2014-10-20 16:47:04 +01001571 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001572 // containing the result of this evaluation. If `operation` cannot
1573 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001574 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001575
1576 // Apply this operation to `x` and `y`.
1577 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1578 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1579
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001580 // Returns an input that can legally be used as the right input and is
1581 // constant, or nullptr.
1582 HConstant* GetConstantRight() const;
1583
1584 // If `GetConstantRight()` returns one of the input, this returns the other
1585 // one. Otherwise it returns nullptr.
1586 HInstruction* GetLeastConstantLeft() const;
1587
Roland Levillainccc07a92014-09-16 14:48:16 +01001588 DECLARE_INSTRUCTION(BinaryOperation);
1589
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001590 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001591 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1592};
1593
Dave Allison20dfc792014-06-16 20:44:29 -07001594class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001595 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001596 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001597 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1598 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001599
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001600 bool NeedsMaterialization() const { return needs_materialization_; }
1601 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001602
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001603 // For code generation purposes, returns whether this instruction is just before
Andreas Gampe0ba62732015-03-24 02:39:46 +00001604 // `if_`, and disregard moves in between.
1605 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001606
Dave Allison20dfc792014-06-16 20:44:29 -07001607 DECLARE_INSTRUCTION(Condition);
1608
1609 virtual IfCondition GetCondition() const = 0;
1610
1611 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001612 // For register allocation purposes, returns whether this instruction needs to be
1613 // materialized (that is, not just be in the processor flags).
1614 bool needs_materialization_;
1615
Dave Allison20dfc792014-06-16 20:44:29 -07001616 DISALLOW_COPY_AND_ASSIGN(HCondition);
1617};
1618
1619// Instruction to check if two inputs are equal to each other.
1620class HEqual : public HCondition {
1621 public:
1622 HEqual(HInstruction* first, HInstruction* second)
1623 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001624
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001625 bool IsCommutative() const OVERRIDE { return true; }
1626
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001627 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001628 return x == y ? 1 : 0;
1629 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001630 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001631 return x == y ? 1 : 0;
1632 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001633
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001634 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001635
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001636 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001637 return kCondEQ;
1638 }
1639
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001640 private:
1641 DISALLOW_COPY_AND_ASSIGN(HEqual);
1642};
1643
Dave Allison20dfc792014-06-16 20:44:29 -07001644class HNotEqual : public HCondition {
1645 public:
1646 HNotEqual(HInstruction* first, HInstruction* second)
1647 : HCondition(first, second) {}
1648
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001649 bool IsCommutative() const OVERRIDE { return true; }
1650
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001651 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001652 return x != y ? 1 : 0;
1653 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001654 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001655 return x != y ? 1 : 0;
1656 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001657
Dave Allison20dfc792014-06-16 20:44:29 -07001658 DECLARE_INSTRUCTION(NotEqual);
1659
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001660 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001661 return kCondNE;
1662 }
1663
1664 private:
1665 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1666};
1667
1668class HLessThan : public HCondition {
1669 public:
1670 HLessThan(HInstruction* first, HInstruction* second)
1671 : HCondition(first, second) {}
1672
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001673 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001674 return x < y ? 1 : 0;
1675 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001676 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001677 return x < y ? 1 : 0;
1678 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001679
Dave Allison20dfc792014-06-16 20:44:29 -07001680 DECLARE_INSTRUCTION(LessThan);
1681
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001682 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001683 return kCondLT;
1684 }
1685
1686 private:
1687 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1688};
1689
1690class HLessThanOrEqual : public HCondition {
1691 public:
1692 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1693 : HCondition(first, second) {}
1694
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001695 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001696 return x <= y ? 1 : 0;
1697 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001698 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001699 return x <= y ? 1 : 0;
1700 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001701
Dave Allison20dfc792014-06-16 20:44:29 -07001702 DECLARE_INSTRUCTION(LessThanOrEqual);
1703
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001704 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001705 return kCondLE;
1706 }
1707
1708 private:
1709 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1710};
1711
1712class HGreaterThan : public HCondition {
1713 public:
1714 HGreaterThan(HInstruction* first, HInstruction* second)
1715 : HCondition(first, second) {}
1716
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001717 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001718 return x > y ? 1 : 0;
1719 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001720 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001721 return x > y ? 1 : 0;
1722 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001723
Dave Allison20dfc792014-06-16 20:44:29 -07001724 DECLARE_INSTRUCTION(GreaterThan);
1725
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001726 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001727 return kCondGT;
1728 }
1729
1730 private:
1731 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1732};
1733
1734class HGreaterThanOrEqual : public HCondition {
1735 public:
1736 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1737 : HCondition(first, second) {}
1738
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001739 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001740 return x >= y ? 1 : 0;
1741 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001742 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001743 return x >= y ? 1 : 0;
1744 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001745
Dave Allison20dfc792014-06-16 20:44:29 -07001746 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1747
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001748 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001749 return kCondGE;
1750 }
1751
1752 private:
1753 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1754};
1755
1756
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001757// Instruction to check how two inputs compare to each other.
1758// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1759class HCompare : public HBinaryOperation {
1760 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001761 // The bias applies for floating point operations and indicates how NaN
1762 // comparisons are treated:
1763 enum Bias {
1764 kNoBias, // bias is not applicable (i.e. for long operation)
1765 kGtBias, // return 1 for NaN comparisons
1766 kLtBias, // return -1 for NaN comparisons
1767 };
1768
1769 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1770 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001771 DCHECK_EQ(type, first->GetType());
1772 DCHECK_EQ(type, second->GetType());
1773 }
1774
Calin Juravleddb7df22014-11-25 20:56:51 +00001775 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001776 return
1777 x == y ? 0 :
1778 x > y ? 1 :
1779 -1;
1780 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001781
1782 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001783 return
1784 x == y ? 0 :
1785 x > y ? 1 :
1786 -1;
1787 }
1788
Calin Juravleddb7df22014-11-25 20:56:51 +00001789 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1790 return bias_ == other->AsCompare()->bias_;
1791 }
1792
1793 bool IsGtBias() { return bias_ == kGtBias; }
1794
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001795 DECLARE_INSTRUCTION(Compare);
1796
1797 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001798 const Bias bias_;
1799
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001800 DISALLOW_COPY_AND_ASSIGN(HCompare);
1801};
1802
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001803// A local in the graph. Corresponds to a Dex register.
1804class HLocal : public HTemplateInstruction<0> {
1805 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001806 explicit HLocal(uint16_t reg_number)
1807 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001808
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001809 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001810
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001811 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001812
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001813 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001814 // The Dex register number.
1815 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001816
1817 DISALLOW_COPY_AND_ASSIGN(HLocal);
1818};
1819
1820// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001821class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001822 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001823 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001824 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001825 SetRawInputAt(0, local);
1826 }
1827
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001828 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1829
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001830 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001831
1832 private:
1833 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1834};
1835
1836// Store a value in a given local. This instruction has two inputs: the value
1837// and the local.
1838class HStoreLocal : public HTemplateInstruction<2> {
1839 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001840 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001841 SetRawInputAt(0, local);
1842 SetRawInputAt(1, value);
1843 }
1844
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001845 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1846
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001847 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001848
1849 private:
1850 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1851};
1852
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001853class HConstant : public HExpression<0> {
1854 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001855 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1856
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001857 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001858
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001859 virtual bool IsMinusOne() const { return false; }
1860 virtual bool IsZero() const { return false; }
1861 virtual bool IsOne() const { return false; }
1862
1863 static HConstant* NewConstant(ArenaAllocator* allocator, Primitive::Type type, int64_t val);
1864
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001865 DECLARE_INSTRUCTION(Constant);
1866
1867 private:
1868 DISALLOW_COPY_AND_ASSIGN(HConstant);
1869};
1870
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001871class HFloatConstant : public HConstant {
1872 public:
1873 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1874
1875 float GetValue() const { return value_; }
1876
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001877 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001878 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
1879 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001880 }
1881
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001882 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001883
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001884 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001885 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
1886 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001887 }
1888 bool IsZero() const OVERRIDE {
1889 return AsFloatConstant()->GetValue() == 0.0f;
1890 }
1891 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001892 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
1893 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001894 }
1895
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001896 DECLARE_INSTRUCTION(FloatConstant);
1897
1898 private:
1899 const float value_;
1900
1901 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1902};
1903
1904class HDoubleConstant : public HConstant {
1905 public:
1906 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1907
1908 double GetValue() const { return value_; }
1909
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001910 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001911 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
1912 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001913 }
1914
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001915 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001916
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001917 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001918 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
1919 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001920 }
1921 bool IsZero() const OVERRIDE {
1922 return AsDoubleConstant()->GetValue() == 0.0;
1923 }
1924 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001925 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
1926 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001927 }
1928
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001929 DECLARE_INSTRUCTION(DoubleConstant);
1930
1931 private:
1932 const double value_;
1933
1934 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1935};
1936
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001937class HNullConstant : public HConstant {
1938 public:
1939 HNullConstant() : HConstant(Primitive::kPrimNot) {}
1940
1941 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
1942 return true;
1943 }
1944
1945 size_t ComputeHashCode() const OVERRIDE { return 0; }
1946
Calin Juravle61d544b2015-02-23 16:46:57 +00001947 bool ActAsNullConstant() const OVERRIDE { return true; }
1948
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001949 DECLARE_INSTRUCTION(NullConstant);
1950
1951 private:
1952 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
1953};
1954
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001955// Constants of the type int. Those can be from Dex instructions, or
1956// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001957class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001958 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001959 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001960
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001961 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001962
Calin Juravle61d544b2015-02-23 16:46:57 +00001963 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001964 return other->AsIntConstant()->value_ == value_;
1965 }
1966
Calin Juravle61d544b2015-02-23 16:46:57 +00001967 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
1968
1969 // TODO: Null is represented by the `0` constant. In most cases we replace it
1970 // with a HNullConstant but we don't do it when comparing (a != null). This
1971 // method is an workaround until we fix the above.
1972 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001973
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001974 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
1975 bool IsZero() const OVERRIDE { return GetValue() == 0; }
1976 bool IsOne() const OVERRIDE { return GetValue() == 1; }
1977
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001978 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001979
1980 private:
1981 const int32_t value_;
1982
1983 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1984};
1985
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001986class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001987 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001988 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001989
1990 int64_t GetValue() const { return value_; }
1991
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001992 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001993 return other->AsLongConstant()->value_ == value_;
1994 }
1995
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001996 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001997
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001998 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
1999 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2000 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2001
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002002 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002003
2004 private:
2005 const int64_t value_;
2006
2007 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2008};
2009
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002010enum class Intrinsics {
2011#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2012#include "intrinsics_list.h"
2013 kNone,
2014 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2015#undef INTRINSICS_LIST
2016#undef OPTIMIZING_INTRINSICS
2017};
2018std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2019
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002020class HInvoke : public HInstruction {
2021 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002022 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002023
2024 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2025 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002026 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002027
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002028 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002029 SetRawInputAt(index, argument);
2030 }
2031
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002032 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002033
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002034 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002035
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002036 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2037
2038 Intrinsics GetIntrinsic() {
2039 return intrinsic_;
2040 }
2041
2042 void SetIntrinsic(Intrinsics intrinsic) {
2043 intrinsic_ = intrinsic;
2044 }
2045
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002046 DECLARE_INSTRUCTION(Invoke);
2047
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002048 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002049 HInvoke(ArenaAllocator* arena,
2050 uint32_t number_of_arguments,
2051 Primitive::Type return_type,
2052 uint32_t dex_pc,
2053 uint32_t dex_method_index)
2054 : HInstruction(SideEffects::All()),
2055 inputs_(arena, number_of_arguments),
2056 return_type_(return_type),
2057 dex_pc_(dex_pc),
2058 dex_method_index_(dex_method_index),
2059 intrinsic_(Intrinsics::kNone) {
2060 inputs_.SetSize(number_of_arguments);
2061 }
2062
David Brazdil1abb4192015-02-17 18:33:36 +00002063 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2064 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2065 inputs_.Put(index, input);
2066 }
2067
2068 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002069 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002070 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002071 const uint32_t dex_method_index_;
2072 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002073
2074 private:
2075 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2076};
2077
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002078class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002079 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002080 HInvokeStaticOrDirect(ArenaAllocator* arena,
2081 uint32_t number_of_arguments,
2082 Primitive::Type return_type,
2083 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002084 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002085 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002086 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002087 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002088 invoke_type_(invoke_type),
2089 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002090
Calin Juravle77520bc2015-01-12 18:45:46 +00002091 bool CanDoImplicitNullCheck() const OVERRIDE {
2092 // We access the method via the dex cache so we can't do an implicit null check.
2093 // TODO: for intrinsics we can generate implicit null checks.
2094 return false;
2095 }
2096
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002097 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002098 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray7e4c3502015-03-18 11:00:52 +00002099 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002100
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002101 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002102
2103 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002104 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002105 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002106
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002107 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002108};
2109
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002110class HInvokeVirtual : public HInvoke {
2111 public:
2112 HInvokeVirtual(ArenaAllocator* arena,
2113 uint32_t number_of_arguments,
2114 Primitive::Type return_type,
2115 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002116 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002117 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002118 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002119 vtable_index_(vtable_index) {}
2120
Calin Juravle77520bc2015-01-12 18:45:46 +00002121 bool CanDoImplicitNullCheck() const OVERRIDE {
2122 // TODO: Add implicit null checks in intrinsics.
2123 return !GetLocations()->Intrinsified();
2124 }
2125
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002126 uint32_t GetVTableIndex() const { return vtable_index_; }
2127
2128 DECLARE_INSTRUCTION(InvokeVirtual);
2129
2130 private:
2131 const uint32_t vtable_index_;
2132
2133 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2134};
2135
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002136class HInvokeInterface : public HInvoke {
2137 public:
2138 HInvokeInterface(ArenaAllocator* arena,
2139 uint32_t number_of_arguments,
2140 Primitive::Type return_type,
2141 uint32_t dex_pc,
2142 uint32_t dex_method_index,
2143 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002144 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002145 imt_index_(imt_index) {}
2146
Calin Juravle77520bc2015-01-12 18:45:46 +00002147 bool CanDoImplicitNullCheck() const OVERRIDE {
2148 // TODO: Add implicit null checks in intrinsics.
2149 return !GetLocations()->Intrinsified();
2150 }
2151
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002152 uint32_t GetImtIndex() const { return imt_index_; }
2153 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2154
2155 DECLARE_INSTRUCTION(InvokeInterface);
2156
2157 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002158 const uint32_t imt_index_;
2159
2160 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2161};
2162
Dave Allison20dfc792014-06-16 20:44:29 -07002163class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002164 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002165 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +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 Geoffray2e7038a2014-04-03 18:49:58 +01002170
2171 uint32_t GetDexPc() const { return dex_pc_; }
2172 uint16_t GetTypeIndex() const { return type_index_; }
2173
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002174 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002175 bool NeedsEnvironment() const OVERRIDE { return true; }
2176 // It may throw when called on:
2177 // - interfaces
2178 // - abstract/innaccessible/unknown classes
2179 // TODO: optimize when possible.
2180 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002181
Calin Juravle10e244f2015-01-26 18:54:32 +00002182 bool CanBeNull() const OVERRIDE { return false; }
2183
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002184 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2185
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002186 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002187
2188 private:
2189 const uint32_t dex_pc_;
2190 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002191 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002192
2193 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2194};
2195
Roland Levillain88cb1752014-10-20 16:36:47 +01002196class HNeg : public HUnaryOperation {
2197 public:
2198 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2199 : HUnaryOperation(result_type, input) {}
2200
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002201 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2202 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002203
Roland Levillain88cb1752014-10-20 16:36:47 +01002204 DECLARE_INSTRUCTION(Neg);
2205
2206 private:
2207 DISALLOW_COPY_AND_ASSIGN(HNeg);
2208};
2209
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002210class HNewArray : public HExpression<1> {
2211 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002212 HNewArray(HInstruction* length,
2213 uint32_t dex_pc,
2214 uint16_t type_index,
2215 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002216 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2217 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002218 type_index_(type_index),
2219 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002220 SetRawInputAt(0, length);
2221 }
2222
2223 uint32_t GetDexPc() const { return dex_pc_; }
2224 uint16_t GetTypeIndex() const { return type_index_; }
2225
2226 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002227 bool NeedsEnvironment() const OVERRIDE { return true; }
2228
2229 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002230
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002231 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2232
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002233 DECLARE_INSTRUCTION(NewArray);
2234
2235 private:
2236 const uint32_t dex_pc_;
2237 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002238 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002239
2240 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2241};
2242
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002243class HAdd : public HBinaryOperation {
2244 public:
2245 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2246 : HBinaryOperation(result_type, left, right) {}
2247
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002248 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002249
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002250 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002251 return x + y;
2252 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002253 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002254 return x + y;
2255 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002256
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002257 DECLARE_INSTRUCTION(Add);
2258
2259 private:
2260 DISALLOW_COPY_AND_ASSIGN(HAdd);
2261};
2262
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002263class HSub : public HBinaryOperation {
2264 public:
2265 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2266 : HBinaryOperation(result_type, left, right) {}
2267
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002268 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002269 return x - y;
2270 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002271 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002272 return x - y;
2273 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002274
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002275 DECLARE_INSTRUCTION(Sub);
2276
2277 private:
2278 DISALLOW_COPY_AND_ASSIGN(HSub);
2279};
2280
Calin Juravle34bacdf2014-10-07 20:23:36 +01002281class HMul : public HBinaryOperation {
2282 public:
2283 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2284 : HBinaryOperation(result_type, left, right) {}
2285
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002286 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002287
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002288 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2289 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002290
2291 DECLARE_INSTRUCTION(Mul);
2292
2293 private:
2294 DISALLOW_COPY_AND_ASSIGN(HMul);
2295};
2296
Calin Juravle7c4954d2014-10-28 16:57:40 +00002297class HDiv : public HBinaryOperation {
2298 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002299 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2300 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002301
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002302 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002303 // Our graph structure ensures we never have 0 for `y` during constant folding.
2304 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002305 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002306 return (y == -1) ? -x : x / y;
2307 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002308
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002309 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002310 DCHECK_NE(y, 0);
2311 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2312 return (y == -1) ? -x : x / y;
2313 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002314
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002315 uint32_t GetDexPc() const { return dex_pc_; }
2316
Calin Juravle7c4954d2014-10-28 16:57:40 +00002317 DECLARE_INSTRUCTION(Div);
2318
2319 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002320 const uint32_t dex_pc_;
2321
Calin Juravle7c4954d2014-10-28 16:57:40 +00002322 DISALLOW_COPY_AND_ASSIGN(HDiv);
2323};
2324
Calin Juravlebacfec32014-11-14 15:54:36 +00002325class HRem : public HBinaryOperation {
2326 public:
2327 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2328 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2329
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002330 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002331 DCHECK_NE(y, 0);
2332 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2333 return (y == -1) ? 0 : x % y;
2334 }
2335
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002336 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002337 DCHECK_NE(y, 0);
2338 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2339 return (y == -1) ? 0 : x % y;
2340 }
2341
2342 uint32_t GetDexPc() const { return dex_pc_; }
2343
2344 DECLARE_INSTRUCTION(Rem);
2345
2346 private:
2347 const uint32_t dex_pc_;
2348
2349 DISALLOW_COPY_AND_ASSIGN(HRem);
2350};
2351
Calin Juravled0d48522014-11-04 16:40:20 +00002352class HDivZeroCheck : public HExpression<1> {
2353 public:
2354 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2355 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2356 SetRawInputAt(0, value);
2357 }
2358
2359 bool CanBeMoved() const OVERRIDE { return true; }
2360
2361 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2362 UNUSED(other);
2363 return true;
2364 }
2365
2366 bool NeedsEnvironment() const OVERRIDE { return true; }
2367 bool CanThrow() const OVERRIDE { return true; }
2368
2369 uint32_t GetDexPc() const { return dex_pc_; }
2370
2371 DECLARE_INSTRUCTION(DivZeroCheck);
2372
2373 private:
2374 const uint32_t dex_pc_;
2375
2376 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2377};
2378
Calin Juravle9aec02f2014-11-18 23:06:35 +00002379class HShl : public HBinaryOperation {
2380 public:
2381 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2382 : HBinaryOperation(result_type, left, right) {}
2383
2384 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2385 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2386
2387 DECLARE_INSTRUCTION(Shl);
2388
2389 private:
2390 DISALLOW_COPY_AND_ASSIGN(HShl);
2391};
2392
2393class HShr : public HBinaryOperation {
2394 public:
2395 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2396 : HBinaryOperation(result_type, left, right) {}
2397
2398 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2399 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2400
2401 DECLARE_INSTRUCTION(Shr);
2402
2403 private:
2404 DISALLOW_COPY_AND_ASSIGN(HShr);
2405};
2406
2407class HUShr : public HBinaryOperation {
2408 public:
2409 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2410 : HBinaryOperation(result_type, left, right) {}
2411
2412 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2413 uint32_t ux = static_cast<uint32_t>(x);
2414 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2415 return static_cast<int32_t>(ux >> uy);
2416 }
2417
2418 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2419 uint64_t ux = static_cast<uint64_t>(x);
2420 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2421 return static_cast<int64_t>(ux >> uy);
2422 }
2423
2424 DECLARE_INSTRUCTION(UShr);
2425
2426 private:
2427 DISALLOW_COPY_AND_ASSIGN(HUShr);
2428};
2429
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002430class HAnd : public HBinaryOperation {
2431 public:
2432 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2433 : HBinaryOperation(result_type, left, right) {}
2434
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002435 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002436
2437 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2438 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2439
2440 DECLARE_INSTRUCTION(And);
2441
2442 private:
2443 DISALLOW_COPY_AND_ASSIGN(HAnd);
2444};
2445
2446class HOr : public HBinaryOperation {
2447 public:
2448 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2449 : HBinaryOperation(result_type, left, right) {}
2450
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002451 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002452
2453 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2454 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2455
2456 DECLARE_INSTRUCTION(Or);
2457
2458 private:
2459 DISALLOW_COPY_AND_ASSIGN(HOr);
2460};
2461
2462class HXor : public HBinaryOperation {
2463 public:
2464 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2465 : HBinaryOperation(result_type, left, right) {}
2466
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002467 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002468
2469 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2470 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2471
2472 DECLARE_INSTRUCTION(Xor);
2473
2474 private:
2475 DISALLOW_COPY_AND_ASSIGN(HXor);
2476};
2477
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002478// The value of a parameter in this method. Its location depends on
2479// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002480class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002481 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002482 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2483 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002484
2485 uint8_t GetIndex() const { return index_; }
2486
Calin Juravle10e244f2015-01-26 18:54:32 +00002487 bool CanBeNull() const OVERRIDE { return !is_this_; }
2488
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002489 DECLARE_INSTRUCTION(ParameterValue);
2490
2491 private:
2492 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002493 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002494 const uint8_t index_;
2495
Calin Juravle10e244f2015-01-26 18:54:32 +00002496 // Whether or not the parameter value corresponds to 'this' argument.
2497 const bool is_this_;
2498
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002499 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2500};
2501
Roland Levillain1cc5f252014-10-22 18:06:21 +01002502class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002503 public:
Roland Levillain1cc5f252014-10-22 18:06:21 +01002504 explicit HNot(Primitive::Type result_type, HInstruction* input)
2505 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002506
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002507 bool CanBeMoved() const OVERRIDE { return true; }
2508 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002509 UNUSED(other);
2510 return true;
2511 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002512
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002513 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2514 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f252014-10-22 18:06:21 +01002515
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002516 DECLARE_INSTRUCTION(Not);
2517
2518 private:
2519 DISALLOW_COPY_AND_ASSIGN(HNot);
2520};
2521
Roland Levillaindff1f282014-11-05 14:15:05 +00002522class HTypeConversion : public HExpression<1> {
2523 public:
2524 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002525 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2526 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002527 SetRawInputAt(0, input);
2528 DCHECK_NE(input->GetType(), result_type);
2529 }
2530
2531 HInstruction* GetInput() const { return InputAt(0); }
2532 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2533 Primitive::Type GetResultType() const { return GetType(); }
2534
Roland Levillain624279f2014-12-04 11:54:28 +00002535 // Required by the x86 and ARM code generators when producing calls
2536 // to the runtime.
2537 uint32_t GetDexPc() const { return dex_pc_; }
2538
Roland Levillaindff1f282014-11-05 14:15:05 +00002539 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002540 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002541
2542 DECLARE_INSTRUCTION(TypeConversion);
2543
2544 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002545 const uint32_t dex_pc_;
2546
Roland Levillaindff1f282014-11-05 14:15:05 +00002547 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2548};
2549
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002550static constexpr uint32_t kNoRegNumber = -1;
2551
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002552class HPhi : public HInstruction {
2553 public:
2554 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002555 : HInstruction(SideEffects::None()),
2556 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002557 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002558 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002559 is_live_(false),
2560 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002561 inputs_.SetSize(number_of_inputs);
2562 }
2563
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002564 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2565 static Primitive::Type ToPhiType(Primitive::Type type) {
2566 switch (type) {
2567 case Primitive::kPrimBoolean:
2568 case Primitive::kPrimByte:
2569 case Primitive::kPrimShort:
2570 case Primitive::kPrimChar:
2571 return Primitive::kPrimInt;
2572 default:
2573 return type;
2574 }
2575 }
2576
Calin Juravle10e244f2015-01-26 18:54:32 +00002577 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002578
2579 void AddInput(HInstruction* input);
2580
Calin Juravle10e244f2015-01-26 18:54:32 +00002581 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002582 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002583
Calin Juravle10e244f2015-01-26 18:54:32 +00002584 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2585 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2586
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002587 uint32_t GetRegNumber() const { return reg_number_; }
2588
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002589 void SetDead() { is_live_ = false; }
2590 void SetLive() { is_live_ = true; }
2591 bool IsDead() const { return !is_live_; }
2592 bool IsLive() const { return is_live_; }
2593
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002594 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002595
David Brazdil1abb4192015-02-17 18:33:36 +00002596 protected:
2597 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2598
2599 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2600 inputs_.Put(index, input);
2601 }
2602
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002603 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002604 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002605 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002606 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002607 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002608 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002609
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002610 DISALLOW_COPY_AND_ASSIGN(HPhi);
2611};
2612
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002613class HNullCheck : public HExpression<1> {
2614 public:
2615 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002616 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002617 SetRawInputAt(0, value);
2618 }
2619
Calin Juravle10e244f2015-01-26 18:54:32 +00002620 bool CanBeMoved() const OVERRIDE { return true; }
2621 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002622 UNUSED(other);
2623 return true;
2624 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002625
Calin Juravle10e244f2015-01-26 18:54:32 +00002626 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002627
Calin Juravle10e244f2015-01-26 18:54:32 +00002628 bool CanThrow() const OVERRIDE { return true; }
2629
2630 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002631
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002632 uint32_t GetDexPc() const { return dex_pc_; }
2633
2634 DECLARE_INSTRUCTION(NullCheck);
2635
2636 private:
2637 const uint32_t dex_pc_;
2638
2639 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2640};
2641
2642class FieldInfo : public ValueObject {
2643 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002644 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2645 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002646
2647 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002648 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002649 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002650
2651 private:
2652 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002653 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002654 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002655};
2656
2657class HInstanceFieldGet : public HExpression<1> {
2658 public:
2659 HInstanceFieldGet(HInstruction* value,
2660 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002661 MemberOffset field_offset,
2662 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002663 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002664 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002665 SetRawInputAt(0, value);
2666 }
2667
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002668 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002669
2670 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2671 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2672 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002673 }
2674
Calin Juravle77520bc2015-01-12 18:45:46 +00002675 bool CanDoImplicitNullCheck() const OVERRIDE {
2676 return GetFieldOffset().Uint32Value() < kPageSize;
2677 }
2678
2679 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002680 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2681 }
2682
Calin Juravle52c48962014-12-16 17:02:57 +00002683 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002684 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002685 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002686 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002687
2688 DECLARE_INSTRUCTION(InstanceFieldGet);
2689
2690 private:
2691 const FieldInfo field_info_;
2692
2693 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2694};
2695
2696class HInstanceFieldSet : public HTemplateInstruction<2> {
2697 public:
2698 HInstanceFieldSet(HInstruction* object,
2699 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002700 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002701 MemberOffset field_offset,
2702 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002703 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002704 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002705 SetRawInputAt(0, object);
2706 SetRawInputAt(1, value);
2707 }
2708
Calin Juravle77520bc2015-01-12 18:45:46 +00002709 bool CanDoImplicitNullCheck() const OVERRIDE {
2710 return GetFieldOffset().Uint32Value() < kPageSize;
2711 }
2712
Calin Juravle52c48962014-12-16 17:02:57 +00002713 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002714 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002715 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002716 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002717 HInstruction* GetValue() const { return InputAt(1); }
2718
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002719 DECLARE_INSTRUCTION(InstanceFieldSet);
2720
2721 private:
2722 const FieldInfo field_info_;
2723
2724 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2725};
2726
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002727class HArrayGet : public HExpression<2> {
2728 public:
2729 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002730 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002731 SetRawInputAt(0, array);
2732 SetRawInputAt(1, index);
2733 }
2734
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002735 bool CanBeMoved() const OVERRIDE { return true; }
2736 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002737 UNUSED(other);
2738 return true;
2739 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002740 bool CanDoImplicitNullCheck() const OVERRIDE {
2741 // TODO: We can be smarter here.
2742 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2743 // which generates the implicit null check. There are cases when these can be removed
2744 // to produce better code. If we ever add optimizations to do so we should allow an
2745 // implicit check here (as long as the address falls in the first page).
2746 return false;
2747 }
2748
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002749 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002750
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002751 HInstruction* GetArray() const { return InputAt(0); }
2752 HInstruction* GetIndex() const { return InputAt(1); }
2753
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002754 DECLARE_INSTRUCTION(ArrayGet);
2755
2756 private:
2757 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2758};
2759
2760class HArraySet : public HTemplateInstruction<3> {
2761 public:
2762 HArraySet(HInstruction* array,
2763 HInstruction* index,
2764 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002765 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002766 uint32_t dex_pc)
2767 : HTemplateInstruction(SideEffects::ChangesSomething()),
2768 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002769 expected_component_type_(expected_component_type),
2770 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002771 SetRawInputAt(0, array);
2772 SetRawInputAt(1, index);
2773 SetRawInputAt(2, value);
2774 }
2775
Calin Juravle77520bc2015-01-12 18:45:46 +00002776 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002777 // We currently always call a runtime method to catch array store
2778 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002779 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002780 }
2781
Calin Juravle77520bc2015-01-12 18:45:46 +00002782 bool CanDoImplicitNullCheck() const OVERRIDE {
2783 // TODO: Same as for ArrayGet.
2784 return false;
2785 }
2786
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002787 void ClearNeedsTypeCheck() {
2788 needs_type_check_ = false;
2789 }
2790
2791 bool NeedsTypeCheck() const { return needs_type_check_; }
2792
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002793 uint32_t GetDexPc() const { return dex_pc_; }
2794
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002795 HInstruction* GetArray() const { return InputAt(0); }
2796 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002797 HInstruction* GetValue() const { return InputAt(2); }
2798
2799 Primitive::Type GetComponentType() const {
2800 // The Dex format does not type floating point index operations. Since the
2801 // `expected_component_type_` is set during building and can therefore not
2802 // be correct, we also check what is the value type. If it is a floating
2803 // point type, we must use that type.
2804 Primitive::Type value_type = GetValue()->GetType();
2805 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2806 ? value_type
2807 : expected_component_type_;
2808 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002809
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002810 DECLARE_INSTRUCTION(ArraySet);
2811
2812 private:
2813 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002814 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002815 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002816
2817 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2818};
2819
2820class HArrayLength : public HExpression<1> {
2821 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002822 explicit HArrayLength(HInstruction* array)
2823 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2824 // Note that arrays do not change length, so the instruction does not
2825 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002826 SetRawInputAt(0, array);
2827 }
2828
Calin Juravle77520bc2015-01-12 18:45:46 +00002829 bool CanBeMoved() const OVERRIDE { return true; }
2830 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002831 UNUSED(other);
2832 return true;
2833 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002834 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002835
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002836 DECLARE_INSTRUCTION(ArrayLength);
2837
2838 private:
2839 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2840};
2841
2842class HBoundsCheck : public HExpression<2> {
2843 public:
2844 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002845 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002846 DCHECK(index->GetType() == Primitive::kPrimInt);
2847 SetRawInputAt(0, index);
2848 SetRawInputAt(1, length);
2849 }
2850
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002851 bool CanBeMoved() const OVERRIDE { return true; }
2852 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002853 UNUSED(other);
2854 return true;
2855 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002856
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002857 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002858
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002859 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002860
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002861 uint32_t GetDexPc() const { return dex_pc_; }
2862
2863 DECLARE_INSTRUCTION(BoundsCheck);
2864
2865 private:
2866 const uint32_t dex_pc_;
2867
2868 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2869};
2870
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002871/**
2872 * Some DEX instructions are folded into multiple HInstructions that need
2873 * to stay live until the last HInstruction. This class
2874 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002875 * HInstruction stays live. `index` represents the stack location index of the
2876 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002877 */
2878class HTemporary : public HTemplateInstruction<0> {
2879 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002880 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002881
2882 size_t GetIndex() const { return index_; }
2883
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002884 Primitive::Type GetType() const OVERRIDE {
2885 // The previous instruction is the one that will be stored in the temporary location.
2886 DCHECK(GetPrevious() != nullptr);
2887 return GetPrevious()->GetType();
2888 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002889
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002890 DECLARE_INSTRUCTION(Temporary);
2891
2892 private:
2893 const size_t index_;
2894
2895 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2896};
2897
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002898class HSuspendCheck : public HTemplateInstruction<0> {
2899 public:
2900 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002901 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002902
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002903 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002904 return true;
2905 }
2906
2907 uint32_t GetDexPc() const { return dex_pc_; }
2908
2909 DECLARE_INSTRUCTION(SuspendCheck);
2910
2911 private:
2912 const uint32_t dex_pc_;
2913
2914 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2915};
2916
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002917/**
2918 * Instruction to load a Class object.
2919 */
2920class HLoadClass : public HExpression<0> {
2921 public:
2922 HLoadClass(uint16_t type_index,
2923 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002924 uint32_t dex_pc)
2925 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2926 type_index_(type_index),
2927 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002928 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00002929 generate_clinit_check_(false),
2930 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002931
2932 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002933
2934 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2935 return other->AsLoadClass()->type_index_ == type_index_;
2936 }
2937
2938 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2939
2940 uint32_t GetDexPc() const { return dex_pc_; }
2941 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002942 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002943
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002944 bool NeedsEnvironment() const OVERRIDE {
2945 // Will call runtime and load the class if the class is not loaded yet.
2946 // TODO: finer grain decision.
2947 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002948 }
2949
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002950 bool MustGenerateClinitCheck() const {
2951 return generate_clinit_check_;
2952 }
2953
2954 void SetMustGenerateClinitCheck() {
2955 generate_clinit_check_ = true;
2956 }
2957
2958 bool CanCallRuntime() const {
2959 return MustGenerateClinitCheck() || !is_referrers_class_;
2960 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002961
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002962 bool CanThrow() const OVERRIDE {
2963 // May call runtime and and therefore can throw.
2964 // TODO: finer grain decision.
2965 return !is_referrers_class_;
2966 }
2967
Calin Juravleacf735c2015-02-12 15:25:22 +00002968 ReferenceTypeInfo GetLoadedClassRTI() {
2969 return loaded_class_rti_;
2970 }
2971
2972 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
2973 // Make sure we only set exact types (the loaded class should never be merged).
2974 DCHECK(rti.IsExact());
2975 loaded_class_rti_ = rti;
2976 }
2977
2978 bool IsResolved() {
2979 return loaded_class_rti_.IsExact();
2980 }
2981
Nicolas Geoffray7e4c3502015-03-18 11:00:52 +00002982 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
2983
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002984 DECLARE_INSTRUCTION(LoadClass);
2985
2986 private:
2987 const uint16_t type_index_;
2988 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002989 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002990 // Whether this instruction must generate the initialization check.
2991 // Used for code generation.
2992 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002993
Calin Juravleacf735c2015-02-12 15:25:22 +00002994 ReferenceTypeInfo loaded_class_rti_;
2995
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002996 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2997};
2998
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002999class HLoadString : public HExpression<0> {
3000 public:
3001 HLoadString(uint32_t string_index, uint32_t dex_pc)
3002 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3003 string_index_(string_index),
3004 dex_pc_(dex_pc) {}
3005
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003006 bool CanBeMoved() const OVERRIDE { return true; }
3007
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003008 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3009 return other->AsLoadString()->string_index_ == string_index_;
3010 }
3011
3012 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3013
3014 uint32_t GetDexPc() const { return dex_pc_; }
3015 uint32_t GetStringIndex() const { return string_index_; }
3016
3017 // TODO: Can we deopt or debug when we resolve a string?
3018 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray7e4c3502015-03-18 11:00:52 +00003019 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003020
3021 DECLARE_INSTRUCTION(LoadString);
3022
3023 private:
3024 const uint32_t string_index_;
3025 const uint32_t dex_pc_;
3026
3027 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3028};
3029
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003030// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003031/**
3032 * Performs an initialization check on its Class object input.
3033 */
3034class HClinitCheck : public HExpression<1> {
3035 public:
3036 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
3037 : HExpression(Primitive::kPrimNot, SideEffects::All()),
3038 dex_pc_(dex_pc) {
3039 SetRawInputAt(0, constant);
3040 }
3041
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003042 bool CanBeMoved() const OVERRIDE { return true; }
3043 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3044 UNUSED(other);
3045 return true;
3046 }
3047
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003048 bool NeedsEnvironment() const OVERRIDE {
3049 // May call runtime to initialize the class.
3050 return true;
3051 }
3052
3053 uint32_t GetDexPc() const { return dex_pc_; }
3054
3055 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3056
3057 DECLARE_INSTRUCTION(ClinitCheck);
3058
3059 private:
3060 const uint32_t dex_pc_;
3061
3062 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3063};
3064
3065class HStaticFieldGet : public HExpression<1> {
3066 public:
3067 HStaticFieldGet(HInstruction* cls,
3068 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003069 MemberOffset field_offset,
3070 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003071 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003072 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003073 SetRawInputAt(0, cls);
3074 }
3075
Calin Juravle52c48962014-12-16 17:02:57 +00003076
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003077 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003078
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003079 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003080 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3081 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003082 }
3083
3084 size_t ComputeHashCode() const OVERRIDE {
3085 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3086 }
3087
Calin Juravle52c48962014-12-16 17:02:57 +00003088 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003089 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3090 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003091 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003092
3093 DECLARE_INSTRUCTION(StaticFieldGet);
3094
3095 private:
3096 const FieldInfo field_info_;
3097
3098 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3099};
3100
3101class HStaticFieldSet : public HTemplateInstruction<2> {
3102 public:
3103 HStaticFieldSet(HInstruction* cls,
3104 HInstruction* value,
3105 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003106 MemberOffset field_offset,
3107 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003108 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003109 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003110 SetRawInputAt(0, cls);
3111 SetRawInputAt(1, value);
3112 }
3113
Calin Juravle52c48962014-12-16 17:02:57 +00003114 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003115 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3116 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003117 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003118
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003119 HInstruction* GetValue() const { return InputAt(1); }
3120
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003121 DECLARE_INSTRUCTION(StaticFieldSet);
3122
3123 private:
3124 const FieldInfo field_info_;
3125
3126 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3127};
3128
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003129// Implement the move-exception DEX instruction.
3130class HLoadException : public HExpression<0> {
3131 public:
3132 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3133
3134 DECLARE_INSTRUCTION(LoadException);
3135
3136 private:
3137 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3138};
3139
3140class HThrow : public HTemplateInstruction<1> {
3141 public:
3142 HThrow(HInstruction* exception, uint32_t dex_pc)
3143 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3144 SetRawInputAt(0, exception);
3145 }
3146
3147 bool IsControlFlow() const OVERRIDE { return true; }
3148
3149 bool NeedsEnvironment() const OVERRIDE { return true; }
3150
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003151 bool CanThrow() const OVERRIDE { return true; }
3152
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003153 uint32_t GetDexPc() const { return dex_pc_; }
3154
3155 DECLARE_INSTRUCTION(Throw);
3156
3157 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003158 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003159
3160 DISALLOW_COPY_AND_ASSIGN(HThrow);
3161};
3162
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003163class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003164 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003165 HInstanceOf(HInstruction* object,
3166 HLoadClass* constant,
3167 bool class_is_final,
3168 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003169 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3170 class_is_final_(class_is_final),
3171 dex_pc_(dex_pc) {
3172 SetRawInputAt(0, object);
3173 SetRawInputAt(1, constant);
3174 }
3175
3176 bool CanBeMoved() const OVERRIDE { return true; }
3177
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003178 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003179 return true;
3180 }
3181
3182 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003183 return false;
3184 }
3185
3186 uint32_t GetDexPc() const { return dex_pc_; }
3187
3188 bool IsClassFinal() const { return class_is_final_; }
3189
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003190 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003191
3192 private:
3193 const bool class_is_final_;
3194 const uint32_t dex_pc_;
3195
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003196 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3197};
3198
Calin Juravleb1498f62015-02-16 13:13:29 +00003199class HBoundType : public HExpression<1> {
3200 public:
3201 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3202 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3203 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003204 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003205 SetRawInputAt(0, input);
3206 }
3207
3208 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3209
3210 bool CanBeNull() const OVERRIDE {
3211 // `null instanceof ClassX` always return false so we can't be null.
3212 return false;
3213 }
3214
3215 DECLARE_INSTRUCTION(BoundType);
3216
3217 private:
3218 // Encodes the most upper class that this instruction can have. In other words
3219 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3220 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3221 const ReferenceTypeInfo bound_type_;
3222
3223 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3224};
3225
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003226class HCheckCast : public HTemplateInstruction<2> {
3227 public:
3228 HCheckCast(HInstruction* object,
3229 HLoadClass* constant,
3230 bool class_is_final,
3231 uint32_t dex_pc)
3232 : HTemplateInstruction(SideEffects::None()),
3233 class_is_final_(class_is_final),
3234 dex_pc_(dex_pc) {
3235 SetRawInputAt(0, object);
3236 SetRawInputAt(1, constant);
3237 }
3238
3239 bool CanBeMoved() const OVERRIDE { return true; }
3240
3241 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3242 return true;
3243 }
3244
3245 bool NeedsEnvironment() const OVERRIDE {
3246 // Instruction may throw a CheckCastError.
3247 return true;
3248 }
3249
3250 bool CanThrow() const OVERRIDE { return true; }
3251
3252 uint32_t GetDexPc() const { return dex_pc_; }
3253
3254 bool IsClassFinal() const { return class_is_final_; }
3255
3256 DECLARE_INSTRUCTION(CheckCast);
3257
3258 private:
3259 const bool class_is_final_;
3260 const uint32_t dex_pc_;
3261
3262 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003263};
3264
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003265class HMonitorOperation : public HTemplateInstruction<1> {
3266 public:
3267 enum OperationKind {
3268 kEnter,
3269 kExit,
3270 };
3271
3272 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3273 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3274 SetRawInputAt(0, object);
3275 }
3276
3277 // Instruction may throw a Java exception, so we need an environment.
3278 bool NeedsEnvironment() const OVERRIDE { return true; }
3279 bool CanThrow() const OVERRIDE { return true; }
3280
3281 uint32_t GetDexPc() const { return dex_pc_; }
3282
3283 bool IsEnter() const { return kind_ == kEnter; }
3284
3285 DECLARE_INSTRUCTION(MonitorOperation);
3286
Calin Juravle52c48962014-12-16 17:02:57 +00003287 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003288 const OperationKind kind_;
3289 const uint32_t dex_pc_;
3290
3291 private:
3292 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3293};
3294
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003295class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003296 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003297 MoveOperands(Location source, Location destination, HInstruction* instruction)
3298 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003299
3300 Location GetSource() const { return source_; }
3301 Location GetDestination() const { return destination_; }
3302
3303 void SetSource(Location value) { source_ = value; }
3304 void SetDestination(Location value) { destination_ = value; }
3305
3306 // The parallel move resolver marks moves as "in-progress" by clearing the
3307 // destination (but not the source).
3308 Location MarkPending() {
3309 DCHECK(!IsPending());
3310 Location dest = destination_;
3311 destination_ = Location::NoLocation();
3312 return dest;
3313 }
3314
3315 void ClearPending(Location dest) {
3316 DCHECK(IsPending());
3317 destination_ = dest;
3318 }
3319
3320 bool IsPending() const {
3321 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3322 return destination_.IsInvalid() && !source_.IsInvalid();
3323 }
3324
3325 // True if this blocks a move from the given location.
3326 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003327 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003328 }
3329
3330 // A move is redundant if it's been eliminated, if its source and
3331 // destination are the same, or if its destination is unneeded.
3332 bool IsRedundant() const {
3333 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3334 }
3335
3336 // We clear both operands to indicate move that's been eliminated.
3337 void Eliminate() {
3338 source_ = destination_ = Location::NoLocation();
3339 }
3340
3341 bool IsEliminated() const {
3342 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3343 return source_.IsInvalid();
3344 }
3345
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003346 HInstruction* GetInstruction() const { return instruction_; }
3347
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003348 private:
3349 Location source_;
3350 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003351 // The instruction this move is assocatied with. Null when this move is
3352 // for moving an input in the expected locations of user (including a phi user).
3353 // This is only used in debug mode, to ensure we do not connect interval siblings
3354 // in the same parallel move.
3355 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003356};
3357
3358static constexpr size_t kDefaultNumberOfMoves = 4;
3359
3360class HParallelMove : public HTemplateInstruction<0> {
3361 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003362 explicit HParallelMove(ArenaAllocator* arena)
3363 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003364
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003365 void AddMove(Location source, Location destination, HInstruction* instruction) {
3366 DCHECK(source.IsValid());
3367 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003368 if (kIsDebugBuild) {
3369 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003370 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003371 if (moves_.Get(i).GetInstruction() == instruction) {
3372 // Special case the situation where the move is for the spill slot
3373 // of the instruction.
3374 if ((GetPrevious() == instruction)
3375 || ((GetPrevious() == nullptr)
3376 && instruction->IsPhi()
3377 && instruction->GetBlock() == GetBlock())) {
3378 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3379 << "Doing parallel moves for the same instruction.";
3380 } else {
3381 DCHECK(false) << "Doing parallel moves for the same instruction.";
3382 }
3383 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003384 }
3385 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003386 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3387 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3388 << "Same destination for two moves in a parallel move.";
3389 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003390 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003391 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003392 }
3393
3394 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003395 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003396 }
3397
3398 size_t NumMoves() const { return moves_.Size(); }
3399
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003400 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003401
3402 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003403 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003404
3405 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3406};
3407
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003408class HGraphVisitor : public ValueObject {
3409 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003410 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3411 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003412
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003413 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003414 virtual void VisitBasicBlock(HBasicBlock* block);
3415
Roland Levillain633021e2014-10-01 14:12:25 +01003416 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003417 void VisitInsertionOrder();
3418
Roland Levillain633021e2014-10-01 14:12:25 +01003419 // Visit the graph following dominator tree reverse post-order.
3420 void VisitReversePostOrder();
3421
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003422 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003423
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003424 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003425#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003426 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3427
3428 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3429
3430#undef DECLARE_VISIT_INSTRUCTION
3431
3432 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003433 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003434
3435 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3436};
3437
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003438class HGraphDelegateVisitor : public HGraphVisitor {
3439 public:
3440 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3441 virtual ~HGraphDelegateVisitor() {}
3442
3443 // Visit functions that delegate to to super class.
3444#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003445 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003446
3447 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3448
3449#undef DECLARE_VISIT_INSTRUCTION
3450
3451 private:
3452 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3453};
3454
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003455class HInsertionOrderIterator : public ValueObject {
3456 public:
3457 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3458
3459 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3460 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3461 void Advance() { ++index_; }
3462
3463 private:
3464 const HGraph& graph_;
3465 size_t index_;
3466
3467 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3468};
3469
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003470class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003471 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003472 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003473
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003474 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3475 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003476 void Advance() { ++index_; }
3477
3478 private:
3479 const HGraph& graph_;
3480 size_t index_;
3481
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003482 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003483};
3484
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003485class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003486 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003487 explicit HPostOrderIterator(const HGraph& graph)
3488 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003489
3490 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003491 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003492 void Advance() { --index_; }
3493
3494 private:
3495 const HGraph& graph_;
3496 size_t index_;
3497
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003498 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003499};
3500
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003501// Iterator over the blocks that art part of the loop. Includes blocks part
3502// of an inner loop. The order in which the blocks are iterated is on their
3503// block id.
3504class HBlocksInLoopIterator : public ValueObject {
3505 public:
3506 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3507 : blocks_in_loop_(info.GetBlocks()),
3508 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3509 index_(0) {
3510 if (!blocks_in_loop_.IsBitSet(index_)) {
3511 Advance();
3512 }
3513 }
3514
3515 bool Done() const { return index_ == blocks_.Size(); }
3516 HBasicBlock* Current() const { return blocks_.Get(index_); }
3517 void Advance() {
3518 ++index_;
3519 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3520 if (blocks_in_loop_.IsBitSet(index_)) {
3521 break;
3522 }
3523 }
3524 }
3525
3526 private:
3527 const BitVector& blocks_in_loop_;
3528 const GrowableArray<HBasicBlock*>& blocks_;
3529 size_t index_;
3530
3531 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3532};
3533
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003534inline int64_t Int64FromConstant(HConstant* constant) {
3535 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3536 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3537 : constant->AsLongConstant()->GetValue();
3538}
3539
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003540} // namespace art
3541
3542#endif // ART_COMPILER_OPTIMIZING_NODES_H_