blob: a35fa1d8c3657e6f4ae02044805d01d0b207d505 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Mathieu Chartierb666f482015-02-18 14:33:14 -080020#include "base/arena_object.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000021#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000022#include "handle.h"
23#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000024#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010025#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000026#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010027#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000029#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "utils/growable_array.h"
31
32namespace art {
33
David Brazdil1abb4192015-02-17 18:33:36 +000034class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010036class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000038class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000039class HInvoke;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040class HGraphVisitor;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000041class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010042class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010043class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010044class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000045class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046
47static const int kDefaultNumberOfBlocks = 8;
48static const int kDefaultNumberOfSuccessors = 2;
49static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010050static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000051static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
Calin Juravle9aec02f2014-11-18 23:06:35 +000053static constexpr uint32_t kMaxIntShiftValue = 0x1f;
54static constexpr uint64_t kMaxLongShiftValue = 0x3f;
55
Dave Allison20dfc792014-06-16 20:44:29 -070056enum IfCondition {
57 kCondEQ,
58 kCondNE,
59 kCondLT,
60 kCondLE,
61 kCondGT,
62 kCondGE,
63};
64
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010065class HInstructionList {
66 public:
67 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
68
69 void AddInstruction(HInstruction* instruction);
70 void RemoveInstruction(HInstruction* instruction);
71
Roland Levillain6b469232014-09-25 10:10:38 +010072 // Return true if this list contains `instruction`.
73 bool Contains(HInstruction* instruction) const;
74
Roland Levillainccc07a92014-09-16 14:48:16 +010075 // Return true if `instruction1` is found before `instruction2` in
76 // this instruction list and false otherwise. Abort if none
77 // of these instructions is found.
78 bool FoundBefore(const HInstruction* instruction1,
79 const HInstruction* instruction2) const;
80
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000081 bool IsEmpty() const { return first_instruction_ == nullptr; }
82 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
83
84 // Update the block of all instructions to be `block`.
85 void SetBlockOfInstructions(HBasicBlock* block) const;
86
87 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
88 void Add(const HInstructionList& instruction_list);
89
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010090 private:
91 HInstruction* first_instruction_;
92 HInstruction* last_instruction_;
93
94 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000095 friend class HGraph;
96 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010097 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010098 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010099
100 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
101};
102
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000103// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700104class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000105 public:
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000106 HGraph(ArenaAllocator* arena, bool debuggable = false, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000107 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000108 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100109 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700110 entry_block_(nullptr),
111 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100112 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100113 number_of_vregs_(0),
114 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000115 temporaries_vreg_slots_(0),
Mingyao Yange4335eb2015-03-02 15:14:13 -0800116 has_array_accesses_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000117 debuggable_(debuggable),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000118 current_instruction_id_(start_instruction_id) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000119
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000120 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100121 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100122 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000123
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000124 HBasicBlock* GetEntryBlock() const { return entry_block_; }
125 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000126
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000127 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
128 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000129
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000130 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100131
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000132 // Try building the SSA form of this graph, with dominance computation and loop
133 // recognition. Returns whether it was successful in doing all these steps.
134 bool TryBuildingSsa() {
135 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000136 // The SSA builder requires loops to all be natural. Specifically, the dead phi
137 // elimination phase checks the consistency of the graph when doing a post-order
138 // visit for eliminating dead phis: a dead phi can only have loop header phi
139 // users remaining when being visited.
140 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000141 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000142 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000143 }
144
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000145 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000146 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100147 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000148
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000149 // Analyze all natural loops in this graph. Returns false if one
150 // loop is not natural, that is the header does not dominate the
151 // back edge.
152 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100153
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000154 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
155 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
156
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100157 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
158 void SimplifyLoop(HBasicBlock* header);
159
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 int32_t GetNextInstructionId() {
161 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000162 return current_instruction_id_++;
163 }
164
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000165 int32_t GetCurrentInstructionId() const {
166 return current_instruction_id_;
167 }
168
169 void SetCurrentInstructionId(int32_t id) {
170 current_instruction_id_ = id;
171 }
172
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100173 uint16_t GetMaximumNumberOfOutVRegs() const {
174 return maximum_number_of_out_vregs_;
175 }
176
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
178 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100179 }
180
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000181 void UpdateTemporariesVRegSlots(size_t slots) {
182 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100183 }
184
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000185 size_t GetTemporariesVRegSlots() const {
186 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100187 }
188
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100189 void SetNumberOfVRegs(uint16_t number_of_vregs) {
190 number_of_vregs_ = number_of_vregs;
191 }
192
193 uint16_t GetNumberOfVRegs() const {
194 return number_of_vregs_;
195 }
196
197 void SetNumberOfInVRegs(uint16_t value) {
198 number_of_in_vregs_ = value;
199 }
200
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100201 uint16_t GetNumberOfLocalVRegs() const {
202 return number_of_vregs_ - number_of_in_vregs_;
203 }
204
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100205 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
206 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100207 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100208
Mingyao Yange4335eb2015-03-02 15:14:13 -0800209 bool HasArrayAccesses() const {
210 return has_array_accesses_;
211 }
212
213 void SetHasArrayAccesses(bool value) {
214 has_array_accesses_ = value;
215 }
216
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000217 bool IsDebuggable() const { return debuggable_; }
218
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000219 HNullConstant* GetNullConstant();
220
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000221 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000222 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
223 void VisitBlockForDominatorTree(HBasicBlock* block,
224 HBasicBlock* predecessor,
225 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100226 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000227 void VisitBlockForBackEdges(HBasicBlock* block,
228 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100229 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000230 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000231 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
232
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000233 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000234
235 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000236 GrowableArray<HBasicBlock*> blocks_;
237
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100238 // List of blocks to perform a reverse post order tree traversal.
239 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000240
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000241 HBasicBlock* entry_block_;
242 HBasicBlock* exit_block_;
243
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100244 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100245 uint16_t maximum_number_of_out_vregs_;
246
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100247 // The number of virtual registers in this method. Contains the parameters.
248 uint16_t number_of_vregs_;
249
250 // The number of virtual registers used by parameters of this method.
251 uint16_t number_of_in_vregs_;
252
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000253 // Number of vreg size slots that the temporaries use (used in baseline compiler).
254 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100255
Mingyao Yange4335eb2015-03-02 15:14:13 -0800256 // Has array accesses. We can totally skip BCE if it's false.
257 bool has_array_accesses_;
258
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000259 // Indicates whether the graph should be compiled in a way that
260 // ensures full debuggability. If false, we can apply more
261 // aggressive optimizations that may limit the level of debugging.
262 const bool debuggable_;
263
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000264 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000265 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000266
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000267 // Cached null constant that might be created when building SSA form.
268 HNullConstant* cached_null_constant_;
269
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000270 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000271 DISALLOW_COPY_AND_ASSIGN(HGraph);
272};
273
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700274class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000275 public:
276 HLoopInformation(HBasicBlock* header, HGraph* graph)
277 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100278 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100279 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100280 // Make bit vector growable, as the number of blocks may change.
281 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100282
283 HBasicBlock* GetHeader() const {
284 return header_;
285 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000286
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000287 void SetHeader(HBasicBlock* block) {
288 header_ = block;
289 }
290
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100291 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
292 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
293 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
294
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000295 void AddBackEdge(HBasicBlock* back_edge) {
296 back_edges_.Add(back_edge);
297 }
298
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100299 void RemoveBackEdge(HBasicBlock* back_edge) {
300 back_edges_.Delete(back_edge);
301 }
302
303 bool IsBackEdge(HBasicBlock* block) {
304 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
305 if (back_edges_.Get(i) == block) return true;
306 }
307 return false;
308 }
309
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000310 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000311 return back_edges_.Size();
312 }
313
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100314 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100315
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100316 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
317 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100318 }
319
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100320 void ClearBackEdges() {
321 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100322 }
323
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100324 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
325 // that is the header dominates the back edge.
326 bool Populate();
327
328 // Returns whether this loop information contains `block`.
329 // Note that this loop information *must* be populated before entering this function.
330 bool Contains(const HBasicBlock& block) const;
331
332 // Returns whether this loop information is an inner loop of `other`.
333 // Note that `other` *must* be populated before entering this function.
334 bool IsIn(const HLoopInformation& other) const;
335
336 const ArenaBitVector& GetBlocks() const { return blocks_; }
337
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000338 void Add(HBasicBlock* block);
339
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000340 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100341 // Internal recursive implementation of `Populate`.
342 void PopulateRecursive(HBasicBlock* block);
343
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000344 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100345 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000346 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100347 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000348
349 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
350};
351
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100352static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100353static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100354
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355// A block in a method. Contains the list of instructions represented
356// as a double linked list. Each block knows its predecessors and
357// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100358
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700359class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000360 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100361 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000362 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000363 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
364 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000365 loop_information_(nullptr),
366 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100367 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100368 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100369 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100370 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000371 lifetime_end_(kNoLifetime),
372 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100374 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
375 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000376 }
377
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100378 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
379 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000380 }
381
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100382 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
383 return dominated_blocks_;
384 }
385
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100386 bool IsEntryBlock() const {
387 return graph_->GetEntryBlock() == this;
388 }
389
390 bool IsExitBlock() const {
391 return graph_->GetExitBlock() == this;
392 }
393
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000394 void AddBackEdge(HBasicBlock* back_edge) {
395 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000396 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000397 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100398 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000399 loop_information_->AddBackEdge(back_edge);
400 }
401
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000402 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000403 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000404
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000405 int GetBlockId() const { return block_id_; }
406 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000407
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000408 HBasicBlock* GetDominator() const { return dominator_; }
409 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100410 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000411 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
412 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
413 if (dominated_blocks_.Get(i) == existing) {
414 dominated_blocks_.Put(i, new_block);
415 return;
416 }
417 }
418 LOG(FATAL) << "Unreachable";
419 UNREACHABLE();
420 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000421
422 int NumberOfBackEdges() const {
423 return loop_information_ == nullptr
424 ? 0
425 : loop_information_->NumberOfBackEdges();
426 }
427
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100428 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
429 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100430 const HInstructionList& GetInstructions() const { return instructions_; }
431 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100432 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000433
434 void AddSuccessor(HBasicBlock* block) {
435 successors_.Add(block);
436 block->predecessors_.Add(this);
437 }
438
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100439 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
440 size_t successor_index = GetSuccessorIndexOf(existing);
441 DCHECK_NE(successor_index, static_cast<size_t>(-1));
442 existing->RemovePredecessor(this);
443 new_block->predecessors_.Add(this);
444 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000445 }
446
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000447 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
448 size_t predecessor_index = GetPredecessorIndexOf(existing);
449 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
450 existing->RemoveSuccessor(this);
451 new_block->successors_.Add(this);
452 predecessors_.Put(predecessor_index, new_block);
453 }
454
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100455 void RemovePredecessor(HBasicBlock* block) {
456 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100457 }
458
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000459 void RemoveSuccessor(HBasicBlock* block) {
460 successors_.Delete(block);
461 }
462
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100463 void ClearAllPredecessors() {
464 predecessors_.Reset();
465 }
466
467 void AddPredecessor(HBasicBlock* block) {
468 predecessors_.Add(block);
469 block->successors_.Add(this);
470 }
471
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100472 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100473 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100474 HBasicBlock* temp = predecessors_.Get(0);
475 predecessors_.Put(0, predecessors_.Get(1));
476 predecessors_.Put(1, temp);
477 }
478
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100479 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
480 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
481 if (predecessors_.Get(i) == predecessor) {
482 return i;
483 }
484 }
485 return -1;
486 }
487
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100488 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
489 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
490 if (successors_.Get(i) == successor) {
491 return i;
492 }
493 }
494 return -1;
495 }
496
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000497 // Split the block into two blocks just after `cursor`. Returns the newly
498 // created block. Note that this method just updates raw block information,
499 // like predecessors, successors, dominators, and instruction list. It does not
500 // update the graph, reverse post order, loop information, nor make sure the
501 // blocks are consistent (for example ending with a control flow instruction).
502 HBasicBlock* SplitAfter(HInstruction* cursor);
503
504 // Merge `other` at the end of `this`. Successors and dominated blocks of
505 // `other` are changed to be successors and dominated blocks of `this`. Note
506 // that this method does not update the graph, reverse post order, loop
507 // information, nor make sure the blocks are consistent (for example ending
508 // with a control flow instruction).
509 void MergeWith(HBasicBlock* other);
510
511 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
512 // of `this` are moved to `other`.
513 // Note that this method does not update the graph, reverse post order, loop
514 // information, nor make sure the blocks are consistent (for example ending
515 void ReplaceWith(HBasicBlock* other);
516
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000517 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100518 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100519 // Replace instruction `initial` with `replacement` within this block.
520 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
521 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100522 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100523 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000524 // RemoveInstruction and RemovePhi delete a given instruction from the respective
525 // instruction list. With 'ensure_safety' set to true, it verifies that the
526 // instruction is not in use and removes it from the use lists of its inputs.
527 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
528 void RemovePhi(HPhi* phi, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100529
530 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100531 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100532 }
533
Roland Levillain6b879dd2014-09-22 17:13:44 +0100534 bool IsLoopPreHeaderFirstPredecessor() const {
535 DCHECK(IsLoopHeader());
536 DCHECK(!GetPredecessors().IsEmpty());
537 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
538 }
539
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100540 HLoopInformation* GetLoopInformation() const {
541 return loop_information_;
542 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000543
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000544 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100545 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000546 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100547 void SetInLoop(HLoopInformation* info) {
548 if (IsLoopHeader()) {
549 // Nothing to do. This just means `info` is an outer loop.
550 } else if (loop_information_ == nullptr) {
551 loop_information_ = info;
552 } else if (loop_information_->Contains(*info->GetHeader())) {
553 // Block is currently part of an outer loop. Make it part of this inner loop.
554 // Note that a non loop header having a loop information means this loop information
555 // has already been populated
556 loop_information_ = info;
557 } else {
558 // Block is part of an inner loop. Do not update the loop information.
559 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
560 // at this point, because this method is being called while populating `info`.
561 }
562 }
563
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000564 // Raw update of the loop information.
565 void SetLoopInformation(HLoopInformation* info) {
566 loop_information_ = info;
567 }
568
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100569 bool IsInLoop() const { return loop_information_ != nullptr; }
570
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100571 // Returns wheter this block dominates the blocked passed as parameter.
572 bool Dominates(HBasicBlock* block) const;
573
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100574 size_t GetLifetimeStart() const { return lifetime_start_; }
575 size_t GetLifetimeEnd() const { return lifetime_end_; }
576
577 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
578 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
579
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100580 uint32_t GetDexPc() const { return dex_pc_; }
581
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000582 bool IsCatchBlock() const { return is_catch_block_; }
583 void SetIsCatchBlock() { is_catch_block_ = true; }
584
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000585 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000586 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000587 GrowableArray<HBasicBlock*> predecessors_;
588 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100589 HInstructionList instructions_;
590 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000591 HLoopInformation* loop_information_;
592 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100593 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000594 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100595 // The dex program counter of the first instruction of this block.
596 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100597 size_t lifetime_start_;
598 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000599 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000600
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000601 friend class HGraph;
602 friend class HInstruction;
603
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000604 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
605};
606
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100607#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
608 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000609 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000610 M(ArrayGet, Instruction) \
611 M(ArrayLength, Instruction) \
612 M(ArraySet, Instruction) \
613 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000614 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000615 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100616 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000617 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100618 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000619 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000620 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000621 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100622 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000623 M(Exit, Instruction) \
624 M(FloatConstant, Constant) \
625 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100626 M(GreaterThan, Condition) \
627 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100628 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000629 M(InstanceFieldGet, Instruction) \
630 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000631 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100632 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000633 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000634 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100635 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000636 M(LessThan, Condition) \
637 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000638 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000639 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100640 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000641 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100642 M(Local, Instruction) \
643 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000644 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000645 M(Mul, BinaryOperation) \
646 M(Neg, UnaryOperation) \
647 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100648 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100649 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000650 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000651 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000652 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000653 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100654 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000655 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100656 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000657 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100658 M(Return, Instruction) \
659 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000660 M(Shl, BinaryOperation) \
661 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100662 M(StaticFieldGet, Instruction) \
663 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100664 M(StoreLocal, Instruction) \
665 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100666 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000667 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000668 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000669 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000670 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000671 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000672
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100673#define FOR_EACH_INSTRUCTION(M) \
674 FOR_EACH_CONCRETE_INSTRUCTION(M) \
675 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100676 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100677 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100678 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700679
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100680#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000681FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
682#undef FORWARD_DECLARATION
683
Roland Levillainccc07a92014-09-16 14:48:16 +0100684#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000685 InstructionKind GetKind() const OVERRIDE { return k##type; } \
686 const char* DebugName() const OVERRIDE { return #type; } \
687 const H##type* As##type() const OVERRIDE { return this; } \
688 H##type* As##type() OVERRIDE { return this; } \
689 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100690 return other->Is##type(); \
691 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000692 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000693
David Brazdiled596192015-01-23 10:39:45 +0000694template <typename T> class HUseList;
695
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100696template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700697class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000698 public:
David Brazdiled596192015-01-23 10:39:45 +0000699 HUseListNode* GetPrevious() const { return prev_; }
700 HUseListNode* GetNext() const { return next_; }
701 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100702 size_t GetIndex() const { return index_; }
703
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000704 private:
David Brazdiled596192015-01-23 10:39:45 +0000705 HUseListNode(T user, size_t index)
706 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
707
708 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100709 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000710 HUseListNode<T>* prev_;
711 HUseListNode<T>* next_;
712
713 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000714
715 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
716};
717
David Brazdiled596192015-01-23 10:39:45 +0000718template <typename T>
719class HUseList : public ValueObject {
720 public:
721 HUseList() : first_(nullptr) {}
722
723 void Clear() {
724 first_ = nullptr;
725 }
726
727 // Adds a new entry at the beginning of the use list and returns
728 // the newly created node.
729 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000730 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000731 if (IsEmpty()) {
732 first_ = new_node;
733 } else {
734 first_->prev_ = new_node;
735 new_node->next_ = first_;
736 first_ = new_node;
737 }
738 return new_node;
739 }
740
741 HUseListNode<T>* GetFirst() const {
742 return first_;
743 }
744
745 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000746 DCHECK(node != nullptr);
747 DCHECK(Contains(node));
748
David Brazdiled596192015-01-23 10:39:45 +0000749 if (node->prev_ != nullptr) {
750 node->prev_->next_ = node->next_;
751 }
752 if (node->next_ != nullptr) {
753 node->next_->prev_ = node->prev_;
754 }
755 if (node == first_) {
756 first_ = node->next_;
757 }
758 }
759
David Brazdil1abb4192015-02-17 18:33:36 +0000760 bool Contains(const HUseListNode<T>* node) const {
761 if (node == nullptr) {
762 return false;
763 }
764 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
765 if (current == node) {
766 return true;
767 }
768 }
769 return false;
770 }
771
David Brazdiled596192015-01-23 10:39:45 +0000772 bool IsEmpty() const {
773 return first_ == nullptr;
774 }
775
776 bool HasOnlyOneUse() const {
777 return first_ != nullptr && first_->next_ == nullptr;
778 }
779
780 private:
781 HUseListNode<T>* first_;
782};
783
784template<typename T>
785class HUseIterator : public ValueObject {
786 public:
787 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
788
789 bool Done() const { return current_ == nullptr; }
790
791 void Advance() {
792 DCHECK(!Done());
793 current_ = current_->GetNext();
794 }
795
796 HUseListNode<T>* Current() const {
797 DCHECK(!Done());
798 return current_;
799 }
800
801 private:
802 HUseListNode<T>* current_;
803
804 friend class HValue;
805};
806
David Brazdil1abb4192015-02-17 18:33:36 +0000807// This class is used by HEnvironment and HInstruction classes to record the
808// instructions they use and pointers to the corresponding HUseListNodes kept
809// by the used instructions.
810template <typename T>
811class HUserRecord : public ValueObject {
812 public:
813 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
814 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
815
816 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
817 : instruction_(old_record.instruction_), use_node_(use_node) {
818 DCHECK(instruction_ != nullptr);
819 DCHECK(use_node_ != nullptr);
820 DCHECK(old_record.use_node_ == nullptr);
821 }
822
823 HInstruction* GetInstruction() const { return instruction_; }
824 HUseListNode<T>* GetUseNode() const { return use_node_; }
825
826 private:
827 // Instruction used by the user.
828 HInstruction* instruction_;
829
830 // Corresponding entry in the use list kept by 'instruction_'.
831 HUseListNode<T>* use_node_;
832};
833
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100834// Represents the side effects an instruction may have.
835class SideEffects : public ValueObject {
836 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100837 SideEffects() : flags_(0) {}
838
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100839 static SideEffects None() {
840 return SideEffects(0);
841 }
842
843 static SideEffects All() {
844 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
845 }
846
847 static SideEffects ChangesSomething() {
848 return SideEffects((1 << kFlagChangesCount) - 1);
849 }
850
851 static SideEffects DependsOnSomething() {
852 int count = kFlagDependsOnCount - kFlagChangesCount;
853 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
854 }
855
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100856 SideEffects Union(SideEffects other) const {
857 return SideEffects(flags_ | other.flags_);
858 }
859
Roland Levillain72bceff2014-09-15 18:29:00 +0100860 bool HasSideEffects() const {
861 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
862 return (flags_ & all_bits_set) != 0;
863 }
864
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100865 bool HasAllSideEffects() const {
866 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
867 return all_bits_set == (flags_ & all_bits_set);
868 }
869
870 bool DependsOn(SideEffects other) const {
871 size_t depends_flags = other.ComputeDependsFlags();
872 return (flags_ & depends_flags) != 0;
873 }
874
875 bool HasDependencies() const {
876 int count = kFlagDependsOnCount - kFlagChangesCount;
877 size_t all_bits_set = (1 << count) - 1;
878 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
879 }
880
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100881 private:
882 static constexpr int kFlagChangesSomething = 0;
883 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
884
885 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
886 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
887
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100888 explicit SideEffects(size_t flags) : flags_(flags) {}
889
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100890 size_t ComputeDependsFlags() const {
891 return flags_ << kFlagChangesCount;
892 }
893
894 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100895};
896
David Brazdiled596192015-01-23 10:39:45 +0000897// A HEnvironment object contains the values of virtual registers at a given location.
898class HEnvironment : public ArenaObject<kArenaAllocMisc> {
899 public:
900 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
901 : vregs_(arena, number_of_vregs) {
902 vregs_.SetSize(number_of_vregs);
903 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000904 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000905 }
906 }
907
908 void CopyFrom(HEnvironment* env);
909
910 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +0000911 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +0000912 }
913
914 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +0000915 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +0000916 }
917
David Brazdil1abb4192015-02-17 18:33:36 +0000918 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +0000919
920 size_t Size() const { return vregs_.Size(); }
921
922 private:
David Brazdil1abb4192015-02-17 18:33:36 +0000923 // Record instructions' use entries of this environment for constant-time removal.
924 // It should only be called by HInstruction when a new environment use is added.
925 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
926 DCHECK(env_use->GetUser() == this);
927 size_t index = env_use->GetIndex();
928 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
929 }
David Brazdiled596192015-01-23 10:39:45 +0000930
David Brazdil1abb4192015-02-17 18:33:36 +0000931 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +0000932
David Brazdil1abb4192015-02-17 18:33:36 +0000933 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +0000934
935 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
936};
937
Calin Juravleacf735c2015-02-12 15:25:22 +0000938class ReferenceTypeInfo : ValueObject {
939 public:
Calin Juravleb1498f62015-02-16 13:13:29 +0000940 typedef Handle<mirror::Class> TypeHandle;
941
942 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
943 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
944 if (type_handle->IsObjectClass()) {
945 // Override the type handle to be consistent with the case when we get to
946 // Top but don't have the Object class available. It avoids having to guess
947 // what value the type_handle has when it's Top.
948 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
949 } else {
950 return ReferenceTypeInfo(type_handle, is_exact, false);
951 }
952 }
953
954 static ReferenceTypeInfo CreateTop(bool is_exact) {
955 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +0000956 }
957
958 bool IsExact() const { return is_exact_; }
959 bool IsTop() const { return is_top_; }
960
961 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
962
Calin Juravleb1498f62015-02-16 13:13:29 +0000963 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000964 if (IsTop()) {
965 // Top (equivalent for java.lang.Object) is supertype of anything.
966 return true;
967 }
968 if (rti.IsTop()) {
969 // If we get here `this` is not Top() so it can't be a supertype.
970 return false;
971 }
972 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
973 }
974
975 // Returns true if the type information provide the same amount of details.
976 // Note that it does not mean that the instructions have the same actual type
977 // (e.g. tops are equal but they can be the result of a merge).
978 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
979 if (IsExact() != rti.IsExact()) {
980 return false;
981 }
982 if (IsTop() && rti.IsTop()) {
983 // `Top` means java.lang.Object, so the types are equivalent.
984 return true;
985 }
986 if (IsTop() || rti.IsTop()) {
987 // If only one is top or object than they are not equivalent.
988 // NB: We need this extra check because the type_handle of `Top` is invalid
989 // and we cannot inspect its reference.
990 return false;
991 }
992
993 // Finally check the types.
994 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
995 }
996
997 private:
Calin Juravleb1498f62015-02-16 13:13:29 +0000998 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
999 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1000 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1001
Calin Juravleacf735c2015-02-12 15:25:22 +00001002 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001003 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001004 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001005 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001006 bool is_exact_;
1007 // A true value here means that the object type should be java.lang.Object.
1008 // We don't have access to the corresponding mirror object every time so this
1009 // flag acts as a substitute. When true, the TypeHandle refers to a null
1010 // pointer and should not be used.
1011 bool is_top_;
1012};
1013
1014std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1015
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001016class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001017 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001018 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001019 : previous_(nullptr),
1020 next_(nullptr),
1021 block_(nullptr),
1022 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001023 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001024 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001025 locations_(nullptr),
1026 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001027 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001028 side_effects_(side_effects),
1029 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001030
Dave Allison20dfc792014-06-16 20:44:29 -07001031 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001032
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001033#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001034 enum InstructionKind {
1035 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1036 };
1037#undef DECLARE_KIND
1038
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001039 HInstruction* GetNext() const { return next_; }
1040 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001041
Calin Juravle77520bc2015-01-12 18:45:46 +00001042 HInstruction* GetNextDisregardingMoves() const;
1043 HInstruction* GetPreviousDisregardingMoves() const;
1044
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001045 HBasicBlock* GetBlock() const { return block_; }
1046 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001047 bool IsInBlock() const { return block_ != nullptr; }
1048 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001049 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001050
Roland Levillain6b879dd2014-09-22 17:13:44 +01001051 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001052 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001053
1054 virtual void Accept(HGraphVisitor* visitor) = 0;
1055 virtual const char* DebugName() const = 0;
1056
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001057 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001058 void SetRawInputAt(size_t index, HInstruction* input) {
1059 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1060 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001061
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001062 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001063 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001064 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001065 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001066
Calin Juravle61d544b2015-02-23 16:46:57 +00001067 virtual bool ActAsNullConstant() const { return false; }
1068
Calin Juravle10e244f2015-01-26 18:54:32 +00001069 // Does not apply for all instructions, but having this at top level greatly
1070 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001071 virtual bool CanBeNull() const {
1072 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1073 return true;
1074 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001075
Calin Juravle77520bc2015-01-12 18:45:46 +00001076 virtual bool CanDoImplicitNullCheck() const { return false; }
1077
Calin Juravleacf735c2015-02-12 15:25:22 +00001078 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001079 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001080 reference_type_info_ = reference_type_info;
1081 }
1082
Calin Juravle61d544b2015-02-23 16:46:57 +00001083 ReferenceTypeInfo GetReferenceTypeInfo() const {
1084 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1085 return reference_type_info_;
1086 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001087
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001088 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001089 DCHECK(user != nullptr);
1090 HUseListNode<HInstruction*>* use =
1091 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1092 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001093 }
1094
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001095 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001096 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001097 HUseListNode<HEnvironment*>* env_use =
1098 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1099 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001100 }
1101
David Brazdil1abb4192015-02-17 18:33:36 +00001102 void RemoveAsUserOfInput(size_t input) {
1103 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1104 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1105 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001106
David Brazdil1abb4192015-02-17 18:33:36 +00001107 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1108 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001109
David Brazdiled596192015-01-23 10:39:45 +00001110 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1111 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001112 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001113
Roland Levillain6c82d402014-10-13 16:10:27 +01001114 // Does this instruction strictly dominate `other_instruction`?
1115 // Returns false if this instruction and `other_instruction` are the same.
1116 // Aborts if this instruction and `other_instruction` are both phis.
1117 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001118
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001119 int GetId() const { return id_; }
1120 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001121
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001122 int GetSsaIndex() const { return ssa_index_; }
1123 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1124 bool HasSsaIndex() const { return ssa_index_ != -1; }
1125
1126 bool HasEnvironment() const { return environment_ != nullptr; }
1127 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001128 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
1129
Nicolas Geoffray39468442014-09-02 15:17:15 +01001130 // Returns the number of entries in the environment. Typically, that is the
1131 // number of dex registers in a method. It could be more in case of inlining.
1132 size_t EnvironmentSize() const;
1133
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001134 LocationSummary* GetLocations() const { return locations_; }
1135 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001136
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001137 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001138 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001139
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001140 // Move `this` instruction before `cursor`.
1141 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001142
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001143#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001144 bool Is##type() const { return (As##type() != nullptr); } \
1145 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001146 virtual H##type* As##type() { return nullptr; }
1147
1148 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1149#undef INSTRUCTION_TYPE_CHECK
1150
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001151 // Returns whether the instruction can be moved within the graph.
1152 virtual bool CanBeMoved() const { return false; }
1153
1154 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001155 virtual bool InstructionTypeEquals(HInstruction* other) const {
1156 UNUSED(other);
1157 return false;
1158 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001159
1160 // Returns whether any data encoded in the two instructions is equal.
1161 // This method does not look at the inputs. Both instructions must be
1162 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001163 virtual bool InstructionDataEquals(HInstruction* other) const {
1164 UNUSED(other);
1165 return false;
1166 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001167
1168 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001169 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001170 // 2) Their inputs are identical.
1171 bool Equals(HInstruction* other) const;
1172
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001173 virtual InstructionKind GetKind() const = 0;
1174
1175 virtual size_t ComputeHashCode() const {
1176 size_t result = GetKind();
1177 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1178 result = (result * 31) + InputAt(i)->GetId();
1179 }
1180 return result;
1181 }
1182
1183 SideEffects GetSideEffects() const { return side_effects_; }
1184
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001185 size_t GetLifetimePosition() const { return lifetime_position_; }
1186 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1187 LiveInterval* GetLiveInterval() const { return live_interval_; }
1188 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1189 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1190
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001191 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1192
1193 // Returns whether the code generation of the instruction will require to have access
1194 // to the current method. Such instructions are:
1195 // (1): Instructions that require an environment, as calling the runtime requires
1196 // to walk the stack and have the current method stored at a specific stack address.
1197 // (2): Object literals like classes and strings, that are loaded from the dex cache
1198 // fields of the current method.
1199 bool NeedsCurrentMethod() const {
1200 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1201 }
1202
David Brazdil1abb4192015-02-17 18:33:36 +00001203 protected:
1204 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1205 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1206
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001207 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001208 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1209
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001210 HInstruction* previous_;
1211 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001212 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001213
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001214 // An instruction gets an id when it is added to the graph.
1215 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001216 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001217 int id_;
1218
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001219 // When doing liveness analysis, instructions that have uses get an SSA index.
1220 int ssa_index_;
1221
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001222 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001223 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001224
1225 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001226 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001227
Nicolas Geoffray39468442014-09-02 15:17:15 +01001228 // The environment associated with this instruction. Not null if the instruction
1229 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001230 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001231
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001232 // Set by the code generator.
1233 LocationSummary* locations_;
1234
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001235 // Set by the liveness analysis.
1236 LiveInterval* live_interval_;
1237
1238 // Set by the liveness analysis, this is the position in a linear
1239 // order of blocks where this instruction's live interval start.
1240 size_t lifetime_position_;
1241
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001242 const SideEffects side_effects_;
1243
Calin Juravleacf735c2015-02-12 15:25:22 +00001244 // TODO: for primitive types this should be marked as invalid.
1245 ReferenceTypeInfo reference_type_info_;
1246
David Brazdil1abb4192015-02-17 18:33:36 +00001247 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001248 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001249 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001250 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001251 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001252
1253 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1254};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001255std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001256
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001257class HInputIterator : public ValueObject {
1258 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001259 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001260
1261 bool Done() const { return index_ == instruction_->InputCount(); }
1262 HInstruction* Current() const { return instruction_->InputAt(index_); }
1263 void Advance() { index_++; }
1264
1265 private:
1266 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001267 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001268
1269 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1270};
1271
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001272class HInstructionIterator : public ValueObject {
1273 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001274 explicit HInstructionIterator(const HInstructionList& instructions)
1275 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001276 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001277 }
1278
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001279 bool Done() const { return instruction_ == nullptr; }
1280 HInstruction* Current() const { return instruction_; }
1281 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001282 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001283 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001284 }
1285
1286 private:
1287 HInstruction* instruction_;
1288 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001289
1290 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001291};
1292
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001293class HBackwardInstructionIterator : public ValueObject {
1294 public:
1295 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1296 : instruction_(instructions.last_instruction_) {
1297 next_ = Done() ? nullptr : instruction_->GetPrevious();
1298 }
1299
1300 bool Done() const { return instruction_ == nullptr; }
1301 HInstruction* Current() const { return instruction_; }
1302 void Advance() {
1303 instruction_ = next_;
1304 next_ = Done() ? nullptr : instruction_->GetPrevious();
1305 }
1306
1307 private:
1308 HInstruction* instruction_;
1309 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001310
1311 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001312};
1313
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001314// An embedded container with N elements of type T. Used (with partial
1315// specialization for N=0) because embedded arrays cannot have size 0.
1316template<typename T, intptr_t N>
1317class EmbeddedArray {
1318 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001319 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001320
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001321 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001322
1323 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001324 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001325 return elements_[i];
1326 }
1327
1328 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001329 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001330 return elements_[i];
1331 }
1332
1333 const T& At(intptr_t i) const {
1334 return (*this)[i];
1335 }
1336
1337 void SetAt(intptr_t i, const T& val) {
1338 (*this)[i] = val;
1339 }
1340
1341 private:
1342 T elements_[N];
1343};
1344
1345template<typename T>
1346class EmbeddedArray<T, 0> {
1347 public:
1348 intptr_t length() const { return 0; }
1349 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001350 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001351 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001352 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001353 }
1354 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001355 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001356 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001357 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001358 }
1359};
1360
1361template<intptr_t N>
1362class HTemplateInstruction: public HInstruction {
1363 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001364 HTemplateInstruction<N>(SideEffects side_effects)
1365 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001366 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001367
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001368 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001369
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001370 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001371 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1372
1373 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1374 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001375 }
1376
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001377 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001378 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001379
1380 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001381};
1382
Dave Allison20dfc792014-06-16 20:44:29 -07001383template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001384class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001385 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001386 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1387 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001388 virtual ~HExpression() {}
1389
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001390 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001391
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001392 protected:
1393 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001394};
1395
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001396// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1397// instruction that branches to the exit block.
1398class HReturnVoid : public HTemplateInstruction<0> {
1399 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001400 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001401
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001402 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001403
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001404 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001405
1406 private:
1407 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1408};
1409
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001410// Represents dex's RETURN opcodes. A HReturn is a control flow
1411// instruction that branches to the exit block.
1412class HReturn : public HTemplateInstruction<1> {
1413 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001414 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001415 SetRawInputAt(0, value);
1416 }
1417
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001418 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001419
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001420 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001421
1422 private:
1423 DISALLOW_COPY_AND_ASSIGN(HReturn);
1424};
1425
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001426// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001427// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001428// exit block.
1429class HExit : public HTemplateInstruction<0> {
1430 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001431 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001432
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001433 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001434
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001435 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001436
1437 private:
1438 DISALLOW_COPY_AND_ASSIGN(HExit);
1439};
1440
1441// Jumps from one block to another.
1442class HGoto : public HTemplateInstruction<0> {
1443 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001444 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1445
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001446 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001447
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001448 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001449 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001450 }
1451
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001452 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001453
1454 private:
1455 DISALLOW_COPY_AND_ASSIGN(HGoto);
1456};
1457
Dave Allison20dfc792014-06-16 20:44:29 -07001458
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001459// Conditional branch. A block ending with an HIf instruction must have
1460// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001461class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001462 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001463 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001464 SetRawInputAt(0, input);
1465 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001466
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001467 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001468
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001469 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001470 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001471 }
1472
1473 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001474 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001475 }
1476
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001477 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001478
Dave Allison20dfc792014-06-16 20:44:29 -07001479 virtual bool IsIfInstruction() const { return true; }
1480
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001481 private:
1482 DISALLOW_COPY_AND_ASSIGN(HIf);
1483};
1484
Roland Levillain88cb1752014-10-20 16:36:47 +01001485class HUnaryOperation : public HExpression<1> {
1486 public:
1487 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1488 : HExpression(result_type, SideEffects::None()) {
1489 SetRawInputAt(0, input);
1490 }
1491
1492 HInstruction* GetInput() const { return InputAt(0); }
1493 Primitive::Type GetResultType() const { return GetType(); }
1494
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001495 bool CanBeMoved() const OVERRIDE { return true; }
1496 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001497 UNUSED(other);
1498 return true;
1499 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001500
Roland Levillain9240d6a2014-10-20 16:47:04 +01001501 // Try to statically evaluate `operation` and return a HConstant
1502 // containing the result of this evaluation. If `operation` cannot
1503 // be evaluated as a constant, return nullptr.
1504 HConstant* TryStaticEvaluation() const;
1505
1506 // Apply this operation to `x`.
1507 virtual int32_t Evaluate(int32_t x) const = 0;
1508 virtual int64_t Evaluate(int64_t x) const = 0;
1509
Roland Levillain88cb1752014-10-20 16:36:47 +01001510 DECLARE_INSTRUCTION(UnaryOperation);
1511
1512 private:
1513 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1514};
1515
Dave Allison20dfc792014-06-16 20:44:29 -07001516class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001517 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001518 HBinaryOperation(Primitive::Type result_type,
1519 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001520 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001521 SetRawInputAt(0, left);
1522 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001523 }
1524
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001525 HInstruction* GetLeft() const { return InputAt(0); }
1526 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001527 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001528
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001529 virtual bool IsCommutative() const { return false; }
1530
1531 // Put constant on the right.
1532 // Returns whether order is changed.
1533 bool OrderInputsWithConstantOnTheRight() {
1534 HInstruction* left = InputAt(0);
1535 HInstruction* right = InputAt(1);
1536 if (left->IsConstant() && !right->IsConstant()) {
1537 ReplaceInput(right, 0);
1538 ReplaceInput(left, 1);
1539 return true;
1540 }
1541 return false;
1542 }
1543
1544 // Order inputs by instruction id, but favor constant on the right side.
1545 // This helps GVN for commutative ops.
1546 void OrderInputs() {
1547 DCHECK(IsCommutative());
1548 HInstruction* left = InputAt(0);
1549 HInstruction* right = InputAt(1);
1550 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1551 return;
1552 }
1553 if (OrderInputsWithConstantOnTheRight()) {
1554 return;
1555 }
1556 // Order according to instruction id.
1557 if (left->GetId() > right->GetId()) {
1558 ReplaceInput(right, 0);
1559 ReplaceInput(left, 1);
1560 }
1561 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001562
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001563 bool CanBeMoved() const OVERRIDE { return true; }
1564 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001565 UNUSED(other);
1566 return true;
1567 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001568
Roland Levillain9240d6a2014-10-20 16:47:04 +01001569 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001570 // containing the result of this evaluation. If `operation` cannot
1571 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001572 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001573
1574 // Apply this operation to `x` and `y`.
1575 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1576 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1577
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001578 // Returns an input that can legally be used as the right input and is
1579 // constant, or nullptr.
1580 HConstant* GetConstantRight() const;
1581
1582 // If `GetConstantRight()` returns one of the input, this returns the other
1583 // one. Otherwise it returns nullptr.
1584 HInstruction* GetLeastConstantLeft() const;
1585
Roland Levillainccc07a92014-09-16 14:48:16 +01001586 DECLARE_INSTRUCTION(BinaryOperation);
1587
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001588 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001589 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1590};
1591
Dave Allison20dfc792014-06-16 20:44:29 -07001592class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001593 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001594 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001595 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1596 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001597
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001598 bool NeedsMaterialization() const { return needs_materialization_; }
1599 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001600
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001601 // For code generation purposes, returns whether this instruction is just before
1602 // `if_`, and disregard moves in between.
1603 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1604
Dave Allison20dfc792014-06-16 20:44:29 -07001605 DECLARE_INSTRUCTION(Condition);
1606
1607 virtual IfCondition GetCondition() const = 0;
1608
1609 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001610 // For register allocation purposes, returns whether this instruction needs to be
1611 // materialized (that is, not just be in the processor flags).
1612 bool needs_materialization_;
1613
Dave Allison20dfc792014-06-16 20:44:29 -07001614 DISALLOW_COPY_AND_ASSIGN(HCondition);
1615};
1616
1617// Instruction to check if two inputs are equal to each other.
1618class HEqual : public HCondition {
1619 public:
1620 HEqual(HInstruction* first, HInstruction* second)
1621 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001622
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001623 bool IsCommutative() const OVERRIDE { return true; }
1624
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001625 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001626 return x == y ? 1 : 0;
1627 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001628 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001629 return x == y ? 1 : 0;
1630 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001631
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001632 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001633
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001634 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001635 return kCondEQ;
1636 }
1637
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001638 private:
1639 DISALLOW_COPY_AND_ASSIGN(HEqual);
1640};
1641
Dave Allison20dfc792014-06-16 20:44:29 -07001642class HNotEqual : public HCondition {
1643 public:
1644 HNotEqual(HInstruction* first, HInstruction* second)
1645 : HCondition(first, second) {}
1646
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001647 bool IsCommutative() const OVERRIDE { return true; }
1648
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001649 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001650 return x != y ? 1 : 0;
1651 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001652 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001653 return x != y ? 1 : 0;
1654 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001655
Dave Allison20dfc792014-06-16 20:44:29 -07001656 DECLARE_INSTRUCTION(NotEqual);
1657
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001658 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001659 return kCondNE;
1660 }
1661
1662 private:
1663 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1664};
1665
1666class HLessThan : public HCondition {
1667 public:
1668 HLessThan(HInstruction* first, HInstruction* second)
1669 : HCondition(first, second) {}
1670
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001671 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001672 return x < y ? 1 : 0;
1673 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001674 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001675 return x < y ? 1 : 0;
1676 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001677
Dave Allison20dfc792014-06-16 20:44:29 -07001678 DECLARE_INSTRUCTION(LessThan);
1679
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001680 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001681 return kCondLT;
1682 }
1683
1684 private:
1685 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1686};
1687
1688class HLessThanOrEqual : public HCondition {
1689 public:
1690 HLessThanOrEqual(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(LessThanOrEqual);
1701
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001702 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001703 return kCondLE;
1704 }
1705
1706 private:
1707 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1708};
1709
1710class HGreaterThan : public HCondition {
1711 public:
1712 HGreaterThan(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(GreaterThan);
1723
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001724 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001725 return kCondGT;
1726 }
1727
1728 private:
1729 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1730};
1731
1732class HGreaterThanOrEqual : public HCondition {
1733 public:
1734 HGreaterThanOrEqual(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(GreaterThanOrEqual);
1745
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001746 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001747 return kCondGE;
1748 }
1749
1750 private:
1751 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1752};
1753
1754
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001755// Instruction to check how two inputs compare to each other.
1756// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1757class HCompare : public HBinaryOperation {
1758 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001759 // The bias applies for floating point operations and indicates how NaN
1760 // comparisons are treated:
1761 enum Bias {
1762 kNoBias, // bias is not applicable (i.e. for long operation)
1763 kGtBias, // return 1 for NaN comparisons
1764 kLtBias, // return -1 for NaN comparisons
1765 };
1766
1767 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1768 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001769 DCHECK_EQ(type, first->GetType());
1770 DCHECK_EQ(type, second->GetType());
1771 }
1772
Calin Juravleddb7df22014-11-25 20:56:51 +00001773 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001774 return
1775 x == y ? 0 :
1776 x > y ? 1 :
1777 -1;
1778 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001779
1780 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001781 return
1782 x == y ? 0 :
1783 x > y ? 1 :
1784 -1;
1785 }
1786
Calin Juravleddb7df22014-11-25 20:56:51 +00001787 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1788 return bias_ == other->AsCompare()->bias_;
1789 }
1790
1791 bool IsGtBias() { return bias_ == kGtBias; }
1792
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001793 DECLARE_INSTRUCTION(Compare);
1794
1795 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001796 const Bias bias_;
1797
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001798 DISALLOW_COPY_AND_ASSIGN(HCompare);
1799};
1800
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001801// A local in the graph. Corresponds to a Dex register.
1802class HLocal : public HTemplateInstruction<0> {
1803 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001804 explicit HLocal(uint16_t reg_number)
1805 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001806
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001807 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001808
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001809 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001810
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001811 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001812 // The Dex register number.
1813 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001814
1815 DISALLOW_COPY_AND_ASSIGN(HLocal);
1816};
1817
1818// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001819class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001820 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001821 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001822 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001823 SetRawInputAt(0, local);
1824 }
1825
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001826 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1827
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001828 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001829
1830 private:
1831 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1832};
1833
1834// Store a value in a given local. This instruction has two inputs: the value
1835// and the local.
1836class HStoreLocal : public HTemplateInstruction<2> {
1837 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001838 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001839 SetRawInputAt(0, local);
1840 SetRawInputAt(1, value);
1841 }
1842
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001843 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1844
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001845 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001846
1847 private:
1848 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1849};
1850
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001851class HConstant : public HExpression<0> {
1852 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001853 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1854
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001855 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001856
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001857 virtual bool IsMinusOne() const { return false; }
1858 virtual bool IsZero() const { return false; }
1859 virtual bool IsOne() const { return false; }
1860
1861 static HConstant* NewConstant(ArenaAllocator* allocator, Primitive::Type type, int64_t val);
1862
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001863 DECLARE_INSTRUCTION(Constant);
1864
1865 private:
1866 DISALLOW_COPY_AND_ASSIGN(HConstant);
1867};
1868
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001869class HFloatConstant : public HConstant {
1870 public:
1871 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1872
1873 float GetValue() const { return value_; }
1874
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001875 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001876 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1877 bit_cast<float, int32_t>(value_);
1878 }
1879
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001880 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001881
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001882 bool IsMinusOne() const OVERRIDE {
1883 return bit_cast<uint32_t>(AsFloatConstant()->GetValue()) == bit_cast<uint32_t>((-1.0f));
1884 }
1885 bool IsZero() const OVERRIDE {
1886 return AsFloatConstant()->GetValue() == 0.0f;
1887 }
1888 bool IsOne() const OVERRIDE {
1889 return bit_cast<uint32_t>(AsFloatConstant()->GetValue()) == bit_cast<uint32_t>(1.0f);
1890 }
1891
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001892 DECLARE_INSTRUCTION(FloatConstant);
1893
1894 private:
1895 const float value_;
1896
1897 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1898};
1899
1900class HDoubleConstant : public HConstant {
1901 public:
1902 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1903
1904 double GetValue() const { return value_; }
1905
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001906 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001907 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1908 bit_cast<double, int64_t>(value_);
1909 }
1910
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001911 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001912
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001913 bool IsMinusOne() const OVERRIDE {
1914 return bit_cast<uint64_t>(AsDoubleConstant()->GetValue()) == bit_cast<uint64_t>((-1.0));
1915 }
1916 bool IsZero() const OVERRIDE {
1917 return AsDoubleConstant()->GetValue() == 0.0;
1918 }
1919 bool IsOne() const OVERRIDE {
1920 return bit_cast<uint64_t>(AsDoubleConstant()->GetValue()) == bit_cast<uint64_t>(1.0);
1921 }
1922
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001923 DECLARE_INSTRUCTION(DoubleConstant);
1924
1925 private:
1926 const double value_;
1927
1928 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1929};
1930
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001931class HNullConstant : public HConstant {
1932 public:
1933 HNullConstant() : HConstant(Primitive::kPrimNot) {}
1934
1935 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
1936 return true;
1937 }
1938
1939 size_t ComputeHashCode() const OVERRIDE { return 0; }
1940
Calin Juravle61d544b2015-02-23 16:46:57 +00001941 bool ActAsNullConstant() const OVERRIDE { return true; }
1942
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001943 DECLARE_INSTRUCTION(NullConstant);
1944
1945 private:
1946 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
1947};
1948
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001949// Constants of the type int. Those can be from Dex instructions, or
1950// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001951class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001952 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001953 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001954
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001955 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001956
Calin Juravle61d544b2015-02-23 16:46:57 +00001957 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001958 return other->AsIntConstant()->value_ == value_;
1959 }
1960
Calin Juravle61d544b2015-02-23 16:46:57 +00001961 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
1962
1963 // TODO: Null is represented by the `0` constant. In most cases we replace it
1964 // with a HNullConstant but we don't do it when comparing (a != null). This
1965 // method is an workaround until we fix the above.
1966 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001967
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001968 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
1969 bool IsZero() const OVERRIDE { return GetValue() == 0; }
1970 bool IsOne() const OVERRIDE { return GetValue() == 1; }
1971
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001972 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001973
1974 private:
1975 const int32_t value_;
1976
1977 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1978};
1979
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001980class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001981 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001982 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001983
1984 int64_t GetValue() const { return value_; }
1985
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001986 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001987 return other->AsLongConstant()->value_ == value_;
1988 }
1989
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001990 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001991
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001992 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
1993 bool IsZero() const OVERRIDE { return GetValue() == 0; }
1994 bool IsOne() const OVERRIDE { return GetValue() == 1; }
1995
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001996 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001997
1998 private:
1999 const int64_t value_;
2000
2001 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2002};
2003
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002004enum class Intrinsics {
2005#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2006#include "intrinsics_list.h"
2007 kNone,
2008 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2009#undef INTRINSICS_LIST
2010#undef OPTIMIZING_INTRINSICS
2011};
2012std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2013
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002014class HInvoke : public HInstruction {
2015 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002016 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002017
2018 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2019 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002020 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002021
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002022 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002023 SetRawInputAt(index, argument);
2024 }
2025
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002026 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002027
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002028 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002029
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002030 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2031
2032 Intrinsics GetIntrinsic() {
2033 return intrinsic_;
2034 }
2035
2036 void SetIntrinsic(Intrinsics intrinsic) {
2037 intrinsic_ = intrinsic;
2038 }
2039
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002040 DECLARE_INSTRUCTION(Invoke);
2041
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002042 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002043 HInvoke(ArenaAllocator* arena,
2044 uint32_t number_of_arguments,
2045 Primitive::Type return_type,
2046 uint32_t dex_pc,
2047 uint32_t dex_method_index)
2048 : HInstruction(SideEffects::All()),
2049 inputs_(arena, number_of_arguments),
2050 return_type_(return_type),
2051 dex_pc_(dex_pc),
2052 dex_method_index_(dex_method_index),
2053 intrinsic_(Intrinsics::kNone) {
2054 inputs_.SetSize(number_of_arguments);
2055 }
2056
David Brazdil1abb4192015-02-17 18:33:36 +00002057 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2058 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2059 inputs_.Put(index, input);
2060 }
2061
2062 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002063 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002064 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002065 const uint32_t dex_method_index_;
2066 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002067
2068 private:
2069 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2070};
2071
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002072class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002073 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002074 HInvokeStaticOrDirect(ArenaAllocator* arena,
2075 uint32_t number_of_arguments,
2076 Primitive::Type return_type,
2077 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002078 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002079 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002080 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002081 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002082 invoke_type_(invoke_type),
2083 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002084
Calin Juravle77520bc2015-01-12 18:45:46 +00002085 bool CanDoImplicitNullCheck() const OVERRIDE {
2086 // We access the method via the dex cache so we can't do an implicit null check.
2087 // TODO: for intrinsics we can generate implicit null checks.
2088 return false;
2089 }
2090
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002091 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002092 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002093
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002094 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002095
2096 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002097 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002098 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002099
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002100 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002101};
2102
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002103class HInvokeVirtual : public HInvoke {
2104 public:
2105 HInvokeVirtual(ArenaAllocator* arena,
2106 uint32_t number_of_arguments,
2107 Primitive::Type return_type,
2108 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002109 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002110 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002111 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002112 vtable_index_(vtable_index) {}
2113
Calin Juravle77520bc2015-01-12 18:45:46 +00002114 bool CanDoImplicitNullCheck() const OVERRIDE {
2115 // TODO: Add implicit null checks in intrinsics.
2116 return !GetLocations()->Intrinsified();
2117 }
2118
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002119 uint32_t GetVTableIndex() const { return vtable_index_; }
2120
2121 DECLARE_INSTRUCTION(InvokeVirtual);
2122
2123 private:
2124 const uint32_t vtable_index_;
2125
2126 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2127};
2128
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002129class HInvokeInterface : public HInvoke {
2130 public:
2131 HInvokeInterface(ArenaAllocator* arena,
2132 uint32_t number_of_arguments,
2133 Primitive::Type return_type,
2134 uint32_t dex_pc,
2135 uint32_t dex_method_index,
2136 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002137 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002138 imt_index_(imt_index) {}
2139
Calin Juravle77520bc2015-01-12 18:45:46 +00002140 bool CanDoImplicitNullCheck() const OVERRIDE {
2141 // TODO: Add implicit null checks in intrinsics.
2142 return !GetLocations()->Intrinsified();
2143 }
2144
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002145 uint32_t GetImtIndex() const { return imt_index_; }
2146 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2147
2148 DECLARE_INSTRUCTION(InvokeInterface);
2149
2150 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002151 const uint32_t imt_index_;
2152
2153 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2154};
2155
Dave Allison20dfc792014-06-16 20:44:29 -07002156class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002157 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002158 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002159 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2160 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002161 type_index_(type_index),
2162 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002163
2164 uint32_t GetDexPc() const { return dex_pc_; }
2165 uint16_t GetTypeIndex() const { return type_index_; }
2166
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002167 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002168 bool NeedsEnvironment() const OVERRIDE { return true; }
2169 // It may throw when called on:
2170 // - interfaces
2171 // - abstract/innaccessible/unknown classes
2172 // TODO: optimize when possible.
2173 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002174
Calin Juravle10e244f2015-01-26 18:54:32 +00002175 bool CanBeNull() const OVERRIDE { return false; }
2176
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002177 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2178
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002179 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002180
2181 private:
2182 const uint32_t dex_pc_;
2183 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002184 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002185
2186 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2187};
2188
Roland Levillain88cb1752014-10-20 16:36:47 +01002189class HNeg : public HUnaryOperation {
2190 public:
2191 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2192 : HUnaryOperation(result_type, input) {}
2193
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002194 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2195 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002196
Roland Levillain88cb1752014-10-20 16:36:47 +01002197 DECLARE_INSTRUCTION(Neg);
2198
2199 private:
2200 DISALLOW_COPY_AND_ASSIGN(HNeg);
2201};
2202
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002203class HNewArray : public HExpression<1> {
2204 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002205 HNewArray(HInstruction* length,
2206 uint32_t dex_pc,
2207 uint16_t type_index,
2208 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002209 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2210 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002211 type_index_(type_index),
2212 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002213 SetRawInputAt(0, length);
2214 }
2215
2216 uint32_t GetDexPc() const { return dex_pc_; }
2217 uint16_t GetTypeIndex() const { return type_index_; }
2218
2219 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002220 bool NeedsEnvironment() const OVERRIDE { return true; }
2221
2222 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002223
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002224 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2225
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002226 DECLARE_INSTRUCTION(NewArray);
2227
2228 private:
2229 const uint32_t dex_pc_;
2230 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002231 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002232
2233 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2234};
2235
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002236class HAdd : public HBinaryOperation {
2237 public:
2238 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2239 : HBinaryOperation(result_type, left, right) {}
2240
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002241 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002242
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002243 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002244 return x + y;
2245 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002246 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002247 return x + y;
2248 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002249
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002250 DECLARE_INSTRUCTION(Add);
2251
2252 private:
2253 DISALLOW_COPY_AND_ASSIGN(HAdd);
2254};
2255
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002256class HSub : public HBinaryOperation {
2257 public:
2258 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2259 : HBinaryOperation(result_type, left, right) {}
2260
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002261 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002262 return x - y;
2263 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002264 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002265 return x - y;
2266 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002267
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002268 DECLARE_INSTRUCTION(Sub);
2269
2270 private:
2271 DISALLOW_COPY_AND_ASSIGN(HSub);
2272};
2273
Calin Juravle34bacdf2014-10-07 20:23:36 +01002274class HMul : public HBinaryOperation {
2275 public:
2276 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2277 : HBinaryOperation(result_type, left, right) {}
2278
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002279 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002280
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002281 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2282 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002283
2284 DECLARE_INSTRUCTION(Mul);
2285
2286 private:
2287 DISALLOW_COPY_AND_ASSIGN(HMul);
2288};
2289
Calin Juravle7c4954d2014-10-28 16:57:40 +00002290class HDiv : public HBinaryOperation {
2291 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002292 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2293 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002294
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002295 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002296 // Our graph structure ensures we never have 0 for `y` during constant folding.
2297 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002298 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002299 return (y == -1) ? -x : x / y;
2300 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002301
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002302 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002303 DCHECK_NE(y, 0);
2304 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2305 return (y == -1) ? -x : x / y;
2306 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002307
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002308 uint32_t GetDexPc() const { return dex_pc_; }
2309
Calin Juravle7c4954d2014-10-28 16:57:40 +00002310 DECLARE_INSTRUCTION(Div);
2311
2312 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002313 const uint32_t dex_pc_;
2314
Calin Juravle7c4954d2014-10-28 16:57:40 +00002315 DISALLOW_COPY_AND_ASSIGN(HDiv);
2316};
2317
Calin Juravlebacfec32014-11-14 15:54:36 +00002318class HRem : public HBinaryOperation {
2319 public:
2320 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2321 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2322
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002323 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002324 DCHECK_NE(y, 0);
2325 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2326 return (y == -1) ? 0 : x % y;
2327 }
2328
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002329 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002330 DCHECK_NE(y, 0);
2331 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2332 return (y == -1) ? 0 : x % y;
2333 }
2334
2335 uint32_t GetDexPc() const { return dex_pc_; }
2336
2337 DECLARE_INSTRUCTION(Rem);
2338
2339 private:
2340 const uint32_t dex_pc_;
2341
2342 DISALLOW_COPY_AND_ASSIGN(HRem);
2343};
2344
Calin Juravled0d48522014-11-04 16:40:20 +00002345class HDivZeroCheck : public HExpression<1> {
2346 public:
2347 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2348 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2349 SetRawInputAt(0, value);
2350 }
2351
2352 bool CanBeMoved() const OVERRIDE { return true; }
2353
2354 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2355 UNUSED(other);
2356 return true;
2357 }
2358
2359 bool NeedsEnvironment() const OVERRIDE { return true; }
2360 bool CanThrow() const OVERRIDE { return true; }
2361
2362 uint32_t GetDexPc() const { return dex_pc_; }
2363
2364 DECLARE_INSTRUCTION(DivZeroCheck);
2365
2366 private:
2367 const uint32_t dex_pc_;
2368
2369 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2370};
2371
Calin Juravle9aec02f2014-11-18 23:06:35 +00002372class HShl : public HBinaryOperation {
2373 public:
2374 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2375 : HBinaryOperation(result_type, left, right) {}
2376
2377 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2378 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2379
2380 DECLARE_INSTRUCTION(Shl);
2381
2382 private:
2383 DISALLOW_COPY_AND_ASSIGN(HShl);
2384};
2385
2386class HShr : public HBinaryOperation {
2387 public:
2388 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2389 : HBinaryOperation(result_type, left, right) {}
2390
2391 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2392 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2393
2394 DECLARE_INSTRUCTION(Shr);
2395
2396 private:
2397 DISALLOW_COPY_AND_ASSIGN(HShr);
2398};
2399
2400class HUShr : public HBinaryOperation {
2401 public:
2402 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2403 : HBinaryOperation(result_type, left, right) {}
2404
2405 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2406 uint32_t ux = static_cast<uint32_t>(x);
2407 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2408 return static_cast<int32_t>(ux >> uy);
2409 }
2410
2411 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2412 uint64_t ux = static_cast<uint64_t>(x);
2413 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2414 return static_cast<int64_t>(ux >> uy);
2415 }
2416
2417 DECLARE_INSTRUCTION(UShr);
2418
2419 private:
2420 DISALLOW_COPY_AND_ASSIGN(HUShr);
2421};
2422
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002423class HAnd : public HBinaryOperation {
2424 public:
2425 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2426 : HBinaryOperation(result_type, left, right) {}
2427
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002428 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002429
2430 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2431 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2432
2433 DECLARE_INSTRUCTION(And);
2434
2435 private:
2436 DISALLOW_COPY_AND_ASSIGN(HAnd);
2437};
2438
2439class HOr : public HBinaryOperation {
2440 public:
2441 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2442 : HBinaryOperation(result_type, left, right) {}
2443
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002444 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002445
2446 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2447 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2448
2449 DECLARE_INSTRUCTION(Or);
2450
2451 private:
2452 DISALLOW_COPY_AND_ASSIGN(HOr);
2453};
2454
2455class HXor : public HBinaryOperation {
2456 public:
2457 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2458 : HBinaryOperation(result_type, left, right) {}
2459
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002460 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002461
2462 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2463 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2464
2465 DECLARE_INSTRUCTION(Xor);
2466
2467 private:
2468 DISALLOW_COPY_AND_ASSIGN(HXor);
2469};
2470
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002471// The value of a parameter in this method. Its location depends on
2472// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002473class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002474 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002475 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2476 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002477
2478 uint8_t GetIndex() const { return index_; }
2479
Calin Juravle10e244f2015-01-26 18:54:32 +00002480 bool CanBeNull() const OVERRIDE { return !is_this_; }
2481
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002482 DECLARE_INSTRUCTION(ParameterValue);
2483
2484 private:
2485 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002486 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002487 const uint8_t index_;
2488
Calin Juravle10e244f2015-01-26 18:54:32 +00002489 // Whether or not the parameter value corresponds to 'this' argument.
2490 const bool is_this_;
2491
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002492 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2493};
2494
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002495class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002496 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002497 explicit HNot(Primitive::Type result_type, HInstruction* input)
2498 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002499
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002500 bool CanBeMoved() const OVERRIDE { return true; }
2501 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002502 UNUSED(other);
2503 return true;
2504 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002505
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002506 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2507 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002508
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002509 DECLARE_INSTRUCTION(Not);
2510
2511 private:
2512 DISALLOW_COPY_AND_ASSIGN(HNot);
2513};
2514
Roland Levillaindff1f282014-11-05 14:15:05 +00002515class HTypeConversion : public HExpression<1> {
2516 public:
2517 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002518 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2519 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002520 SetRawInputAt(0, input);
2521 DCHECK_NE(input->GetType(), result_type);
2522 }
2523
2524 HInstruction* GetInput() const { return InputAt(0); }
2525 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2526 Primitive::Type GetResultType() const { return GetType(); }
2527
Roland Levillain624279f2014-12-04 11:54:28 +00002528 // Required by the x86 and ARM code generators when producing calls
2529 // to the runtime.
2530 uint32_t GetDexPc() const { return dex_pc_; }
2531
Roland Levillaindff1f282014-11-05 14:15:05 +00002532 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002533 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002534
2535 DECLARE_INSTRUCTION(TypeConversion);
2536
2537 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002538 const uint32_t dex_pc_;
2539
Roland Levillaindff1f282014-11-05 14:15:05 +00002540 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2541};
2542
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002543static constexpr uint32_t kNoRegNumber = -1;
2544
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002545class HPhi : public HInstruction {
2546 public:
2547 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002548 : HInstruction(SideEffects::None()),
2549 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002550 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002551 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002552 is_live_(false),
2553 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002554 inputs_.SetSize(number_of_inputs);
2555 }
2556
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002557 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2558 static Primitive::Type ToPhiType(Primitive::Type type) {
2559 switch (type) {
2560 case Primitive::kPrimBoolean:
2561 case Primitive::kPrimByte:
2562 case Primitive::kPrimShort:
2563 case Primitive::kPrimChar:
2564 return Primitive::kPrimInt;
2565 default:
2566 return type;
2567 }
2568 }
2569
Calin Juravle10e244f2015-01-26 18:54:32 +00002570 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002571
2572 void AddInput(HInstruction* input);
2573
Calin Juravle10e244f2015-01-26 18:54:32 +00002574 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002575 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002576
Calin Juravle10e244f2015-01-26 18:54:32 +00002577 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2578 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2579
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002580 uint32_t GetRegNumber() const { return reg_number_; }
2581
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002582 void SetDead() { is_live_ = false; }
2583 void SetLive() { is_live_ = true; }
2584 bool IsDead() const { return !is_live_; }
2585 bool IsLive() const { return is_live_; }
2586
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002587 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002588
David Brazdil1abb4192015-02-17 18:33:36 +00002589 protected:
2590 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2591
2592 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2593 inputs_.Put(index, input);
2594 }
2595
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002596 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002597 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002598 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002599 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002600 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002601 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002602
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002603 DISALLOW_COPY_AND_ASSIGN(HPhi);
2604};
2605
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002606class HNullCheck : public HExpression<1> {
2607 public:
2608 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002609 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002610 SetRawInputAt(0, value);
2611 }
2612
Calin Juravle10e244f2015-01-26 18:54:32 +00002613 bool CanBeMoved() const OVERRIDE { return true; }
2614 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002615 UNUSED(other);
2616 return true;
2617 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002618
Calin Juravle10e244f2015-01-26 18:54:32 +00002619 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002620
Calin Juravle10e244f2015-01-26 18:54:32 +00002621 bool CanThrow() const OVERRIDE { return true; }
2622
2623 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002624
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002625 uint32_t GetDexPc() const { return dex_pc_; }
2626
2627 DECLARE_INSTRUCTION(NullCheck);
2628
2629 private:
2630 const uint32_t dex_pc_;
2631
2632 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2633};
2634
2635class FieldInfo : public ValueObject {
2636 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002637 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2638 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002639
2640 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002641 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002642 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002643
2644 private:
2645 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002646 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002647 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002648};
2649
2650class HInstanceFieldGet : public HExpression<1> {
2651 public:
2652 HInstanceFieldGet(HInstruction* value,
2653 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002654 MemberOffset field_offset,
2655 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002656 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002657 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002658 SetRawInputAt(0, value);
2659 }
2660
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002661 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002662
2663 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2664 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2665 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002666 }
2667
Calin Juravle77520bc2015-01-12 18:45:46 +00002668 bool CanDoImplicitNullCheck() const OVERRIDE {
2669 return GetFieldOffset().Uint32Value() < kPageSize;
2670 }
2671
2672 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002673 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2674 }
2675
Calin Juravle52c48962014-12-16 17:02:57 +00002676 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002677 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002678 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002679 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002680
2681 DECLARE_INSTRUCTION(InstanceFieldGet);
2682
2683 private:
2684 const FieldInfo field_info_;
2685
2686 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2687};
2688
2689class HInstanceFieldSet : public HTemplateInstruction<2> {
2690 public:
2691 HInstanceFieldSet(HInstruction* object,
2692 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002693 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002694 MemberOffset field_offset,
2695 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002696 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002697 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002698 SetRawInputAt(0, object);
2699 SetRawInputAt(1, value);
2700 }
2701
Calin Juravle77520bc2015-01-12 18:45:46 +00002702 bool CanDoImplicitNullCheck() const OVERRIDE {
2703 return GetFieldOffset().Uint32Value() < kPageSize;
2704 }
2705
Calin Juravle52c48962014-12-16 17:02:57 +00002706 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002707 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002708 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002709 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002710 HInstruction* GetValue() const { return InputAt(1); }
2711
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002712 DECLARE_INSTRUCTION(InstanceFieldSet);
2713
2714 private:
2715 const FieldInfo field_info_;
2716
2717 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2718};
2719
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002720class HArrayGet : public HExpression<2> {
2721 public:
2722 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002723 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002724 SetRawInputAt(0, array);
2725 SetRawInputAt(1, index);
2726 }
2727
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002728 bool CanBeMoved() const OVERRIDE { return true; }
2729 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002730 UNUSED(other);
2731 return true;
2732 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002733 bool CanDoImplicitNullCheck() const OVERRIDE {
2734 // TODO: We can be smarter here.
2735 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2736 // which generates the implicit null check. There are cases when these can be removed
2737 // to produce better code. If we ever add optimizations to do so we should allow an
2738 // implicit check here (as long as the address falls in the first page).
2739 return false;
2740 }
2741
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002742 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002743
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002744 HInstruction* GetArray() const { return InputAt(0); }
2745 HInstruction* GetIndex() const { return InputAt(1); }
2746
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002747 DECLARE_INSTRUCTION(ArrayGet);
2748
2749 private:
2750 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2751};
2752
2753class HArraySet : public HTemplateInstruction<3> {
2754 public:
2755 HArraySet(HInstruction* array,
2756 HInstruction* index,
2757 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002758 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002759 uint32_t dex_pc)
2760 : HTemplateInstruction(SideEffects::ChangesSomething()),
2761 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002762 expected_component_type_(expected_component_type),
2763 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002764 SetRawInputAt(0, array);
2765 SetRawInputAt(1, index);
2766 SetRawInputAt(2, value);
2767 }
2768
Calin Juravle77520bc2015-01-12 18:45:46 +00002769 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002770 // We currently always call a runtime method to catch array store
2771 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002772 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002773 }
2774
Calin Juravle77520bc2015-01-12 18:45:46 +00002775 bool CanDoImplicitNullCheck() const OVERRIDE {
2776 // TODO: Same as for ArrayGet.
2777 return false;
2778 }
2779
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002780 void ClearNeedsTypeCheck() {
2781 needs_type_check_ = false;
2782 }
2783
2784 bool NeedsTypeCheck() const { return needs_type_check_; }
2785
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002786 uint32_t GetDexPc() const { return dex_pc_; }
2787
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002788 HInstruction* GetArray() const { return InputAt(0); }
2789 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002790 HInstruction* GetValue() const { return InputAt(2); }
2791
2792 Primitive::Type GetComponentType() const {
2793 // The Dex format does not type floating point index operations. Since the
2794 // `expected_component_type_` is set during building and can therefore not
2795 // be correct, we also check what is the value type. If it is a floating
2796 // point type, we must use that type.
2797 Primitive::Type value_type = GetValue()->GetType();
2798 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2799 ? value_type
2800 : expected_component_type_;
2801 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002802
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002803 DECLARE_INSTRUCTION(ArraySet);
2804
2805 private:
2806 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002807 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002808 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002809
2810 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2811};
2812
2813class HArrayLength : public HExpression<1> {
2814 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002815 explicit HArrayLength(HInstruction* array)
2816 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2817 // Note that arrays do not change length, so the instruction does not
2818 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002819 SetRawInputAt(0, array);
2820 }
2821
Calin Juravle77520bc2015-01-12 18:45:46 +00002822 bool CanBeMoved() const OVERRIDE { return true; }
2823 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002824 UNUSED(other);
2825 return true;
2826 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002827 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002828
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002829 DECLARE_INSTRUCTION(ArrayLength);
2830
2831 private:
2832 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2833};
2834
2835class HBoundsCheck : public HExpression<2> {
2836 public:
2837 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002838 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002839 DCHECK(index->GetType() == Primitive::kPrimInt);
2840 SetRawInputAt(0, index);
2841 SetRawInputAt(1, length);
2842 }
2843
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002844 bool CanBeMoved() const OVERRIDE { return true; }
2845 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002846 UNUSED(other);
2847 return true;
2848 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002849
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002850 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002851
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002852 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002853
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002854 uint32_t GetDexPc() const { return dex_pc_; }
2855
2856 DECLARE_INSTRUCTION(BoundsCheck);
2857
2858 private:
2859 const uint32_t dex_pc_;
2860
2861 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2862};
2863
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002864/**
2865 * Some DEX instructions are folded into multiple HInstructions that need
2866 * to stay live until the last HInstruction. This class
2867 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002868 * HInstruction stays live. `index` represents the stack location index of the
2869 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002870 */
2871class HTemporary : public HTemplateInstruction<0> {
2872 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002873 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002874
2875 size_t GetIndex() const { return index_; }
2876
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002877 Primitive::Type GetType() const OVERRIDE {
2878 // The previous instruction is the one that will be stored in the temporary location.
2879 DCHECK(GetPrevious() != nullptr);
2880 return GetPrevious()->GetType();
2881 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002882
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002883 DECLARE_INSTRUCTION(Temporary);
2884
2885 private:
2886 const size_t index_;
2887
2888 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2889};
2890
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002891class HSuspendCheck : public HTemplateInstruction<0> {
2892 public:
2893 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002894 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002895
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002896 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002897 return true;
2898 }
2899
2900 uint32_t GetDexPc() const { return dex_pc_; }
2901
2902 DECLARE_INSTRUCTION(SuspendCheck);
2903
2904 private:
2905 const uint32_t dex_pc_;
2906
2907 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2908};
2909
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002910/**
2911 * Instruction to load a Class object.
2912 */
2913class HLoadClass : public HExpression<0> {
2914 public:
2915 HLoadClass(uint16_t type_index,
2916 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002917 uint32_t dex_pc)
2918 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2919 type_index_(type_index),
2920 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002921 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00002922 generate_clinit_check_(false),
2923 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002924
2925 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002926
2927 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2928 return other->AsLoadClass()->type_index_ == type_index_;
2929 }
2930
2931 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2932
2933 uint32_t GetDexPc() const { return dex_pc_; }
2934 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002935 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002936
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002937 bool NeedsEnvironment() const OVERRIDE {
2938 // Will call runtime and load the class if the class is not loaded yet.
2939 // TODO: finer grain decision.
2940 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002941 }
2942
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002943 bool MustGenerateClinitCheck() const {
2944 return generate_clinit_check_;
2945 }
2946
2947 void SetMustGenerateClinitCheck() {
2948 generate_clinit_check_ = true;
2949 }
2950
2951 bool CanCallRuntime() const {
2952 return MustGenerateClinitCheck() || !is_referrers_class_;
2953 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002954
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002955 bool CanThrow() const OVERRIDE {
2956 // May call runtime and and therefore can throw.
2957 // TODO: finer grain decision.
2958 return !is_referrers_class_;
2959 }
2960
Calin Juravleacf735c2015-02-12 15:25:22 +00002961 ReferenceTypeInfo GetLoadedClassRTI() {
2962 return loaded_class_rti_;
2963 }
2964
2965 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
2966 // Make sure we only set exact types (the loaded class should never be merged).
2967 DCHECK(rti.IsExact());
2968 loaded_class_rti_ = rti;
2969 }
2970
2971 bool IsResolved() {
2972 return loaded_class_rti_.IsExact();
2973 }
2974
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002975 DECLARE_INSTRUCTION(LoadClass);
2976
2977 private:
2978 const uint16_t type_index_;
2979 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002980 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002981 // Whether this instruction must generate the initialization check.
2982 // Used for code generation.
2983 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002984
Calin Juravleacf735c2015-02-12 15:25:22 +00002985 ReferenceTypeInfo loaded_class_rti_;
2986
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002987 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2988};
2989
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002990class HLoadString : public HExpression<0> {
2991 public:
2992 HLoadString(uint32_t string_index, uint32_t dex_pc)
2993 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2994 string_index_(string_index),
2995 dex_pc_(dex_pc) {}
2996
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002997 bool CanBeMoved() const OVERRIDE { return true; }
2998
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002999 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3000 return other->AsLoadString()->string_index_ == string_index_;
3001 }
3002
3003 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3004
3005 uint32_t GetDexPc() const { return dex_pc_; }
3006 uint32_t GetStringIndex() const { return string_index_; }
3007
3008 // TODO: Can we deopt or debug when we resolve a string?
3009 bool NeedsEnvironment() const OVERRIDE { return false; }
3010
3011 DECLARE_INSTRUCTION(LoadString);
3012
3013 private:
3014 const uint32_t string_index_;
3015 const uint32_t dex_pc_;
3016
3017 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3018};
3019
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003020// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003021/**
3022 * Performs an initialization check on its Class object input.
3023 */
3024class HClinitCheck : public HExpression<1> {
3025 public:
3026 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
3027 : HExpression(Primitive::kPrimNot, SideEffects::All()),
3028 dex_pc_(dex_pc) {
3029 SetRawInputAt(0, constant);
3030 }
3031
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003032 bool CanBeMoved() const OVERRIDE { return true; }
3033 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3034 UNUSED(other);
3035 return true;
3036 }
3037
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003038 bool NeedsEnvironment() const OVERRIDE {
3039 // May call runtime to initialize the class.
3040 return true;
3041 }
3042
3043 uint32_t GetDexPc() const { return dex_pc_; }
3044
3045 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3046
3047 DECLARE_INSTRUCTION(ClinitCheck);
3048
3049 private:
3050 const uint32_t dex_pc_;
3051
3052 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3053};
3054
3055class HStaticFieldGet : public HExpression<1> {
3056 public:
3057 HStaticFieldGet(HInstruction* cls,
3058 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003059 MemberOffset field_offset,
3060 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003061 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003062 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003063 SetRawInputAt(0, cls);
3064 }
3065
Calin Juravle52c48962014-12-16 17:02:57 +00003066
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003067 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003068
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003069 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003070 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3071 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003072 }
3073
3074 size_t ComputeHashCode() const OVERRIDE {
3075 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3076 }
3077
Calin Juravle52c48962014-12-16 17:02:57 +00003078 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003079 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3080 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003081 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003082
3083 DECLARE_INSTRUCTION(StaticFieldGet);
3084
3085 private:
3086 const FieldInfo field_info_;
3087
3088 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3089};
3090
3091class HStaticFieldSet : public HTemplateInstruction<2> {
3092 public:
3093 HStaticFieldSet(HInstruction* cls,
3094 HInstruction* value,
3095 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003096 MemberOffset field_offset,
3097 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003098 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003099 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003100 SetRawInputAt(0, cls);
3101 SetRawInputAt(1, value);
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
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003109 HInstruction* GetValue() const { return InputAt(1); }
3110
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003111 DECLARE_INSTRUCTION(StaticFieldSet);
3112
3113 private:
3114 const FieldInfo field_info_;
3115
3116 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3117};
3118
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003119// Implement the move-exception DEX instruction.
3120class HLoadException : public HExpression<0> {
3121 public:
3122 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3123
3124 DECLARE_INSTRUCTION(LoadException);
3125
3126 private:
3127 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3128};
3129
3130class HThrow : public HTemplateInstruction<1> {
3131 public:
3132 HThrow(HInstruction* exception, uint32_t dex_pc)
3133 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3134 SetRawInputAt(0, exception);
3135 }
3136
3137 bool IsControlFlow() const OVERRIDE { return true; }
3138
3139 bool NeedsEnvironment() const OVERRIDE { return true; }
3140
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003141 bool CanThrow() const OVERRIDE { return true; }
3142
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003143 uint32_t GetDexPc() const { return dex_pc_; }
3144
3145 DECLARE_INSTRUCTION(Throw);
3146
3147 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003148 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003149
3150 DISALLOW_COPY_AND_ASSIGN(HThrow);
3151};
3152
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003153class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003154 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003155 HInstanceOf(HInstruction* object,
3156 HLoadClass* constant,
3157 bool class_is_final,
3158 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003159 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3160 class_is_final_(class_is_final),
3161 dex_pc_(dex_pc) {
3162 SetRawInputAt(0, object);
3163 SetRawInputAt(1, constant);
3164 }
3165
3166 bool CanBeMoved() const OVERRIDE { return true; }
3167
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003168 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003169 return true;
3170 }
3171
3172 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003173 return false;
3174 }
3175
3176 uint32_t GetDexPc() const { return dex_pc_; }
3177
3178 bool IsClassFinal() const { return class_is_final_; }
3179
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003180 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003181
3182 private:
3183 const bool class_is_final_;
3184 const uint32_t dex_pc_;
3185
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003186 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3187};
3188
Calin Juravleb1498f62015-02-16 13:13:29 +00003189class HBoundType : public HExpression<1> {
3190 public:
3191 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3192 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3193 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003194 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003195 SetRawInputAt(0, input);
3196 }
3197
3198 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3199
3200 bool CanBeNull() const OVERRIDE {
3201 // `null instanceof ClassX` always return false so we can't be null.
3202 return false;
3203 }
3204
3205 DECLARE_INSTRUCTION(BoundType);
3206
3207 private:
3208 // Encodes the most upper class that this instruction can have. In other words
3209 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3210 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3211 const ReferenceTypeInfo bound_type_;
3212
3213 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3214};
3215
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003216class HCheckCast : public HTemplateInstruction<2> {
3217 public:
3218 HCheckCast(HInstruction* object,
3219 HLoadClass* constant,
3220 bool class_is_final,
3221 uint32_t dex_pc)
3222 : HTemplateInstruction(SideEffects::None()),
3223 class_is_final_(class_is_final),
3224 dex_pc_(dex_pc) {
3225 SetRawInputAt(0, object);
3226 SetRawInputAt(1, constant);
3227 }
3228
3229 bool CanBeMoved() const OVERRIDE { return true; }
3230
3231 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3232 return true;
3233 }
3234
3235 bool NeedsEnvironment() const OVERRIDE {
3236 // Instruction may throw a CheckCastError.
3237 return true;
3238 }
3239
3240 bool CanThrow() const OVERRIDE { return true; }
3241
3242 uint32_t GetDexPc() const { return dex_pc_; }
3243
3244 bool IsClassFinal() const { return class_is_final_; }
3245
3246 DECLARE_INSTRUCTION(CheckCast);
3247
3248 private:
3249 const bool class_is_final_;
3250 const uint32_t dex_pc_;
3251
3252 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003253};
3254
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003255class HMonitorOperation : public HTemplateInstruction<1> {
3256 public:
3257 enum OperationKind {
3258 kEnter,
3259 kExit,
3260 };
3261
3262 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3263 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3264 SetRawInputAt(0, object);
3265 }
3266
3267 // Instruction may throw a Java exception, so we need an environment.
3268 bool NeedsEnvironment() const OVERRIDE { return true; }
3269 bool CanThrow() const OVERRIDE { return true; }
3270
3271 uint32_t GetDexPc() const { return dex_pc_; }
3272
3273 bool IsEnter() const { return kind_ == kEnter; }
3274
3275 DECLARE_INSTRUCTION(MonitorOperation);
3276
Calin Juravle52c48962014-12-16 17:02:57 +00003277 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003278 const OperationKind kind_;
3279 const uint32_t dex_pc_;
3280
3281 private:
3282 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3283};
3284
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003285class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003286 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003287 MoveOperands(Location source, Location destination, HInstruction* instruction)
3288 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003289
3290 Location GetSource() const { return source_; }
3291 Location GetDestination() const { return destination_; }
3292
3293 void SetSource(Location value) { source_ = value; }
3294 void SetDestination(Location value) { destination_ = value; }
3295
3296 // The parallel move resolver marks moves as "in-progress" by clearing the
3297 // destination (but not the source).
3298 Location MarkPending() {
3299 DCHECK(!IsPending());
3300 Location dest = destination_;
3301 destination_ = Location::NoLocation();
3302 return dest;
3303 }
3304
3305 void ClearPending(Location dest) {
3306 DCHECK(IsPending());
3307 destination_ = dest;
3308 }
3309
3310 bool IsPending() const {
3311 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3312 return destination_.IsInvalid() && !source_.IsInvalid();
3313 }
3314
3315 // True if this blocks a move from the given location.
3316 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003317 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003318 }
3319
3320 // A move is redundant if it's been eliminated, if its source and
3321 // destination are the same, or if its destination is unneeded.
3322 bool IsRedundant() const {
3323 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3324 }
3325
3326 // We clear both operands to indicate move that's been eliminated.
3327 void Eliminate() {
3328 source_ = destination_ = Location::NoLocation();
3329 }
3330
3331 bool IsEliminated() const {
3332 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3333 return source_.IsInvalid();
3334 }
3335
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003336 HInstruction* GetInstruction() const { return instruction_; }
3337
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003338 private:
3339 Location source_;
3340 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003341 // The instruction this move is assocatied with. Null when this move is
3342 // for moving an input in the expected locations of user (including a phi user).
3343 // This is only used in debug mode, to ensure we do not connect interval siblings
3344 // in the same parallel move.
3345 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003346};
3347
3348static constexpr size_t kDefaultNumberOfMoves = 4;
3349
3350class HParallelMove : public HTemplateInstruction<0> {
3351 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003352 explicit HParallelMove(ArenaAllocator* arena)
3353 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003354
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003355 void AddMove(Location source, Location destination, HInstruction* instruction) {
3356 DCHECK(source.IsValid());
3357 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003358 if (kIsDebugBuild) {
3359 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003360 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003361 if (moves_.Get(i).GetInstruction() == instruction) {
3362 // Special case the situation where the move is for the spill slot
3363 // of the instruction.
3364 if ((GetPrevious() == instruction)
3365 || ((GetPrevious() == nullptr)
3366 && instruction->IsPhi()
3367 && instruction->GetBlock() == GetBlock())) {
3368 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3369 << "Doing parallel moves for the same instruction.";
3370 } else {
3371 DCHECK(false) << "Doing parallel moves for the same instruction.";
3372 }
3373 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003374 }
3375 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003376 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3377 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3378 << "Same destination for two moves in a parallel move.";
3379 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003380 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003381 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003382 }
3383
3384 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003385 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003386 }
3387
3388 size_t NumMoves() const { return moves_.Size(); }
3389
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003390 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003391
3392 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003393 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003394
3395 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3396};
3397
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003398class HGraphVisitor : public ValueObject {
3399 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003400 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3401 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003402
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003403 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003404 virtual void VisitBasicBlock(HBasicBlock* block);
3405
Roland Levillain633021e2014-10-01 14:12:25 +01003406 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003407 void VisitInsertionOrder();
3408
Roland Levillain633021e2014-10-01 14:12:25 +01003409 // Visit the graph following dominator tree reverse post-order.
3410 void VisitReversePostOrder();
3411
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003412 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003413
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003414 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003415#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003416 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3417
3418 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3419
3420#undef DECLARE_VISIT_INSTRUCTION
3421
3422 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003423 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003424
3425 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3426};
3427
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003428class HGraphDelegateVisitor : public HGraphVisitor {
3429 public:
3430 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3431 virtual ~HGraphDelegateVisitor() {}
3432
3433 // Visit functions that delegate to to super class.
3434#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003435 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003436
3437 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3438
3439#undef DECLARE_VISIT_INSTRUCTION
3440
3441 private:
3442 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3443};
3444
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003445class HInsertionOrderIterator : public ValueObject {
3446 public:
3447 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3448
3449 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3450 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3451 void Advance() { ++index_; }
3452
3453 private:
3454 const HGraph& graph_;
3455 size_t index_;
3456
3457 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3458};
3459
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003460class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003461 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003462 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003463
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003464 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3465 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003466 void Advance() { ++index_; }
3467
3468 private:
3469 const HGraph& graph_;
3470 size_t index_;
3471
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003472 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003473};
3474
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003475class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003476 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003477 explicit HPostOrderIterator(const HGraph& graph)
3478 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003479
3480 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003481 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003482 void Advance() { --index_; }
3483
3484 private:
3485 const HGraph& graph_;
3486 size_t index_;
3487
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003488 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003489};
3490
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003491// Iterator over the blocks that art part of the loop. Includes blocks part
3492// of an inner loop. The order in which the blocks are iterated is on their
3493// block id.
3494class HBlocksInLoopIterator : public ValueObject {
3495 public:
3496 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3497 : blocks_in_loop_(info.GetBlocks()),
3498 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3499 index_(0) {
3500 if (!blocks_in_loop_.IsBitSet(index_)) {
3501 Advance();
3502 }
3503 }
3504
3505 bool Done() const { return index_ == blocks_.Size(); }
3506 HBasicBlock* Current() const { return blocks_.Get(index_); }
3507 void Advance() {
3508 ++index_;
3509 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3510 if (blocks_in_loop_.IsBitSet(index_)) {
3511 break;
3512 }
3513 }
3514 }
3515
3516 private:
3517 const BitVector& blocks_in_loop_;
3518 const GrowableArray<HBasicBlock*>& blocks_;
3519 size_t index_;
3520
3521 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3522};
3523
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003524inline int64_t Int64FromConstant(HConstant* constant) {
3525 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3526 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3527 : constant->AsLongConstant()->GetValue();
3528}
3529
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003530} // namespace art
3531
3532#endif // ART_COMPILER_OPTIMIZING_NODES_H_