blob: b10231e0c636fbfdfc333145e73ac6303b62e812 [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) \
Mingyao Yange295e6e2015-03-07 06:37:59 -0800619 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000620 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000621 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000622 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100623 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000624 M(Exit, Instruction) \
625 M(FloatConstant, Constant) \
626 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100627 M(GreaterThan, Condition) \
628 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100629 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000630 M(InstanceFieldGet, Instruction) \
631 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000632 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100633 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000634 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000635 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100636 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000637 M(LessThan, Condition) \
638 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000639 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000640 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100641 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000642 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100643 M(Local, Instruction) \
644 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000645 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000646 M(Mul, BinaryOperation) \
647 M(Neg, UnaryOperation) \
648 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100649 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100650 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000651 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000652 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000653 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000654 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100655 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000656 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100657 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000658 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100659 M(Return, Instruction) \
660 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000661 M(Shl, BinaryOperation) \
662 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100663 M(StaticFieldGet, Instruction) \
664 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100665 M(StoreLocal, Instruction) \
666 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100667 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000668 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000669 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000670 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000671 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000672 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000673
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100674#define FOR_EACH_INSTRUCTION(M) \
675 FOR_EACH_CONCRETE_INSTRUCTION(M) \
676 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100677 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100678 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100679 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700680
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100681#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000682FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
683#undef FORWARD_DECLARATION
684
Roland Levillainccc07a92014-09-16 14:48:16 +0100685#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000686 InstructionKind GetKind() const OVERRIDE { return k##type; } \
687 const char* DebugName() const OVERRIDE { return #type; } \
688 const H##type* As##type() const OVERRIDE { return this; } \
689 H##type* As##type() OVERRIDE { return this; } \
690 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100691 return other->Is##type(); \
692 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000693 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000694
David Brazdiled596192015-01-23 10:39:45 +0000695template <typename T> class HUseList;
696
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100697template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700698class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000699 public:
David Brazdiled596192015-01-23 10:39:45 +0000700 HUseListNode* GetPrevious() const { return prev_; }
701 HUseListNode* GetNext() const { return next_; }
702 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100703 size_t GetIndex() const { return index_; }
704
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000705 private:
David Brazdiled596192015-01-23 10:39:45 +0000706 HUseListNode(T user, size_t index)
707 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
708
709 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100710 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000711 HUseListNode<T>* prev_;
712 HUseListNode<T>* next_;
713
714 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000715
716 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
717};
718
David Brazdiled596192015-01-23 10:39:45 +0000719template <typename T>
720class HUseList : public ValueObject {
721 public:
722 HUseList() : first_(nullptr) {}
723
724 void Clear() {
725 first_ = nullptr;
726 }
727
728 // Adds a new entry at the beginning of the use list and returns
729 // the newly created node.
730 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000731 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000732 if (IsEmpty()) {
733 first_ = new_node;
734 } else {
735 first_->prev_ = new_node;
736 new_node->next_ = first_;
737 first_ = new_node;
738 }
739 return new_node;
740 }
741
742 HUseListNode<T>* GetFirst() const {
743 return first_;
744 }
745
746 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000747 DCHECK(node != nullptr);
748 DCHECK(Contains(node));
749
David Brazdiled596192015-01-23 10:39:45 +0000750 if (node->prev_ != nullptr) {
751 node->prev_->next_ = node->next_;
752 }
753 if (node->next_ != nullptr) {
754 node->next_->prev_ = node->prev_;
755 }
756 if (node == first_) {
757 first_ = node->next_;
758 }
759 }
760
David Brazdil1abb4192015-02-17 18:33:36 +0000761 bool Contains(const HUseListNode<T>* node) const {
762 if (node == nullptr) {
763 return false;
764 }
765 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
766 if (current == node) {
767 return true;
768 }
769 }
770 return false;
771 }
772
David Brazdiled596192015-01-23 10:39:45 +0000773 bool IsEmpty() const {
774 return first_ == nullptr;
775 }
776
777 bool HasOnlyOneUse() const {
778 return first_ != nullptr && first_->next_ == nullptr;
779 }
780
781 private:
782 HUseListNode<T>* first_;
783};
784
785template<typename T>
786class HUseIterator : public ValueObject {
787 public:
788 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
789
790 bool Done() const { return current_ == nullptr; }
791
792 void Advance() {
793 DCHECK(!Done());
794 current_ = current_->GetNext();
795 }
796
797 HUseListNode<T>* Current() const {
798 DCHECK(!Done());
799 return current_;
800 }
801
802 private:
803 HUseListNode<T>* current_;
804
805 friend class HValue;
806};
807
David Brazdil1abb4192015-02-17 18:33:36 +0000808// This class is used by HEnvironment and HInstruction classes to record the
809// instructions they use and pointers to the corresponding HUseListNodes kept
810// by the used instructions.
811template <typename T>
812class HUserRecord : public ValueObject {
813 public:
814 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
815 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
816
817 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
818 : instruction_(old_record.instruction_), use_node_(use_node) {
819 DCHECK(instruction_ != nullptr);
820 DCHECK(use_node_ != nullptr);
821 DCHECK(old_record.use_node_ == nullptr);
822 }
823
824 HInstruction* GetInstruction() const { return instruction_; }
825 HUseListNode<T>* GetUseNode() const { return use_node_; }
826
827 private:
828 // Instruction used by the user.
829 HInstruction* instruction_;
830
831 // Corresponding entry in the use list kept by 'instruction_'.
832 HUseListNode<T>* use_node_;
833};
834
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100835// Represents the side effects an instruction may have.
836class SideEffects : public ValueObject {
837 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100838 SideEffects() : flags_(0) {}
839
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100840 static SideEffects None() {
841 return SideEffects(0);
842 }
843
844 static SideEffects All() {
845 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
846 }
847
848 static SideEffects ChangesSomething() {
849 return SideEffects((1 << kFlagChangesCount) - 1);
850 }
851
852 static SideEffects DependsOnSomething() {
853 int count = kFlagDependsOnCount - kFlagChangesCount;
854 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
855 }
856
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100857 SideEffects Union(SideEffects other) const {
858 return SideEffects(flags_ | other.flags_);
859 }
860
Roland Levillain72bceff2014-09-15 18:29:00 +0100861 bool HasSideEffects() const {
862 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
863 return (flags_ & all_bits_set) != 0;
864 }
865
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100866 bool HasAllSideEffects() const {
867 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
868 return all_bits_set == (flags_ & all_bits_set);
869 }
870
871 bool DependsOn(SideEffects other) const {
872 size_t depends_flags = other.ComputeDependsFlags();
873 return (flags_ & depends_flags) != 0;
874 }
875
876 bool HasDependencies() const {
877 int count = kFlagDependsOnCount - kFlagChangesCount;
878 size_t all_bits_set = (1 << count) - 1;
879 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
880 }
881
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100882 private:
883 static constexpr int kFlagChangesSomething = 0;
884 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
885
886 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
887 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
888
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100889 explicit SideEffects(size_t flags) : flags_(flags) {}
890
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100891 size_t ComputeDependsFlags() const {
892 return flags_ << kFlagChangesCount;
893 }
894
895 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100896};
897
David Brazdiled596192015-01-23 10:39:45 +0000898// A HEnvironment object contains the values of virtual registers at a given location.
899class HEnvironment : public ArenaObject<kArenaAllocMisc> {
900 public:
901 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
902 : vregs_(arena, number_of_vregs) {
903 vregs_.SetSize(number_of_vregs);
904 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000905 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000906 }
907 }
908
909 void CopyFrom(HEnvironment* env);
910
911 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +0000912 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +0000913 }
914
915 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +0000916 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +0000917 }
918
David Brazdil1abb4192015-02-17 18:33:36 +0000919 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +0000920
921 size_t Size() const { return vregs_.Size(); }
922
923 private:
David Brazdil1abb4192015-02-17 18:33:36 +0000924 // Record instructions' use entries of this environment for constant-time removal.
925 // It should only be called by HInstruction when a new environment use is added.
926 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
927 DCHECK(env_use->GetUser() == this);
928 size_t index = env_use->GetIndex();
929 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
930 }
David Brazdiled596192015-01-23 10:39:45 +0000931
David Brazdil1abb4192015-02-17 18:33:36 +0000932 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +0000933
David Brazdil1abb4192015-02-17 18:33:36 +0000934 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +0000935
936 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
937};
938
Calin Juravleacf735c2015-02-12 15:25:22 +0000939class ReferenceTypeInfo : ValueObject {
940 public:
Calin Juravleb1498f62015-02-16 13:13:29 +0000941 typedef Handle<mirror::Class> TypeHandle;
942
943 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
944 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
945 if (type_handle->IsObjectClass()) {
946 // Override the type handle to be consistent with the case when we get to
947 // Top but don't have the Object class available. It avoids having to guess
948 // what value the type_handle has when it's Top.
949 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
950 } else {
951 return ReferenceTypeInfo(type_handle, is_exact, false);
952 }
953 }
954
955 static ReferenceTypeInfo CreateTop(bool is_exact) {
956 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +0000957 }
958
959 bool IsExact() const { return is_exact_; }
960 bool IsTop() const { return is_top_; }
961
962 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
963
Calin Juravleb1498f62015-02-16 13:13:29 +0000964 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000965 if (IsTop()) {
966 // Top (equivalent for java.lang.Object) is supertype of anything.
967 return true;
968 }
969 if (rti.IsTop()) {
970 // If we get here `this` is not Top() so it can't be a supertype.
971 return false;
972 }
973 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
974 }
975
976 // Returns true if the type information provide the same amount of details.
977 // Note that it does not mean that the instructions have the same actual type
978 // (e.g. tops are equal but they can be the result of a merge).
979 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
980 if (IsExact() != rti.IsExact()) {
981 return false;
982 }
983 if (IsTop() && rti.IsTop()) {
984 // `Top` means java.lang.Object, so the types are equivalent.
985 return true;
986 }
987 if (IsTop() || rti.IsTop()) {
988 // If only one is top or object than they are not equivalent.
989 // NB: We need this extra check because the type_handle of `Top` is invalid
990 // and we cannot inspect its reference.
991 return false;
992 }
993
994 // Finally check the types.
995 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
996 }
997
998 private:
Calin Juravleb1498f62015-02-16 13:13:29 +0000999 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1000 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1001 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1002
Calin Juravleacf735c2015-02-12 15:25:22 +00001003 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001004 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001005 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001006 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001007 bool is_exact_;
1008 // A true value here means that the object type should be java.lang.Object.
1009 // We don't have access to the corresponding mirror object every time so this
1010 // flag acts as a substitute. When true, the TypeHandle refers to a null
1011 // pointer and should not be used.
1012 bool is_top_;
1013};
1014
1015std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1016
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001017class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001018 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001019 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001020 : previous_(nullptr),
1021 next_(nullptr),
1022 block_(nullptr),
1023 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001024 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001025 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001026 locations_(nullptr),
1027 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001028 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001029 side_effects_(side_effects),
1030 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001031
Dave Allison20dfc792014-06-16 20:44:29 -07001032 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001033
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001034#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001035 enum InstructionKind {
1036 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1037 };
1038#undef DECLARE_KIND
1039
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001040 HInstruction* GetNext() const { return next_; }
1041 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001042
Calin Juravle77520bc2015-01-12 18:45:46 +00001043 HInstruction* GetNextDisregardingMoves() const;
1044 HInstruction* GetPreviousDisregardingMoves() const;
1045
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001046 HBasicBlock* GetBlock() const { return block_; }
1047 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001048 bool IsInBlock() const { return block_ != nullptr; }
1049 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001050 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001051
Roland Levillain6b879dd2014-09-22 17:13:44 +01001052 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001053 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001054
1055 virtual void Accept(HGraphVisitor* visitor) = 0;
1056 virtual const char* DebugName() const = 0;
1057
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001058 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001059 void SetRawInputAt(size_t index, HInstruction* input) {
1060 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1061 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001062
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001063 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001064 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001065 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001066 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001067
Calin Juravle61d544b2015-02-23 16:46:57 +00001068 virtual bool ActAsNullConstant() const { return false; }
1069
Calin Juravle10e244f2015-01-26 18:54:32 +00001070 // Does not apply for all instructions, but having this at top level greatly
1071 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001072 virtual bool CanBeNull() const {
1073 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1074 return true;
1075 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001076
Calin Juravle77520bc2015-01-12 18:45:46 +00001077 virtual bool CanDoImplicitNullCheck() const { return false; }
1078
Calin Juravleacf735c2015-02-12 15:25:22 +00001079 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001080 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001081 reference_type_info_ = reference_type_info;
1082 }
1083
Calin Juravle61d544b2015-02-23 16:46:57 +00001084 ReferenceTypeInfo GetReferenceTypeInfo() const {
1085 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1086 return reference_type_info_;
1087 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001088
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001089 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001090 DCHECK(user != nullptr);
1091 HUseListNode<HInstruction*>* use =
1092 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1093 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001094 }
1095
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001096 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001097 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001098 HUseListNode<HEnvironment*>* env_use =
1099 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1100 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001101 }
1102
David Brazdil1abb4192015-02-17 18:33:36 +00001103 void RemoveAsUserOfInput(size_t input) {
1104 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1105 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1106 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001107
David Brazdil1abb4192015-02-17 18:33:36 +00001108 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1109 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001110
David Brazdiled596192015-01-23 10:39:45 +00001111 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1112 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001113 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001114
Roland Levillain6c82d402014-10-13 16:10:27 +01001115 // Does this instruction strictly dominate `other_instruction`?
1116 // Returns false if this instruction and `other_instruction` are the same.
1117 // Aborts if this instruction and `other_instruction` are both phis.
1118 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001119
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001120 int GetId() const { return id_; }
1121 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001122
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001123 int GetSsaIndex() const { return ssa_index_; }
1124 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1125 bool HasSsaIndex() const { return ssa_index_ != -1; }
1126
1127 bool HasEnvironment() const { return environment_ != nullptr; }
1128 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001129 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
1130
Nicolas Geoffray39468442014-09-02 15:17:15 +01001131 // Returns the number of entries in the environment. Typically, that is the
1132 // number of dex registers in a method. It could be more in case of inlining.
1133 size_t EnvironmentSize() const;
1134
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001135 LocationSummary* GetLocations() const { return locations_; }
1136 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001137
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001138 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001139 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001140
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001141 // Move `this` instruction before `cursor`.
1142 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001143
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001144#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001145 bool Is##type() const { return (As##type() != nullptr); } \
1146 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001147 virtual H##type* As##type() { return nullptr; }
1148
1149 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1150#undef INSTRUCTION_TYPE_CHECK
1151
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001152 // Returns whether the instruction can be moved within the graph.
1153 virtual bool CanBeMoved() const { return false; }
1154
1155 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001156 virtual bool InstructionTypeEquals(HInstruction* other) const {
1157 UNUSED(other);
1158 return false;
1159 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001160
1161 // Returns whether any data encoded in the two instructions is equal.
1162 // This method does not look at the inputs. Both instructions must be
1163 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001164 virtual bool InstructionDataEquals(HInstruction* other) const {
1165 UNUSED(other);
1166 return false;
1167 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001168
1169 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001170 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001171 // 2) Their inputs are identical.
1172 bool Equals(HInstruction* other) const;
1173
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001174 virtual InstructionKind GetKind() const = 0;
1175
1176 virtual size_t ComputeHashCode() const {
1177 size_t result = GetKind();
1178 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1179 result = (result * 31) + InputAt(i)->GetId();
1180 }
1181 return result;
1182 }
1183
1184 SideEffects GetSideEffects() const { return side_effects_; }
1185
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001186 size_t GetLifetimePosition() const { return lifetime_position_; }
1187 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1188 LiveInterval* GetLiveInterval() const { return live_interval_; }
1189 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1190 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1191
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001192 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1193
1194 // Returns whether the code generation of the instruction will require to have access
1195 // to the current method. Such instructions are:
1196 // (1): Instructions that require an environment, as calling the runtime requires
1197 // to walk the stack and have the current method stored at a specific stack address.
1198 // (2): Object literals like classes and strings, that are loaded from the dex cache
1199 // fields of the current method.
1200 bool NeedsCurrentMethod() const {
1201 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1202 }
1203
Nicolas Geoffray7e4c3502015-03-18 11:00:52 +00001204 virtual bool NeedsDexCache() const { return false; }
1205
David Brazdil1abb4192015-02-17 18:33:36 +00001206 protected:
1207 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1208 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1209
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001210 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001211 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1212
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001213 HInstruction* previous_;
1214 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001215 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001216
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001217 // An instruction gets an id when it is added to the graph.
1218 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001219 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001220 int id_;
1221
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001222 // When doing liveness analysis, instructions that have uses get an SSA index.
1223 int ssa_index_;
1224
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001225 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001226 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001227
1228 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001229 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001230
Nicolas Geoffray39468442014-09-02 15:17:15 +01001231 // The environment associated with this instruction. Not null if the instruction
1232 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001233 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001234
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001235 // Set by the code generator.
1236 LocationSummary* locations_;
1237
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001238 // Set by the liveness analysis.
1239 LiveInterval* live_interval_;
1240
1241 // Set by the liveness analysis, this is the position in a linear
1242 // order of blocks where this instruction's live interval start.
1243 size_t lifetime_position_;
1244
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001245 const SideEffects side_effects_;
1246
Calin Juravleacf735c2015-02-12 15:25:22 +00001247 // TODO: for primitive types this should be marked as invalid.
1248 ReferenceTypeInfo reference_type_info_;
1249
David Brazdil1abb4192015-02-17 18:33:36 +00001250 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001251 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001252 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001253 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001254 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001255
1256 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1257};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001258std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001259
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001260class HInputIterator : public ValueObject {
1261 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001262 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001263
1264 bool Done() const { return index_ == instruction_->InputCount(); }
1265 HInstruction* Current() const { return instruction_->InputAt(index_); }
1266 void Advance() { index_++; }
1267
1268 private:
1269 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001270 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001271
1272 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1273};
1274
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001275class HInstructionIterator : public ValueObject {
1276 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001277 explicit HInstructionIterator(const HInstructionList& instructions)
1278 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001279 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001280 }
1281
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001282 bool Done() const { return instruction_ == nullptr; }
1283 HInstruction* Current() const { return instruction_; }
1284 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001285 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001286 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001287 }
1288
1289 private:
1290 HInstruction* instruction_;
1291 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001292
1293 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001294};
1295
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001296class HBackwardInstructionIterator : public ValueObject {
1297 public:
1298 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1299 : instruction_(instructions.last_instruction_) {
1300 next_ = Done() ? nullptr : instruction_->GetPrevious();
1301 }
1302
1303 bool Done() const { return instruction_ == nullptr; }
1304 HInstruction* Current() const { return instruction_; }
1305 void Advance() {
1306 instruction_ = next_;
1307 next_ = Done() ? nullptr : instruction_->GetPrevious();
1308 }
1309
1310 private:
1311 HInstruction* instruction_;
1312 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001313
1314 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001315};
1316
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001317// An embedded container with N elements of type T. Used (with partial
1318// specialization for N=0) because embedded arrays cannot have size 0.
1319template<typename T, intptr_t N>
1320class EmbeddedArray {
1321 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001322 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001323
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001324 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001325
1326 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001327 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001328 return elements_[i];
1329 }
1330
1331 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001332 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001333 return elements_[i];
1334 }
1335
1336 const T& At(intptr_t i) const {
1337 return (*this)[i];
1338 }
1339
1340 void SetAt(intptr_t i, const T& val) {
1341 (*this)[i] = val;
1342 }
1343
1344 private:
1345 T elements_[N];
1346};
1347
1348template<typename T>
1349class EmbeddedArray<T, 0> {
1350 public:
1351 intptr_t length() const { return 0; }
1352 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001353 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001354 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001355 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001356 }
1357 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001358 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001359 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001360 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001361 }
1362};
1363
1364template<intptr_t N>
1365class HTemplateInstruction: public HInstruction {
1366 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001367 HTemplateInstruction<N>(SideEffects side_effects)
1368 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001369 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001370
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001371 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001372
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001373 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001374 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1375
1376 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1377 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001378 }
1379
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001380 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001381 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001382
1383 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001384};
1385
Dave Allison20dfc792014-06-16 20:44:29 -07001386template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001387class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001388 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001389 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1390 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001391 virtual ~HExpression() {}
1392
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001393 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001394
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001395 protected:
1396 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001397};
1398
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001399// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1400// instruction that branches to the exit block.
1401class HReturnVoid : public HTemplateInstruction<0> {
1402 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001403 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001404
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001405 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001406
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001407 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001408
1409 private:
1410 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1411};
1412
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001413// Represents dex's RETURN opcodes. A HReturn is a control flow
1414// instruction that branches to the exit block.
1415class HReturn : public HTemplateInstruction<1> {
1416 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001417 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001418 SetRawInputAt(0, value);
1419 }
1420
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001421 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001422
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001423 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001424
1425 private:
1426 DISALLOW_COPY_AND_ASSIGN(HReturn);
1427};
1428
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001429// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001430// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001431// exit block.
1432class HExit : public HTemplateInstruction<0> {
1433 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001434 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001435
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001436 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001437
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001438 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001439
1440 private:
1441 DISALLOW_COPY_AND_ASSIGN(HExit);
1442};
1443
1444// Jumps from one block to another.
1445class HGoto : public HTemplateInstruction<0> {
1446 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001447 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1448
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001449 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001450
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001451 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001452 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001453 }
1454
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001455 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001456
1457 private:
1458 DISALLOW_COPY_AND_ASSIGN(HGoto);
1459};
1460
Dave Allison20dfc792014-06-16 20:44:29 -07001461
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001462// Conditional branch. A block ending with an HIf instruction must have
1463// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001464class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001465 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001466 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001467 SetRawInputAt(0, input);
1468 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001469
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001470 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001471
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001472 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001473 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001474 }
1475
1476 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001477 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001478 }
1479
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001480 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001481
1482 private:
1483 DISALLOW_COPY_AND_ASSIGN(HIf);
1484};
1485
Mingyao Yange295e6e2015-03-07 06:37:59 -08001486// Deoptimize to interpreter, upon checking a condition.
1487class HDeoptimize : public HTemplateInstruction<1> {
1488 public:
1489 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1490 : HTemplateInstruction(SideEffects::None()),
1491 dex_pc_(dex_pc) {
1492 SetRawInputAt(0, cond);
1493 }
1494
1495 bool NeedsEnvironment() const OVERRIDE { return true; }
1496 bool CanThrow() const OVERRIDE { return true; }
1497 uint32_t GetDexPc() const { return dex_pc_; }
1498
1499 DECLARE_INSTRUCTION(Deoptimize);
1500
1501 private:
1502 uint32_t dex_pc_;
1503
1504 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1505};
1506
Roland Levillain88cb1752014-10-20 16:36:47 +01001507class HUnaryOperation : public HExpression<1> {
1508 public:
1509 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1510 : HExpression(result_type, SideEffects::None()) {
1511 SetRawInputAt(0, input);
1512 }
1513
1514 HInstruction* GetInput() const { return InputAt(0); }
1515 Primitive::Type GetResultType() const { return GetType(); }
1516
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001517 bool CanBeMoved() const OVERRIDE { return true; }
1518 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001519 UNUSED(other);
1520 return true;
1521 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001522
Roland Levillain9240d6a2014-10-20 16:47:04 +01001523 // Try to statically evaluate `operation` and return a HConstant
1524 // containing the result of this evaluation. If `operation` cannot
1525 // be evaluated as a constant, return nullptr.
1526 HConstant* TryStaticEvaluation() const;
1527
1528 // Apply this operation to `x`.
1529 virtual int32_t Evaluate(int32_t x) const = 0;
1530 virtual int64_t Evaluate(int64_t x) const = 0;
1531
Roland Levillain88cb1752014-10-20 16:36:47 +01001532 DECLARE_INSTRUCTION(UnaryOperation);
1533
1534 private:
1535 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1536};
1537
Dave Allison20dfc792014-06-16 20:44:29 -07001538class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001539 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001540 HBinaryOperation(Primitive::Type result_type,
1541 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001542 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001543 SetRawInputAt(0, left);
1544 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001545 }
1546
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001547 HInstruction* GetLeft() const { return InputAt(0); }
1548 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001549 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001550
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001551 virtual bool IsCommutative() const { return false; }
1552
1553 // Put constant on the right.
1554 // Returns whether order is changed.
1555 bool OrderInputsWithConstantOnTheRight() {
1556 HInstruction* left = InputAt(0);
1557 HInstruction* right = InputAt(1);
1558 if (left->IsConstant() && !right->IsConstant()) {
1559 ReplaceInput(right, 0);
1560 ReplaceInput(left, 1);
1561 return true;
1562 }
1563 return false;
1564 }
1565
1566 // Order inputs by instruction id, but favor constant on the right side.
1567 // This helps GVN for commutative ops.
1568 void OrderInputs() {
1569 DCHECK(IsCommutative());
1570 HInstruction* left = InputAt(0);
1571 HInstruction* right = InputAt(1);
1572 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1573 return;
1574 }
1575 if (OrderInputsWithConstantOnTheRight()) {
1576 return;
1577 }
1578 // Order according to instruction id.
1579 if (left->GetId() > right->GetId()) {
1580 ReplaceInput(right, 0);
1581 ReplaceInput(left, 1);
1582 }
1583 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001584
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001585 bool CanBeMoved() const OVERRIDE { return true; }
1586 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001587 UNUSED(other);
1588 return true;
1589 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001590
Roland Levillain9240d6a2014-10-20 16:47:04 +01001591 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001592 // containing the result of this evaluation. If `operation` cannot
1593 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001594 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001595
1596 // Apply this operation to `x` and `y`.
1597 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1598 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1599
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001600 // Returns an input that can legally be used as the right input and is
1601 // constant, or nullptr.
1602 HConstant* GetConstantRight() const;
1603
1604 // If `GetConstantRight()` returns one of the input, this returns the other
1605 // one. Otherwise it returns nullptr.
1606 HInstruction* GetLeastConstantLeft() const;
1607
Roland Levillainccc07a92014-09-16 14:48:16 +01001608 DECLARE_INSTRUCTION(BinaryOperation);
1609
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001610 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001611 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1612};
1613
Dave Allison20dfc792014-06-16 20:44:29 -07001614class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001615 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001616 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001617 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1618 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001619
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001620 bool NeedsMaterialization() const { return needs_materialization_; }
1621 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001622
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001623 // For code generation purposes, returns whether this instruction is just before
Mingyao Yange295e6e2015-03-07 06:37:59 -08001624 // `instruction`, and disregard moves in between.
1625 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001626
Dave Allison20dfc792014-06-16 20:44:29 -07001627 DECLARE_INSTRUCTION(Condition);
1628
1629 virtual IfCondition GetCondition() const = 0;
1630
1631 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001632 // For register allocation purposes, returns whether this instruction needs to be
1633 // materialized (that is, not just be in the processor flags).
1634 bool needs_materialization_;
1635
Dave Allison20dfc792014-06-16 20:44:29 -07001636 DISALLOW_COPY_AND_ASSIGN(HCondition);
1637};
1638
1639// Instruction to check if two inputs are equal to each other.
1640class HEqual : public HCondition {
1641 public:
1642 HEqual(HInstruction* first, HInstruction* second)
1643 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001644
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001645 bool IsCommutative() const OVERRIDE { return true; }
1646
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001647 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001648 return x == y ? 1 : 0;
1649 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001650 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001651 return x == y ? 1 : 0;
1652 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001653
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001654 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001655
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001656 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001657 return kCondEQ;
1658 }
1659
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001660 private:
1661 DISALLOW_COPY_AND_ASSIGN(HEqual);
1662};
1663
Dave Allison20dfc792014-06-16 20:44:29 -07001664class HNotEqual : public HCondition {
1665 public:
1666 HNotEqual(HInstruction* first, HInstruction* second)
1667 : HCondition(first, second) {}
1668
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001669 bool IsCommutative() const OVERRIDE { return true; }
1670
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001671 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001672 return x != y ? 1 : 0;
1673 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001674 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001675 return x != y ? 1 : 0;
1676 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001677
Dave Allison20dfc792014-06-16 20:44:29 -07001678 DECLARE_INSTRUCTION(NotEqual);
1679
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001680 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001681 return kCondNE;
1682 }
1683
1684 private:
1685 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1686};
1687
1688class HLessThan : public HCondition {
1689 public:
1690 HLessThan(HInstruction* first, HInstruction* second)
1691 : HCondition(first, second) {}
1692
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001693 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001694 return x < y ? 1 : 0;
1695 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001696 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001697 return x < y ? 1 : 0;
1698 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001699
Dave Allison20dfc792014-06-16 20:44:29 -07001700 DECLARE_INSTRUCTION(LessThan);
1701
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001702 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001703 return kCondLT;
1704 }
1705
1706 private:
1707 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1708};
1709
1710class HLessThanOrEqual : public HCondition {
1711 public:
1712 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1713 : HCondition(first, second) {}
1714
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001715 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001716 return x <= y ? 1 : 0;
1717 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001718 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001719 return x <= y ? 1 : 0;
1720 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001721
Dave Allison20dfc792014-06-16 20:44:29 -07001722 DECLARE_INSTRUCTION(LessThanOrEqual);
1723
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001724 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001725 return kCondLE;
1726 }
1727
1728 private:
1729 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1730};
1731
1732class HGreaterThan : public HCondition {
1733 public:
1734 HGreaterThan(HInstruction* first, HInstruction* second)
1735 : HCondition(first, second) {}
1736
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001737 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001738 return x > y ? 1 : 0;
1739 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001740 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001741 return x > y ? 1 : 0;
1742 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001743
Dave Allison20dfc792014-06-16 20:44:29 -07001744 DECLARE_INSTRUCTION(GreaterThan);
1745
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001746 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001747 return kCondGT;
1748 }
1749
1750 private:
1751 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1752};
1753
1754class HGreaterThanOrEqual : public HCondition {
1755 public:
1756 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1757 : HCondition(first, second) {}
1758
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001759 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001760 return x >= y ? 1 : 0;
1761 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001762 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001763 return x >= y ? 1 : 0;
1764 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001765
Dave Allison20dfc792014-06-16 20:44:29 -07001766 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1767
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001768 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001769 return kCondGE;
1770 }
1771
1772 private:
1773 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1774};
1775
1776
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001777// Instruction to check how two inputs compare to each other.
1778// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1779class HCompare : public HBinaryOperation {
1780 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001781 // The bias applies for floating point operations and indicates how NaN
1782 // comparisons are treated:
1783 enum Bias {
1784 kNoBias, // bias is not applicable (i.e. for long operation)
1785 kGtBias, // return 1 for NaN comparisons
1786 kLtBias, // return -1 for NaN comparisons
1787 };
1788
1789 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1790 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001791 DCHECK_EQ(type, first->GetType());
1792 DCHECK_EQ(type, second->GetType());
1793 }
1794
Calin Juravleddb7df22014-11-25 20:56:51 +00001795 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001796 return
1797 x == y ? 0 :
1798 x > y ? 1 :
1799 -1;
1800 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001801
1802 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001803 return
1804 x == y ? 0 :
1805 x > y ? 1 :
1806 -1;
1807 }
1808
Calin Juravleddb7df22014-11-25 20:56:51 +00001809 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1810 return bias_ == other->AsCompare()->bias_;
1811 }
1812
1813 bool IsGtBias() { return bias_ == kGtBias; }
1814
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001815 DECLARE_INSTRUCTION(Compare);
1816
1817 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001818 const Bias bias_;
1819
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001820 DISALLOW_COPY_AND_ASSIGN(HCompare);
1821};
1822
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001823// A local in the graph. Corresponds to a Dex register.
1824class HLocal : public HTemplateInstruction<0> {
1825 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001826 explicit HLocal(uint16_t reg_number)
1827 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001828
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001829 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001830
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001831 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001832
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001833 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001834 // The Dex register number.
1835 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001836
1837 DISALLOW_COPY_AND_ASSIGN(HLocal);
1838};
1839
1840// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001841class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001842 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001843 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001844 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001845 SetRawInputAt(0, local);
1846 }
1847
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001848 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1849
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001850 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001851
1852 private:
1853 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1854};
1855
1856// Store a value in a given local. This instruction has two inputs: the value
1857// and the local.
1858class HStoreLocal : public HTemplateInstruction<2> {
1859 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001860 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001861 SetRawInputAt(0, local);
1862 SetRawInputAt(1, value);
1863 }
1864
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001865 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1866
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001867 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001868
1869 private:
1870 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1871};
1872
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001873class HConstant : public HExpression<0> {
1874 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001875 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1876
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001877 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001878
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001879 virtual bool IsMinusOne() const { return false; }
1880 virtual bool IsZero() const { return false; }
1881 virtual bool IsOne() const { return false; }
1882
1883 static HConstant* NewConstant(ArenaAllocator* allocator, Primitive::Type type, int64_t val);
1884
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001885 DECLARE_INSTRUCTION(Constant);
1886
1887 private:
1888 DISALLOW_COPY_AND_ASSIGN(HConstant);
1889};
1890
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001891class HFloatConstant : public HConstant {
1892 public:
1893 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1894
1895 float GetValue() const { return value_; }
1896
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001897 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001898 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1899 bit_cast<float, int32_t>(value_);
1900 }
1901
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001902 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001903
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001904 bool IsMinusOne() const OVERRIDE {
1905 return bit_cast<uint32_t>(AsFloatConstant()->GetValue()) == bit_cast<uint32_t>((-1.0f));
1906 }
1907 bool IsZero() const OVERRIDE {
1908 return AsFloatConstant()->GetValue() == 0.0f;
1909 }
1910 bool IsOne() const OVERRIDE {
1911 return bit_cast<uint32_t>(AsFloatConstant()->GetValue()) == bit_cast<uint32_t>(1.0f);
1912 }
1913
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001914 DECLARE_INSTRUCTION(FloatConstant);
1915
1916 private:
1917 const float value_;
1918
1919 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1920};
1921
1922class HDoubleConstant : public HConstant {
1923 public:
1924 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1925
1926 double GetValue() const { return value_; }
1927
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001928 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001929 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1930 bit_cast<double, int64_t>(value_);
1931 }
1932
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001933 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001934
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001935 bool IsMinusOne() const OVERRIDE {
1936 return bit_cast<uint64_t>(AsDoubleConstant()->GetValue()) == bit_cast<uint64_t>((-1.0));
1937 }
1938 bool IsZero() const OVERRIDE {
1939 return AsDoubleConstant()->GetValue() == 0.0;
1940 }
1941 bool IsOne() const OVERRIDE {
1942 return bit_cast<uint64_t>(AsDoubleConstant()->GetValue()) == bit_cast<uint64_t>(1.0);
1943 }
1944
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001945 DECLARE_INSTRUCTION(DoubleConstant);
1946
1947 private:
1948 const double value_;
1949
1950 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1951};
1952
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001953class HNullConstant : public HConstant {
1954 public:
1955 HNullConstant() : HConstant(Primitive::kPrimNot) {}
1956
1957 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
1958 return true;
1959 }
1960
1961 size_t ComputeHashCode() const OVERRIDE { return 0; }
1962
Calin Juravle61d544b2015-02-23 16:46:57 +00001963 bool ActAsNullConstant() const OVERRIDE { return true; }
1964
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001965 DECLARE_INSTRUCTION(NullConstant);
1966
1967 private:
1968 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
1969};
1970
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001971// Constants of the type int. Those can be from Dex instructions, or
1972// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001973class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001974 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001975 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001976
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001977 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001978
Calin Juravle61d544b2015-02-23 16:46:57 +00001979 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001980 return other->AsIntConstant()->value_ == value_;
1981 }
1982
Calin Juravle61d544b2015-02-23 16:46:57 +00001983 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
1984
1985 // TODO: Null is represented by the `0` constant. In most cases we replace it
1986 // with a HNullConstant but we don't do it when comparing (a != null). This
1987 // method is an workaround until we fix the above.
1988 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001989
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001990 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
1991 bool IsZero() const OVERRIDE { return GetValue() == 0; }
1992 bool IsOne() const OVERRIDE { return GetValue() == 1; }
1993
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001994 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001995
1996 private:
1997 const int32_t value_;
1998
1999 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2000};
2001
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002002class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002003 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002004 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002005
2006 int64_t GetValue() const { return value_; }
2007
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002008 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002009 return other->AsLongConstant()->value_ == value_;
2010 }
2011
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002012 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002013
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002014 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2015 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2016 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2017
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002018 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002019
2020 private:
2021 const int64_t value_;
2022
2023 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2024};
2025
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002026enum class Intrinsics {
2027#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2028#include "intrinsics_list.h"
2029 kNone,
2030 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2031#undef INTRINSICS_LIST
2032#undef OPTIMIZING_INTRINSICS
2033};
2034std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2035
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002036class HInvoke : public HInstruction {
2037 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002038 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002039
2040 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2041 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002042 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002043
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002044 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002045 SetRawInputAt(index, argument);
2046 }
2047
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002048 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002049
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002050 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002051
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002052 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2053
2054 Intrinsics GetIntrinsic() {
2055 return intrinsic_;
2056 }
2057
2058 void SetIntrinsic(Intrinsics intrinsic) {
2059 intrinsic_ = intrinsic;
2060 }
2061
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002062 DECLARE_INSTRUCTION(Invoke);
2063
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002064 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002065 HInvoke(ArenaAllocator* arena,
2066 uint32_t number_of_arguments,
2067 Primitive::Type return_type,
2068 uint32_t dex_pc,
2069 uint32_t dex_method_index)
2070 : HInstruction(SideEffects::All()),
2071 inputs_(arena, number_of_arguments),
2072 return_type_(return_type),
2073 dex_pc_(dex_pc),
2074 dex_method_index_(dex_method_index),
2075 intrinsic_(Intrinsics::kNone) {
2076 inputs_.SetSize(number_of_arguments);
2077 }
2078
David Brazdil1abb4192015-02-17 18:33:36 +00002079 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2080 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2081 inputs_.Put(index, input);
2082 }
2083
2084 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002085 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002086 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002087 const uint32_t dex_method_index_;
2088 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002089
2090 private:
2091 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2092};
2093
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002094class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002095 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002096 HInvokeStaticOrDirect(ArenaAllocator* arena,
2097 uint32_t number_of_arguments,
2098 Primitive::Type return_type,
2099 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002100 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002101 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002102 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002103 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002104 invoke_type_(invoke_type),
2105 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002106
Calin Juravle77520bc2015-01-12 18:45:46 +00002107 bool CanDoImplicitNullCheck() const OVERRIDE {
2108 // We access the method via the dex cache so we can't do an implicit null check.
2109 // TODO: for intrinsics we can generate implicit null checks.
2110 return false;
2111 }
2112
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002113 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002114 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray7e4c3502015-03-18 11:00:52 +00002115 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002116
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002117 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002118
2119 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002120 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002121 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002122
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002123 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002124};
2125
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002126class HInvokeVirtual : public HInvoke {
2127 public:
2128 HInvokeVirtual(ArenaAllocator* arena,
2129 uint32_t number_of_arguments,
2130 Primitive::Type return_type,
2131 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002132 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002133 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002134 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002135 vtable_index_(vtable_index) {}
2136
Calin Juravle77520bc2015-01-12 18:45:46 +00002137 bool CanDoImplicitNullCheck() const OVERRIDE {
2138 // TODO: Add implicit null checks in intrinsics.
2139 return !GetLocations()->Intrinsified();
2140 }
2141
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002142 uint32_t GetVTableIndex() const { return vtable_index_; }
2143
2144 DECLARE_INSTRUCTION(InvokeVirtual);
2145
2146 private:
2147 const uint32_t vtable_index_;
2148
2149 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2150};
2151
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002152class HInvokeInterface : public HInvoke {
2153 public:
2154 HInvokeInterface(ArenaAllocator* arena,
2155 uint32_t number_of_arguments,
2156 Primitive::Type return_type,
2157 uint32_t dex_pc,
2158 uint32_t dex_method_index,
2159 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002160 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002161 imt_index_(imt_index) {}
2162
Calin Juravle77520bc2015-01-12 18:45:46 +00002163 bool CanDoImplicitNullCheck() const OVERRIDE {
2164 // TODO: Add implicit null checks in intrinsics.
2165 return !GetLocations()->Intrinsified();
2166 }
2167
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002168 uint32_t GetImtIndex() const { return imt_index_; }
2169 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2170
2171 DECLARE_INSTRUCTION(InvokeInterface);
2172
2173 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002174 const uint32_t imt_index_;
2175
2176 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2177};
2178
Dave Allison20dfc792014-06-16 20:44:29 -07002179class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002180 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002181 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002182 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2183 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002184 type_index_(type_index),
2185 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002186
2187 uint32_t GetDexPc() const { return dex_pc_; }
2188 uint16_t GetTypeIndex() const { return type_index_; }
2189
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002190 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002191 bool NeedsEnvironment() const OVERRIDE { return true; }
2192 // It may throw when called on:
2193 // - interfaces
2194 // - abstract/innaccessible/unknown classes
2195 // TODO: optimize when possible.
2196 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002197
Calin Juravle10e244f2015-01-26 18:54:32 +00002198 bool CanBeNull() const OVERRIDE { return false; }
2199
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002200 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2201
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002202 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002203
2204 private:
2205 const uint32_t dex_pc_;
2206 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002207 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002208
2209 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2210};
2211
Roland Levillain88cb1752014-10-20 16:36:47 +01002212class HNeg : public HUnaryOperation {
2213 public:
2214 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2215 : HUnaryOperation(result_type, input) {}
2216
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002217 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2218 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002219
Roland Levillain88cb1752014-10-20 16:36:47 +01002220 DECLARE_INSTRUCTION(Neg);
2221
2222 private:
2223 DISALLOW_COPY_AND_ASSIGN(HNeg);
2224};
2225
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002226class HNewArray : public HExpression<1> {
2227 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002228 HNewArray(HInstruction* length,
2229 uint32_t dex_pc,
2230 uint16_t type_index,
2231 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002232 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2233 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002234 type_index_(type_index),
2235 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002236 SetRawInputAt(0, length);
2237 }
2238
2239 uint32_t GetDexPc() const { return dex_pc_; }
2240 uint16_t GetTypeIndex() const { return type_index_; }
2241
2242 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002243 bool NeedsEnvironment() const OVERRIDE { return true; }
2244
2245 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002246
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002247 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2248
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002249 DECLARE_INSTRUCTION(NewArray);
2250
2251 private:
2252 const uint32_t dex_pc_;
2253 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002254 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002255
2256 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2257};
2258
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002259class HAdd : public HBinaryOperation {
2260 public:
2261 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2262 : HBinaryOperation(result_type, left, right) {}
2263
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002264 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002265
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002266 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002267 return x + y;
2268 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002269 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002270 return x + y;
2271 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002272
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002273 DECLARE_INSTRUCTION(Add);
2274
2275 private:
2276 DISALLOW_COPY_AND_ASSIGN(HAdd);
2277};
2278
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002279class HSub : public HBinaryOperation {
2280 public:
2281 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2282 : HBinaryOperation(result_type, left, right) {}
2283
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002284 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002285 return x - y;
2286 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002287 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002288 return x - y;
2289 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002290
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002291 DECLARE_INSTRUCTION(Sub);
2292
2293 private:
2294 DISALLOW_COPY_AND_ASSIGN(HSub);
2295};
2296
Calin Juravle34bacdf2014-10-07 20:23:36 +01002297class HMul : public HBinaryOperation {
2298 public:
2299 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2300 : HBinaryOperation(result_type, left, right) {}
2301
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002302 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002303
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002304 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2305 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002306
2307 DECLARE_INSTRUCTION(Mul);
2308
2309 private:
2310 DISALLOW_COPY_AND_ASSIGN(HMul);
2311};
2312
Calin Juravle7c4954d2014-10-28 16:57:40 +00002313class HDiv : public HBinaryOperation {
2314 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002315 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2316 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002317
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002318 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002319 // Our graph structure ensures we never have 0 for `y` during constant folding.
2320 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002321 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002322 return (y == -1) ? -x : x / y;
2323 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002324
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002325 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002326 DCHECK_NE(y, 0);
2327 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2328 return (y == -1) ? -x : x / y;
2329 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002330
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002331 uint32_t GetDexPc() const { return dex_pc_; }
2332
Calin Juravle7c4954d2014-10-28 16:57:40 +00002333 DECLARE_INSTRUCTION(Div);
2334
2335 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002336 const uint32_t dex_pc_;
2337
Calin Juravle7c4954d2014-10-28 16:57:40 +00002338 DISALLOW_COPY_AND_ASSIGN(HDiv);
2339};
2340
Calin Juravlebacfec32014-11-14 15:54:36 +00002341class HRem : public HBinaryOperation {
2342 public:
2343 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2344 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2345
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002346 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002347 DCHECK_NE(y, 0);
2348 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2349 return (y == -1) ? 0 : x % y;
2350 }
2351
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002352 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002353 DCHECK_NE(y, 0);
2354 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2355 return (y == -1) ? 0 : x % y;
2356 }
2357
2358 uint32_t GetDexPc() const { return dex_pc_; }
2359
2360 DECLARE_INSTRUCTION(Rem);
2361
2362 private:
2363 const uint32_t dex_pc_;
2364
2365 DISALLOW_COPY_AND_ASSIGN(HRem);
2366};
2367
Calin Juravled0d48522014-11-04 16:40:20 +00002368class HDivZeroCheck : public HExpression<1> {
2369 public:
2370 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2371 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2372 SetRawInputAt(0, value);
2373 }
2374
2375 bool CanBeMoved() const OVERRIDE { return true; }
2376
2377 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2378 UNUSED(other);
2379 return true;
2380 }
2381
2382 bool NeedsEnvironment() const OVERRIDE { return true; }
2383 bool CanThrow() const OVERRIDE { return true; }
2384
2385 uint32_t GetDexPc() const { return dex_pc_; }
2386
2387 DECLARE_INSTRUCTION(DivZeroCheck);
2388
2389 private:
2390 const uint32_t dex_pc_;
2391
2392 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2393};
2394
Calin Juravle9aec02f2014-11-18 23:06:35 +00002395class HShl : public HBinaryOperation {
2396 public:
2397 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2398 : HBinaryOperation(result_type, left, right) {}
2399
2400 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2401 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2402
2403 DECLARE_INSTRUCTION(Shl);
2404
2405 private:
2406 DISALLOW_COPY_AND_ASSIGN(HShl);
2407};
2408
2409class HShr : public HBinaryOperation {
2410 public:
2411 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2412 : HBinaryOperation(result_type, left, right) {}
2413
2414 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2415 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2416
2417 DECLARE_INSTRUCTION(Shr);
2418
2419 private:
2420 DISALLOW_COPY_AND_ASSIGN(HShr);
2421};
2422
2423class HUShr : public HBinaryOperation {
2424 public:
2425 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2426 : HBinaryOperation(result_type, left, right) {}
2427
2428 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2429 uint32_t ux = static_cast<uint32_t>(x);
2430 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2431 return static_cast<int32_t>(ux >> uy);
2432 }
2433
2434 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2435 uint64_t ux = static_cast<uint64_t>(x);
2436 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2437 return static_cast<int64_t>(ux >> uy);
2438 }
2439
2440 DECLARE_INSTRUCTION(UShr);
2441
2442 private:
2443 DISALLOW_COPY_AND_ASSIGN(HUShr);
2444};
2445
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002446class HAnd : public HBinaryOperation {
2447 public:
2448 HAnd(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(And);
2457
2458 private:
2459 DISALLOW_COPY_AND_ASSIGN(HAnd);
2460};
2461
2462class HOr : public HBinaryOperation {
2463 public:
2464 HOr(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(Or);
2473
2474 private:
2475 DISALLOW_COPY_AND_ASSIGN(HOr);
2476};
2477
2478class HXor : public HBinaryOperation {
2479 public:
2480 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2481 : HBinaryOperation(result_type, left, right) {}
2482
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002483 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002484
2485 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2486 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2487
2488 DECLARE_INSTRUCTION(Xor);
2489
2490 private:
2491 DISALLOW_COPY_AND_ASSIGN(HXor);
2492};
2493
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002494// The value of a parameter in this method. Its location depends on
2495// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002496class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002497 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002498 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2499 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002500
2501 uint8_t GetIndex() const { return index_; }
2502
Calin Juravle10e244f2015-01-26 18:54:32 +00002503 bool CanBeNull() const OVERRIDE { return !is_this_; }
2504
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002505 DECLARE_INSTRUCTION(ParameterValue);
2506
2507 private:
2508 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002509 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002510 const uint8_t index_;
2511
Calin Juravle10e244f2015-01-26 18:54:32 +00002512 // Whether or not the parameter value corresponds to 'this' argument.
2513 const bool is_this_;
2514
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002515 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2516};
2517
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002518class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002519 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002520 explicit HNot(Primitive::Type result_type, HInstruction* input)
2521 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002522
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002523 bool CanBeMoved() const OVERRIDE { return true; }
2524 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002525 UNUSED(other);
2526 return true;
2527 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002528
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002529 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2530 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002531
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002532 DECLARE_INSTRUCTION(Not);
2533
2534 private:
2535 DISALLOW_COPY_AND_ASSIGN(HNot);
2536};
2537
Roland Levillaindff1f282014-11-05 14:15:05 +00002538class HTypeConversion : public HExpression<1> {
2539 public:
2540 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002541 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2542 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002543 SetRawInputAt(0, input);
2544 DCHECK_NE(input->GetType(), result_type);
2545 }
2546
2547 HInstruction* GetInput() const { return InputAt(0); }
2548 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2549 Primitive::Type GetResultType() const { return GetType(); }
2550
Roland Levillain624279f2014-12-04 11:54:28 +00002551 // Required by the x86 and ARM code generators when producing calls
2552 // to the runtime.
2553 uint32_t GetDexPc() const { return dex_pc_; }
2554
Roland Levillaindff1f282014-11-05 14:15:05 +00002555 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002556 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002557
2558 DECLARE_INSTRUCTION(TypeConversion);
2559
2560 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002561 const uint32_t dex_pc_;
2562
Roland Levillaindff1f282014-11-05 14:15:05 +00002563 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2564};
2565
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002566static constexpr uint32_t kNoRegNumber = -1;
2567
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002568class HPhi : public HInstruction {
2569 public:
2570 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002571 : HInstruction(SideEffects::None()),
2572 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002573 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002574 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002575 is_live_(false),
2576 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002577 inputs_.SetSize(number_of_inputs);
2578 }
2579
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002580 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2581 static Primitive::Type ToPhiType(Primitive::Type type) {
2582 switch (type) {
2583 case Primitive::kPrimBoolean:
2584 case Primitive::kPrimByte:
2585 case Primitive::kPrimShort:
2586 case Primitive::kPrimChar:
2587 return Primitive::kPrimInt;
2588 default:
2589 return type;
2590 }
2591 }
2592
Calin Juravle10e244f2015-01-26 18:54:32 +00002593 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002594
2595 void AddInput(HInstruction* input);
2596
Calin Juravle10e244f2015-01-26 18:54:32 +00002597 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002598 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002599
Calin Juravle10e244f2015-01-26 18:54:32 +00002600 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2601 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2602
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002603 uint32_t GetRegNumber() const { return reg_number_; }
2604
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002605 void SetDead() { is_live_ = false; }
2606 void SetLive() { is_live_ = true; }
2607 bool IsDead() const { return !is_live_; }
2608 bool IsLive() const { return is_live_; }
2609
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002610 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002611
David Brazdil1abb4192015-02-17 18:33:36 +00002612 protected:
2613 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2614
2615 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2616 inputs_.Put(index, input);
2617 }
2618
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002619 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002620 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002621 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002622 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002623 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002624 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002625
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002626 DISALLOW_COPY_AND_ASSIGN(HPhi);
2627};
2628
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002629class HNullCheck : public HExpression<1> {
2630 public:
2631 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002632 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002633 SetRawInputAt(0, value);
2634 }
2635
Calin Juravle10e244f2015-01-26 18:54:32 +00002636 bool CanBeMoved() const OVERRIDE { return true; }
2637 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002638 UNUSED(other);
2639 return true;
2640 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002641
Calin Juravle10e244f2015-01-26 18:54:32 +00002642 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002643
Calin Juravle10e244f2015-01-26 18:54:32 +00002644 bool CanThrow() const OVERRIDE { return true; }
2645
2646 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002647
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002648 uint32_t GetDexPc() const { return dex_pc_; }
2649
2650 DECLARE_INSTRUCTION(NullCheck);
2651
2652 private:
2653 const uint32_t dex_pc_;
2654
2655 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2656};
2657
2658class FieldInfo : public ValueObject {
2659 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002660 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2661 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002662
2663 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002664 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002665 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002666
2667 private:
2668 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002669 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002670 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002671};
2672
2673class HInstanceFieldGet : public HExpression<1> {
2674 public:
2675 HInstanceFieldGet(HInstruction* value,
2676 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002677 MemberOffset field_offset,
2678 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002679 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002680 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002681 SetRawInputAt(0, value);
2682 }
2683
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002684 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002685
2686 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2687 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2688 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002689 }
2690
Calin Juravle77520bc2015-01-12 18:45:46 +00002691 bool CanDoImplicitNullCheck() const OVERRIDE {
2692 return GetFieldOffset().Uint32Value() < kPageSize;
2693 }
2694
2695 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002696 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2697 }
2698
Calin Juravle52c48962014-12-16 17:02:57 +00002699 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002700 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002701 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002702 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002703
2704 DECLARE_INSTRUCTION(InstanceFieldGet);
2705
2706 private:
2707 const FieldInfo field_info_;
2708
2709 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2710};
2711
2712class HInstanceFieldSet : public HTemplateInstruction<2> {
2713 public:
2714 HInstanceFieldSet(HInstruction* object,
2715 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002716 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002717 MemberOffset field_offset,
2718 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002719 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002720 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002721 SetRawInputAt(0, object);
2722 SetRawInputAt(1, value);
2723 }
2724
Calin Juravle77520bc2015-01-12 18:45:46 +00002725 bool CanDoImplicitNullCheck() const OVERRIDE {
2726 return GetFieldOffset().Uint32Value() < kPageSize;
2727 }
2728
Calin Juravle52c48962014-12-16 17:02:57 +00002729 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002730 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002731 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002732 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002733 HInstruction* GetValue() const { return InputAt(1); }
2734
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002735 DECLARE_INSTRUCTION(InstanceFieldSet);
2736
2737 private:
2738 const FieldInfo field_info_;
2739
2740 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2741};
2742
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002743class HArrayGet : public HExpression<2> {
2744 public:
2745 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002746 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002747 SetRawInputAt(0, array);
2748 SetRawInputAt(1, index);
2749 }
2750
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002751 bool CanBeMoved() const OVERRIDE { return true; }
2752 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002753 UNUSED(other);
2754 return true;
2755 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002756 bool CanDoImplicitNullCheck() const OVERRIDE {
2757 // TODO: We can be smarter here.
2758 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2759 // which generates the implicit null check. There are cases when these can be removed
2760 // to produce better code. If we ever add optimizations to do so we should allow an
2761 // implicit check here (as long as the address falls in the first page).
2762 return false;
2763 }
2764
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002765 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002766
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002767 HInstruction* GetArray() const { return InputAt(0); }
2768 HInstruction* GetIndex() const { return InputAt(1); }
2769
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002770 DECLARE_INSTRUCTION(ArrayGet);
2771
2772 private:
2773 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2774};
2775
2776class HArraySet : public HTemplateInstruction<3> {
2777 public:
2778 HArraySet(HInstruction* array,
2779 HInstruction* index,
2780 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002781 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002782 uint32_t dex_pc)
2783 : HTemplateInstruction(SideEffects::ChangesSomething()),
2784 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002785 expected_component_type_(expected_component_type),
2786 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002787 SetRawInputAt(0, array);
2788 SetRawInputAt(1, index);
2789 SetRawInputAt(2, value);
2790 }
2791
Calin Juravle77520bc2015-01-12 18:45:46 +00002792 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002793 // We currently always call a runtime method to catch array store
2794 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002795 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002796 }
2797
Calin Juravle77520bc2015-01-12 18:45:46 +00002798 bool CanDoImplicitNullCheck() const OVERRIDE {
2799 // TODO: Same as for ArrayGet.
2800 return false;
2801 }
2802
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002803 void ClearNeedsTypeCheck() {
2804 needs_type_check_ = false;
2805 }
2806
2807 bool NeedsTypeCheck() const { return needs_type_check_; }
2808
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002809 uint32_t GetDexPc() const { return dex_pc_; }
2810
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002811 HInstruction* GetArray() const { return InputAt(0); }
2812 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002813 HInstruction* GetValue() const { return InputAt(2); }
2814
2815 Primitive::Type GetComponentType() const {
2816 // The Dex format does not type floating point index operations. Since the
2817 // `expected_component_type_` is set during building and can therefore not
2818 // be correct, we also check what is the value type. If it is a floating
2819 // point type, we must use that type.
2820 Primitive::Type value_type = GetValue()->GetType();
2821 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2822 ? value_type
2823 : expected_component_type_;
2824 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002825
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002826 DECLARE_INSTRUCTION(ArraySet);
2827
2828 private:
2829 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002830 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002831 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002832
2833 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2834};
2835
2836class HArrayLength : public HExpression<1> {
2837 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002838 explicit HArrayLength(HInstruction* array)
2839 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2840 // Note that arrays do not change length, so the instruction does not
2841 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002842 SetRawInputAt(0, array);
2843 }
2844
Calin Juravle77520bc2015-01-12 18:45:46 +00002845 bool CanBeMoved() const OVERRIDE { return true; }
2846 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002847 UNUSED(other);
2848 return true;
2849 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002850 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002851
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002852 DECLARE_INSTRUCTION(ArrayLength);
2853
2854 private:
2855 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2856};
2857
2858class HBoundsCheck : public HExpression<2> {
2859 public:
2860 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002861 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002862 DCHECK(index->GetType() == Primitive::kPrimInt);
2863 SetRawInputAt(0, index);
2864 SetRawInputAt(1, length);
2865 }
2866
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002867 bool CanBeMoved() const OVERRIDE { return true; }
2868 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002869 UNUSED(other);
2870 return true;
2871 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002872
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002873 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002874
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002875 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002876
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002877 uint32_t GetDexPc() const { return dex_pc_; }
2878
2879 DECLARE_INSTRUCTION(BoundsCheck);
2880
2881 private:
2882 const uint32_t dex_pc_;
2883
2884 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2885};
2886
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002887/**
2888 * Some DEX instructions are folded into multiple HInstructions that need
2889 * to stay live until the last HInstruction. This class
2890 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002891 * HInstruction stays live. `index` represents the stack location index of the
2892 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002893 */
2894class HTemporary : public HTemplateInstruction<0> {
2895 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002896 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002897
2898 size_t GetIndex() const { return index_; }
2899
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002900 Primitive::Type GetType() const OVERRIDE {
2901 // The previous instruction is the one that will be stored in the temporary location.
2902 DCHECK(GetPrevious() != nullptr);
2903 return GetPrevious()->GetType();
2904 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002905
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002906 DECLARE_INSTRUCTION(Temporary);
2907
2908 private:
2909 const size_t index_;
2910
2911 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2912};
2913
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002914class HSuspendCheck : public HTemplateInstruction<0> {
2915 public:
2916 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002917 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002918
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002919 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002920 return true;
2921 }
2922
2923 uint32_t GetDexPc() const { return dex_pc_; }
2924
2925 DECLARE_INSTRUCTION(SuspendCheck);
2926
2927 private:
2928 const uint32_t dex_pc_;
2929
2930 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2931};
2932
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002933/**
2934 * Instruction to load a Class object.
2935 */
2936class HLoadClass : public HExpression<0> {
2937 public:
2938 HLoadClass(uint16_t type_index,
2939 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002940 uint32_t dex_pc)
2941 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2942 type_index_(type_index),
2943 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002944 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00002945 generate_clinit_check_(false),
2946 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002947
2948 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002949
2950 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2951 return other->AsLoadClass()->type_index_ == type_index_;
2952 }
2953
2954 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2955
2956 uint32_t GetDexPc() const { return dex_pc_; }
2957 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002958 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002959
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002960 bool NeedsEnvironment() const OVERRIDE {
2961 // Will call runtime and load the class if the class is not loaded yet.
2962 // TODO: finer grain decision.
2963 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002964 }
2965
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002966 bool MustGenerateClinitCheck() const {
2967 return generate_clinit_check_;
2968 }
2969
2970 void SetMustGenerateClinitCheck() {
2971 generate_clinit_check_ = true;
2972 }
2973
2974 bool CanCallRuntime() const {
2975 return MustGenerateClinitCheck() || !is_referrers_class_;
2976 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002977
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002978 bool CanThrow() const OVERRIDE {
2979 // May call runtime and and therefore can throw.
2980 // TODO: finer grain decision.
2981 return !is_referrers_class_;
2982 }
2983
Calin Juravleacf735c2015-02-12 15:25:22 +00002984 ReferenceTypeInfo GetLoadedClassRTI() {
2985 return loaded_class_rti_;
2986 }
2987
2988 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
2989 // Make sure we only set exact types (the loaded class should never be merged).
2990 DCHECK(rti.IsExact());
2991 loaded_class_rti_ = rti;
2992 }
2993
2994 bool IsResolved() {
2995 return loaded_class_rti_.IsExact();
2996 }
2997
Nicolas Geoffray7e4c3502015-03-18 11:00:52 +00002998 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
2999
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003000 DECLARE_INSTRUCTION(LoadClass);
3001
3002 private:
3003 const uint16_t type_index_;
3004 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003005 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003006 // Whether this instruction must generate the initialization check.
3007 // Used for code generation.
3008 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003009
Calin Juravleacf735c2015-02-12 15:25:22 +00003010 ReferenceTypeInfo loaded_class_rti_;
3011
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003012 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3013};
3014
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003015class HLoadString : public HExpression<0> {
3016 public:
3017 HLoadString(uint32_t string_index, uint32_t dex_pc)
3018 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3019 string_index_(string_index),
3020 dex_pc_(dex_pc) {}
3021
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003022 bool CanBeMoved() const OVERRIDE { return true; }
3023
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003024 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3025 return other->AsLoadString()->string_index_ == string_index_;
3026 }
3027
3028 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3029
3030 uint32_t GetDexPc() const { return dex_pc_; }
3031 uint32_t GetStringIndex() const { return string_index_; }
3032
3033 // TODO: Can we deopt or debug when we resolve a string?
3034 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray7e4c3502015-03-18 11:00:52 +00003035 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003036
3037 DECLARE_INSTRUCTION(LoadString);
3038
3039 private:
3040 const uint32_t string_index_;
3041 const uint32_t dex_pc_;
3042
3043 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3044};
3045
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003046// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003047/**
3048 * Performs an initialization check on its Class object input.
3049 */
3050class HClinitCheck : public HExpression<1> {
3051 public:
3052 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
3053 : HExpression(Primitive::kPrimNot, SideEffects::All()),
3054 dex_pc_(dex_pc) {
3055 SetRawInputAt(0, constant);
3056 }
3057
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003058 bool CanBeMoved() const OVERRIDE { return true; }
3059 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3060 UNUSED(other);
3061 return true;
3062 }
3063
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003064 bool NeedsEnvironment() const OVERRIDE {
3065 // May call runtime to initialize the class.
3066 return true;
3067 }
3068
3069 uint32_t GetDexPc() const { return dex_pc_; }
3070
3071 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3072
3073 DECLARE_INSTRUCTION(ClinitCheck);
3074
3075 private:
3076 const uint32_t dex_pc_;
3077
3078 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3079};
3080
3081class HStaticFieldGet : public HExpression<1> {
3082 public:
3083 HStaticFieldGet(HInstruction* cls,
3084 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003085 MemberOffset field_offset,
3086 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003087 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003088 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003089 SetRawInputAt(0, cls);
3090 }
3091
Calin Juravle52c48962014-12-16 17:02:57 +00003092
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003093 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003094
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003095 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003096 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3097 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003098 }
3099
3100 size_t ComputeHashCode() const OVERRIDE {
3101 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3102 }
3103
Calin Juravle52c48962014-12-16 17:02:57 +00003104 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003105 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3106 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003107 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003108
3109 DECLARE_INSTRUCTION(StaticFieldGet);
3110
3111 private:
3112 const FieldInfo field_info_;
3113
3114 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3115};
3116
3117class HStaticFieldSet : public HTemplateInstruction<2> {
3118 public:
3119 HStaticFieldSet(HInstruction* cls,
3120 HInstruction* value,
3121 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003122 MemberOffset field_offset,
3123 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003124 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003125 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003126 SetRawInputAt(0, cls);
3127 SetRawInputAt(1, value);
3128 }
3129
Calin Juravle52c48962014-12-16 17:02:57 +00003130 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003131 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3132 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003133 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003134
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003135 HInstruction* GetValue() const { return InputAt(1); }
3136
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003137 DECLARE_INSTRUCTION(StaticFieldSet);
3138
3139 private:
3140 const FieldInfo field_info_;
3141
3142 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3143};
3144
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003145// Implement the move-exception DEX instruction.
3146class HLoadException : public HExpression<0> {
3147 public:
3148 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3149
3150 DECLARE_INSTRUCTION(LoadException);
3151
3152 private:
3153 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3154};
3155
3156class HThrow : public HTemplateInstruction<1> {
3157 public:
3158 HThrow(HInstruction* exception, uint32_t dex_pc)
3159 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3160 SetRawInputAt(0, exception);
3161 }
3162
3163 bool IsControlFlow() const OVERRIDE { return true; }
3164
3165 bool NeedsEnvironment() const OVERRIDE { return true; }
3166
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003167 bool CanThrow() const OVERRIDE { return true; }
3168
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003169 uint32_t GetDexPc() const { return dex_pc_; }
3170
3171 DECLARE_INSTRUCTION(Throw);
3172
3173 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003174 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003175
3176 DISALLOW_COPY_AND_ASSIGN(HThrow);
3177};
3178
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003179class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003180 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003181 HInstanceOf(HInstruction* object,
3182 HLoadClass* constant,
3183 bool class_is_final,
3184 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003185 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3186 class_is_final_(class_is_final),
3187 dex_pc_(dex_pc) {
3188 SetRawInputAt(0, object);
3189 SetRawInputAt(1, constant);
3190 }
3191
3192 bool CanBeMoved() const OVERRIDE { return true; }
3193
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003194 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003195 return true;
3196 }
3197
3198 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003199 return false;
3200 }
3201
3202 uint32_t GetDexPc() const { return dex_pc_; }
3203
3204 bool IsClassFinal() const { return class_is_final_; }
3205
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003206 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003207
3208 private:
3209 const bool class_is_final_;
3210 const uint32_t dex_pc_;
3211
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003212 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3213};
3214
Calin Juravleb1498f62015-02-16 13:13:29 +00003215class HBoundType : public HExpression<1> {
3216 public:
3217 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3218 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3219 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003220 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003221 SetRawInputAt(0, input);
3222 }
3223
3224 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3225
3226 bool CanBeNull() const OVERRIDE {
3227 // `null instanceof ClassX` always return false so we can't be null.
3228 return false;
3229 }
3230
3231 DECLARE_INSTRUCTION(BoundType);
3232
3233 private:
3234 // Encodes the most upper class that this instruction can have. In other words
3235 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3236 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3237 const ReferenceTypeInfo bound_type_;
3238
3239 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3240};
3241
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003242class HCheckCast : public HTemplateInstruction<2> {
3243 public:
3244 HCheckCast(HInstruction* object,
3245 HLoadClass* constant,
3246 bool class_is_final,
3247 uint32_t dex_pc)
3248 : HTemplateInstruction(SideEffects::None()),
3249 class_is_final_(class_is_final),
3250 dex_pc_(dex_pc) {
3251 SetRawInputAt(0, object);
3252 SetRawInputAt(1, constant);
3253 }
3254
3255 bool CanBeMoved() const OVERRIDE { return true; }
3256
3257 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3258 return true;
3259 }
3260
3261 bool NeedsEnvironment() const OVERRIDE {
3262 // Instruction may throw a CheckCastError.
3263 return true;
3264 }
3265
3266 bool CanThrow() const OVERRIDE { return true; }
3267
3268 uint32_t GetDexPc() const { return dex_pc_; }
3269
3270 bool IsClassFinal() const { return class_is_final_; }
3271
3272 DECLARE_INSTRUCTION(CheckCast);
3273
3274 private:
3275 const bool class_is_final_;
3276 const uint32_t dex_pc_;
3277
3278 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003279};
3280
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003281class HMonitorOperation : public HTemplateInstruction<1> {
3282 public:
3283 enum OperationKind {
3284 kEnter,
3285 kExit,
3286 };
3287
3288 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3289 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3290 SetRawInputAt(0, object);
3291 }
3292
3293 // Instruction may throw a Java exception, so we need an environment.
3294 bool NeedsEnvironment() const OVERRIDE { return true; }
3295 bool CanThrow() const OVERRIDE { return true; }
3296
3297 uint32_t GetDexPc() const { return dex_pc_; }
3298
3299 bool IsEnter() const { return kind_ == kEnter; }
3300
3301 DECLARE_INSTRUCTION(MonitorOperation);
3302
Calin Juravle52c48962014-12-16 17:02:57 +00003303 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003304 const OperationKind kind_;
3305 const uint32_t dex_pc_;
3306
3307 private:
3308 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3309};
3310
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003311class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003312 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003313 MoveOperands(Location source, Location destination, HInstruction* instruction)
3314 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003315
3316 Location GetSource() const { return source_; }
3317 Location GetDestination() const { return destination_; }
3318
3319 void SetSource(Location value) { source_ = value; }
3320 void SetDestination(Location value) { destination_ = value; }
3321
3322 // The parallel move resolver marks moves as "in-progress" by clearing the
3323 // destination (but not the source).
3324 Location MarkPending() {
3325 DCHECK(!IsPending());
3326 Location dest = destination_;
3327 destination_ = Location::NoLocation();
3328 return dest;
3329 }
3330
3331 void ClearPending(Location dest) {
3332 DCHECK(IsPending());
3333 destination_ = dest;
3334 }
3335
3336 bool IsPending() const {
3337 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3338 return destination_.IsInvalid() && !source_.IsInvalid();
3339 }
3340
3341 // True if this blocks a move from the given location.
3342 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003343 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003344 }
3345
3346 // A move is redundant if it's been eliminated, if its source and
3347 // destination are the same, or if its destination is unneeded.
3348 bool IsRedundant() const {
3349 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3350 }
3351
3352 // We clear both operands to indicate move that's been eliminated.
3353 void Eliminate() {
3354 source_ = destination_ = Location::NoLocation();
3355 }
3356
3357 bool IsEliminated() const {
3358 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3359 return source_.IsInvalid();
3360 }
3361
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003362 HInstruction* GetInstruction() const { return instruction_; }
3363
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003364 private:
3365 Location source_;
3366 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003367 // The instruction this move is assocatied with. Null when this move is
3368 // for moving an input in the expected locations of user (including a phi user).
3369 // This is only used in debug mode, to ensure we do not connect interval siblings
3370 // in the same parallel move.
3371 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003372};
3373
3374static constexpr size_t kDefaultNumberOfMoves = 4;
3375
3376class HParallelMove : public HTemplateInstruction<0> {
3377 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003378 explicit HParallelMove(ArenaAllocator* arena)
3379 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003380
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003381 void AddMove(Location source, Location destination, HInstruction* instruction) {
3382 DCHECK(source.IsValid());
3383 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003384 if (kIsDebugBuild) {
3385 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003386 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003387 if (moves_.Get(i).GetInstruction() == instruction) {
3388 // Special case the situation where the move is for the spill slot
3389 // of the instruction.
3390 if ((GetPrevious() == instruction)
3391 || ((GetPrevious() == nullptr)
3392 && instruction->IsPhi()
3393 && instruction->GetBlock() == GetBlock())) {
3394 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3395 << "Doing parallel moves for the same instruction.";
3396 } else {
3397 DCHECK(false) << "Doing parallel moves for the same instruction.";
3398 }
3399 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003400 }
3401 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003402 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3403 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3404 << "Same destination for two moves in a parallel move.";
3405 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003406 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003407 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003408 }
3409
3410 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003411 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003412 }
3413
3414 size_t NumMoves() const { return moves_.Size(); }
3415
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003416 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003417
3418 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003419 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003420
3421 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3422};
3423
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003424class HGraphVisitor : public ValueObject {
3425 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003426 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3427 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003428
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003429 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003430 virtual void VisitBasicBlock(HBasicBlock* block);
3431
Roland Levillain633021e2014-10-01 14:12:25 +01003432 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003433 void VisitInsertionOrder();
3434
Roland Levillain633021e2014-10-01 14:12:25 +01003435 // Visit the graph following dominator tree reverse post-order.
3436 void VisitReversePostOrder();
3437
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003438 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003439
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003440 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003441#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003442 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3443
3444 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3445
3446#undef DECLARE_VISIT_INSTRUCTION
3447
3448 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003449 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003450
3451 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3452};
3453
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003454class HGraphDelegateVisitor : public HGraphVisitor {
3455 public:
3456 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3457 virtual ~HGraphDelegateVisitor() {}
3458
3459 // Visit functions that delegate to to super class.
3460#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003461 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003462
3463 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3464
3465#undef DECLARE_VISIT_INSTRUCTION
3466
3467 private:
3468 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3469};
3470
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003471class HInsertionOrderIterator : public ValueObject {
3472 public:
3473 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3474
3475 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3476 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3477 void Advance() { ++index_; }
3478
3479 private:
3480 const HGraph& graph_;
3481 size_t index_;
3482
3483 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3484};
3485
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003486class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003487 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003488 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003489
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003490 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3491 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
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(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003499};
3500
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003501class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003502 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003503 explicit HPostOrderIterator(const HGraph& graph)
3504 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003505
3506 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003507 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003508 void Advance() { --index_; }
3509
3510 private:
3511 const HGraph& graph_;
3512 size_t index_;
3513
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003514 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003515};
3516
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003517// Iterator over the blocks that art part of the loop. Includes blocks part
3518// of an inner loop. The order in which the blocks are iterated is on their
3519// block id.
3520class HBlocksInLoopIterator : public ValueObject {
3521 public:
3522 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3523 : blocks_in_loop_(info.GetBlocks()),
3524 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3525 index_(0) {
3526 if (!blocks_in_loop_.IsBitSet(index_)) {
3527 Advance();
3528 }
3529 }
3530
3531 bool Done() const { return index_ == blocks_.Size(); }
3532 HBasicBlock* Current() const { return blocks_.Get(index_); }
3533 void Advance() {
3534 ++index_;
3535 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3536 if (blocks_in_loop_.IsBitSet(index_)) {
3537 break;
3538 }
3539 }
3540 }
3541
3542 private:
3543 const BitVector& blocks_in_loop_;
3544 const GrowableArray<HBasicBlock*>& blocks_;
3545 size_t index_;
3546
3547 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3548};
3549
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003550inline int64_t Int64FromConstant(HConstant* constant) {
3551 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3552 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3553 : constant->AsLongConstant()->GetValue();
3554}
3555
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003556} // namespace art
3557
3558#endif // ART_COMPILER_OPTIMIZING_NODES_H_