blob: ec3d7438ab2816f74324836c10f2ee2fce2ef8f3 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Mathieu Chartierb666f482015-02-18 14:33:14 -080020#include "base/arena_object.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000021#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000022#include "handle.h"
23#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000024#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010025#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000026#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010027#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000029#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "utils/growable_array.h"
31
32namespace art {
33
David Brazdil1abb4192015-02-17 18:33:36 +000034class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010036class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000038class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000039class HInvoke;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040class HGraphVisitor;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000041class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010042class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010043class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010044class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000045class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046
47static const int kDefaultNumberOfBlocks = 8;
48static const int kDefaultNumberOfSuccessors = 2;
49static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010050static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000051static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
Calin Juravle9aec02f2014-11-18 23:06:35 +000053static constexpr uint32_t kMaxIntShiftValue = 0x1f;
54static constexpr uint64_t kMaxLongShiftValue = 0x3f;
55
Dave Allison20dfc792014-06-16 20:44:29 -070056enum IfCondition {
57 kCondEQ,
58 kCondNE,
59 kCondLT,
60 kCondLE,
61 kCondGT,
62 kCondGE,
63};
64
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010065class HInstructionList {
66 public:
67 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
68
69 void AddInstruction(HInstruction* instruction);
70 void RemoveInstruction(HInstruction* instruction);
71
Roland Levillain6b469232014-09-25 10:10:38 +010072 // Return true if this list contains `instruction`.
73 bool Contains(HInstruction* instruction) const;
74
Roland Levillainccc07a92014-09-16 14:48:16 +010075 // Return true if `instruction1` is found before `instruction2` in
76 // this instruction list and false otherwise. Abort if none
77 // of these instructions is found.
78 bool FoundBefore(const HInstruction* instruction1,
79 const HInstruction* instruction2) const;
80
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000081 bool IsEmpty() const { return first_instruction_ == nullptr; }
82 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
83
84 // Update the block of all instructions to be `block`.
85 void SetBlockOfInstructions(HBasicBlock* block) const;
86
87 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
88 void Add(const HInstructionList& instruction_list);
89
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010090 private:
91 HInstruction* first_instruction_;
92 HInstruction* last_instruction_;
93
94 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000095 friend class HGraph;
96 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010097 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010098 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010099
100 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
101};
102
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000103// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700104class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000105 public:
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000106 HGraph(ArenaAllocator* arena, bool debuggable = false, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000107 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000108 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100109 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700110 entry_block_(nullptr),
111 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100112 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100113 number_of_vregs_(0),
114 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000115 temporaries_vreg_slots_(0),
Mingyao Yange4335eb2015-03-02 15:14:13 -0800116 has_array_accesses_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000117 debuggable_(debuggable),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000118 current_instruction_id_(start_instruction_id) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000119
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000120 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100121 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100122 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000123
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000124 HBasicBlock* GetEntryBlock() const { return entry_block_; }
125 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000126
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000127 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
128 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000129
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000130 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100131
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000132 // Try building the SSA form of this graph, with dominance computation and loop
133 // recognition. Returns whether it was successful in doing all these steps.
134 bool TryBuildingSsa() {
135 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000136 // The SSA builder requires loops to all be natural. Specifically, the dead phi
137 // elimination phase checks the consistency of the graph when doing a post-order
138 // visit for eliminating dead phis: a dead phi can only have loop header phi
139 // users remaining when being visited.
140 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000141 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000142 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000143 }
144
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000145 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000146 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100147 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000148
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000149 // Analyze all natural loops in this graph. Returns false if one
150 // loop is not natural, that is the header does not dominate the
151 // back edge.
152 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100153
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000154 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
155 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
156
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100157 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
158 void SimplifyLoop(HBasicBlock* header);
159
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 int32_t GetNextInstructionId() {
161 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000162 return current_instruction_id_++;
163 }
164
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000165 int32_t GetCurrentInstructionId() const {
166 return current_instruction_id_;
167 }
168
169 void SetCurrentInstructionId(int32_t id) {
170 current_instruction_id_ = id;
171 }
172
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100173 uint16_t GetMaximumNumberOfOutVRegs() const {
174 return maximum_number_of_out_vregs_;
175 }
176
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
178 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100179 }
180
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000181 void UpdateTemporariesVRegSlots(size_t slots) {
182 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100183 }
184
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000185 size_t GetTemporariesVRegSlots() const {
186 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100187 }
188
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100189 void SetNumberOfVRegs(uint16_t number_of_vregs) {
190 number_of_vregs_ = number_of_vregs;
191 }
192
193 uint16_t GetNumberOfVRegs() const {
194 return number_of_vregs_;
195 }
196
197 void SetNumberOfInVRegs(uint16_t value) {
198 number_of_in_vregs_ = value;
199 }
200
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100201 uint16_t GetNumberOfLocalVRegs() const {
202 return number_of_vregs_ - number_of_in_vregs_;
203 }
204
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100205 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
206 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100207 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100208
Mingyao Yange4335eb2015-03-02 15:14:13 -0800209 bool HasArrayAccesses() const {
210 return has_array_accesses_;
211 }
212
213 void SetHasArrayAccesses(bool value) {
214 has_array_accesses_ = value;
215 }
216
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000217 bool IsDebuggable() const { return debuggable_; }
218
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000219 HNullConstant* GetNullConstant();
220
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000221 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000222 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
223 void VisitBlockForDominatorTree(HBasicBlock* block,
224 HBasicBlock* predecessor,
225 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100226 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000227 void VisitBlockForBackEdges(HBasicBlock* block,
228 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100229 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000230 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000231 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
232
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000233 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000234
235 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000236 GrowableArray<HBasicBlock*> blocks_;
237
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100238 // List of blocks to perform a reverse post order tree traversal.
239 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000240
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000241 HBasicBlock* entry_block_;
242 HBasicBlock* exit_block_;
243
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100244 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100245 uint16_t maximum_number_of_out_vregs_;
246
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100247 // The number of virtual registers in this method. Contains the parameters.
248 uint16_t number_of_vregs_;
249
250 // The number of virtual registers used by parameters of this method.
251 uint16_t number_of_in_vregs_;
252
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000253 // Number of vreg size slots that the temporaries use (used in baseline compiler).
254 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100255
Mingyao Yange4335eb2015-03-02 15:14:13 -0800256 // Has array accesses. We can totally skip BCE if it's false.
257 bool has_array_accesses_;
258
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000259 // Indicates whether the graph should be compiled in a way that
260 // ensures full debuggability. If false, we can apply more
261 // aggressive optimizations that may limit the level of debugging.
262 const bool debuggable_;
263
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000264 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000265 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000266
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000267 // Cached null constant that might be created when building SSA form.
268 HNullConstant* cached_null_constant_;
269
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000270 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000271 DISALLOW_COPY_AND_ASSIGN(HGraph);
272};
273
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700274class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000275 public:
276 HLoopInformation(HBasicBlock* header, HGraph* graph)
277 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100278 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100279 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100280 // Make bit vector growable, as the number of blocks may change.
281 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100282
283 HBasicBlock* GetHeader() const {
284 return header_;
285 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000286
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000287 void SetHeader(HBasicBlock* block) {
288 header_ = block;
289 }
290
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100291 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
292 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
293 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
294
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000295 void AddBackEdge(HBasicBlock* back_edge) {
296 back_edges_.Add(back_edge);
297 }
298
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100299 void RemoveBackEdge(HBasicBlock* back_edge) {
300 back_edges_.Delete(back_edge);
301 }
302
303 bool IsBackEdge(HBasicBlock* block) {
304 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
305 if (back_edges_.Get(i) == block) return true;
306 }
307 return false;
308 }
309
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000310 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000311 return back_edges_.Size();
312 }
313
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100314 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100315
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100316 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
317 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100318 }
319
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100320 void ClearBackEdges() {
321 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100322 }
323
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100324 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
325 // that is the header dominates the back edge.
326 bool Populate();
327
328 // Returns whether this loop information contains `block`.
329 // Note that this loop information *must* be populated before entering this function.
330 bool Contains(const HBasicBlock& block) const;
331
332 // Returns whether this loop information is an inner loop of `other`.
333 // Note that `other` *must* be populated before entering this function.
334 bool IsIn(const HLoopInformation& other) const;
335
336 const ArenaBitVector& GetBlocks() const { return blocks_; }
337
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000338 void Add(HBasicBlock* block);
339
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000340 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100341 // Internal recursive implementation of `Populate`.
342 void PopulateRecursive(HBasicBlock* block);
343
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000344 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100345 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000346 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100347 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000348
349 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
350};
351
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100352static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100353static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100354
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355// A block in a method. Contains the list of instructions represented
356// as a double linked list. Each block knows its predecessors and
357// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100358
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700359class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000360 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100361 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000362 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000363 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
364 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000365 loop_information_(nullptr),
366 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100367 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100368 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100369 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100370 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000371 lifetime_end_(kNoLifetime),
372 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100374 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
375 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000376 }
377
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100378 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
379 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000380 }
381
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100382 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
383 return dominated_blocks_;
384 }
385
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100386 bool IsEntryBlock() const {
387 return graph_->GetEntryBlock() == this;
388 }
389
390 bool IsExitBlock() const {
391 return graph_->GetExitBlock() == this;
392 }
393
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000394 void AddBackEdge(HBasicBlock* back_edge) {
395 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000396 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000397 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100398 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000399 loop_information_->AddBackEdge(back_edge);
400 }
401
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000402 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000403 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000404
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000405 int GetBlockId() const { return block_id_; }
406 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000407
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000408 HBasicBlock* GetDominator() const { return dominator_; }
409 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100410 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000411 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
412 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
413 if (dominated_blocks_.Get(i) == existing) {
414 dominated_blocks_.Put(i, new_block);
415 return;
416 }
417 }
418 LOG(FATAL) << "Unreachable";
419 UNREACHABLE();
420 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000421
422 int NumberOfBackEdges() const {
423 return loop_information_ == nullptr
424 ? 0
425 : loop_information_->NumberOfBackEdges();
426 }
427
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100428 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
429 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100430 const HInstructionList& GetInstructions() const { return instructions_; }
431 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100432 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000433
434 void AddSuccessor(HBasicBlock* block) {
435 successors_.Add(block);
436 block->predecessors_.Add(this);
437 }
438
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100439 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
440 size_t successor_index = GetSuccessorIndexOf(existing);
441 DCHECK_NE(successor_index, static_cast<size_t>(-1));
442 existing->RemovePredecessor(this);
443 new_block->predecessors_.Add(this);
444 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000445 }
446
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000447 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
448 size_t predecessor_index = GetPredecessorIndexOf(existing);
449 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
450 existing->RemoveSuccessor(this);
451 new_block->successors_.Add(this);
452 predecessors_.Put(predecessor_index, new_block);
453 }
454
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100455 void RemovePredecessor(HBasicBlock* block) {
456 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100457 }
458
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000459 void RemoveSuccessor(HBasicBlock* block) {
460 successors_.Delete(block);
461 }
462
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100463 void ClearAllPredecessors() {
464 predecessors_.Reset();
465 }
466
467 void AddPredecessor(HBasicBlock* block) {
468 predecessors_.Add(block);
469 block->successors_.Add(this);
470 }
471
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100472 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100473 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100474 HBasicBlock* temp = predecessors_.Get(0);
475 predecessors_.Put(0, predecessors_.Get(1));
476 predecessors_.Put(1, temp);
477 }
478
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100479 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
480 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
481 if (predecessors_.Get(i) == predecessor) {
482 return i;
483 }
484 }
485 return -1;
486 }
487
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100488 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
489 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
490 if (successors_.Get(i) == successor) {
491 return i;
492 }
493 }
494 return -1;
495 }
496
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000497 // Split the block into two blocks just after `cursor`. Returns the newly
498 // created block. Note that this method just updates raw block information,
499 // like predecessors, successors, dominators, and instruction list. It does not
500 // update the graph, reverse post order, loop information, nor make sure the
501 // blocks are consistent (for example ending with a control flow instruction).
502 HBasicBlock* SplitAfter(HInstruction* cursor);
503
504 // Merge `other` at the end of `this`. Successors and dominated blocks of
505 // `other` are changed to be successors and dominated blocks of `this`. Note
506 // that this method does not update the graph, reverse post order, loop
507 // information, nor make sure the blocks are consistent (for example ending
508 // with a control flow instruction).
509 void MergeWith(HBasicBlock* other);
510
511 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
512 // of `this` are moved to `other`.
513 // Note that this method does not update the graph, reverse post order, loop
514 // information, nor make sure the blocks are consistent (for example ending
515 void ReplaceWith(HBasicBlock* other);
516
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000517 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100518 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100519 // Replace instruction `initial` with `replacement` within this block.
520 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
521 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100522 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100523 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000524 // RemoveInstruction and RemovePhi delete a given instruction from the respective
525 // instruction list. With 'ensure_safety' set to true, it verifies that the
526 // instruction is not in use and removes it from the use lists of its inputs.
527 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
528 void RemovePhi(HPhi* phi, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100529
530 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100531 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100532 }
533
Roland Levillain6b879dd2014-09-22 17:13:44 +0100534 bool IsLoopPreHeaderFirstPredecessor() const {
535 DCHECK(IsLoopHeader());
536 DCHECK(!GetPredecessors().IsEmpty());
537 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
538 }
539
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100540 HLoopInformation* GetLoopInformation() const {
541 return loop_information_;
542 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000543
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000544 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100545 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000546 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100547 void SetInLoop(HLoopInformation* info) {
548 if (IsLoopHeader()) {
549 // Nothing to do. This just means `info` is an outer loop.
550 } else if (loop_information_ == nullptr) {
551 loop_information_ = info;
552 } else if (loop_information_->Contains(*info->GetHeader())) {
553 // Block is currently part of an outer loop. Make it part of this inner loop.
554 // Note that a non loop header having a loop information means this loop information
555 // has already been populated
556 loop_information_ = info;
557 } else {
558 // Block is part of an inner loop. Do not update the loop information.
559 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
560 // at this point, because this method is being called while populating `info`.
561 }
562 }
563
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000564 // Raw update of the loop information.
565 void SetLoopInformation(HLoopInformation* info) {
566 loop_information_ = info;
567 }
568
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100569 bool IsInLoop() const { return loop_information_ != nullptr; }
570
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100571 // Returns wheter this block dominates the blocked passed as parameter.
572 bool Dominates(HBasicBlock* block) const;
573
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100574 size_t GetLifetimeStart() const { return lifetime_start_; }
575 size_t GetLifetimeEnd() const { return lifetime_end_; }
576
577 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
578 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
579
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100580 uint32_t GetDexPc() const { return dex_pc_; }
581
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000582 bool IsCatchBlock() const { return is_catch_block_; }
583 void SetIsCatchBlock() { is_catch_block_ = true; }
584
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000585 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000586 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000587 GrowableArray<HBasicBlock*> predecessors_;
588 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100589 HInstructionList instructions_;
590 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000591 HLoopInformation* loop_information_;
592 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100593 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000594 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100595 // The dex program counter of the first instruction of this block.
596 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100597 size_t lifetime_start_;
598 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000599 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000600
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000601 friend class HGraph;
602 friend class HInstruction;
603
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000604 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
605};
606
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100607#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
608 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000609 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000610 M(ArrayGet, Instruction) \
611 M(ArrayLength, Instruction) \
612 M(ArraySet, Instruction) \
613 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000614 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000615 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100616 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000617 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100618 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000619 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000620 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000621 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100622 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000623 M(Exit, Instruction) \
624 M(FloatConstant, Constant) \
625 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100626 M(GreaterThan, Condition) \
627 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100628 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000629 M(InstanceFieldGet, Instruction) \
630 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000631 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100632 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000633 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000634 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100635 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000636 M(LessThan, Condition) \
637 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000638 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000639 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100640 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000641 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100642 M(Local, Instruction) \
643 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000644 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000645 M(Mul, BinaryOperation) \
646 M(Neg, UnaryOperation) \
647 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100648 M(NewInstance, Instruction) \
Roland Levillain1cc5f252014-10-22 18:06:21 +0100649 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000650 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000651 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000652 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000653 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100654 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000655 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100656 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000657 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100658 M(Return, Instruction) \
659 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000660 M(Shl, BinaryOperation) \
661 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100662 M(StaticFieldGet, Instruction) \
663 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100664 M(StoreLocal, Instruction) \
665 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100666 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000667 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000668 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000669 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000670 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000671 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000672
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100673#define FOR_EACH_INSTRUCTION(M) \
674 FOR_EACH_CONCRETE_INSTRUCTION(M) \
675 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100676 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100677 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100678 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700679
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100680#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000681FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
682#undef FORWARD_DECLARATION
683
Roland Levillainccc07a92014-09-16 14:48:16 +0100684#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000685 InstructionKind GetKind() const OVERRIDE { return k##type; } \
686 const char* DebugName() const OVERRIDE { return #type; } \
687 const H##type* As##type() const OVERRIDE { return this; } \
688 H##type* As##type() OVERRIDE { return this; } \
689 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100690 return other->Is##type(); \
691 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000692 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000693
David Brazdiled596192015-01-23 10:39:45 +0000694template <typename T> class HUseList;
695
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100696template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700697class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000698 public:
David Brazdiled596192015-01-23 10:39:45 +0000699 HUseListNode* GetPrevious() const { return prev_; }
700 HUseListNode* GetNext() const { return next_; }
701 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100702 size_t GetIndex() const { return index_; }
703
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000704 private:
David Brazdiled596192015-01-23 10:39:45 +0000705 HUseListNode(T user, size_t index)
706 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
707
708 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100709 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000710 HUseListNode<T>* prev_;
711 HUseListNode<T>* next_;
712
713 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000714
715 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
716};
717
David Brazdiled596192015-01-23 10:39:45 +0000718template <typename T>
719class HUseList : public ValueObject {
720 public:
721 HUseList() : first_(nullptr) {}
722
723 void Clear() {
724 first_ = nullptr;
725 }
726
727 // Adds a new entry at the beginning of the use list and returns
728 // the newly created node.
729 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000730 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000731 if (IsEmpty()) {
732 first_ = new_node;
733 } else {
734 first_->prev_ = new_node;
735 new_node->next_ = first_;
736 first_ = new_node;
737 }
738 return new_node;
739 }
740
741 HUseListNode<T>* GetFirst() const {
742 return first_;
743 }
744
745 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000746 DCHECK(node != nullptr);
747 DCHECK(Contains(node));
748
David Brazdiled596192015-01-23 10:39:45 +0000749 if (node->prev_ != nullptr) {
750 node->prev_->next_ = node->next_;
751 }
752 if (node->next_ != nullptr) {
753 node->next_->prev_ = node->prev_;
754 }
755 if (node == first_) {
756 first_ = node->next_;
757 }
758 }
759
David Brazdil1abb4192015-02-17 18:33:36 +0000760 bool Contains(const HUseListNode<T>* node) const {
761 if (node == nullptr) {
762 return false;
763 }
764 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
765 if (current == node) {
766 return true;
767 }
768 }
769 return false;
770 }
771
David Brazdiled596192015-01-23 10:39:45 +0000772 bool IsEmpty() const {
773 return first_ == nullptr;
774 }
775
776 bool HasOnlyOneUse() const {
777 return first_ != nullptr && first_->next_ == nullptr;
778 }
779
780 private:
781 HUseListNode<T>* first_;
782};
783
784template<typename T>
785class HUseIterator : public ValueObject {
786 public:
787 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
788
789 bool Done() const { return current_ == nullptr; }
790
791 void Advance() {
792 DCHECK(!Done());
793 current_ = current_->GetNext();
794 }
795
796 HUseListNode<T>* Current() const {
797 DCHECK(!Done());
798 return current_;
799 }
800
801 private:
802 HUseListNode<T>* current_;
803
804 friend class HValue;
805};
806
David Brazdil1abb4192015-02-17 18:33:36 +0000807// This class is used by HEnvironment and HInstruction classes to record the
808// instructions they use and pointers to the corresponding HUseListNodes kept
809// by the used instructions.
810template <typename T>
811class HUserRecord : public ValueObject {
812 public:
813 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
814 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
815
816 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
817 : instruction_(old_record.instruction_), use_node_(use_node) {
818 DCHECK(instruction_ != nullptr);
819 DCHECK(use_node_ != nullptr);
820 DCHECK(old_record.use_node_ == nullptr);
821 }
822
823 HInstruction* GetInstruction() const { return instruction_; }
824 HUseListNode<T>* GetUseNode() const { return use_node_; }
825
826 private:
827 // Instruction used by the user.
828 HInstruction* instruction_;
829
830 // Corresponding entry in the use list kept by 'instruction_'.
831 HUseListNode<T>* use_node_;
832};
833
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100834// Represents the side effects an instruction may have.
835class SideEffects : public ValueObject {
836 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100837 SideEffects() : flags_(0) {}
838
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100839 static SideEffects None() {
840 return SideEffects(0);
841 }
842
843 static SideEffects All() {
844 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
845 }
846
847 static SideEffects ChangesSomething() {
848 return SideEffects((1 << kFlagChangesCount) - 1);
849 }
850
851 static SideEffects DependsOnSomething() {
852 int count = kFlagDependsOnCount - kFlagChangesCount;
853 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
854 }
855
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100856 SideEffects Union(SideEffects other) const {
857 return SideEffects(flags_ | other.flags_);
858 }
859
Roland Levillain72bceff2014-09-15 18:29:00 +0100860 bool HasSideEffects() const {
861 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
862 return (flags_ & all_bits_set) != 0;
863 }
864
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100865 bool HasAllSideEffects() const {
866 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
867 return all_bits_set == (flags_ & all_bits_set);
868 }
869
870 bool DependsOn(SideEffects other) const {
871 size_t depends_flags = other.ComputeDependsFlags();
872 return (flags_ & depends_flags) != 0;
873 }
874
875 bool HasDependencies() const {
876 int count = kFlagDependsOnCount - kFlagChangesCount;
877 size_t all_bits_set = (1 << count) - 1;
878 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
879 }
880
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100881 private:
882 static constexpr int kFlagChangesSomething = 0;
883 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
884
885 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
886 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
887
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100888 explicit SideEffects(size_t flags) : flags_(flags) {}
889
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100890 size_t ComputeDependsFlags() const {
891 return flags_ << kFlagChangesCount;
892 }
893
894 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100895};
896
David Brazdiled596192015-01-23 10:39:45 +0000897// A HEnvironment object contains the values of virtual registers at a given location.
898class HEnvironment : public ArenaObject<kArenaAllocMisc> {
899 public:
900 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
901 : vregs_(arena, number_of_vregs) {
902 vregs_.SetSize(number_of_vregs);
903 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000904 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000905 }
906 }
907
908 void CopyFrom(HEnvironment* env);
909
910 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +0000911 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +0000912 }
913
914 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +0000915 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +0000916 }
917
David Brazdil1abb4192015-02-17 18:33:36 +0000918 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +0000919
920 size_t Size() const { return vregs_.Size(); }
921
922 private:
David Brazdil1abb4192015-02-17 18:33:36 +0000923 // Record instructions' use entries of this environment for constant-time removal.
924 // It should only be called by HInstruction when a new environment use is added.
925 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
926 DCHECK(env_use->GetUser() == this);
927 size_t index = env_use->GetIndex();
928 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
929 }
David Brazdiled596192015-01-23 10:39:45 +0000930
David Brazdil1abb4192015-02-17 18:33:36 +0000931 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +0000932
David Brazdil1abb4192015-02-17 18:33:36 +0000933 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +0000934
935 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
936};
937
Calin Juravleacf735c2015-02-12 15:25:22 +0000938class ReferenceTypeInfo : ValueObject {
939 public:
Calin Juravleb1498f62015-02-16 13:13:29 +0000940 typedef Handle<mirror::Class> TypeHandle;
941
942 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
943 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
944 if (type_handle->IsObjectClass()) {
945 // Override the type handle to be consistent with the case when we get to
946 // Top but don't have the Object class available. It avoids having to guess
947 // what value the type_handle has when it's Top.
948 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
949 } else {
950 return ReferenceTypeInfo(type_handle, is_exact, false);
951 }
952 }
953
954 static ReferenceTypeInfo CreateTop(bool is_exact) {
955 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +0000956 }
957
958 bool IsExact() const { return is_exact_; }
959 bool IsTop() const { return is_top_; }
960
961 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
962
Calin Juravleb1498f62015-02-16 13:13:29 +0000963 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000964 if (IsTop()) {
965 // Top (equivalent for java.lang.Object) is supertype of anything.
966 return true;
967 }
968 if (rti.IsTop()) {
969 // If we get here `this` is not Top() so it can't be a supertype.
970 return false;
971 }
972 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
973 }
974
975 // Returns true if the type information provide the same amount of details.
976 // Note that it does not mean that the instructions have the same actual type
977 // (e.g. tops are equal but they can be the result of a merge).
978 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
979 if (IsExact() != rti.IsExact()) {
980 return false;
981 }
982 if (IsTop() && rti.IsTop()) {
983 // `Top` means java.lang.Object, so the types are equivalent.
984 return true;
985 }
986 if (IsTop() || rti.IsTop()) {
987 // If only one is top or object than they are not equivalent.
988 // NB: We need this extra check because the type_handle of `Top` is invalid
989 // and we cannot inspect its reference.
990 return false;
991 }
992
993 // Finally check the types.
994 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
995 }
996
997 private:
Calin Juravleb1498f62015-02-16 13:13:29 +0000998 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
999 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1000 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1001
Calin Juravleacf735c2015-02-12 15:25:22 +00001002 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001003 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001004 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001005 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001006 bool is_exact_;
1007 // A true value here means that the object type should be java.lang.Object.
1008 // We don't have access to the corresponding mirror object every time so this
1009 // flag acts as a substitute. When true, the TypeHandle refers to a null
1010 // pointer and should not be used.
1011 bool is_top_;
1012};
1013
1014std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1015
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001016class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001017 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001018 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001019 : previous_(nullptr),
1020 next_(nullptr),
1021 block_(nullptr),
1022 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001023 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001024 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001025 locations_(nullptr),
1026 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001027 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001028 side_effects_(side_effects),
1029 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001030
Dave Allison20dfc792014-06-16 20:44:29 -07001031 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001032
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001033#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001034 enum InstructionKind {
1035 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1036 };
1037#undef DECLARE_KIND
1038
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001039 HInstruction* GetNext() const { return next_; }
1040 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001041
Calin Juravle77520bc2015-01-12 18:45:46 +00001042 HInstruction* GetNextDisregardingMoves() const;
1043 HInstruction* GetPreviousDisregardingMoves() const;
1044
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001045 HBasicBlock* GetBlock() const { return block_; }
1046 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001047 bool IsInBlock() const { return block_ != nullptr; }
1048 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001049 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001050
Roland Levillain6b879dd2014-09-22 17:13:44 +01001051 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001052 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001053
1054 virtual void Accept(HGraphVisitor* visitor) = 0;
1055 virtual const char* DebugName() const = 0;
1056
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001057 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001058 void SetRawInputAt(size_t index, HInstruction* input) {
1059 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1060 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001061
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001062 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001063 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001064 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001065 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001066
Calin Juravle61d544b2015-02-23 16:46:57 +00001067 virtual bool ActAsNullConstant() const { return false; }
1068
Calin Juravle10e244f2015-01-26 18:54:32 +00001069 // Does not apply for all instructions, but having this at top level greatly
1070 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001071 virtual bool CanBeNull() const {
1072 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1073 return true;
1074 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001075
Calin Juravle77520bc2015-01-12 18:45:46 +00001076 virtual bool CanDoImplicitNullCheck() const { return false; }
1077
Calin Juravleacf735c2015-02-12 15:25:22 +00001078 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001079 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001080 reference_type_info_ = reference_type_info;
1081 }
1082
Calin Juravle61d544b2015-02-23 16:46:57 +00001083 ReferenceTypeInfo GetReferenceTypeInfo() const {
1084 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1085 return reference_type_info_;
1086 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001087
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001088 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001089 DCHECK(user != nullptr);
1090 HUseListNode<HInstruction*>* use =
1091 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1092 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001093 }
1094
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001095 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001096 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001097 HUseListNode<HEnvironment*>* env_use =
1098 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1099 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001100 }
1101
David Brazdil1abb4192015-02-17 18:33:36 +00001102 void RemoveAsUserOfInput(size_t input) {
1103 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1104 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1105 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001106
David Brazdil1abb4192015-02-17 18:33:36 +00001107 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1108 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001109
David Brazdiled596192015-01-23 10:39:45 +00001110 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1111 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001112
Roland Levillain6c82d402014-10-13 16:10:27 +01001113 // Does this instruction strictly dominate `other_instruction`?
1114 // Returns false if this instruction and `other_instruction` are the same.
1115 // Aborts if this instruction and `other_instruction` are both phis.
1116 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001117
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001118 int GetId() const { return id_; }
1119 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001120
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001121 int GetSsaIndex() const { return ssa_index_; }
1122 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1123 bool HasSsaIndex() const { return ssa_index_ != -1; }
1124
1125 bool HasEnvironment() const { return environment_ != nullptr; }
1126 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001127 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
1128
Nicolas Geoffray39468442014-09-02 15:17:15 +01001129 // Returns the number of entries in the environment. Typically, that is the
1130 // number of dex registers in a method. It could be more in case of inlining.
1131 size_t EnvironmentSize() const;
1132
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001133 LocationSummary* GetLocations() const { return locations_; }
1134 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001135
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001136 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001137 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001138
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001139 // Move `this` instruction before `cursor`.
1140 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001141
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001142#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001143 bool Is##type() const { return (As##type() != nullptr); } \
1144 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001145 virtual H##type* As##type() { return nullptr; }
1146
1147 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1148#undef INSTRUCTION_TYPE_CHECK
1149
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001150 // Returns whether the instruction can be moved within the graph.
1151 virtual bool CanBeMoved() const { return false; }
1152
1153 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001154 virtual bool InstructionTypeEquals(HInstruction* other) const {
1155 UNUSED(other);
1156 return false;
1157 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001158
1159 // Returns whether any data encoded in the two instructions is equal.
1160 // This method does not look at the inputs. Both instructions must be
1161 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001162 virtual bool InstructionDataEquals(HInstruction* other) const {
1163 UNUSED(other);
1164 return false;
1165 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001166
1167 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001168 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001169 // 2) Their inputs are identical.
1170 bool Equals(HInstruction* other) const;
1171
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001172 virtual InstructionKind GetKind() const = 0;
1173
1174 virtual size_t ComputeHashCode() const {
1175 size_t result = GetKind();
1176 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1177 result = (result * 31) + InputAt(i)->GetId();
1178 }
1179 return result;
1180 }
1181
1182 SideEffects GetSideEffects() const { return side_effects_; }
1183
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001184 size_t GetLifetimePosition() const { return lifetime_position_; }
1185 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1186 LiveInterval* GetLiveInterval() const { return live_interval_; }
1187 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1188 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1189
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001190 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1191
1192 // Returns whether the code generation of the instruction will require to have access
1193 // to the current method. Such instructions are:
1194 // (1): Instructions that require an environment, as calling the runtime requires
1195 // to walk the stack and have the current method stored at a specific stack address.
1196 // (2): Object literals like classes and strings, that are loaded from the dex cache
1197 // fields of the current method.
1198 bool NeedsCurrentMethod() const {
1199 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1200 }
1201
David Brazdil1abb4192015-02-17 18:33:36 +00001202 protected:
1203 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1204 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1205
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001206 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001207 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1208
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001209 HInstruction* previous_;
1210 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001211 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001212
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001213 // An instruction gets an id when it is added to the graph.
1214 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001215 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001216 int id_;
1217
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001218 // When doing liveness analysis, instructions that have uses get an SSA index.
1219 int ssa_index_;
1220
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001221 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001222 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001223
1224 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001225 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001226
Nicolas Geoffray39468442014-09-02 15:17:15 +01001227 // The environment associated with this instruction. Not null if the instruction
1228 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001229 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001230
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001231 // Set by the code generator.
1232 LocationSummary* locations_;
1233
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001234 // Set by the liveness analysis.
1235 LiveInterval* live_interval_;
1236
1237 // Set by the liveness analysis, this is the position in a linear
1238 // order of blocks where this instruction's live interval start.
1239 size_t lifetime_position_;
1240
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001241 const SideEffects side_effects_;
1242
Calin Juravleacf735c2015-02-12 15:25:22 +00001243 // TODO: for primitive types this should be marked as invalid.
1244 ReferenceTypeInfo reference_type_info_;
1245
David Brazdil1abb4192015-02-17 18:33:36 +00001246 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001247 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001248 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001249 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001250 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001251
1252 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1253};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001254std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001255
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001256class HInputIterator : public ValueObject {
1257 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001258 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001259
1260 bool Done() const { return index_ == instruction_->InputCount(); }
1261 HInstruction* Current() const { return instruction_->InputAt(index_); }
1262 void Advance() { index_++; }
1263
1264 private:
1265 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001266 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001267
1268 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1269};
1270
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001271class HInstructionIterator : public ValueObject {
1272 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001273 explicit HInstructionIterator(const HInstructionList& instructions)
1274 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001275 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001276 }
1277
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001278 bool Done() const { return instruction_ == nullptr; }
1279 HInstruction* Current() const { return instruction_; }
1280 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001281 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001282 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001283 }
1284
1285 private:
1286 HInstruction* instruction_;
1287 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001288
1289 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001290};
1291
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001292class HBackwardInstructionIterator : public ValueObject {
1293 public:
1294 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1295 : instruction_(instructions.last_instruction_) {
1296 next_ = Done() ? nullptr : instruction_->GetPrevious();
1297 }
1298
1299 bool Done() const { return instruction_ == nullptr; }
1300 HInstruction* Current() const { return instruction_; }
1301 void Advance() {
1302 instruction_ = next_;
1303 next_ = Done() ? nullptr : instruction_->GetPrevious();
1304 }
1305
1306 private:
1307 HInstruction* instruction_;
1308 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001309
1310 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001311};
1312
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001313// An embedded container with N elements of type T. Used (with partial
1314// specialization for N=0) because embedded arrays cannot have size 0.
1315template<typename T, intptr_t N>
1316class EmbeddedArray {
1317 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001318 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001319
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001320 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001321
1322 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001323 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001324 return elements_[i];
1325 }
1326
1327 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001328 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001329 return elements_[i];
1330 }
1331
1332 const T& At(intptr_t i) const {
1333 return (*this)[i];
1334 }
1335
1336 void SetAt(intptr_t i, const T& val) {
1337 (*this)[i] = val;
1338 }
1339
1340 private:
1341 T elements_[N];
1342};
1343
1344template<typename T>
1345class EmbeddedArray<T, 0> {
1346 public:
1347 intptr_t length() const { return 0; }
1348 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001349 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001350 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001351 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001352 }
1353 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001354 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001355 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001356 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001357 }
1358};
1359
1360template<intptr_t N>
1361class HTemplateInstruction: public HInstruction {
1362 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001363 HTemplateInstruction<N>(SideEffects side_effects)
1364 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001365 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001366
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001367 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001368
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001369 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001370 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1371
1372 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1373 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001374 }
1375
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001376 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001377 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001378
1379 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001380};
1381
Dave Allison20dfc792014-06-16 20:44:29 -07001382template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001383class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001384 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001385 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1386 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001387 virtual ~HExpression() {}
1388
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001389 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001390
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001391 protected:
1392 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001393};
1394
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001395// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1396// instruction that branches to the exit block.
1397class HReturnVoid : public HTemplateInstruction<0> {
1398 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001399 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001400
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001401 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001402
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001403 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001404
1405 private:
1406 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1407};
1408
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001409// Represents dex's RETURN opcodes. A HReturn is a control flow
1410// instruction that branches to the exit block.
1411class HReturn : public HTemplateInstruction<1> {
1412 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001413 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001414 SetRawInputAt(0, value);
1415 }
1416
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001417 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001418
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001419 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001420
1421 private:
1422 DISALLOW_COPY_AND_ASSIGN(HReturn);
1423};
1424
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001425// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001426// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001427// exit block.
1428class HExit : public HTemplateInstruction<0> {
1429 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001430 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001431
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001432 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001433
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001434 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001435
1436 private:
1437 DISALLOW_COPY_AND_ASSIGN(HExit);
1438};
1439
1440// Jumps from one block to another.
1441class HGoto : public HTemplateInstruction<0> {
1442 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001443 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1444
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001445 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001446
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001447 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001448 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001449 }
1450
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001451 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001452
1453 private:
1454 DISALLOW_COPY_AND_ASSIGN(HGoto);
1455};
1456
Dave Allison20dfc792014-06-16 20:44:29 -07001457
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001458// Conditional branch. A block ending with an HIf instruction must have
1459// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001460class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001461 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001462 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001463 SetRawInputAt(0, input);
1464 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001465
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001466 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001467
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001468 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001469 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001470 }
1471
1472 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001473 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001474 }
1475
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001476 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001477
Dave Allison20dfc792014-06-16 20:44:29 -07001478 virtual bool IsIfInstruction() const { return true; }
1479
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001480 private:
1481 DISALLOW_COPY_AND_ASSIGN(HIf);
1482};
1483
Roland Levillain88cb1752014-10-20 16:36:47 +01001484class HUnaryOperation : public HExpression<1> {
1485 public:
1486 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1487 : HExpression(result_type, SideEffects::None()) {
1488 SetRawInputAt(0, input);
1489 }
1490
1491 HInstruction* GetInput() const { return InputAt(0); }
1492 Primitive::Type GetResultType() const { return GetType(); }
1493
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001494 bool CanBeMoved() const OVERRIDE { return true; }
1495 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001496 UNUSED(other);
1497 return true;
1498 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001499
Roland Levillain9240d6a2014-10-20 16:47:04 +01001500 // Try to statically evaluate `operation` and return a HConstant
1501 // containing the result of this evaluation. If `operation` cannot
1502 // be evaluated as a constant, return nullptr.
1503 HConstant* TryStaticEvaluation() const;
1504
1505 // Apply this operation to `x`.
1506 virtual int32_t Evaluate(int32_t x) const = 0;
1507 virtual int64_t Evaluate(int64_t x) const = 0;
1508
Roland Levillain88cb1752014-10-20 16:36:47 +01001509 DECLARE_INSTRUCTION(UnaryOperation);
1510
1511 private:
1512 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1513};
1514
Dave Allison20dfc792014-06-16 20:44:29 -07001515class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001516 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001517 HBinaryOperation(Primitive::Type result_type,
1518 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001519 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001520 SetRawInputAt(0, left);
1521 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001522 }
1523
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001524 HInstruction* GetLeft() const { return InputAt(0); }
1525 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001526 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001527
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001528 virtual bool IsCommutative() const { return false; }
1529
1530 // Put constant on the right.
1531 // Returns whether order is changed.
1532 bool OrderInputsWithConstantOnTheRight() {
1533 HInstruction* left = InputAt(0);
1534 HInstruction* right = InputAt(1);
1535 if (left->IsConstant() && !right->IsConstant()) {
1536 ReplaceInput(right, 0);
1537 ReplaceInput(left, 1);
1538 return true;
1539 }
1540 return false;
1541 }
1542
1543 // Order inputs by instruction id, but favor constant on the right side.
1544 // This helps GVN for commutative ops.
1545 void OrderInputs() {
1546 DCHECK(IsCommutative());
1547 HInstruction* left = InputAt(0);
1548 HInstruction* right = InputAt(1);
1549 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1550 return;
1551 }
1552 if (OrderInputsWithConstantOnTheRight()) {
1553 return;
1554 }
1555 // Order according to instruction id.
1556 if (left->GetId() > right->GetId()) {
1557 ReplaceInput(right, 0);
1558 ReplaceInput(left, 1);
1559 }
1560 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001561
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001562 bool CanBeMoved() const OVERRIDE { return true; }
1563 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001564 UNUSED(other);
1565 return true;
1566 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001567
Roland Levillain9240d6a2014-10-20 16:47:04 +01001568 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001569 // containing the result of this evaluation. If `operation` cannot
1570 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001571 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001572
1573 // Apply this operation to `x` and `y`.
1574 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1575 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1576
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001577 // Returns an input that can legally be used as the right input and is
1578 // constant, or nullptr.
1579 HConstant* GetConstantRight() const;
1580
1581 // If `GetConstantRight()` returns one of the input, this returns the other
1582 // one. Otherwise it returns nullptr.
1583 HInstruction* GetLeastConstantLeft() const;
1584
Roland Levillainccc07a92014-09-16 14:48:16 +01001585 DECLARE_INSTRUCTION(BinaryOperation);
1586
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001587 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001588 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1589};
1590
Dave Allison20dfc792014-06-16 20:44:29 -07001591class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001592 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001593 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001594 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1595 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001596
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001597 bool NeedsMaterialization() const { return needs_materialization_; }
1598 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001599
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001600 // For code generation purposes, returns whether this instruction is just before
1601 // `if_`, and disregard moves in between.
1602 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1603
Dave Allison20dfc792014-06-16 20:44:29 -07001604 DECLARE_INSTRUCTION(Condition);
1605
1606 virtual IfCondition GetCondition() const = 0;
1607
1608 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001609 // For register allocation purposes, returns whether this instruction needs to be
1610 // materialized (that is, not just be in the processor flags).
1611 bool needs_materialization_;
1612
Dave Allison20dfc792014-06-16 20:44:29 -07001613 DISALLOW_COPY_AND_ASSIGN(HCondition);
1614};
1615
1616// Instruction to check if two inputs are equal to each other.
1617class HEqual : public HCondition {
1618 public:
1619 HEqual(HInstruction* first, HInstruction* second)
1620 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001621
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001622 bool IsCommutative() const OVERRIDE { return true; }
1623
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001624 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001625 return x == y ? 1 : 0;
1626 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001627 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001628 return x == y ? 1 : 0;
1629 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001630
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001631 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001632
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001633 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001634 return kCondEQ;
1635 }
1636
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001637 private:
1638 DISALLOW_COPY_AND_ASSIGN(HEqual);
1639};
1640
Dave Allison20dfc792014-06-16 20:44:29 -07001641class HNotEqual : public HCondition {
1642 public:
1643 HNotEqual(HInstruction* first, HInstruction* second)
1644 : HCondition(first, second) {}
1645
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001646 bool IsCommutative() const OVERRIDE { return true; }
1647
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001648 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001649 return x != y ? 1 : 0;
1650 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001651 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001652 return x != y ? 1 : 0;
1653 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001654
Dave Allison20dfc792014-06-16 20:44:29 -07001655 DECLARE_INSTRUCTION(NotEqual);
1656
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001657 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001658 return kCondNE;
1659 }
1660
1661 private:
1662 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1663};
1664
1665class HLessThan : public HCondition {
1666 public:
1667 HLessThan(HInstruction* first, HInstruction* second)
1668 : HCondition(first, second) {}
1669
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001670 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001671 return x < y ? 1 : 0;
1672 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001673 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001674 return x < y ? 1 : 0;
1675 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001676
Dave Allison20dfc792014-06-16 20:44:29 -07001677 DECLARE_INSTRUCTION(LessThan);
1678
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001679 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001680 return kCondLT;
1681 }
1682
1683 private:
1684 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1685};
1686
1687class HLessThanOrEqual : public HCondition {
1688 public:
1689 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1690 : HCondition(first, second) {}
1691
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001692 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001693 return x <= y ? 1 : 0;
1694 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001695 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001696 return x <= y ? 1 : 0;
1697 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001698
Dave Allison20dfc792014-06-16 20:44:29 -07001699 DECLARE_INSTRUCTION(LessThanOrEqual);
1700
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001701 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001702 return kCondLE;
1703 }
1704
1705 private:
1706 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1707};
1708
1709class HGreaterThan : public HCondition {
1710 public:
1711 HGreaterThan(HInstruction* first, HInstruction* second)
1712 : HCondition(first, second) {}
1713
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001714 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001715 return x > y ? 1 : 0;
1716 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001717 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001718 return x > y ? 1 : 0;
1719 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001720
Dave Allison20dfc792014-06-16 20:44:29 -07001721 DECLARE_INSTRUCTION(GreaterThan);
1722
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001723 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001724 return kCondGT;
1725 }
1726
1727 private:
1728 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1729};
1730
1731class HGreaterThanOrEqual : public HCondition {
1732 public:
1733 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1734 : HCondition(first, second) {}
1735
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001736 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001737 return x >= y ? 1 : 0;
1738 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001739 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001740 return x >= y ? 1 : 0;
1741 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001742
Dave Allison20dfc792014-06-16 20:44:29 -07001743 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1744
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001745 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001746 return kCondGE;
1747 }
1748
1749 private:
1750 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1751};
1752
1753
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001754// Instruction to check how two inputs compare to each other.
1755// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1756class HCompare : public HBinaryOperation {
1757 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001758 // The bias applies for floating point operations and indicates how NaN
1759 // comparisons are treated:
1760 enum Bias {
1761 kNoBias, // bias is not applicable (i.e. for long operation)
1762 kGtBias, // return 1 for NaN comparisons
1763 kLtBias, // return -1 for NaN comparisons
1764 };
1765
1766 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1767 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001768 DCHECK_EQ(type, first->GetType());
1769 DCHECK_EQ(type, second->GetType());
1770 }
1771
Calin Juravleddb7df22014-11-25 20:56:51 +00001772 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001773 return
1774 x == y ? 0 :
1775 x > y ? 1 :
1776 -1;
1777 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001778
1779 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001780 return
1781 x == y ? 0 :
1782 x > y ? 1 :
1783 -1;
1784 }
1785
Calin Juravleddb7df22014-11-25 20:56:51 +00001786 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1787 return bias_ == other->AsCompare()->bias_;
1788 }
1789
1790 bool IsGtBias() { return bias_ == kGtBias; }
1791
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001792 DECLARE_INSTRUCTION(Compare);
1793
1794 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001795 const Bias bias_;
1796
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001797 DISALLOW_COPY_AND_ASSIGN(HCompare);
1798};
1799
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001800// A local in the graph. Corresponds to a Dex register.
1801class HLocal : public HTemplateInstruction<0> {
1802 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001803 explicit HLocal(uint16_t reg_number)
1804 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001805
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001806 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001807
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001808 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001809
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001810 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001811 // The Dex register number.
1812 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001813
1814 DISALLOW_COPY_AND_ASSIGN(HLocal);
1815};
1816
1817// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001818class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001819 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001820 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001821 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001822 SetRawInputAt(0, local);
1823 }
1824
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001825 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1826
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001827 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001828
1829 private:
1830 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1831};
1832
1833// Store a value in a given local. This instruction has two inputs: the value
1834// and the local.
1835class HStoreLocal : public HTemplateInstruction<2> {
1836 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001837 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001838 SetRawInputAt(0, local);
1839 SetRawInputAt(1, value);
1840 }
1841
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001842 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1843
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001844 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001845
1846 private:
1847 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1848};
1849
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001850class HConstant : public HExpression<0> {
1851 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001852 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1853
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001854 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001855
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001856 virtual bool IsMinusOne() const { return false; }
1857 virtual bool IsZero() const { return false; }
1858 virtual bool IsOne() const { return false; }
1859
1860 static HConstant* NewConstant(ArenaAllocator* allocator, Primitive::Type type, int64_t val);
1861
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001862 DECLARE_INSTRUCTION(Constant);
1863
1864 private:
1865 DISALLOW_COPY_AND_ASSIGN(HConstant);
1866};
1867
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001868class HFloatConstant : public HConstant {
1869 public:
1870 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1871
1872 float GetValue() const { return value_; }
1873
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001874 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001875 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1876 bit_cast<float, int32_t>(value_);
1877 }
1878
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001879 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001880
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001881 bool IsMinusOne() const OVERRIDE {
1882 return bit_cast<uint32_t>(AsFloatConstant()->GetValue()) == bit_cast<uint32_t>((-1.0f));
1883 }
1884 bool IsZero() const OVERRIDE {
1885 return AsFloatConstant()->GetValue() == 0.0f;
1886 }
1887 bool IsOne() const OVERRIDE {
1888 return bit_cast<uint32_t>(AsFloatConstant()->GetValue()) == bit_cast<uint32_t>(1.0f);
1889 }
1890
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001891 DECLARE_INSTRUCTION(FloatConstant);
1892
1893 private:
1894 const float value_;
1895
1896 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1897};
1898
1899class HDoubleConstant : public HConstant {
1900 public:
1901 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1902
1903 double GetValue() const { return value_; }
1904
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001905 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001906 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1907 bit_cast<double, int64_t>(value_);
1908 }
1909
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001910 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001911
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001912 bool IsMinusOne() const OVERRIDE {
1913 return bit_cast<uint64_t>(AsDoubleConstant()->GetValue()) == bit_cast<uint64_t>((-1.0));
1914 }
1915 bool IsZero() const OVERRIDE {
1916 return AsDoubleConstant()->GetValue() == 0.0;
1917 }
1918 bool IsOne() const OVERRIDE {
1919 return bit_cast<uint64_t>(AsDoubleConstant()->GetValue()) == bit_cast<uint64_t>(1.0);
1920 }
1921
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001922 DECLARE_INSTRUCTION(DoubleConstant);
1923
1924 private:
1925 const double value_;
1926
1927 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1928};
1929
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001930class HNullConstant : public HConstant {
1931 public:
1932 HNullConstant() : HConstant(Primitive::kPrimNot) {}
1933
1934 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
1935 return true;
1936 }
1937
1938 size_t ComputeHashCode() const OVERRIDE { return 0; }
1939
Calin Juravle61d544b2015-02-23 16:46:57 +00001940 bool ActAsNullConstant() const OVERRIDE { return true; }
1941
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001942 DECLARE_INSTRUCTION(NullConstant);
1943
1944 private:
1945 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
1946};
1947
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001948// Constants of the type int. Those can be from Dex instructions, or
1949// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001950class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001951 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001952 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001953
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001954 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001955
Calin Juravle61d544b2015-02-23 16:46:57 +00001956 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001957 return other->AsIntConstant()->value_ == value_;
1958 }
1959
Calin Juravle61d544b2015-02-23 16:46:57 +00001960 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
1961
1962 // TODO: Null is represented by the `0` constant. In most cases we replace it
1963 // with a HNullConstant but we don't do it when comparing (a != null). This
1964 // method is an workaround until we fix the above.
1965 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001966
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001967 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
1968 bool IsZero() const OVERRIDE { return GetValue() == 0; }
1969 bool IsOne() const OVERRIDE { return GetValue() == 1; }
1970
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001971 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001972
1973 private:
1974 const int32_t value_;
1975
1976 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1977};
1978
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001979class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001980 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001981 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001982
1983 int64_t GetValue() const { return value_; }
1984
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001985 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001986 return other->AsLongConstant()->value_ == value_;
1987 }
1988
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001989 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001990
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001991 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
1992 bool IsZero() const OVERRIDE { return GetValue() == 0; }
1993 bool IsOne() const OVERRIDE { return GetValue() == 1; }
1994
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001995 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001996
1997 private:
1998 const int64_t value_;
1999
2000 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2001};
2002
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002003enum class Intrinsics {
2004#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2005#include "intrinsics_list.h"
2006 kNone,
2007 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2008#undef INTRINSICS_LIST
2009#undef OPTIMIZING_INTRINSICS
2010};
2011std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2012
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002013class HInvoke : public HInstruction {
2014 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002015 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002016
2017 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2018 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002019 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002020
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002021 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002022 SetRawInputAt(index, argument);
2023 }
2024
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002025 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002026
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002027 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002028
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002029 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2030
2031 Intrinsics GetIntrinsic() {
2032 return intrinsic_;
2033 }
2034
2035 void SetIntrinsic(Intrinsics intrinsic) {
2036 intrinsic_ = intrinsic;
2037 }
2038
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002039 DECLARE_INSTRUCTION(Invoke);
2040
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002041 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002042 HInvoke(ArenaAllocator* arena,
2043 uint32_t number_of_arguments,
2044 Primitive::Type return_type,
2045 uint32_t dex_pc,
2046 uint32_t dex_method_index)
2047 : HInstruction(SideEffects::All()),
2048 inputs_(arena, number_of_arguments),
2049 return_type_(return_type),
2050 dex_pc_(dex_pc),
2051 dex_method_index_(dex_method_index),
2052 intrinsic_(Intrinsics::kNone) {
2053 inputs_.SetSize(number_of_arguments);
2054 }
2055
David Brazdil1abb4192015-02-17 18:33:36 +00002056 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2057 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2058 inputs_.Put(index, input);
2059 }
2060
2061 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002062 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002063 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002064 const uint32_t dex_method_index_;
2065 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002066
2067 private:
2068 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2069};
2070
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002071class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002072 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002073 HInvokeStaticOrDirect(ArenaAllocator* arena,
2074 uint32_t number_of_arguments,
2075 Primitive::Type return_type,
2076 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002077 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002078 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002079 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002080 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002081 invoke_type_(invoke_type),
2082 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002083
Calin Juravle77520bc2015-01-12 18:45:46 +00002084 bool CanDoImplicitNullCheck() const OVERRIDE {
2085 // We access the method via the dex cache so we can't do an implicit null check.
2086 // TODO: for intrinsics we can generate implicit null checks.
2087 return false;
2088 }
2089
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002090 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002091 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002092
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002093 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002094
2095 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002096 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002097 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002098
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002099 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002100};
2101
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002102class HInvokeVirtual : public HInvoke {
2103 public:
2104 HInvokeVirtual(ArenaAllocator* arena,
2105 uint32_t number_of_arguments,
2106 Primitive::Type return_type,
2107 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002108 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002109 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002110 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002111 vtable_index_(vtable_index) {}
2112
Calin Juravle77520bc2015-01-12 18:45:46 +00002113 bool CanDoImplicitNullCheck() const OVERRIDE {
2114 // TODO: Add implicit null checks in intrinsics.
2115 return !GetLocations()->Intrinsified();
2116 }
2117
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002118 uint32_t GetVTableIndex() const { return vtable_index_; }
2119
2120 DECLARE_INSTRUCTION(InvokeVirtual);
2121
2122 private:
2123 const uint32_t vtable_index_;
2124
2125 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2126};
2127
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002128class HInvokeInterface : public HInvoke {
2129 public:
2130 HInvokeInterface(ArenaAllocator* arena,
2131 uint32_t number_of_arguments,
2132 Primitive::Type return_type,
2133 uint32_t dex_pc,
2134 uint32_t dex_method_index,
2135 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002136 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002137 imt_index_(imt_index) {}
2138
Calin Juravle77520bc2015-01-12 18:45:46 +00002139 bool CanDoImplicitNullCheck() const OVERRIDE {
2140 // TODO: Add implicit null checks in intrinsics.
2141 return !GetLocations()->Intrinsified();
2142 }
2143
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002144 uint32_t GetImtIndex() const { return imt_index_; }
2145 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2146
2147 DECLARE_INSTRUCTION(InvokeInterface);
2148
2149 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002150 const uint32_t imt_index_;
2151
2152 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2153};
2154
Dave Allison20dfc792014-06-16 20:44:29 -07002155class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002156 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002157 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002158 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2159 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002160 type_index_(type_index),
2161 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002162
2163 uint32_t GetDexPc() const { return dex_pc_; }
2164 uint16_t GetTypeIndex() const { return type_index_; }
2165
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002166 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002167 bool NeedsEnvironment() const OVERRIDE { return true; }
2168 // It may throw when called on:
2169 // - interfaces
2170 // - abstract/innaccessible/unknown classes
2171 // TODO: optimize when possible.
2172 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002173
Calin Juravle10e244f2015-01-26 18:54:32 +00002174 bool CanBeNull() const OVERRIDE { return false; }
2175
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002176 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2177
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002178 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002179
2180 private:
2181 const uint32_t dex_pc_;
2182 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002183 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002184
2185 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2186};
2187
Roland Levillain88cb1752014-10-20 16:36:47 +01002188class HNeg : public HUnaryOperation {
2189 public:
2190 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2191 : HUnaryOperation(result_type, input) {}
2192
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002193 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2194 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002195
Roland Levillain88cb1752014-10-20 16:36:47 +01002196 DECLARE_INSTRUCTION(Neg);
2197
2198 private:
2199 DISALLOW_COPY_AND_ASSIGN(HNeg);
2200};
2201
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002202class HNewArray : public HExpression<1> {
2203 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002204 HNewArray(HInstruction* length,
2205 uint32_t dex_pc,
2206 uint16_t type_index,
2207 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002208 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2209 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002210 type_index_(type_index),
2211 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002212 SetRawInputAt(0, length);
2213 }
2214
2215 uint32_t GetDexPc() const { return dex_pc_; }
2216 uint16_t GetTypeIndex() const { return type_index_; }
2217
2218 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002219 bool NeedsEnvironment() const OVERRIDE { return true; }
2220
2221 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002222
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002223 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2224
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002225 DECLARE_INSTRUCTION(NewArray);
2226
2227 private:
2228 const uint32_t dex_pc_;
2229 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002230 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002231
2232 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2233};
2234
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002235class HAdd : public HBinaryOperation {
2236 public:
2237 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2238 : HBinaryOperation(result_type, left, right) {}
2239
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002240 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002241
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002242 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002243 return x + y;
2244 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002245 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002246 return x + y;
2247 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002248
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002249 DECLARE_INSTRUCTION(Add);
2250
2251 private:
2252 DISALLOW_COPY_AND_ASSIGN(HAdd);
2253};
2254
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002255class HSub : public HBinaryOperation {
2256 public:
2257 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2258 : HBinaryOperation(result_type, left, right) {}
2259
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002260 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002261 return x - y;
2262 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002263 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002264 return x - y;
2265 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002266
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002267 DECLARE_INSTRUCTION(Sub);
2268
2269 private:
2270 DISALLOW_COPY_AND_ASSIGN(HSub);
2271};
2272
Calin Juravle34bacdf2014-10-07 20:23:36 +01002273class HMul : public HBinaryOperation {
2274 public:
2275 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2276 : HBinaryOperation(result_type, left, right) {}
2277
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002278 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002279
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002280 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2281 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002282
2283 DECLARE_INSTRUCTION(Mul);
2284
2285 private:
2286 DISALLOW_COPY_AND_ASSIGN(HMul);
2287};
2288
Calin Juravle7c4954d2014-10-28 16:57:40 +00002289class HDiv : public HBinaryOperation {
2290 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002291 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2292 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002293
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002294 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002295 // Our graph structure ensures we never have 0 for `y` during constant folding.
2296 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002297 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002298 return (y == -1) ? -x : x / y;
2299 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002300
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002301 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002302 DCHECK_NE(y, 0);
2303 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2304 return (y == -1) ? -x : x / y;
2305 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002306
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002307 uint32_t GetDexPc() const { return dex_pc_; }
2308
Calin Juravle7c4954d2014-10-28 16:57:40 +00002309 DECLARE_INSTRUCTION(Div);
2310
2311 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002312 const uint32_t dex_pc_;
2313
Calin Juravle7c4954d2014-10-28 16:57:40 +00002314 DISALLOW_COPY_AND_ASSIGN(HDiv);
2315};
2316
Calin Juravlebacfec32014-11-14 15:54:36 +00002317class HRem : public HBinaryOperation {
2318 public:
2319 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2320 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2321
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002322 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002323 DCHECK_NE(y, 0);
2324 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2325 return (y == -1) ? 0 : x % y;
2326 }
2327
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002328 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002329 DCHECK_NE(y, 0);
2330 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2331 return (y == -1) ? 0 : x % y;
2332 }
2333
2334 uint32_t GetDexPc() const { return dex_pc_; }
2335
2336 DECLARE_INSTRUCTION(Rem);
2337
2338 private:
2339 const uint32_t dex_pc_;
2340
2341 DISALLOW_COPY_AND_ASSIGN(HRem);
2342};
2343
Calin Juravled0d48522014-11-04 16:40:20 +00002344class HDivZeroCheck : public HExpression<1> {
2345 public:
2346 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2347 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2348 SetRawInputAt(0, value);
2349 }
2350
2351 bool CanBeMoved() const OVERRIDE { return true; }
2352
2353 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2354 UNUSED(other);
2355 return true;
2356 }
2357
2358 bool NeedsEnvironment() const OVERRIDE { return true; }
2359 bool CanThrow() const OVERRIDE { return true; }
2360
2361 uint32_t GetDexPc() const { return dex_pc_; }
2362
2363 DECLARE_INSTRUCTION(DivZeroCheck);
2364
2365 private:
2366 const uint32_t dex_pc_;
2367
2368 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2369};
2370
Calin Juravle9aec02f2014-11-18 23:06:35 +00002371class HShl : public HBinaryOperation {
2372 public:
2373 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2374 : HBinaryOperation(result_type, left, right) {}
2375
2376 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2377 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2378
2379 DECLARE_INSTRUCTION(Shl);
2380
2381 private:
2382 DISALLOW_COPY_AND_ASSIGN(HShl);
2383};
2384
2385class HShr : public HBinaryOperation {
2386 public:
2387 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2388 : HBinaryOperation(result_type, left, right) {}
2389
2390 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2391 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2392
2393 DECLARE_INSTRUCTION(Shr);
2394
2395 private:
2396 DISALLOW_COPY_AND_ASSIGN(HShr);
2397};
2398
2399class HUShr : public HBinaryOperation {
2400 public:
2401 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2402 : HBinaryOperation(result_type, left, right) {}
2403
2404 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2405 uint32_t ux = static_cast<uint32_t>(x);
2406 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2407 return static_cast<int32_t>(ux >> uy);
2408 }
2409
2410 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2411 uint64_t ux = static_cast<uint64_t>(x);
2412 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2413 return static_cast<int64_t>(ux >> uy);
2414 }
2415
2416 DECLARE_INSTRUCTION(UShr);
2417
2418 private:
2419 DISALLOW_COPY_AND_ASSIGN(HUShr);
2420};
2421
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002422class HAnd : public HBinaryOperation {
2423 public:
2424 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2425 : HBinaryOperation(result_type, left, right) {}
2426
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002427 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002428
2429 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2430 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2431
2432 DECLARE_INSTRUCTION(And);
2433
2434 private:
2435 DISALLOW_COPY_AND_ASSIGN(HAnd);
2436};
2437
2438class HOr : public HBinaryOperation {
2439 public:
2440 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2441 : HBinaryOperation(result_type, left, right) {}
2442
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002443 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002444
2445 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2446 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2447
2448 DECLARE_INSTRUCTION(Or);
2449
2450 private:
2451 DISALLOW_COPY_AND_ASSIGN(HOr);
2452};
2453
2454class HXor : public HBinaryOperation {
2455 public:
2456 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2457 : HBinaryOperation(result_type, left, right) {}
2458
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002459 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002460
2461 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2462 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2463
2464 DECLARE_INSTRUCTION(Xor);
2465
2466 private:
2467 DISALLOW_COPY_AND_ASSIGN(HXor);
2468};
2469
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002470// The value of a parameter in this method. Its location depends on
2471// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002472class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002473 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002474 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2475 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002476
2477 uint8_t GetIndex() const { return index_; }
2478
Calin Juravle10e244f2015-01-26 18:54:32 +00002479 bool CanBeNull() const OVERRIDE { return !is_this_; }
2480
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002481 DECLARE_INSTRUCTION(ParameterValue);
2482
2483 private:
2484 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002485 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002486 const uint8_t index_;
2487
Calin Juravle10e244f2015-01-26 18:54:32 +00002488 // Whether or not the parameter value corresponds to 'this' argument.
2489 const bool is_this_;
2490
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002491 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2492};
2493
Roland Levillain1cc5f252014-10-22 18:06:21 +01002494class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002495 public:
Roland Levillain1cc5f252014-10-22 18:06:21 +01002496 explicit HNot(Primitive::Type result_type, HInstruction* input)
2497 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002498
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002499 bool CanBeMoved() const OVERRIDE { return true; }
2500 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002501 UNUSED(other);
2502 return true;
2503 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002504
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002505 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2506 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f252014-10-22 18:06:21 +01002507
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002508 DECLARE_INSTRUCTION(Not);
2509
2510 private:
2511 DISALLOW_COPY_AND_ASSIGN(HNot);
2512};
2513
Roland Levillaindff1f282014-11-05 14:15:05 +00002514class HTypeConversion : public HExpression<1> {
2515 public:
2516 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002517 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2518 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002519 SetRawInputAt(0, input);
2520 DCHECK_NE(input->GetType(), result_type);
2521 }
2522
2523 HInstruction* GetInput() const { return InputAt(0); }
2524 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2525 Primitive::Type GetResultType() const { return GetType(); }
2526
Roland Levillain624279f2014-12-04 11:54:28 +00002527 // Required by the x86 and ARM code generators when producing calls
2528 // to the runtime.
2529 uint32_t GetDexPc() const { return dex_pc_; }
2530
Roland Levillaindff1f282014-11-05 14:15:05 +00002531 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002532 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002533
2534 DECLARE_INSTRUCTION(TypeConversion);
2535
2536 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002537 const uint32_t dex_pc_;
2538
Roland Levillaindff1f282014-11-05 14:15:05 +00002539 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2540};
2541
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002542static constexpr uint32_t kNoRegNumber = -1;
2543
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002544class HPhi : public HInstruction {
2545 public:
2546 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002547 : HInstruction(SideEffects::None()),
2548 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002549 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002550 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002551 is_live_(false),
2552 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002553 inputs_.SetSize(number_of_inputs);
2554 }
2555
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002556 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2557 static Primitive::Type ToPhiType(Primitive::Type type) {
2558 switch (type) {
2559 case Primitive::kPrimBoolean:
2560 case Primitive::kPrimByte:
2561 case Primitive::kPrimShort:
2562 case Primitive::kPrimChar:
2563 return Primitive::kPrimInt;
2564 default:
2565 return type;
2566 }
2567 }
2568
Calin Juravle10e244f2015-01-26 18:54:32 +00002569 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002570
2571 void AddInput(HInstruction* input);
2572
Calin Juravle10e244f2015-01-26 18:54:32 +00002573 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002574 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002575
Calin Juravle10e244f2015-01-26 18:54:32 +00002576 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2577 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2578
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002579 uint32_t GetRegNumber() const { return reg_number_; }
2580
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002581 void SetDead() { is_live_ = false; }
2582 void SetLive() { is_live_ = true; }
2583 bool IsDead() const { return !is_live_; }
2584 bool IsLive() const { return is_live_; }
2585
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002586 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002587
David Brazdil1abb4192015-02-17 18:33:36 +00002588 protected:
2589 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2590
2591 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2592 inputs_.Put(index, input);
2593 }
2594
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002595 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002596 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002597 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002598 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002599 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002600 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002601
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002602 DISALLOW_COPY_AND_ASSIGN(HPhi);
2603};
2604
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002605class HNullCheck : public HExpression<1> {
2606 public:
2607 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002608 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002609 SetRawInputAt(0, value);
2610 }
2611
Calin Juravle10e244f2015-01-26 18:54:32 +00002612 bool CanBeMoved() const OVERRIDE { return true; }
2613 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002614 UNUSED(other);
2615 return true;
2616 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002617
Calin Juravle10e244f2015-01-26 18:54:32 +00002618 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002619
Calin Juravle10e244f2015-01-26 18:54:32 +00002620 bool CanThrow() const OVERRIDE { return true; }
2621
2622 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002623
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002624 uint32_t GetDexPc() const { return dex_pc_; }
2625
2626 DECLARE_INSTRUCTION(NullCheck);
2627
2628 private:
2629 const uint32_t dex_pc_;
2630
2631 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2632};
2633
2634class FieldInfo : public ValueObject {
2635 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002636 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2637 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002638
2639 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002640 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002641 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002642
2643 private:
2644 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002645 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002646 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002647};
2648
2649class HInstanceFieldGet : public HExpression<1> {
2650 public:
2651 HInstanceFieldGet(HInstruction* value,
2652 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002653 MemberOffset field_offset,
2654 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002655 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002656 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002657 SetRawInputAt(0, value);
2658 }
2659
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002660 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002661
2662 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2663 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2664 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002665 }
2666
Calin Juravle77520bc2015-01-12 18:45:46 +00002667 bool CanDoImplicitNullCheck() const OVERRIDE {
2668 return GetFieldOffset().Uint32Value() < kPageSize;
2669 }
2670
2671 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002672 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2673 }
2674
Calin Juravle52c48962014-12-16 17:02:57 +00002675 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002676 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002677 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002678 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002679
2680 DECLARE_INSTRUCTION(InstanceFieldGet);
2681
2682 private:
2683 const FieldInfo field_info_;
2684
2685 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2686};
2687
2688class HInstanceFieldSet : public HTemplateInstruction<2> {
2689 public:
2690 HInstanceFieldSet(HInstruction* object,
2691 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002692 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002693 MemberOffset field_offset,
2694 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002695 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002696 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002697 SetRawInputAt(0, object);
2698 SetRawInputAt(1, value);
2699 }
2700
Calin Juravle77520bc2015-01-12 18:45:46 +00002701 bool CanDoImplicitNullCheck() const OVERRIDE {
2702 return GetFieldOffset().Uint32Value() < kPageSize;
2703 }
2704
Calin Juravle52c48962014-12-16 17:02:57 +00002705 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002706 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002707 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002708 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002709 HInstruction* GetValue() const { return InputAt(1); }
2710
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002711 DECLARE_INSTRUCTION(InstanceFieldSet);
2712
2713 private:
2714 const FieldInfo field_info_;
2715
2716 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2717};
2718
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002719class HArrayGet : public HExpression<2> {
2720 public:
2721 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002722 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002723 SetRawInputAt(0, array);
2724 SetRawInputAt(1, index);
2725 }
2726
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002727 bool CanBeMoved() const OVERRIDE { return true; }
2728 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002729 UNUSED(other);
2730 return true;
2731 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002732 bool CanDoImplicitNullCheck() const OVERRIDE {
2733 // TODO: We can be smarter here.
2734 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2735 // which generates the implicit null check. There are cases when these can be removed
2736 // to produce better code. If we ever add optimizations to do so we should allow an
2737 // implicit check here (as long as the address falls in the first page).
2738 return false;
2739 }
2740
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002741 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002742
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002743 HInstruction* GetArray() const { return InputAt(0); }
2744 HInstruction* GetIndex() const { return InputAt(1); }
2745
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002746 DECLARE_INSTRUCTION(ArrayGet);
2747
2748 private:
2749 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2750};
2751
2752class HArraySet : public HTemplateInstruction<3> {
2753 public:
2754 HArraySet(HInstruction* array,
2755 HInstruction* index,
2756 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002757 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002758 uint32_t dex_pc)
2759 : HTemplateInstruction(SideEffects::ChangesSomething()),
2760 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002761 expected_component_type_(expected_component_type),
2762 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002763 SetRawInputAt(0, array);
2764 SetRawInputAt(1, index);
2765 SetRawInputAt(2, value);
2766 }
2767
Calin Juravle77520bc2015-01-12 18:45:46 +00002768 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002769 // We currently always call a runtime method to catch array store
2770 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002771 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002772 }
2773
Calin Juravle77520bc2015-01-12 18:45:46 +00002774 bool CanDoImplicitNullCheck() const OVERRIDE {
2775 // TODO: Same as for ArrayGet.
2776 return false;
2777 }
2778
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002779 void ClearNeedsTypeCheck() {
2780 needs_type_check_ = false;
2781 }
2782
2783 bool NeedsTypeCheck() const { return needs_type_check_; }
2784
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002785 uint32_t GetDexPc() const { return dex_pc_; }
2786
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002787 HInstruction* GetArray() const { return InputAt(0); }
2788 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002789 HInstruction* GetValue() const { return InputAt(2); }
2790
2791 Primitive::Type GetComponentType() const {
2792 // The Dex format does not type floating point index operations. Since the
2793 // `expected_component_type_` is set during building and can therefore not
2794 // be correct, we also check what is the value type. If it is a floating
2795 // point type, we must use that type.
2796 Primitive::Type value_type = GetValue()->GetType();
2797 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2798 ? value_type
2799 : expected_component_type_;
2800 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002801
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002802 DECLARE_INSTRUCTION(ArraySet);
2803
2804 private:
2805 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002806 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002807 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002808
2809 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2810};
2811
2812class HArrayLength : public HExpression<1> {
2813 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002814 explicit HArrayLength(HInstruction* array)
2815 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2816 // Note that arrays do not change length, so the instruction does not
2817 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002818 SetRawInputAt(0, array);
2819 }
2820
Calin Juravle77520bc2015-01-12 18:45:46 +00002821 bool CanBeMoved() const OVERRIDE { return true; }
2822 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002823 UNUSED(other);
2824 return true;
2825 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002826 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002827
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002828 DECLARE_INSTRUCTION(ArrayLength);
2829
2830 private:
2831 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2832};
2833
2834class HBoundsCheck : public HExpression<2> {
2835 public:
2836 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002837 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002838 DCHECK(index->GetType() == Primitive::kPrimInt);
2839 SetRawInputAt(0, index);
2840 SetRawInputAt(1, length);
2841 }
2842
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002843 bool CanBeMoved() const OVERRIDE { return true; }
2844 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002845 UNUSED(other);
2846 return true;
2847 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002848
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002849 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002850
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002851 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002852
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002853 uint32_t GetDexPc() const { return dex_pc_; }
2854
2855 DECLARE_INSTRUCTION(BoundsCheck);
2856
2857 private:
2858 const uint32_t dex_pc_;
2859
2860 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2861};
2862
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002863/**
2864 * Some DEX instructions are folded into multiple HInstructions that need
2865 * to stay live until the last HInstruction. This class
2866 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002867 * HInstruction stays live. `index` represents the stack location index of the
2868 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002869 */
2870class HTemporary : public HTemplateInstruction<0> {
2871 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002872 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002873
2874 size_t GetIndex() const { return index_; }
2875
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002876 Primitive::Type GetType() const OVERRIDE {
2877 // The previous instruction is the one that will be stored in the temporary location.
2878 DCHECK(GetPrevious() != nullptr);
2879 return GetPrevious()->GetType();
2880 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002881
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002882 DECLARE_INSTRUCTION(Temporary);
2883
2884 private:
2885 const size_t index_;
2886
2887 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2888};
2889
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002890class HSuspendCheck : public HTemplateInstruction<0> {
2891 public:
2892 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002893 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002894
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002895 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002896 return true;
2897 }
2898
2899 uint32_t GetDexPc() const { return dex_pc_; }
2900
2901 DECLARE_INSTRUCTION(SuspendCheck);
2902
2903 private:
2904 const uint32_t dex_pc_;
2905
2906 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2907};
2908
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002909/**
2910 * Instruction to load a Class object.
2911 */
2912class HLoadClass : public HExpression<0> {
2913 public:
2914 HLoadClass(uint16_t type_index,
2915 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002916 uint32_t dex_pc)
2917 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2918 type_index_(type_index),
2919 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002920 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00002921 generate_clinit_check_(false),
2922 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002923
2924 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002925
2926 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2927 return other->AsLoadClass()->type_index_ == type_index_;
2928 }
2929
2930 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2931
2932 uint32_t GetDexPc() const { return dex_pc_; }
2933 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002934 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002935
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002936 bool NeedsEnvironment() const OVERRIDE {
2937 // Will call runtime and load the class if the class is not loaded yet.
2938 // TODO: finer grain decision.
2939 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002940 }
2941
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002942 bool MustGenerateClinitCheck() const {
2943 return generate_clinit_check_;
2944 }
2945
2946 void SetMustGenerateClinitCheck() {
2947 generate_clinit_check_ = true;
2948 }
2949
2950 bool CanCallRuntime() const {
2951 return MustGenerateClinitCheck() || !is_referrers_class_;
2952 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002953
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002954 bool CanThrow() const OVERRIDE {
2955 // May call runtime and and therefore can throw.
2956 // TODO: finer grain decision.
2957 return !is_referrers_class_;
2958 }
2959
Calin Juravleacf735c2015-02-12 15:25:22 +00002960 ReferenceTypeInfo GetLoadedClassRTI() {
2961 return loaded_class_rti_;
2962 }
2963
2964 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
2965 // Make sure we only set exact types (the loaded class should never be merged).
2966 DCHECK(rti.IsExact());
2967 loaded_class_rti_ = rti;
2968 }
2969
2970 bool IsResolved() {
2971 return loaded_class_rti_.IsExact();
2972 }
2973
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002974 DECLARE_INSTRUCTION(LoadClass);
2975
2976 private:
2977 const uint16_t type_index_;
2978 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002979 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002980 // Whether this instruction must generate the initialization check.
2981 // Used for code generation.
2982 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002983
Calin Juravleacf735c2015-02-12 15:25:22 +00002984 ReferenceTypeInfo loaded_class_rti_;
2985
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002986 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2987};
2988
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002989class HLoadString : public HExpression<0> {
2990 public:
2991 HLoadString(uint32_t string_index, uint32_t dex_pc)
2992 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2993 string_index_(string_index),
2994 dex_pc_(dex_pc) {}
2995
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002996 bool CanBeMoved() const OVERRIDE { return true; }
2997
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002998 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2999 return other->AsLoadString()->string_index_ == string_index_;
3000 }
3001
3002 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3003
3004 uint32_t GetDexPc() const { return dex_pc_; }
3005 uint32_t GetStringIndex() const { return string_index_; }
3006
3007 // TODO: Can we deopt or debug when we resolve a string?
3008 bool NeedsEnvironment() const OVERRIDE { return false; }
3009
3010 DECLARE_INSTRUCTION(LoadString);
3011
3012 private:
3013 const uint32_t string_index_;
3014 const uint32_t dex_pc_;
3015
3016 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3017};
3018
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003019// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003020/**
3021 * Performs an initialization check on its Class object input.
3022 */
3023class HClinitCheck : public HExpression<1> {
3024 public:
3025 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
3026 : HExpression(Primitive::kPrimNot, SideEffects::All()),
3027 dex_pc_(dex_pc) {
3028 SetRawInputAt(0, constant);
3029 }
3030
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003031 bool CanBeMoved() const OVERRIDE { return true; }
3032 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3033 UNUSED(other);
3034 return true;
3035 }
3036
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003037 bool NeedsEnvironment() const OVERRIDE {
3038 // May call runtime to initialize the class.
3039 return true;
3040 }
3041
3042 uint32_t GetDexPc() const { return dex_pc_; }
3043
3044 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3045
3046 DECLARE_INSTRUCTION(ClinitCheck);
3047
3048 private:
3049 const uint32_t dex_pc_;
3050
3051 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3052};
3053
3054class HStaticFieldGet : public HExpression<1> {
3055 public:
3056 HStaticFieldGet(HInstruction* cls,
3057 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003058 MemberOffset field_offset,
3059 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003060 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003061 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003062 SetRawInputAt(0, cls);
3063 }
3064
Calin Juravle52c48962014-12-16 17:02:57 +00003065
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003066 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003067
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003068 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003069 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3070 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003071 }
3072
3073 size_t ComputeHashCode() const OVERRIDE {
3074 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3075 }
3076
Calin Juravle52c48962014-12-16 17:02:57 +00003077 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003078 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3079 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003080 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003081
3082 DECLARE_INSTRUCTION(StaticFieldGet);
3083
3084 private:
3085 const FieldInfo field_info_;
3086
3087 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3088};
3089
3090class HStaticFieldSet : public HTemplateInstruction<2> {
3091 public:
3092 HStaticFieldSet(HInstruction* cls,
3093 HInstruction* value,
3094 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003095 MemberOffset field_offset,
3096 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003097 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003098 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003099 SetRawInputAt(0, cls);
3100 SetRawInputAt(1, value);
3101 }
3102
Calin Juravle52c48962014-12-16 17:02:57 +00003103 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003104 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3105 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003106 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003107
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003108 HInstruction* GetValue() const { return InputAt(1); }
3109
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003110 DECLARE_INSTRUCTION(StaticFieldSet);
3111
3112 private:
3113 const FieldInfo field_info_;
3114
3115 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3116};
3117
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003118// Implement the move-exception DEX instruction.
3119class HLoadException : public HExpression<0> {
3120 public:
3121 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3122
3123 DECLARE_INSTRUCTION(LoadException);
3124
3125 private:
3126 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3127};
3128
3129class HThrow : public HTemplateInstruction<1> {
3130 public:
3131 HThrow(HInstruction* exception, uint32_t dex_pc)
3132 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3133 SetRawInputAt(0, exception);
3134 }
3135
3136 bool IsControlFlow() const OVERRIDE { return true; }
3137
3138 bool NeedsEnvironment() const OVERRIDE { return true; }
3139
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003140 bool CanThrow() const OVERRIDE { return true; }
3141
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003142 uint32_t GetDexPc() const { return dex_pc_; }
3143
3144 DECLARE_INSTRUCTION(Throw);
3145
3146 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003147 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003148
3149 DISALLOW_COPY_AND_ASSIGN(HThrow);
3150};
3151
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003152class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003153 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003154 HInstanceOf(HInstruction* object,
3155 HLoadClass* constant,
3156 bool class_is_final,
3157 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003158 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3159 class_is_final_(class_is_final),
3160 dex_pc_(dex_pc) {
3161 SetRawInputAt(0, object);
3162 SetRawInputAt(1, constant);
3163 }
3164
3165 bool CanBeMoved() const OVERRIDE { return true; }
3166
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003167 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003168 return true;
3169 }
3170
3171 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003172 return false;
3173 }
3174
3175 uint32_t GetDexPc() const { return dex_pc_; }
3176
3177 bool IsClassFinal() const { return class_is_final_; }
3178
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003179 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003180
3181 private:
3182 const bool class_is_final_;
3183 const uint32_t dex_pc_;
3184
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003185 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3186};
3187
Calin Juravleb1498f62015-02-16 13:13:29 +00003188class HBoundType : public HExpression<1> {
3189 public:
3190 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3191 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3192 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003193 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003194 SetRawInputAt(0, input);
3195 }
3196
3197 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3198
3199 bool CanBeNull() const OVERRIDE {
3200 // `null instanceof ClassX` always return false so we can't be null.
3201 return false;
3202 }
3203
3204 DECLARE_INSTRUCTION(BoundType);
3205
3206 private:
3207 // Encodes the most upper class that this instruction can have. In other words
3208 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3209 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3210 const ReferenceTypeInfo bound_type_;
3211
3212 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3213};
3214
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003215class HCheckCast : public HTemplateInstruction<2> {
3216 public:
3217 HCheckCast(HInstruction* object,
3218 HLoadClass* constant,
3219 bool class_is_final,
3220 uint32_t dex_pc)
3221 : HTemplateInstruction(SideEffects::None()),
3222 class_is_final_(class_is_final),
3223 dex_pc_(dex_pc) {
3224 SetRawInputAt(0, object);
3225 SetRawInputAt(1, constant);
3226 }
3227
3228 bool CanBeMoved() const OVERRIDE { return true; }
3229
3230 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3231 return true;
3232 }
3233
3234 bool NeedsEnvironment() const OVERRIDE {
3235 // Instruction may throw a CheckCastError.
3236 return true;
3237 }
3238
3239 bool CanThrow() const OVERRIDE { return true; }
3240
3241 uint32_t GetDexPc() const { return dex_pc_; }
3242
3243 bool IsClassFinal() const { return class_is_final_; }
3244
3245 DECLARE_INSTRUCTION(CheckCast);
3246
3247 private:
3248 const bool class_is_final_;
3249 const uint32_t dex_pc_;
3250
3251 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003252};
3253
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003254class HMonitorOperation : public HTemplateInstruction<1> {
3255 public:
3256 enum OperationKind {
3257 kEnter,
3258 kExit,
3259 };
3260
3261 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3262 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3263 SetRawInputAt(0, object);
3264 }
3265
3266 // Instruction may throw a Java exception, so we need an environment.
3267 bool NeedsEnvironment() const OVERRIDE { return true; }
3268 bool CanThrow() const OVERRIDE { return true; }
3269
3270 uint32_t GetDexPc() const { return dex_pc_; }
3271
3272 bool IsEnter() const { return kind_ == kEnter; }
3273
3274 DECLARE_INSTRUCTION(MonitorOperation);
3275
Calin Juravle52c48962014-12-16 17:02:57 +00003276 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003277 const OperationKind kind_;
3278 const uint32_t dex_pc_;
3279
3280 private:
3281 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3282};
3283
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003284class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003285 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003286 MoveOperands(Location source, Location destination, HInstruction* instruction)
3287 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003288
3289 Location GetSource() const { return source_; }
3290 Location GetDestination() const { return destination_; }
3291
3292 void SetSource(Location value) { source_ = value; }
3293 void SetDestination(Location value) { destination_ = value; }
3294
3295 // The parallel move resolver marks moves as "in-progress" by clearing the
3296 // destination (but not the source).
3297 Location MarkPending() {
3298 DCHECK(!IsPending());
3299 Location dest = destination_;
3300 destination_ = Location::NoLocation();
3301 return dest;
3302 }
3303
3304 void ClearPending(Location dest) {
3305 DCHECK(IsPending());
3306 destination_ = dest;
3307 }
3308
3309 bool IsPending() const {
3310 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3311 return destination_.IsInvalid() && !source_.IsInvalid();
3312 }
3313
3314 // True if this blocks a move from the given location.
3315 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003316 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003317 }
3318
3319 // A move is redundant if it's been eliminated, if its source and
3320 // destination are the same, or if its destination is unneeded.
3321 bool IsRedundant() const {
3322 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3323 }
3324
3325 // We clear both operands to indicate move that's been eliminated.
3326 void Eliminate() {
3327 source_ = destination_ = Location::NoLocation();
3328 }
3329
3330 bool IsEliminated() const {
3331 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3332 return source_.IsInvalid();
3333 }
3334
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003335 HInstruction* GetInstruction() const { return instruction_; }
3336
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003337 private:
3338 Location source_;
3339 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003340 // The instruction this move is assocatied with. Null when this move is
3341 // for moving an input in the expected locations of user (including a phi user).
3342 // This is only used in debug mode, to ensure we do not connect interval siblings
3343 // in the same parallel move.
3344 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003345};
3346
3347static constexpr size_t kDefaultNumberOfMoves = 4;
3348
3349class HParallelMove : public HTemplateInstruction<0> {
3350 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003351 explicit HParallelMove(ArenaAllocator* arena)
3352 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003353
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003354 void AddMove(Location source, Location destination, HInstruction* instruction) {
3355 DCHECK(source.IsValid());
3356 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003357 if (kIsDebugBuild) {
3358 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003359 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003360 if (moves_.Get(i).GetInstruction() == instruction) {
3361 // Special case the situation where the move is for the spill slot
3362 // of the instruction.
3363 if ((GetPrevious() == instruction)
3364 || ((GetPrevious() == nullptr)
3365 && instruction->IsPhi()
3366 && instruction->GetBlock() == GetBlock())) {
3367 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3368 << "Doing parallel moves for the same instruction.";
3369 } else {
3370 DCHECK(false) << "Doing parallel moves for the same instruction.";
3371 }
3372 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003373 }
3374 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003375 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3376 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3377 << "Same destination for two moves in a parallel move.";
3378 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003379 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003380 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003381 }
3382
3383 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003384 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003385 }
3386
3387 size_t NumMoves() const { return moves_.Size(); }
3388
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003389 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003390
3391 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003392 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003393
3394 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3395};
3396
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003397class HGraphVisitor : public ValueObject {
3398 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003399 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3400 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003401
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003402 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003403 virtual void VisitBasicBlock(HBasicBlock* block);
3404
Roland Levillain633021e2014-10-01 14:12:25 +01003405 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003406 void VisitInsertionOrder();
3407
Roland Levillain633021e2014-10-01 14:12:25 +01003408 // Visit the graph following dominator tree reverse post-order.
3409 void VisitReversePostOrder();
3410
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003411 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003412
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003413 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003414#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003415 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3416
3417 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3418
3419#undef DECLARE_VISIT_INSTRUCTION
3420
3421 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003422 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003423
3424 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3425};
3426
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003427class HGraphDelegateVisitor : public HGraphVisitor {
3428 public:
3429 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3430 virtual ~HGraphDelegateVisitor() {}
3431
3432 // Visit functions that delegate to to super class.
3433#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003434 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003435
3436 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3437
3438#undef DECLARE_VISIT_INSTRUCTION
3439
3440 private:
3441 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3442};
3443
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003444class HInsertionOrderIterator : public ValueObject {
3445 public:
3446 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3447
3448 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3449 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3450 void Advance() { ++index_; }
3451
3452 private:
3453 const HGraph& graph_;
3454 size_t index_;
3455
3456 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3457};
3458
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003459class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003460 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003461 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003462
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003463 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3464 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003465 void Advance() { ++index_; }
3466
3467 private:
3468 const HGraph& graph_;
3469 size_t index_;
3470
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003471 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003472};
3473
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003474class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003475 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003476 explicit HPostOrderIterator(const HGraph& graph)
3477 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003478
3479 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003480 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003481 void Advance() { --index_; }
3482
3483 private:
3484 const HGraph& graph_;
3485 size_t index_;
3486
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003487 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003488};
3489
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003490// Iterator over the blocks that art part of the loop. Includes blocks part
3491// of an inner loop. The order in which the blocks are iterated is on their
3492// block id.
3493class HBlocksInLoopIterator : public ValueObject {
3494 public:
3495 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3496 : blocks_in_loop_(info.GetBlocks()),
3497 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3498 index_(0) {
3499 if (!blocks_in_loop_.IsBitSet(index_)) {
3500 Advance();
3501 }
3502 }
3503
3504 bool Done() const { return index_ == blocks_.Size(); }
3505 HBasicBlock* Current() const { return blocks_.Get(index_); }
3506 void Advance() {
3507 ++index_;
3508 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3509 if (blocks_in_loop_.IsBitSet(index_)) {
3510 break;
3511 }
3512 }
3513 }
3514
3515 private:
3516 const BitVector& blocks_in_loop_;
3517 const GrowableArray<HBasicBlock*>& blocks_;
3518 size_t index_;
3519
3520 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3521};
3522
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003523inline int64_t Int64FromConstant(HConstant* constant) {
3524 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3525 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3526 : constant->AsLongConstant()->GetValue();
3527}
3528
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003529} // namespace art
3530
3531#endif // ART_COMPILER_OPTIMIZING_NODES_H_