blob: 07ff8ba0105f2fed25d11f6a3ced5f02a44181f4 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Mathieu Chartierb666f482015-02-18 14:33:14 -080020#include "base/arena_object.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000021#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000022#include "handle.h"
23#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000024#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010025#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000026#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010027#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000029#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "utils/growable_array.h"
31
32namespace art {
33
David Brazdil1abb4192015-02-17 18:33:36 +000034class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010036class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000038class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000039class HInvoke;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040class HGraphVisitor;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000041class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010042class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010043class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010044class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000045class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046
47static const int kDefaultNumberOfBlocks = 8;
48static const int kDefaultNumberOfSuccessors = 2;
49static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010050static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000051static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
Calin Juravle9aec02f2014-11-18 23:06:35 +000053static constexpr uint32_t kMaxIntShiftValue = 0x1f;
54static constexpr uint64_t kMaxLongShiftValue = 0x3f;
55
Dave Allison20dfc792014-06-16 20:44:29 -070056enum IfCondition {
57 kCondEQ,
58 kCondNE,
59 kCondLT,
60 kCondLE,
61 kCondGT,
62 kCondGE,
63};
64
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010065class HInstructionList {
66 public:
67 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
68
69 void AddInstruction(HInstruction* instruction);
70 void RemoveInstruction(HInstruction* instruction);
71
Roland Levillain6b469232014-09-25 10:10:38 +010072 // Return true if this list contains `instruction`.
73 bool Contains(HInstruction* instruction) const;
74
Roland Levillainccc07a92014-09-16 14:48:16 +010075 // Return true if `instruction1` is found before `instruction2` in
76 // this instruction list and false otherwise. Abort if none
77 // of these instructions is found.
78 bool FoundBefore(const HInstruction* instruction1,
79 const HInstruction* instruction2) const;
80
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000081 bool IsEmpty() const { return first_instruction_ == nullptr; }
82 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
83
84 // Update the block of all instructions to be `block`.
85 void SetBlockOfInstructions(HBasicBlock* block) const;
86
87 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
88 void Add(const HInstructionList& instruction_list);
89
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010090 private:
91 HInstruction* first_instruction_;
92 HInstruction* last_instruction_;
93
94 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000095 friend class HGraph;
96 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010097 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010098 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010099
100 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
101};
102
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000103// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700104class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000105 public:
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000106 HGraph(ArenaAllocator* arena, bool debuggable = false, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000107 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000108 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100109 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700110 entry_block_(nullptr),
111 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100112 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100113 number_of_vregs_(0),
114 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000115 temporaries_vreg_slots_(0),
Mingyao Yange4335eb2015-03-02 15:14:13 -0800116 has_array_accesses_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000117 debuggable_(debuggable),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000118 current_instruction_id_(start_instruction_id) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000119
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000120 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100121 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100122 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000123
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000124 HBasicBlock* GetEntryBlock() const { return entry_block_; }
125 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000126
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000127 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
128 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000129
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000130 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100131
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000132 // Try building the SSA form of this graph, with dominance computation and loop
133 // recognition. Returns whether it was successful in doing all these steps.
134 bool TryBuildingSsa() {
135 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000136 // The SSA builder requires loops to all be natural. Specifically, the dead phi
137 // elimination phase checks the consistency of the graph when doing a post-order
138 // visit for eliminating dead phis: a dead phi can only have loop header phi
139 // users remaining when being visited.
140 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000141 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000142 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000143 }
144
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000145 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000146 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100147 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000148
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000149 // Analyze all natural loops in this graph. Returns false if one
150 // loop is not natural, that is the header does not dominate the
151 // back edge.
152 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100153
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000154 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
155 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
156
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100157 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
158 void SimplifyLoop(HBasicBlock* header);
159
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 int32_t GetNextInstructionId() {
161 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000162 return current_instruction_id_++;
163 }
164
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000165 int32_t GetCurrentInstructionId() const {
166 return current_instruction_id_;
167 }
168
169 void SetCurrentInstructionId(int32_t id) {
170 current_instruction_id_ = id;
171 }
172
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100173 uint16_t GetMaximumNumberOfOutVRegs() const {
174 return maximum_number_of_out_vregs_;
175 }
176
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
178 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100179 }
180
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000181 void UpdateTemporariesVRegSlots(size_t slots) {
182 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100183 }
184
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000185 size_t GetTemporariesVRegSlots() const {
186 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100187 }
188
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100189 void SetNumberOfVRegs(uint16_t number_of_vregs) {
190 number_of_vregs_ = number_of_vregs;
191 }
192
193 uint16_t GetNumberOfVRegs() const {
194 return number_of_vregs_;
195 }
196
197 void SetNumberOfInVRegs(uint16_t value) {
198 number_of_in_vregs_ = value;
199 }
200
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100201 uint16_t GetNumberOfLocalVRegs() const {
202 return number_of_vregs_ - number_of_in_vregs_;
203 }
204
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100205 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
206 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100207 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100208
Mingyao Yange4335eb2015-03-02 15:14:13 -0800209 bool HasArrayAccesses() const {
210 return has_array_accesses_;
211 }
212
213 void SetHasArrayAccesses(bool value) {
214 has_array_accesses_ = value;
215 }
216
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000217 bool IsDebuggable() const { return debuggable_; }
218
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000219 HNullConstant* GetNullConstant();
220
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000221 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000222 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
223 void VisitBlockForDominatorTree(HBasicBlock* block,
224 HBasicBlock* predecessor,
225 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100226 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000227 void VisitBlockForBackEdges(HBasicBlock* block,
228 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100229 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000230 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000231 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
232
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000233 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000234
235 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000236 GrowableArray<HBasicBlock*> blocks_;
237
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100238 // List of blocks to perform a reverse post order tree traversal.
239 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000240
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000241 HBasicBlock* entry_block_;
242 HBasicBlock* exit_block_;
243
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100244 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100245 uint16_t maximum_number_of_out_vregs_;
246
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100247 // The number of virtual registers in this method. Contains the parameters.
248 uint16_t number_of_vregs_;
249
250 // The number of virtual registers used by parameters of this method.
251 uint16_t number_of_in_vregs_;
252
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000253 // Number of vreg size slots that the temporaries use (used in baseline compiler).
254 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100255
Mingyao Yange4335eb2015-03-02 15:14:13 -0800256 // Has array accesses. We can totally skip BCE if it's false.
257 bool has_array_accesses_;
258
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000259 // Indicates whether the graph should be compiled in a way that
260 // ensures full debuggability. If false, we can apply more
261 // aggressive optimizations that may limit the level of debugging.
262 const bool debuggable_;
263
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000264 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000265 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000266
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000267 // Cached null constant that might be created when building SSA form.
268 HNullConstant* cached_null_constant_;
269
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000270 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000271 DISALLOW_COPY_AND_ASSIGN(HGraph);
272};
273
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700274class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000275 public:
276 HLoopInformation(HBasicBlock* header, HGraph* graph)
277 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100278 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100279 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100280 // Make bit vector growable, as the number of blocks may change.
281 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100282
283 HBasicBlock* GetHeader() const {
284 return header_;
285 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000286
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000287 void SetHeader(HBasicBlock* block) {
288 header_ = block;
289 }
290
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100291 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
292 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
293 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
294
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000295 void AddBackEdge(HBasicBlock* back_edge) {
296 back_edges_.Add(back_edge);
297 }
298
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100299 void RemoveBackEdge(HBasicBlock* back_edge) {
300 back_edges_.Delete(back_edge);
301 }
302
303 bool IsBackEdge(HBasicBlock* block) {
304 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
305 if (back_edges_.Get(i) == block) return true;
306 }
307 return false;
308 }
309
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000310 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000311 return back_edges_.Size();
312 }
313
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100314 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100315
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100316 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
317 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100318 }
319
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100320 void ClearBackEdges() {
321 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100322 }
323
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100324 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
325 // that is the header dominates the back edge.
326 bool Populate();
327
328 // Returns whether this loop information contains `block`.
329 // Note that this loop information *must* be populated before entering this function.
330 bool Contains(const HBasicBlock& block) const;
331
332 // Returns whether this loop information is an inner loop of `other`.
333 // Note that `other` *must* be populated before entering this function.
334 bool IsIn(const HLoopInformation& other) const;
335
336 const ArenaBitVector& GetBlocks() const { return blocks_; }
337
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000338 void Add(HBasicBlock* block);
339
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000340 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100341 // Internal recursive implementation of `Populate`.
342 void PopulateRecursive(HBasicBlock* block);
343
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000344 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100345 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000346 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100347 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000348
349 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
350};
351
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100352static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100353static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100354
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355// A block in a method. Contains the list of instructions represented
356// as a double linked list. Each block knows its predecessors and
357// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100358
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700359class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000360 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100361 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000362 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000363 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
364 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000365 loop_information_(nullptr),
366 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100367 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100368 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100369 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100370 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000371 lifetime_end_(kNoLifetime),
372 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100374 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
375 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000376 }
377
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100378 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
379 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000380 }
381
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100382 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
383 return dominated_blocks_;
384 }
385
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100386 bool IsEntryBlock() const {
387 return graph_->GetEntryBlock() == this;
388 }
389
390 bool IsExitBlock() const {
391 return graph_->GetExitBlock() == this;
392 }
393
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000394 void AddBackEdge(HBasicBlock* back_edge) {
395 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000396 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000397 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100398 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000399 loop_information_->AddBackEdge(back_edge);
400 }
401
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000402 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000403 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000404
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000405 int GetBlockId() const { return block_id_; }
406 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000407
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000408 HBasicBlock* GetDominator() const { return dominator_; }
409 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100410 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000411 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
412 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
413 if (dominated_blocks_.Get(i) == existing) {
414 dominated_blocks_.Put(i, new_block);
415 return;
416 }
417 }
418 LOG(FATAL) << "Unreachable";
419 UNREACHABLE();
420 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000421
422 int NumberOfBackEdges() const {
423 return loop_information_ == nullptr
424 ? 0
425 : loop_information_->NumberOfBackEdges();
426 }
427
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100428 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
429 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100430 const HInstructionList& GetInstructions() const { return instructions_; }
431 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100432 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000433
434 void AddSuccessor(HBasicBlock* block) {
435 successors_.Add(block);
436 block->predecessors_.Add(this);
437 }
438
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100439 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
440 size_t successor_index = GetSuccessorIndexOf(existing);
441 DCHECK_NE(successor_index, static_cast<size_t>(-1));
442 existing->RemovePredecessor(this);
443 new_block->predecessors_.Add(this);
444 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000445 }
446
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000447 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
448 size_t predecessor_index = GetPredecessorIndexOf(existing);
449 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
450 existing->RemoveSuccessor(this);
451 new_block->successors_.Add(this);
452 predecessors_.Put(predecessor_index, new_block);
453 }
454
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100455 void RemovePredecessor(HBasicBlock* block) {
456 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100457 }
458
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000459 void RemoveSuccessor(HBasicBlock* block) {
460 successors_.Delete(block);
461 }
462
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100463 void ClearAllPredecessors() {
464 predecessors_.Reset();
465 }
466
467 void AddPredecessor(HBasicBlock* block) {
468 predecessors_.Add(block);
469 block->successors_.Add(this);
470 }
471
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100472 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100473 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100474 HBasicBlock* temp = predecessors_.Get(0);
475 predecessors_.Put(0, predecessors_.Get(1));
476 predecessors_.Put(1, temp);
477 }
478
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100479 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
480 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
481 if (predecessors_.Get(i) == predecessor) {
482 return i;
483 }
484 }
485 return -1;
486 }
487
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100488 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
489 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
490 if (successors_.Get(i) == successor) {
491 return i;
492 }
493 }
494 return -1;
495 }
496
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000497 // Split the block into two blocks just after `cursor`. Returns the newly
498 // created block. Note that this method just updates raw block information,
499 // like predecessors, successors, dominators, and instruction list. It does not
500 // update the graph, reverse post order, loop information, nor make sure the
501 // blocks are consistent (for example ending with a control flow instruction).
502 HBasicBlock* SplitAfter(HInstruction* cursor);
503
504 // Merge `other` at the end of `this`. Successors and dominated blocks of
505 // `other` are changed to be successors and dominated blocks of `this`. Note
506 // that this method does not update the graph, reverse post order, loop
507 // information, nor make sure the blocks are consistent (for example ending
508 // with a control flow instruction).
509 void MergeWith(HBasicBlock* other);
510
511 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
512 // of `this` are moved to `other`.
513 // Note that this method does not update the graph, reverse post order, loop
514 // information, nor make sure the blocks are consistent (for example ending
515 void ReplaceWith(HBasicBlock* other);
516
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000517 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100518 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100519 // Replace instruction `initial` with `replacement` within this block.
520 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
521 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100522 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100523 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000524 // RemoveInstruction and RemovePhi delete a given instruction from the respective
525 // instruction list. With 'ensure_safety' set to true, it verifies that the
526 // instruction is not in use and removes it from the use lists of its inputs.
527 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
528 void RemovePhi(HPhi* phi, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100529
530 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100531 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100532 }
533
Roland Levillain6b879dd2014-09-22 17:13:44 +0100534 bool IsLoopPreHeaderFirstPredecessor() const {
535 DCHECK(IsLoopHeader());
536 DCHECK(!GetPredecessors().IsEmpty());
537 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
538 }
539
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100540 HLoopInformation* GetLoopInformation() const {
541 return loop_information_;
542 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000543
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000544 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100545 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000546 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100547 void SetInLoop(HLoopInformation* info) {
548 if (IsLoopHeader()) {
549 // Nothing to do. This just means `info` is an outer loop.
550 } else if (loop_information_ == nullptr) {
551 loop_information_ = info;
552 } else if (loop_information_->Contains(*info->GetHeader())) {
553 // Block is currently part of an outer loop. Make it part of this inner loop.
554 // Note that a non loop header having a loop information means this loop information
555 // has already been populated
556 loop_information_ = info;
557 } else {
558 // Block is part of an inner loop. Do not update the loop information.
559 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
560 // at this point, because this method is being called while populating `info`.
561 }
562 }
563
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000564 // Raw update of the loop information.
565 void SetLoopInformation(HLoopInformation* info) {
566 loop_information_ = info;
567 }
568
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100569 bool IsInLoop() const { return loop_information_ != nullptr; }
570
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100571 // Returns wheter this block dominates the blocked passed as parameter.
572 bool Dominates(HBasicBlock* block) const;
573
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100574 size_t GetLifetimeStart() const { return lifetime_start_; }
575 size_t GetLifetimeEnd() const { return lifetime_end_; }
576
577 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
578 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
579
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100580 uint32_t GetDexPc() const { return dex_pc_; }
581
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000582 bool IsCatchBlock() const { return is_catch_block_; }
583 void SetIsCatchBlock() { is_catch_block_ = true; }
584
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000585 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000586 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000587 GrowableArray<HBasicBlock*> predecessors_;
588 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100589 HInstructionList instructions_;
590 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000591 HLoopInformation* loop_information_;
592 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100593 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000594 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100595 // The dex program counter of the first instruction of this block.
596 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100597 size_t lifetime_start_;
598 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000599 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000600
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000601 friend class HGraph;
602 friend class HInstruction;
603
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000604 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
605};
606
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100607#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
608 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000609 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000610 M(ArrayGet, Instruction) \
611 M(ArrayLength, Instruction) \
612 M(ArraySet, Instruction) \
613 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000614 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000615 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100616 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000617 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100618 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000619 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000620 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000621 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100622 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000623 M(Exit, Instruction) \
624 M(FloatConstant, Constant) \
625 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100626 M(GreaterThan, Condition) \
627 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100628 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000629 M(InstanceFieldGet, Instruction) \
630 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000631 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100632 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000633 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000634 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100635 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000636 M(LessThan, Condition) \
637 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000638 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000639 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100640 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000641 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100642 M(Local, Instruction) \
643 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000644 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000645 M(Mul, BinaryOperation) \
646 M(Neg, UnaryOperation) \
647 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100648 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100649 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000650 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000651 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000652 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000653 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100654 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000655 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100656 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000657 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100658 M(Return, Instruction) \
659 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000660 M(Shl, BinaryOperation) \
661 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100662 M(StaticFieldGet, Instruction) \
663 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100664 M(StoreLocal, Instruction) \
665 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100666 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000667 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000668 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000669 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000670 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000671 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000672
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100673#define FOR_EACH_INSTRUCTION(M) \
674 FOR_EACH_CONCRETE_INSTRUCTION(M) \
675 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100676 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100677 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100678 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700679
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100680#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000681FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
682#undef FORWARD_DECLARATION
683
Roland Levillainccc07a92014-09-16 14:48:16 +0100684#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000685 InstructionKind GetKind() const OVERRIDE { return k##type; } \
686 const char* DebugName() const OVERRIDE { return #type; } \
687 const H##type* As##type() const OVERRIDE { return this; } \
688 H##type* As##type() OVERRIDE { return this; } \
689 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100690 return other->Is##type(); \
691 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000692 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000693
David Brazdiled596192015-01-23 10:39:45 +0000694template <typename T> class HUseList;
695
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100696template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700697class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000698 public:
David Brazdiled596192015-01-23 10:39:45 +0000699 HUseListNode* GetPrevious() const { return prev_; }
700 HUseListNode* GetNext() const { return next_; }
701 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100702 size_t GetIndex() const { return index_; }
703
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000704 private:
David Brazdiled596192015-01-23 10:39:45 +0000705 HUseListNode(T user, size_t index)
706 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
707
708 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100709 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000710 HUseListNode<T>* prev_;
711 HUseListNode<T>* next_;
712
713 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000714
715 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
716};
717
David Brazdiled596192015-01-23 10:39:45 +0000718template <typename T>
719class HUseList : public ValueObject {
720 public:
721 HUseList() : first_(nullptr) {}
722
723 void Clear() {
724 first_ = nullptr;
725 }
726
727 // Adds a new entry at the beginning of the use list and returns
728 // the newly created node.
729 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000730 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000731 if (IsEmpty()) {
732 first_ = new_node;
733 } else {
734 first_->prev_ = new_node;
735 new_node->next_ = first_;
736 first_ = new_node;
737 }
738 return new_node;
739 }
740
741 HUseListNode<T>* GetFirst() const {
742 return first_;
743 }
744
745 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000746 DCHECK(node != nullptr);
747 DCHECK(Contains(node));
748
David Brazdiled596192015-01-23 10:39:45 +0000749 if (node->prev_ != nullptr) {
750 node->prev_->next_ = node->next_;
751 }
752 if (node->next_ != nullptr) {
753 node->next_->prev_ = node->prev_;
754 }
755 if (node == first_) {
756 first_ = node->next_;
757 }
758 }
759
David Brazdil1abb4192015-02-17 18:33:36 +0000760 bool Contains(const HUseListNode<T>* node) const {
761 if (node == nullptr) {
762 return false;
763 }
764 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
765 if (current == node) {
766 return true;
767 }
768 }
769 return false;
770 }
771
David Brazdiled596192015-01-23 10:39:45 +0000772 bool IsEmpty() const {
773 return first_ == nullptr;
774 }
775
776 bool HasOnlyOneUse() const {
777 return first_ != nullptr && first_->next_ == nullptr;
778 }
779
780 private:
781 HUseListNode<T>* first_;
782};
783
784template<typename T>
785class HUseIterator : public ValueObject {
786 public:
787 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
788
789 bool Done() const { return current_ == nullptr; }
790
791 void Advance() {
792 DCHECK(!Done());
793 current_ = current_->GetNext();
794 }
795
796 HUseListNode<T>* Current() const {
797 DCHECK(!Done());
798 return current_;
799 }
800
801 private:
802 HUseListNode<T>* current_;
803
804 friend class HValue;
805};
806
David Brazdil1abb4192015-02-17 18:33:36 +0000807// This class is used by HEnvironment and HInstruction classes to record the
808// instructions they use and pointers to the corresponding HUseListNodes kept
809// by the used instructions.
810template <typename T>
811class HUserRecord : public ValueObject {
812 public:
813 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
814 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
815
816 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
817 : instruction_(old_record.instruction_), use_node_(use_node) {
818 DCHECK(instruction_ != nullptr);
819 DCHECK(use_node_ != nullptr);
820 DCHECK(old_record.use_node_ == nullptr);
821 }
822
823 HInstruction* GetInstruction() const { return instruction_; }
824 HUseListNode<T>* GetUseNode() const { return use_node_; }
825
826 private:
827 // Instruction used by the user.
828 HInstruction* instruction_;
829
830 // Corresponding entry in the use list kept by 'instruction_'.
831 HUseListNode<T>* use_node_;
832};
833
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100834// Represents the side effects an instruction may have.
835class SideEffects : public ValueObject {
836 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100837 SideEffects() : flags_(0) {}
838
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100839 static SideEffects None() {
840 return SideEffects(0);
841 }
842
843 static SideEffects All() {
844 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
845 }
846
847 static SideEffects ChangesSomething() {
848 return SideEffects((1 << kFlagChangesCount) - 1);
849 }
850
851 static SideEffects DependsOnSomething() {
852 int count = kFlagDependsOnCount - kFlagChangesCount;
853 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
854 }
855
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100856 SideEffects Union(SideEffects other) const {
857 return SideEffects(flags_ | other.flags_);
858 }
859
Roland Levillain72bceff2014-09-15 18:29:00 +0100860 bool HasSideEffects() const {
861 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
862 return (flags_ & all_bits_set) != 0;
863 }
864
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100865 bool HasAllSideEffects() const {
866 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
867 return all_bits_set == (flags_ & all_bits_set);
868 }
869
870 bool DependsOn(SideEffects other) const {
871 size_t depends_flags = other.ComputeDependsFlags();
872 return (flags_ & depends_flags) != 0;
873 }
874
875 bool HasDependencies() const {
876 int count = kFlagDependsOnCount - kFlagChangesCount;
877 size_t all_bits_set = (1 << count) - 1;
878 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
879 }
880
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100881 private:
882 static constexpr int kFlagChangesSomething = 0;
883 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
884
885 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
886 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
887
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100888 explicit SideEffects(size_t flags) : flags_(flags) {}
889
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100890 size_t ComputeDependsFlags() const {
891 return flags_ << kFlagChangesCount;
892 }
893
894 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100895};
896
David Brazdiled596192015-01-23 10:39:45 +0000897// A HEnvironment object contains the values of virtual registers at a given location.
898class HEnvironment : public ArenaObject<kArenaAllocMisc> {
899 public:
900 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
901 : vregs_(arena, number_of_vregs) {
902 vregs_.SetSize(number_of_vregs);
903 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000904 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000905 }
906 }
907
908 void CopyFrom(HEnvironment* env);
909
910 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +0000911 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +0000912 }
913
914 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +0000915 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +0000916 }
917
David Brazdil1abb4192015-02-17 18:33:36 +0000918 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +0000919
920 size_t Size() const { return vregs_.Size(); }
921
922 private:
David Brazdil1abb4192015-02-17 18:33:36 +0000923 // Record instructions' use entries of this environment for constant-time removal.
924 // It should only be called by HInstruction when a new environment use is added.
925 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
926 DCHECK(env_use->GetUser() == this);
927 size_t index = env_use->GetIndex();
928 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
929 }
David Brazdiled596192015-01-23 10:39:45 +0000930
David Brazdil1abb4192015-02-17 18:33:36 +0000931 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +0000932
David Brazdil1abb4192015-02-17 18:33:36 +0000933 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +0000934
935 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
936};
937
Calin Juravleacf735c2015-02-12 15:25:22 +0000938class ReferenceTypeInfo : ValueObject {
939 public:
Calin Juravleb1498f62015-02-16 13:13:29 +0000940 typedef Handle<mirror::Class> TypeHandle;
941
942 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
943 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
944 if (type_handle->IsObjectClass()) {
945 // Override the type handle to be consistent with the case when we get to
946 // Top but don't have the Object class available. It avoids having to guess
947 // what value the type_handle has when it's Top.
948 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
949 } else {
950 return ReferenceTypeInfo(type_handle, is_exact, false);
951 }
952 }
953
954 static ReferenceTypeInfo CreateTop(bool is_exact) {
955 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +0000956 }
957
958 bool IsExact() const { return is_exact_; }
959 bool IsTop() const { return is_top_; }
960
961 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
962
Calin Juravleb1498f62015-02-16 13:13:29 +0000963 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000964 if (IsTop()) {
965 // Top (equivalent for java.lang.Object) is supertype of anything.
966 return true;
967 }
968 if (rti.IsTop()) {
969 // If we get here `this` is not Top() so it can't be a supertype.
970 return false;
971 }
972 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
973 }
974
975 // Returns true if the type information provide the same amount of details.
976 // Note that it does not mean that the instructions have the same actual type
977 // (e.g. tops are equal but they can be the result of a merge).
978 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
979 if (IsExact() != rti.IsExact()) {
980 return false;
981 }
982 if (IsTop() && rti.IsTop()) {
983 // `Top` means java.lang.Object, so the types are equivalent.
984 return true;
985 }
986 if (IsTop() || rti.IsTop()) {
987 // If only one is top or object than they are not equivalent.
988 // NB: We need this extra check because the type_handle of `Top` is invalid
989 // and we cannot inspect its reference.
990 return false;
991 }
992
993 // Finally check the types.
994 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
995 }
996
997 private:
Calin Juravleb1498f62015-02-16 13:13:29 +0000998 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
999 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1000 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1001
Calin Juravleacf735c2015-02-12 15:25:22 +00001002 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001003 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001004 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001005 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001006 bool is_exact_;
1007 // A true value here means that the object type should be java.lang.Object.
1008 // We don't have access to the corresponding mirror object every time so this
1009 // flag acts as a substitute. When true, the TypeHandle refers to a null
1010 // pointer and should not be used.
1011 bool is_top_;
1012};
1013
1014std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1015
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001016class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001017 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001018 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001019 : previous_(nullptr),
1020 next_(nullptr),
1021 block_(nullptr),
1022 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001023 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001024 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001025 locations_(nullptr),
1026 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001027 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001028 side_effects_(side_effects),
1029 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001030
Dave Allison20dfc792014-06-16 20:44:29 -07001031 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001032
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001033#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001034 enum InstructionKind {
1035 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1036 };
1037#undef DECLARE_KIND
1038
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001039 HInstruction* GetNext() const { return next_; }
1040 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001041
Calin Juravle77520bc2015-01-12 18:45:46 +00001042 HInstruction* GetNextDisregardingMoves() const;
1043 HInstruction* GetPreviousDisregardingMoves() const;
1044
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001045 HBasicBlock* GetBlock() const { return block_; }
1046 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001047 bool IsInBlock() const { return block_ != nullptr; }
1048 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001049 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001050
Roland Levillain6b879dd2014-09-22 17:13:44 +01001051 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001052 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001053
1054 virtual void Accept(HGraphVisitor* visitor) = 0;
1055 virtual const char* DebugName() const = 0;
1056
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001057 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001058 void SetRawInputAt(size_t index, HInstruction* input) {
1059 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1060 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001061
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001062 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001063 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001064 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001065 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001066
Calin Juravle61d544b2015-02-23 16:46:57 +00001067 virtual bool ActAsNullConstant() const { return false; }
1068
Calin Juravle10e244f2015-01-26 18:54:32 +00001069 // Does not apply for all instructions, but having this at top level greatly
1070 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001071 virtual bool CanBeNull() const {
1072 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1073 return true;
1074 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001075
Calin Juravle77520bc2015-01-12 18:45:46 +00001076 virtual bool CanDoImplicitNullCheck() const { return false; }
1077
Calin Juravleacf735c2015-02-12 15:25:22 +00001078 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001079 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001080 reference_type_info_ = reference_type_info;
1081 }
1082
Calin Juravle61d544b2015-02-23 16:46:57 +00001083 ReferenceTypeInfo GetReferenceTypeInfo() const {
1084 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1085 return reference_type_info_;
1086 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001087
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001088 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001089 DCHECK(user != nullptr);
1090 HUseListNode<HInstruction*>* use =
1091 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1092 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001093 }
1094
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001095 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001096 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001097 HUseListNode<HEnvironment*>* env_use =
1098 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1099 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001100 }
1101
David Brazdil1abb4192015-02-17 18:33:36 +00001102 void RemoveAsUserOfInput(size_t input) {
1103 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1104 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1105 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001106
David Brazdil1abb4192015-02-17 18:33:36 +00001107 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1108 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001109
David Brazdiled596192015-01-23 10:39:45 +00001110 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1111 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001112 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001113
Roland Levillain6c82d402014-10-13 16:10:27 +01001114 // Does this instruction strictly dominate `other_instruction`?
1115 // Returns false if this instruction and `other_instruction` are the same.
1116 // Aborts if this instruction and `other_instruction` are both phis.
1117 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001118
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001119 int GetId() const { return id_; }
1120 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001121
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001122 int GetSsaIndex() const { return ssa_index_; }
1123 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1124 bool HasSsaIndex() const { return ssa_index_ != -1; }
1125
1126 bool HasEnvironment() const { return environment_ != nullptr; }
1127 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001128 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
1129
Nicolas Geoffray39468442014-09-02 15:17:15 +01001130 // Returns the number of entries in the environment. Typically, that is the
1131 // number of dex registers in a method. It could be more in case of inlining.
1132 size_t EnvironmentSize() const;
1133
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001134 LocationSummary* GetLocations() const { return locations_; }
1135 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001136
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001137 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001138 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001139
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001140 // Move `this` instruction before `cursor`.
1141 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001142
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001143#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001144 bool Is##type() const { return (As##type() != nullptr); } \
1145 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001146 virtual H##type* As##type() { return nullptr; }
1147
1148 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1149#undef INSTRUCTION_TYPE_CHECK
1150
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001151 // Returns whether the instruction can be moved within the graph.
1152 virtual bool CanBeMoved() const { return false; }
1153
1154 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001155 virtual bool InstructionTypeEquals(HInstruction* other) const {
1156 UNUSED(other);
1157 return false;
1158 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001159
1160 // Returns whether any data encoded in the two instructions is equal.
1161 // This method does not look at the inputs. Both instructions must be
1162 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001163 virtual bool InstructionDataEquals(HInstruction* other) const {
1164 UNUSED(other);
1165 return false;
1166 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001167
1168 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001169 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001170 // 2) Their inputs are identical.
1171 bool Equals(HInstruction* other) const;
1172
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001173 virtual InstructionKind GetKind() const = 0;
1174
1175 virtual size_t ComputeHashCode() const {
1176 size_t result = GetKind();
1177 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1178 result = (result * 31) + InputAt(i)->GetId();
1179 }
1180 return result;
1181 }
1182
1183 SideEffects GetSideEffects() const { return side_effects_; }
1184
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001185 size_t GetLifetimePosition() const { return lifetime_position_; }
1186 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1187 LiveInterval* GetLiveInterval() const { return live_interval_; }
1188 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1189 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1190
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001191 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1192
1193 // Returns whether the code generation of the instruction will require to have access
1194 // to the current method. Such instructions are:
1195 // (1): Instructions that require an environment, as calling the runtime requires
1196 // to walk the stack and have the current method stored at a specific stack address.
1197 // (2): Object literals like classes and strings, that are loaded from the dex cache
1198 // fields of the current method.
1199 bool NeedsCurrentMethod() const {
1200 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1201 }
1202
Nicolas Geoffray7e4c3502015-03-18 11:00:52 +00001203 virtual bool NeedsDexCache() const { return false; }
1204
David Brazdil1abb4192015-02-17 18:33:36 +00001205 protected:
1206 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1207 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1208
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001209 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001210 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1211
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001212 HInstruction* previous_;
1213 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001214 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001215
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001216 // An instruction gets an id when it is added to the graph.
1217 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001218 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001219 int id_;
1220
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001221 // When doing liveness analysis, instructions that have uses get an SSA index.
1222 int ssa_index_;
1223
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001224 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001225 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001226
1227 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001228 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001229
Nicolas Geoffray39468442014-09-02 15:17:15 +01001230 // The environment associated with this instruction. Not null if the instruction
1231 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001232 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001233
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001234 // Set by the code generator.
1235 LocationSummary* locations_;
1236
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001237 // Set by the liveness analysis.
1238 LiveInterval* live_interval_;
1239
1240 // Set by the liveness analysis, this is the position in a linear
1241 // order of blocks where this instruction's live interval start.
1242 size_t lifetime_position_;
1243
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001244 const SideEffects side_effects_;
1245
Calin Juravleacf735c2015-02-12 15:25:22 +00001246 // TODO: for primitive types this should be marked as invalid.
1247 ReferenceTypeInfo reference_type_info_;
1248
David Brazdil1abb4192015-02-17 18:33:36 +00001249 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001250 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001251 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001252 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001253 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001254
1255 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1256};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001257std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001258
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001259class HInputIterator : public ValueObject {
1260 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001261 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001262
1263 bool Done() const { return index_ == instruction_->InputCount(); }
1264 HInstruction* Current() const { return instruction_->InputAt(index_); }
1265 void Advance() { index_++; }
1266
1267 private:
1268 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001269 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001270
1271 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1272};
1273
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001274class HInstructionIterator : public ValueObject {
1275 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001276 explicit HInstructionIterator(const HInstructionList& instructions)
1277 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001278 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001279 }
1280
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001281 bool Done() const { return instruction_ == nullptr; }
1282 HInstruction* Current() const { return instruction_; }
1283 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001284 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001285 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001286 }
1287
1288 private:
1289 HInstruction* instruction_;
1290 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001291
1292 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001293};
1294
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001295class HBackwardInstructionIterator : public ValueObject {
1296 public:
1297 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1298 : instruction_(instructions.last_instruction_) {
1299 next_ = Done() ? nullptr : instruction_->GetPrevious();
1300 }
1301
1302 bool Done() const { return instruction_ == nullptr; }
1303 HInstruction* Current() const { return instruction_; }
1304 void Advance() {
1305 instruction_ = next_;
1306 next_ = Done() ? nullptr : instruction_->GetPrevious();
1307 }
1308
1309 private:
1310 HInstruction* instruction_;
1311 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001312
1313 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001314};
1315
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001316// An embedded container with N elements of type T. Used (with partial
1317// specialization for N=0) because embedded arrays cannot have size 0.
1318template<typename T, intptr_t N>
1319class EmbeddedArray {
1320 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001321 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001322
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001323 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001324
1325 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001326 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001327 return elements_[i];
1328 }
1329
1330 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001331 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001332 return elements_[i];
1333 }
1334
1335 const T& At(intptr_t i) const {
1336 return (*this)[i];
1337 }
1338
1339 void SetAt(intptr_t i, const T& val) {
1340 (*this)[i] = val;
1341 }
1342
1343 private:
1344 T elements_[N];
1345};
1346
1347template<typename T>
1348class EmbeddedArray<T, 0> {
1349 public:
1350 intptr_t length() const { return 0; }
1351 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001352 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001353 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001354 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001355 }
1356 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001357 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001358 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001359 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001360 }
1361};
1362
1363template<intptr_t N>
1364class HTemplateInstruction: public HInstruction {
1365 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001366 HTemplateInstruction<N>(SideEffects side_effects)
1367 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001368 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001369
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001370 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001371
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001372 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001373 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1374
1375 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1376 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001377 }
1378
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001379 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001380 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001381
1382 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001383};
1384
Dave Allison20dfc792014-06-16 20:44:29 -07001385template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001386class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001387 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001388 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1389 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001390 virtual ~HExpression() {}
1391
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001392 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001393
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001394 protected:
1395 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001396};
1397
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001398// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1399// instruction that branches to the exit block.
1400class HReturnVoid : public HTemplateInstruction<0> {
1401 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001402 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001403
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001404 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001405
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001406 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001407
1408 private:
1409 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1410};
1411
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001412// Represents dex's RETURN opcodes. A HReturn is a control flow
1413// instruction that branches to the exit block.
1414class HReturn : public HTemplateInstruction<1> {
1415 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001416 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001417 SetRawInputAt(0, value);
1418 }
1419
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001420 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001421
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001422 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001423
1424 private:
1425 DISALLOW_COPY_AND_ASSIGN(HReturn);
1426};
1427
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001428// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001429// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001430// exit block.
1431class HExit : public HTemplateInstruction<0> {
1432 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001433 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001434
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001435 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001436
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001437 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001438
1439 private:
1440 DISALLOW_COPY_AND_ASSIGN(HExit);
1441};
1442
1443// Jumps from one block to another.
1444class HGoto : public HTemplateInstruction<0> {
1445 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001446 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1447
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001448 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001449
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001450 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001451 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001452 }
1453
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001454 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001455
1456 private:
1457 DISALLOW_COPY_AND_ASSIGN(HGoto);
1458};
1459
Dave Allison20dfc792014-06-16 20:44:29 -07001460
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001461// Conditional branch. A block ending with an HIf instruction must have
1462// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001463class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001464 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001465 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001466 SetRawInputAt(0, input);
1467 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001468
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001469 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001470
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001471 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001472 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001473 }
1474
1475 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001476 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001477 }
1478
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001479 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001480
Andreas Gampe0ba62732015-03-24 02:39:46 +00001481 virtual bool IsIfInstruction() const { return true; }
1482
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001483 private:
1484 DISALLOW_COPY_AND_ASSIGN(HIf);
1485};
1486
Roland Levillain88cb1752014-10-20 16:36:47 +01001487class HUnaryOperation : public HExpression<1> {
1488 public:
1489 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1490 : HExpression(result_type, SideEffects::None()) {
1491 SetRawInputAt(0, input);
1492 }
1493
1494 HInstruction* GetInput() const { return InputAt(0); }
1495 Primitive::Type GetResultType() const { return GetType(); }
1496
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001497 bool CanBeMoved() const OVERRIDE { return true; }
1498 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001499 UNUSED(other);
1500 return true;
1501 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001502
Roland Levillain9240d6a2014-10-20 16:47:04 +01001503 // Try to statically evaluate `operation` and return a HConstant
1504 // containing the result of this evaluation. If `operation` cannot
1505 // be evaluated as a constant, return nullptr.
1506 HConstant* TryStaticEvaluation() const;
1507
1508 // Apply this operation to `x`.
1509 virtual int32_t Evaluate(int32_t x) const = 0;
1510 virtual int64_t Evaluate(int64_t x) const = 0;
1511
Roland Levillain88cb1752014-10-20 16:36:47 +01001512 DECLARE_INSTRUCTION(UnaryOperation);
1513
1514 private:
1515 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1516};
1517
Dave Allison20dfc792014-06-16 20:44:29 -07001518class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001519 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001520 HBinaryOperation(Primitive::Type result_type,
1521 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001522 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001523 SetRawInputAt(0, left);
1524 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001525 }
1526
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001527 HInstruction* GetLeft() const { return InputAt(0); }
1528 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001529 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001530
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001531 virtual bool IsCommutative() const { return false; }
1532
1533 // Put constant on the right.
1534 // Returns whether order is changed.
1535 bool OrderInputsWithConstantOnTheRight() {
1536 HInstruction* left = InputAt(0);
1537 HInstruction* right = InputAt(1);
1538 if (left->IsConstant() && !right->IsConstant()) {
1539 ReplaceInput(right, 0);
1540 ReplaceInput(left, 1);
1541 return true;
1542 }
1543 return false;
1544 }
1545
1546 // Order inputs by instruction id, but favor constant on the right side.
1547 // This helps GVN for commutative ops.
1548 void OrderInputs() {
1549 DCHECK(IsCommutative());
1550 HInstruction* left = InputAt(0);
1551 HInstruction* right = InputAt(1);
1552 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1553 return;
1554 }
1555 if (OrderInputsWithConstantOnTheRight()) {
1556 return;
1557 }
1558 // Order according to instruction id.
1559 if (left->GetId() > right->GetId()) {
1560 ReplaceInput(right, 0);
1561 ReplaceInput(left, 1);
1562 }
1563 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001564
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001565 bool CanBeMoved() const OVERRIDE { return true; }
1566 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001567 UNUSED(other);
1568 return true;
1569 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001570
Roland Levillain9240d6a2014-10-20 16:47:04 +01001571 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001572 // containing the result of this evaluation. If `operation` cannot
1573 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001574 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001575
1576 // Apply this operation to `x` and `y`.
1577 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1578 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1579
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001580 // Returns an input that can legally be used as the right input and is
1581 // constant, or nullptr.
1582 HConstant* GetConstantRight() const;
1583
1584 // If `GetConstantRight()` returns one of the input, this returns the other
1585 // one. Otherwise it returns nullptr.
1586 HInstruction* GetLeastConstantLeft() const;
1587
Roland Levillainccc07a92014-09-16 14:48:16 +01001588 DECLARE_INSTRUCTION(BinaryOperation);
1589
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001590 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001591 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1592};
1593
Dave Allison20dfc792014-06-16 20:44:29 -07001594class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001595 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001596 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001597 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1598 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001599
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001600 bool NeedsMaterialization() const { return needs_materialization_; }
1601 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001602
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001603 // For code generation purposes, returns whether this instruction is just before
Andreas Gampe0ba62732015-03-24 02:39:46 +00001604 // `if_`, and disregard moves in between.
1605 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001606
Dave Allison20dfc792014-06-16 20:44:29 -07001607 DECLARE_INSTRUCTION(Condition);
1608
1609 virtual IfCondition GetCondition() const = 0;
1610
1611 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001612 // For register allocation purposes, returns whether this instruction needs to be
1613 // materialized (that is, not just be in the processor flags).
1614 bool needs_materialization_;
1615
Dave Allison20dfc792014-06-16 20:44:29 -07001616 DISALLOW_COPY_AND_ASSIGN(HCondition);
1617};
1618
1619// Instruction to check if two inputs are equal to each other.
1620class HEqual : public HCondition {
1621 public:
1622 HEqual(HInstruction* first, HInstruction* second)
1623 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001624
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001625 bool IsCommutative() const OVERRIDE { return true; }
1626
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001627 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001628 return x == y ? 1 : 0;
1629 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001630 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001631 return x == y ? 1 : 0;
1632 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001633
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001634 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001635
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001636 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001637 return kCondEQ;
1638 }
1639
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001640 private:
1641 DISALLOW_COPY_AND_ASSIGN(HEqual);
1642};
1643
Dave Allison20dfc792014-06-16 20:44:29 -07001644class HNotEqual : public HCondition {
1645 public:
1646 HNotEqual(HInstruction* first, HInstruction* second)
1647 : HCondition(first, second) {}
1648
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001649 bool IsCommutative() const OVERRIDE { return true; }
1650
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001651 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001652 return x != y ? 1 : 0;
1653 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001654 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001655 return x != y ? 1 : 0;
1656 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001657
Dave Allison20dfc792014-06-16 20:44:29 -07001658 DECLARE_INSTRUCTION(NotEqual);
1659
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001660 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001661 return kCondNE;
1662 }
1663
1664 private:
1665 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1666};
1667
1668class HLessThan : public HCondition {
1669 public:
1670 HLessThan(HInstruction* first, HInstruction* second)
1671 : HCondition(first, second) {}
1672
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001673 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001674 return x < y ? 1 : 0;
1675 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001676 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001677 return x < y ? 1 : 0;
1678 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001679
Dave Allison20dfc792014-06-16 20:44:29 -07001680 DECLARE_INSTRUCTION(LessThan);
1681
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001682 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001683 return kCondLT;
1684 }
1685
1686 private:
1687 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1688};
1689
1690class HLessThanOrEqual : public HCondition {
1691 public:
1692 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1693 : HCondition(first, second) {}
1694
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001695 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001696 return x <= y ? 1 : 0;
1697 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001698 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001699 return x <= y ? 1 : 0;
1700 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001701
Dave Allison20dfc792014-06-16 20:44:29 -07001702 DECLARE_INSTRUCTION(LessThanOrEqual);
1703
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001704 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001705 return kCondLE;
1706 }
1707
1708 private:
1709 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1710};
1711
1712class HGreaterThan : public HCondition {
1713 public:
1714 HGreaterThan(HInstruction* first, HInstruction* second)
1715 : HCondition(first, second) {}
1716
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001717 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001718 return x > y ? 1 : 0;
1719 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001720 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001721 return x > y ? 1 : 0;
1722 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001723
Dave Allison20dfc792014-06-16 20:44:29 -07001724 DECLARE_INSTRUCTION(GreaterThan);
1725
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001726 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001727 return kCondGT;
1728 }
1729
1730 private:
1731 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1732};
1733
1734class HGreaterThanOrEqual : public HCondition {
1735 public:
1736 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1737 : HCondition(first, second) {}
1738
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001739 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001740 return x >= y ? 1 : 0;
1741 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001742 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001743 return x >= y ? 1 : 0;
1744 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001745
Dave Allison20dfc792014-06-16 20:44:29 -07001746 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1747
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001748 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001749 return kCondGE;
1750 }
1751
1752 private:
1753 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1754};
1755
1756
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001757// Instruction to check how two inputs compare to each other.
1758// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1759class HCompare : public HBinaryOperation {
1760 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001761 // The bias applies for floating point operations and indicates how NaN
1762 // comparisons are treated:
1763 enum Bias {
1764 kNoBias, // bias is not applicable (i.e. for long operation)
1765 kGtBias, // return 1 for NaN comparisons
1766 kLtBias, // return -1 for NaN comparisons
1767 };
1768
1769 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1770 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001771 DCHECK_EQ(type, first->GetType());
1772 DCHECK_EQ(type, second->GetType());
1773 }
1774
Calin Juravleddb7df22014-11-25 20:56:51 +00001775 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001776 return
1777 x == y ? 0 :
1778 x > y ? 1 :
1779 -1;
1780 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001781
1782 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001783 return
1784 x == y ? 0 :
1785 x > y ? 1 :
1786 -1;
1787 }
1788
Calin Juravleddb7df22014-11-25 20:56:51 +00001789 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1790 return bias_ == other->AsCompare()->bias_;
1791 }
1792
1793 bool IsGtBias() { return bias_ == kGtBias; }
1794
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001795 DECLARE_INSTRUCTION(Compare);
1796
1797 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001798 const Bias bias_;
1799
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001800 DISALLOW_COPY_AND_ASSIGN(HCompare);
1801};
1802
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001803// A local in the graph. Corresponds to a Dex register.
1804class HLocal : public HTemplateInstruction<0> {
1805 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001806 explicit HLocal(uint16_t reg_number)
1807 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001808
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001809 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001810
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001811 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001812
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001813 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001814 // The Dex register number.
1815 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001816
1817 DISALLOW_COPY_AND_ASSIGN(HLocal);
1818};
1819
1820// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001821class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001822 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001823 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001824 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001825 SetRawInputAt(0, local);
1826 }
1827
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001828 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1829
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001830 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001831
1832 private:
1833 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1834};
1835
1836// Store a value in a given local. This instruction has two inputs: the value
1837// and the local.
1838class HStoreLocal : public HTemplateInstruction<2> {
1839 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001840 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001841 SetRawInputAt(0, local);
1842 SetRawInputAt(1, value);
1843 }
1844
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001845 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1846
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001847 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001848
1849 private:
1850 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1851};
1852
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001853class HConstant : public HExpression<0> {
1854 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001855 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1856
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001857 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001858
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001859 virtual bool IsMinusOne() const { return false; }
1860 virtual bool IsZero() const { return false; }
1861 virtual bool IsOne() const { return false; }
1862
1863 static HConstant* NewConstant(ArenaAllocator* allocator, Primitive::Type type, int64_t val);
1864
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001865 DECLARE_INSTRUCTION(Constant);
1866
1867 private:
1868 DISALLOW_COPY_AND_ASSIGN(HConstant);
1869};
1870
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001871class HFloatConstant : public HConstant {
1872 public:
1873 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1874
1875 float GetValue() const { return value_; }
1876
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001877 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001878 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1879 bit_cast<float, int32_t>(value_);
1880 }
1881
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001882 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001883
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001884 bool IsMinusOne() const OVERRIDE {
1885 return bit_cast<uint32_t>(AsFloatConstant()->GetValue()) == bit_cast<uint32_t>((-1.0f));
1886 }
1887 bool IsZero() const OVERRIDE {
1888 return AsFloatConstant()->GetValue() == 0.0f;
1889 }
1890 bool IsOne() const OVERRIDE {
1891 return bit_cast<uint32_t>(AsFloatConstant()->GetValue()) == bit_cast<uint32_t>(1.0f);
1892 }
1893
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001894 DECLARE_INSTRUCTION(FloatConstant);
1895
1896 private:
1897 const float value_;
1898
1899 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1900};
1901
1902class HDoubleConstant : public HConstant {
1903 public:
1904 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1905
1906 double GetValue() const { return value_; }
1907
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001908 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001909 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1910 bit_cast<double, int64_t>(value_);
1911 }
1912
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001913 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001914
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001915 bool IsMinusOne() const OVERRIDE {
1916 return bit_cast<uint64_t>(AsDoubleConstant()->GetValue()) == bit_cast<uint64_t>((-1.0));
1917 }
1918 bool IsZero() const OVERRIDE {
1919 return AsDoubleConstant()->GetValue() == 0.0;
1920 }
1921 bool IsOne() const OVERRIDE {
1922 return bit_cast<uint64_t>(AsDoubleConstant()->GetValue()) == bit_cast<uint64_t>(1.0);
1923 }
1924
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001925 DECLARE_INSTRUCTION(DoubleConstant);
1926
1927 private:
1928 const double value_;
1929
1930 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1931};
1932
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001933class HNullConstant : public HConstant {
1934 public:
1935 HNullConstant() : HConstant(Primitive::kPrimNot) {}
1936
1937 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
1938 return true;
1939 }
1940
1941 size_t ComputeHashCode() const OVERRIDE { return 0; }
1942
Calin Juravle61d544b2015-02-23 16:46:57 +00001943 bool ActAsNullConstant() const OVERRIDE { return true; }
1944
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001945 DECLARE_INSTRUCTION(NullConstant);
1946
1947 private:
1948 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
1949};
1950
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001951// Constants of the type int. Those can be from Dex instructions, or
1952// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001953class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001954 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001955 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001956
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001957 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001958
Calin Juravle61d544b2015-02-23 16:46:57 +00001959 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001960 return other->AsIntConstant()->value_ == value_;
1961 }
1962
Calin Juravle61d544b2015-02-23 16:46:57 +00001963 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
1964
1965 // TODO: Null is represented by the `0` constant. In most cases we replace it
1966 // with a HNullConstant but we don't do it when comparing (a != null). This
1967 // method is an workaround until we fix the above.
1968 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001969
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001970 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
1971 bool IsZero() const OVERRIDE { return GetValue() == 0; }
1972 bool IsOne() const OVERRIDE { return GetValue() == 1; }
1973
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001974 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001975
1976 private:
1977 const int32_t value_;
1978
1979 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1980};
1981
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001982class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001983 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001984 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001985
1986 int64_t GetValue() const { return value_; }
1987
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001988 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001989 return other->AsLongConstant()->value_ == value_;
1990 }
1991
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001992 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001993
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001994 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
1995 bool IsZero() const OVERRIDE { return GetValue() == 0; }
1996 bool IsOne() const OVERRIDE { return GetValue() == 1; }
1997
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001998 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001999
2000 private:
2001 const int64_t value_;
2002
2003 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2004};
2005
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002006enum class Intrinsics {
2007#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2008#include "intrinsics_list.h"
2009 kNone,
2010 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2011#undef INTRINSICS_LIST
2012#undef OPTIMIZING_INTRINSICS
2013};
2014std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2015
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002016class HInvoke : public HInstruction {
2017 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002018 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002019
2020 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2021 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002022 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002023
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002024 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002025 SetRawInputAt(index, argument);
2026 }
2027
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002028 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002029
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002030 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002031
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002032 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2033
2034 Intrinsics GetIntrinsic() {
2035 return intrinsic_;
2036 }
2037
2038 void SetIntrinsic(Intrinsics intrinsic) {
2039 intrinsic_ = intrinsic;
2040 }
2041
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002042 DECLARE_INSTRUCTION(Invoke);
2043
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002044 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002045 HInvoke(ArenaAllocator* arena,
2046 uint32_t number_of_arguments,
2047 Primitive::Type return_type,
2048 uint32_t dex_pc,
2049 uint32_t dex_method_index)
2050 : HInstruction(SideEffects::All()),
2051 inputs_(arena, number_of_arguments),
2052 return_type_(return_type),
2053 dex_pc_(dex_pc),
2054 dex_method_index_(dex_method_index),
2055 intrinsic_(Intrinsics::kNone) {
2056 inputs_.SetSize(number_of_arguments);
2057 }
2058
David Brazdil1abb4192015-02-17 18:33:36 +00002059 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2060 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2061 inputs_.Put(index, input);
2062 }
2063
2064 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002065 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002066 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002067 const uint32_t dex_method_index_;
2068 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002069
2070 private:
2071 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2072};
2073
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002074class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002075 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002076 HInvokeStaticOrDirect(ArenaAllocator* arena,
2077 uint32_t number_of_arguments,
2078 Primitive::Type return_type,
2079 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002080 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002081 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002082 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002083 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002084 invoke_type_(invoke_type),
2085 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002086
Calin Juravle77520bc2015-01-12 18:45:46 +00002087 bool CanDoImplicitNullCheck() const OVERRIDE {
2088 // We access the method via the dex cache so we can't do an implicit null check.
2089 // TODO: for intrinsics we can generate implicit null checks.
2090 return false;
2091 }
2092
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002093 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002094 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray7e4c3502015-03-18 11:00:52 +00002095 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002096
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002097 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002098
2099 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002100 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002101 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002102
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002103 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002104};
2105
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002106class HInvokeVirtual : public HInvoke {
2107 public:
2108 HInvokeVirtual(ArenaAllocator* arena,
2109 uint32_t number_of_arguments,
2110 Primitive::Type return_type,
2111 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002112 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002113 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002114 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002115 vtable_index_(vtable_index) {}
2116
Calin Juravle77520bc2015-01-12 18:45:46 +00002117 bool CanDoImplicitNullCheck() const OVERRIDE {
2118 // TODO: Add implicit null checks in intrinsics.
2119 return !GetLocations()->Intrinsified();
2120 }
2121
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002122 uint32_t GetVTableIndex() const { return vtable_index_; }
2123
2124 DECLARE_INSTRUCTION(InvokeVirtual);
2125
2126 private:
2127 const uint32_t vtable_index_;
2128
2129 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2130};
2131
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002132class HInvokeInterface : public HInvoke {
2133 public:
2134 HInvokeInterface(ArenaAllocator* arena,
2135 uint32_t number_of_arguments,
2136 Primitive::Type return_type,
2137 uint32_t dex_pc,
2138 uint32_t dex_method_index,
2139 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002140 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002141 imt_index_(imt_index) {}
2142
Calin Juravle77520bc2015-01-12 18:45:46 +00002143 bool CanDoImplicitNullCheck() const OVERRIDE {
2144 // TODO: Add implicit null checks in intrinsics.
2145 return !GetLocations()->Intrinsified();
2146 }
2147
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002148 uint32_t GetImtIndex() const { return imt_index_; }
2149 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2150
2151 DECLARE_INSTRUCTION(InvokeInterface);
2152
2153 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002154 const uint32_t imt_index_;
2155
2156 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2157};
2158
Dave Allison20dfc792014-06-16 20:44:29 -07002159class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002160 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002161 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002162 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2163 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002164 type_index_(type_index),
2165 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002166
2167 uint32_t GetDexPc() const { return dex_pc_; }
2168 uint16_t GetTypeIndex() const { return type_index_; }
2169
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002170 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002171 bool NeedsEnvironment() const OVERRIDE { return true; }
2172 // It may throw when called on:
2173 // - interfaces
2174 // - abstract/innaccessible/unknown classes
2175 // TODO: optimize when possible.
2176 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002177
Calin Juravle10e244f2015-01-26 18:54:32 +00002178 bool CanBeNull() const OVERRIDE { return false; }
2179
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002180 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2181
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002182 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002183
2184 private:
2185 const uint32_t dex_pc_;
2186 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002187 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002188
2189 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2190};
2191
Roland Levillain88cb1752014-10-20 16:36:47 +01002192class HNeg : public HUnaryOperation {
2193 public:
2194 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2195 : HUnaryOperation(result_type, input) {}
2196
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002197 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2198 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002199
Roland Levillain88cb1752014-10-20 16:36:47 +01002200 DECLARE_INSTRUCTION(Neg);
2201
2202 private:
2203 DISALLOW_COPY_AND_ASSIGN(HNeg);
2204};
2205
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002206class HNewArray : public HExpression<1> {
2207 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002208 HNewArray(HInstruction* length,
2209 uint32_t dex_pc,
2210 uint16_t type_index,
2211 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002212 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2213 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002214 type_index_(type_index),
2215 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002216 SetRawInputAt(0, length);
2217 }
2218
2219 uint32_t GetDexPc() const { return dex_pc_; }
2220 uint16_t GetTypeIndex() const { return type_index_; }
2221
2222 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002223 bool NeedsEnvironment() const OVERRIDE { return true; }
2224
2225 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002226
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002227 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2228
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002229 DECLARE_INSTRUCTION(NewArray);
2230
2231 private:
2232 const uint32_t dex_pc_;
2233 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002234 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002235
2236 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2237};
2238
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002239class HAdd : public HBinaryOperation {
2240 public:
2241 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2242 : HBinaryOperation(result_type, left, right) {}
2243
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002244 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002245
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002246 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002247 return x + y;
2248 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002249 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002250 return x + y;
2251 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002252
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002253 DECLARE_INSTRUCTION(Add);
2254
2255 private:
2256 DISALLOW_COPY_AND_ASSIGN(HAdd);
2257};
2258
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002259class HSub : public HBinaryOperation {
2260 public:
2261 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2262 : HBinaryOperation(result_type, left, right) {}
2263
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002264 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002265 return x - y;
2266 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002267 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002268 return x - y;
2269 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002270
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002271 DECLARE_INSTRUCTION(Sub);
2272
2273 private:
2274 DISALLOW_COPY_AND_ASSIGN(HSub);
2275};
2276
Calin Juravle34bacdf2014-10-07 20:23:36 +01002277class HMul : public HBinaryOperation {
2278 public:
2279 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2280 : HBinaryOperation(result_type, left, right) {}
2281
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002282 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002283
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002284 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2285 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002286
2287 DECLARE_INSTRUCTION(Mul);
2288
2289 private:
2290 DISALLOW_COPY_AND_ASSIGN(HMul);
2291};
2292
Calin Juravle7c4954d2014-10-28 16:57:40 +00002293class HDiv : public HBinaryOperation {
2294 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002295 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2296 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002297
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002298 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002299 // Our graph structure ensures we never have 0 for `y` during constant folding.
2300 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002301 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002302 return (y == -1) ? -x : x / y;
2303 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002304
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002305 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002306 DCHECK_NE(y, 0);
2307 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2308 return (y == -1) ? -x : x / y;
2309 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002310
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002311 uint32_t GetDexPc() const { return dex_pc_; }
2312
Calin Juravle7c4954d2014-10-28 16:57:40 +00002313 DECLARE_INSTRUCTION(Div);
2314
2315 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002316 const uint32_t dex_pc_;
2317
Calin Juravle7c4954d2014-10-28 16:57:40 +00002318 DISALLOW_COPY_AND_ASSIGN(HDiv);
2319};
2320
Calin Juravlebacfec32014-11-14 15:54:36 +00002321class HRem : public HBinaryOperation {
2322 public:
2323 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2324 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2325
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002326 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002327 DCHECK_NE(y, 0);
2328 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2329 return (y == -1) ? 0 : x % y;
2330 }
2331
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002332 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002333 DCHECK_NE(y, 0);
2334 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2335 return (y == -1) ? 0 : x % y;
2336 }
2337
2338 uint32_t GetDexPc() const { return dex_pc_; }
2339
2340 DECLARE_INSTRUCTION(Rem);
2341
2342 private:
2343 const uint32_t dex_pc_;
2344
2345 DISALLOW_COPY_AND_ASSIGN(HRem);
2346};
2347
Calin Juravled0d48522014-11-04 16:40:20 +00002348class HDivZeroCheck : public HExpression<1> {
2349 public:
2350 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2351 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2352 SetRawInputAt(0, value);
2353 }
2354
2355 bool CanBeMoved() const OVERRIDE { return true; }
2356
2357 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2358 UNUSED(other);
2359 return true;
2360 }
2361
2362 bool NeedsEnvironment() const OVERRIDE { return true; }
2363 bool CanThrow() const OVERRIDE { return true; }
2364
2365 uint32_t GetDexPc() const { return dex_pc_; }
2366
2367 DECLARE_INSTRUCTION(DivZeroCheck);
2368
2369 private:
2370 const uint32_t dex_pc_;
2371
2372 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2373};
2374
Calin Juravle9aec02f2014-11-18 23:06:35 +00002375class HShl : public HBinaryOperation {
2376 public:
2377 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2378 : HBinaryOperation(result_type, left, right) {}
2379
2380 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2381 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2382
2383 DECLARE_INSTRUCTION(Shl);
2384
2385 private:
2386 DISALLOW_COPY_AND_ASSIGN(HShl);
2387};
2388
2389class HShr : public HBinaryOperation {
2390 public:
2391 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2392 : HBinaryOperation(result_type, left, right) {}
2393
2394 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2395 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2396
2397 DECLARE_INSTRUCTION(Shr);
2398
2399 private:
2400 DISALLOW_COPY_AND_ASSIGN(HShr);
2401};
2402
2403class HUShr : public HBinaryOperation {
2404 public:
2405 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2406 : HBinaryOperation(result_type, left, right) {}
2407
2408 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2409 uint32_t ux = static_cast<uint32_t>(x);
2410 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2411 return static_cast<int32_t>(ux >> uy);
2412 }
2413
2414 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2415 uint64_t ux = static_cast<uint64_t>(x);
2416 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2417 return static_cast<int64_t>(ux >> uy);
2418 }
2419
2420 DECLARE_INSTRUCTION(UShr);
2421
2422 private:
2423 DISALLOW_COPY_AND_ASSIGN(HUShr);
2424};
2425
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002426class HAnd : public HBinaryOperation {
2427 public:
2428 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2429 : HBinaryOperation(result_type, left, right) {}
2430
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002431 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002432
2433 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2434 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2435
2436 DECLARE_INSTRUCTION(And);
2437
2438 private:
2439 DISALLOW_COPY_AND_ASSIGN(HAnd);
2440};
2441
2442class HOr : public HBinaryOperation {
2443 public:
2444 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2445 : HBinaryOperation(result_type, left, right) {}
2446
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002447 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002448
2449 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2450 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2451
2452 DECLARE_INSTRUCTION(Or);
2453
2454 private:
2455 DISALLOW_COPY_AND_ASSIGN(HOr);
2456};
2457
2458class HXor : public HBinaryOperation {
2459 public:
2460 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2461 : HBinaryOperation(result_type, left, right) {}
2462
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002463 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002464
2465 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2466 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2467
2468 DECLARE_INSTRUCTION(Xor);
2469
2470 private:
2471 DISALLOW_COPY_AND_ASSIGN(HXor);
2472};
2473
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002474// The value of a parameter in this method. Its location depends on
2475// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002476class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002477 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002478 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2479 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002480
2481 uint8_t GetIndex() const { return index_; }
2482
Calin Juravle10e244f2015-01-26 18:54:32 +00002483 bool CanBeNull() const OVERRIDE { return !is_this_; }
2484
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002485 DECLARE_INSTRUCTION(ParameterValue);
2486
2487 private:
2488 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002489 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002490 const uint8_t index_;
2491
Calin Juravle10e244f2015-01-26 18:54:32 +00002492 // Whether or not the parameter value corresponds to 'this' argument.
2493 const bool is_this_;
2494
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002495 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2496};
2497
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002498class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002499 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002500 explicit HNot(Primitive::Type result_type, HInstruction* input)
2501 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002502
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002503 bool CanBeMoved() const OVERRIDE { return true; }
2504 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002505 UNUSED(other);
2506 return true;
2507 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002508
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002509 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2510 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002511
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002512 DECLARE_INSTRUCTION(Not);
2513
2514 private:
2515 DISALLOW_COPY_AND_ASSIGN(HNot);
2516};
2517
Roland Levillaindff1f282014-11-05 14:15:05 +00002518class HTypeConversion : public HExpression<1> {
2519 public:
2520 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002521 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2522 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002523 SetRawInputAt(0, input);
2524 DCHECK_NE(input->GetType(), result_type);
2525 }
2526
2527 HInstruction* GetInput() const { return InputAt(0); }
2528 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2529 Primitive::Type GetResultType() const { return GetType(); }
2530
Roland Levillain624279f2014-12-04 11:54:28 +00002531 // Required by the x86 and ARM code generators when producing calls
2532 // to the runtime.
2533 uint32_t GetDexPc() const { return dex_pc_; }
2534
Roland Levillaindff1f282014-11-05 14:15:05 +00002535 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002536 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002537
2538 DECLARE_INSTRUCTION(TypeConversion);
2539
2540 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002541 const uint32_t dex_pc_;
2542
Roland Levillaindff1f282014-11-05 14:15:05 +00002543 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2544};
2545
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002546static constexpr uint32_t kNoRegNumber = -1;
2547
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002548class HPhi : public HInstruction {
2549 public:
2550 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002551 : HInstruction(SideEffects::None()),
2552 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002553 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002554 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002555 is_live_(false),
2556 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002557 inputs_.SetSize(number_of_inputs);
2558 }
2559
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002560 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2561 static Primitive::Type ToPhiType(Primitive::Type type) {
2562 switch (type) {
2563 case Primitive::kPrimBoolean:
2564 case Primitive::kPrimByte:
2565 case Primitive::kPrimShort:
2566 case Primitive::kPrimChar:
2567 return Primitive::kPrimInt;
2568 default:
2569 return type;
2570 }
2571 }
2572
Calin Juravle10e244f2015-01-26 18:54:32 +00002573 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002574
2575 void AddInput(HInstruction* input);
2576
Calin Juravle10e244f2015-01-26 18:54:32 +00002577 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002578 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002579
Calin Juravle10e244f2015-01-26 18:54:32 +00002580 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2581 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2582
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002583 uint32_t GetRegNumber() const { return reg_number_; }
2584
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002585 void SetDead() { is_live_ = false; }
2586 void SetLive() { is_live_ = true; }
2587 bool IsDead() const { return !is_live_; }
2588 bool IsLive() const { return is_live_; }
2589
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002590 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002591
David Brazdil1abb4192015-02-17 18:33:36 +00002592 protected:
2593 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2594
2595 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2596 inputs_.Put(index, input);
2597 }
2598
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002599 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002600 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002601 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002602 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002603 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002604 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002605
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002606 DISALLOW_COPY_AND_ASSIGN(HPhi);
2607};
2608
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002609class HNullCheck : public HExpression<1> {
2610 public:
2611 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002612 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002613 SetRawInputAt(0, value);
2614 }
2615
Calin Juravle10e244f2015-01-26 18:54:32 +00002616 bool CanBeMoved() const OVERRIDE { return true; }
2617 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002618 UNUSED(other);
2619 return true;
2620 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002621
Calin Juravle10e244f2015-01-26 18:54:32 +00002622 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002623
Calin Juravle10e244f2015-01-26 18:54:32 +00002624 bool CanThrow() const OVERRIDE { return true; }
2625
2626 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002627
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002628 uint32_t GetDexPc() const { return dex_pc_; }
2629
2630 DECLARE_INSTRUCTION(NullCheck);
2631
2632 private:
2633 const uint32_t dex_pc_;
2634
2635 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2636};
2637
2638class FieldInfo : public ValueObject {
2639 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002640 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2641 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002642
2643 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002644 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002645 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002646
2647 private:
2648 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002649 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002650 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002651};
2652
2653class HInstanceFieldGet : public HExpression<1> {
2654 public:
2655 HInstanceFieldGet(HInstruction* value,
2656 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002657 MemberOffset field_offset,
2658 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002659 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002660 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002661 SetRawInputAt(0, value);
2662 }
2663
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002664 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002665
2666 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2667 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2668 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002669 }
2670
Calin Juravle77520bc2015-01-12 18:45:46 +00002671 bool CanDoImplicitNullCheck() const OVERRIDE {
2672 return GetFieldOffset().Uint32Value() < kPageSize;
2673 }
2674
2675 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002676 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2677 }
2678
Calin Juravle52c48962014-12-16 17:02:57 +00002679 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002680 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002681 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002682 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002683
2684 DECLARE_INSTRUCTION(InstanceFieldGet);
2685
2686 private:
2687 const FieldInfo field_info_;
2688
2689 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2690};
2691
2692class HInstanceFieldSet : public HTemplateInstruction<2> {
2693 public:
2694 HInstanceFieldSet(HInstruction* object,
2695 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002696 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002697 MemberOffset field_offset,
2698 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002699 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002700 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002701 SetRawInputAt(0, object);
2702 SetRawInputAt(1, value);
2703 }
2704
Calin Juravle77520bc2015-01-12 18:45:46 +00002705 bool CanDoImplicitNullCheck() const OVERRIDE {
2706 return GetFieldOffset().Uint32Value() < kPageSize;
2707 }
2708
Calin Juravle52c48962014-12-16 17:02:57 +00002709 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002710 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002711 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002712 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002713 HInstruction* GetValue() const { return InputAt(1); }
2714
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002715 DECLARE_INSTRUCTION(InstanceFieldSet);
2716
2717 private:
2718 const FieldInfo field_info_;
2719
2720 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2721};
2722
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002723class HArrayGet : public HExpression<2> {
2724 public:
2725 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002726 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002727 SetRawInputAt(0, array);
2728 SetRawInputAt(1, index);
2729 }
2730
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002731 bool CanBeMoved() const OVERRIDE { return true; }
2732 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002733 UNUSED(other);
2734 return true;
2735 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002736 bool CanDoImplicitNullCheck() const OVERRIDE {
2737 // TODO: We can be smarter here.
2738 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2739 // which generates the implicit null check. There are cases when these can be removed
2740 // to produce better code. If we ever add optimizations to do so we should allow an
2741 // implicit check here (as long as the address falls in the first page).
2742 return false;
2743 }
2744
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002745 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002746
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002747 HInstruction* GetArray() const { return InputAt(0); }
2748 HInstruction* GetIndex() const { return InputAt(1); }
2749
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002750 DECLARE_INSTRUCTION(ArrayGet);
2751
2752 private:
2753 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2754};
2755
2756class HArraySet : public HTemplateInstruction<3> {
2757 public:
2758 HArraySet(HInstruction* array,
2759 HInstruction* index,
2760 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002761 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002762 uint32_t dex_pc)
2763 : HTemplateInstruction(SideEffects::ChangesSomething()),
2764 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002765 expected_component_type_(expected_component_type),
2766 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002767 SetRawInputAt(0, array);
2768 SetRawInputAt(1, index);
2769 SetRawInputAt(2, value);
2770 }
2771
Calin Juravle77520bc2015-01-12 18:45:46 +00002772 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002773 // We currently always call a runtime method to catch array store
2774 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002775 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002776 }
2777
Calin Juravle77520bc2015-01-12 18:45:46 +00002778 bool CanDoImplicitNullCheck() const OVERRIDE {
2779 // TODO: Same as for ArrayGet.
2780 return false;
2781 }
2782
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002783 void ClearNeedsTypeCheck() {
2784 needs_type_check_ = false;
2785 }
2786
2787 bool NeedsTypeCheck() const { return needs_type_check_; }
2788
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002789 uint32_t GetDexPc() const { return dex_pc_; }
2790
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002791 HInstruction* GetArray() const { return InputAt(0); }
2792 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002793 HInstruction* GetValue() const { return InputAt(2); }
2794
2795 Primitive::Type GetComponentType() const {
2796 // The Dex format does not type floating point index operations. Since the
2797 // `expected_component_type_` is set during building and can therefore not
2798 // be correct, we also check what is the value type. If it is a floating
2799 // point type, we must use that type.
2800 Primitive::Type value_type = GetValue()->GetType();
2801 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2802 ? value_type
2803 : expected_component_type_;
2804 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002805
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002806 DECLARE_INSTRUCTION(ArraySet);
2807
2808 private:
2809 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002810 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002811 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002812
2813 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2814};
2815
2816class HArrayLength : public HExpression<1> {
2817 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002818 explicit HArrayLength(HInstruction* array)
2819 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2820 // Note that arrays do not change length, so the instruction does not
2821 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002822 SetRawInputAt(0, array);
2823 }
2824
Calin Juravle77520bc2015-01-12 18:45:46 +00002825 bool CanBeMoved() const OVERRIDE { return true; }
2826 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002827 UNUSED(other);
2828 return true;
2829 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002830 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002831
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002832 DECLARE_INSTRUCTION(ArrayLength);
2833
2834 private:
2835 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2836};
2837
2838class HBoundsCheck : public HExpression<2> {
2839 public:
2840 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002841 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002842 DCHECK(index->GetType() == Primitive::kPrimInt);
2843 SetRawInputAt(0, index);
2844 SetRawInputAt(1, length);
2845 }
2846
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002847 bool CanBeMoved() const OVERRIDE { return true; }
2848 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002849 UNUSED(other);
2850 return true;
2851 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002852
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002853 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002854
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002855 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002856
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002857 uint32_t GetDexPc() const { return dex_pc_; }
2858
2859 DECLARE_INSTRUCTION(BoundsCheck);
2860
2861 private:
2862 const uint32_t dex_pc_;
2863
2864 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2865};
2866
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002867/**
2868 * Some DEX instructions are folded into multiple HInstructions that need
2869 * to stay live until the last HInstruction. This class
2870 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002871 * HInstruction stays live. `index` represents the stack location index of the
2872 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002873 */
2874class HTemporary : public HTemplateInstruction<0> {
2875 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002876 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002877
2878 size_t GetIndex() const { return index_; }
2879
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002880 Primitive::Type GetType() const OVERRIDE {
2881 // The previous instruction is the one that will be stored in the temporary location.
2882 DCHECK(GetPrevious() != nullptr);
2883 return GetPrevious()->GetType();
2884 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002885
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002886 DECLARE_INSTRUCTION(Temporary);
2887
2888 private:
2889 const size_t index_;
2890
2891 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2892};
2893
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002894class HSuspendCheck : public HTemplateInstruction<0> {
2895 public:
2896 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002897 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002898
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002899 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002900 return true;
2901 }
2902
2903 uint32_t GetDexPc() const { return dex_pc_; }
2904
2905 DECLARE_INSTRUCTION(SuspendCheck);
2906
2907 private:
2908 const uint32_t dex_pc_;
2909
2910 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2911};
2912
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002913/**
2914 * Instruction to load a Class object.
2915 */
2916class HLoadClass : public HExpression<0> {
2917 public:
2918 HLoadClass(uint16_t type_index,
2919 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002920 uint32_t dex_pc)
2921 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2922 type_index_(type_index),
2923 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002924 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00002925 generate_clinit_check_(false),
2926 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002927
2928 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002929
2930 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2931 return other->AsLoadClass()->type_index_ == type_index_;
2932 }
2933
2934 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2935
2936 uint32_t GetDexPc() const { return dex_pc_; }
2937 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002938 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002939
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002940 bool NeedsEnvironment() const OVERRIDE {
2941 // Will call runtime and load the class if the class is not loaded yet.
2942 // TODO: finer grain decision.
2943 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002944 }
2945
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002946 bool MustGenerateClinitCheck() const {
2947 return generate_clinit_check_;
2948 }
2949
2950 void SetMustGenerateClinitCheck() {
2951 generate_clinit_check_ = true;
2952 }
2953
2954 bool CanCallRuntime() const {
2955 return MustGenerateClinitCheck() || !is_referrers_class_;
2956 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002957
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002958 bool CanThrow() const OVERRIDE {
2959 // May call runtime and and therefore can throw.
2960 // TODO: finer grain decision.
2961 return !is_referrers_class_;
2962 }
2963
Calin Juravleacf735c2015-02-12 15:25:22 +00002964 ReferenceTypeInfo GetLoadedClassRTI() {
2965 return loaded_class_rti_;
2966 }
2967
2968 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
2969 // Make sure we only set exact types (the loaded class should never be merged).
2970 DCHECK(rti.IsExact());
2971 loaded_class_rti_ = rti;
2972 }
2973
2974 bool IsResolved() {
2975 return loaded_class_rti_.IsExact();
2976 }
2977
Nicolas Geoffray7e4c3502015-03-18 11:00:52 +00002978 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
2979
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002980 DECLARE_INSTRUCTION(LoadClass);
2981
2982 private:
2983 const uint16_t type_index_;
2984 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002985 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002986 // Whether this instruction must generate the initialization check.
2987 // Used for code generation.
2988 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002989
Calin Juravleacf735c2015-02-12 15:25:22 +00002990 ReferenceTypeInfo loaded_class_rti_;
2991
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002992 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2993};
2994
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002995class HLoadString : public HExpression<0> {
2996 public:
2997 HLoadString(uint32_t string_index, uint32_t dex_pc)
2998 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2999 string_index_(string_index),
3000 dex_pc_(dex_pc) {}
3001
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003002 bool CanBeMoved() const OVERRIDE { return true; }
3003
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003004 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3005 return other->AsLoadString()->string_index_ == string_index_;
3006 }
3007
3008 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3009
3010 uint32_t GetDexPc() const { return dex_pc_; }
3011 uint32_t GetStringIndex() const { return string_index_; }
3012
3013 // TODO: Can we deopt or debug when we resolve a string?
3014 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray7e4c3502015-03-18 11:00:52 +00003015 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003016
3017 DECLARE_INSTRUCTION(LoadString);
3018
3019 private:
3020 const uint32_t string_index_;
3021 const uint32_t dex_pc_;
3022
3023 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3024};
3025
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003026// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003027/**
3028 * Performs an initialization check on its Class object input.
3029 */
3030class HClinitCheck : public HExpression<1> {
3031 public:
3032 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
3033 : HExpression(Primitive::kPrimNot, SideEffects::All()),
3034 dex_pc_(dex_pc) {
3035 SetRawInputAt(0, constant);
3036 }
3037
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003038 bool CanBeMoved() const OVERRIDE { return true; }
3039 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3040 UNUSED(other);
3041 return true;
3042 }
3043
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003044 bool NeedsEnvironment() const OVERRIDE {
3045 // May call runtime to initialize the class.
3046 return true;
3047 }
3048
3049 uint32_t GetDexPc() const { return dex_pc_; }
3050
3051 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3052
3053 DECLARE_INSTRUCTION(ClinitCheck);
3054
3055 private:
3056 const uint32_t dex_pc_;
3057
3058 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3059};
3060
3061class HStaticFieldGet : public HExpression<1> {
3062 public:
3063 HStaticFieldGet(HInstruction* cls,
3064 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003065 MemberOffset field_offset,
3066 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003067 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003068 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003069 SetRawInputAt(0, cls);
3070 }
3071
Calin Juravle52c48962014-12-16 17:02:57 +00003072
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003073 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003074
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003075 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003076 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3077 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003078 }
3079
3080 size_t ComputeHashCode() const OVERRIDE {
3081 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3082 }
3083
Calin Juravle52c48962014-12-16 17:02:57 +00003084 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003085 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3086 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003087 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003088
3089 DECLARE_INSTRUCTION(StaticFieldGet);
3090
3091 private:
3092 const FieldInfo field_info_;
3093
3094 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3095};
3096
3097class HStaticFieldSet : public HTemplateInstruction<2> {
3098 public:
3099 HStaticFieldSet(HInstruction* cls,
3100 HInstruction* value,
3101 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003102 MemberOffset field_offset,
3103 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003104 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003105 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003106 SetRawInputAt(0, cls);
3107 SetRawInputAt(1, value);
3108 }
3109
Calin Juravle52c48962014-12-16 17:02:57 +00003110 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003111 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3112 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003113 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003114
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003115 HInstruction* GetValue() const { return InputAt(1); }
3116
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003117 DECLARE_INSTRUCTION(StaticFieldSet);
3118
3119 private:
3120 const FieldInfo field_info_;
3121
3122 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3123};
3124
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003125// Implement the move-exception DEX instruction.
3126class HLoadException : public HExpression<0> {
3127 public:
3128 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3129
3130 DECLARE_INSTRUCTION(LoadException);
3131
3132 private:
3133 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3134};
3135
3136class HThrow : public HTemplateInstruction<1> {
3137 public:
3138 HThrow(HInstruction* exception, uint32_t dex_pc)
3139 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3140 SetRawInputAt(0, exception);
3141 }
3142
3143 bool IsControlFlow() const OVERRIDE { return true; }
3144
3145 bool NeedsEnvironment() const OVERRIDE { return true; }
3146
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003147 bool CanThrow() const OVERRIDE { return true; }
3148
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003149 uint32_t GetDexPc() const { return dex_pc_; }
3150
3151 DECLARE_INSTRUCTION(Throw);
3152
3153 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003154 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003155
3156 DISALLOW_COPY_AND_ASSIGN(HThrow);
3157};
3158
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003159class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003160 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003161 HInstanceOf(HInstruction* object,
3162 HLoadClass* constant,
3163 bool class_is_final,
3164 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003165 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3166 class_is_final_(class_is_final),
3167 dex_pc_(dex_pc) {
3168 SetRawInputAt(0, object);
3169 SetRawInputAt(1, constant);
3170 }
3171
3172 bool CanBeMoved() const OVERRIDE { return true; }
3173
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003174 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003175 return true;
3176 }
3177
3178 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003179 return false;
3180 }
3181
3182 uint32_t GetDexPc() const { return dex_pc_; }
3183
3184 bool IsClassFinal() const { return class_is_final_; }
3185
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003186 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003187
3188 private:
3189 const bool class_is_final_;
3190 const uint32_t dex_pc_;
3191
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003192 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3193};
3194
Calin Juravleb1498f62015-02-16 13:13:29 +00003195class HBoundType : public HExpression<1> {
3196 public:
3197 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3198 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3199 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003200 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003201 SetRawInputAt(0, input);
3202 }
3203
3204 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3205
3206 bool CanBeNull() const OVERRIDE {
3207 // `null instanceof ClassX` always return false so we can't be null.
3208 return false;
3209 }
3210
3211 DECLARE_INSTRUCTION(BoundType);
3212
3213 private:
3214 // Encodes the most upper class that this instruction can have. In other words
3215 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3216 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3217 const ReferenceTypeInfo bound_type_;
3218
3219 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3220};
3221
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003222class HCheckCast : public HTemplateInstruction<2> {
3223 public:
3224 HCheckCast(HInstruction* object,
3225 HLoadClass* constant,
3226 bool class_is_final,
3227 uint32_t dex_pc)
3228 : HTemplateInstruction(SideEffects::None()),
3229 class_is_final_(class_is_final),
3230 dex_pc_(dex_pc) {
3231 SetRawInputAt(0, object);
3232 SetRawInputAt(1, constant);
3233 }
3234
3235 bool CanBeMoved() const OVERRIDE { return true; }
3236
3237 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3238 return true;
3239 }
3240
3241 bool NeedsEnvironment() const OVERRIDE {
3242 // Instruction may throw a CheckCastError.
3243 return true;
3244 }
3245
3246 bool CanThrow() const OVERRIDE { return true; }
3247
3248 uint32_t GetDexPc() const { return dex_pc_; }
3249
3250 bool IsClassFinal() const { return class_is_final_; }
3251
3252 DECLARE_INSTRUCTION(CheckCast);
3253
3254 private:
3255 const bool class_is_final_;
3256 const uint32_t dex_pc_;
3257
3258 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003259};
3260
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003261class HMonitorOperation : public HTemplateInstruction<1> {
3262 public:
3263 enum OperationKind {
3264 kEnter,
3265 kExit,
3266 };
3267
3268 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3269 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3270 SetRawInputAt(0, object);
3271 }
3272
3273 // Instruction may throw a Java exception, so we need an environment.
3274 bool NeedsEnvironment() const OVERRIDE { return true; }
3275 bool CanThrow() const OVERRIDE { return true; }
3276
3277 uint32_t GetDexPc() const { return dex_pc_; }
3278
3279 bool IsEnter() const { return kind_ == kEnter; }
3280
3281 DECLARE_INSTRUCTION(MonitorOperation);
3282
Calin Juravle52c48962014-12-16 17:02:57 +00003283 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003284 const OperationKind kind_;
3285 const uint32_t dex_pc_;
3286
3287 private:
3288 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3289};
3290
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003291class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003292 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003293 MoveOperands(Location source, Location destination, HInstruction* instruction)
3294 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003295
3296 Location GetSource() const { return source_; }
3297 Location GetDestination() const { return destination_; }
3298
3299 void SetSource(Location value) { source_ = value; }
3300 void SetDestination(Location value) { destination_ = value; }
3301
3302 // The parallel move resolver marks moves as "in-progress" by clearing the
3303 // destination (but not the source).
3304 Location MarkPending() {
3305 DCHECK(!IsPending());
3306 Location dest = destination_;
3307 destination_ = Location::NoLocation();
3308 return dest;
3309 }
3310
3311 void ClearPending(Location dest) {
3312 DCHECK(IsPending());
3313 destination_ = dest;
3314 }
3315
3316 bool IsPending() const {
3317 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3318 return destination_.IsInvalid() && !source_.IsInvalid();
3319 }
3320
3321 // True if this blocks a move from the given location.
3322 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003323 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003324 }
3325
3326 // A move is redundant if it's been eliminated, if its source and
3327 // destination are the same, or if its destination is unneeded.
3328 bool IsRedundant() const {
3329 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3330 }
3331
3332 // We clear both operands to indicate move that's been eliminated.
3333 void Eliminate() {
3334 source_ = destination_ = Location::NoLocation();
3335 }
3336
3337 bool IsEliminated() const {
3338 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3339 return source_.IsInvalid();
3340 }
3341
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003342 HInstruction* GetInstruction() const { return instruction_; }
3343
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003344 private:
3345 Location source_;
3346 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003347 // The instruction this move is assocatied with. Null when this move is
3348 // for moving an input in the expected locations of user (including a phi user).
3349 // This is only used in debug mode, to ensure we do not connect interval siblings
3350 // in the same parallel move.
3351 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003352};
3353
3354static constexpr size_t kDefaultNumberOfMoves = 4;
3355
3356class HParallelMove : public HTemplateInstruction<0> {
3357 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003358 explicit HParallelMove(ArenaAllocator* arena)
3359 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003360
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003361 void AddMove(Location source, Location destination, HInstruction* instruction) {
3362 DCHECK(source.IsValid());
3363 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003364 if (kIsDebugBuild) {
3365 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003366 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003367 if (moves_.Get(i).GetInstruction() == instruction) {
3368 // Special case the situation where the move is for the spill slot
3369 // of the instruction.
3370 if ((GetPrevious() == instruction)
3371 || ((GetPrevious() == nullptr)
3372 && instruction->IsPhi()
3373 && instruction->GetBlock() == GetBlock())) {
3374 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3375 << "Doing parallel moves for the same instruction.";
3376 } else {
3377 DCHECK(false) << "Doing parallel moves for the same instruction.";
3378 }
3379 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003380 }
3381 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003382 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3383 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3384 << "Same destination for two moves in a parallel move.";
3385 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003386 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003387 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003388 }
3389
3390 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003391 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003392 }
3393
3394 size_t NumMoves() const { return moves_.Size(); }
3395
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003396 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003397
3398 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003399 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003400
3401 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3402};
3403
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003404class HGraphVisitor : public ValueObject {
3405 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003406 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3407 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003408
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003409 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003410 virtual void VisitBasicBlock(HBasicBlock* block);
3411
Roland Levillain633021e2014-10-01 14:12:25 +01003412 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003413 void VisitInsertionOrder();
3414
Roland Levillain633021e2014-10-01 14:12:25 +01003415 // Visit the graph following dominator tree reverse post-order.
3416 void VisitReversePostOrder();
3417
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003418 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003419
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003420 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003421#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003422 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3423
3424 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3425
3426#undef DECLARE_VISIT_INSTRUCTION
3427
3428 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003429 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003430
3431 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3432};
3433
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003434class HGraphDelegateVisitor : public HGraphVisitor {
3435 public:
3436 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3437 virtual ~HGraphDelegateVisitor() {}
3438
3439 // Visit functions that delegate to to super class.
3440#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003441 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003442
3443 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3444
3445#undef DECLARE_VISIT_INSTRUCTION
3446
3447 private:
3448 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3449};
3450
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003451class HInsertionOrderIterator : public ValueObject {
3452 public:
3453 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3454
3455 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3456 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3457 void Advance() { ++index_; }
3458
3459 private:
3460 const HGraph& graph_;
3461 size_t index_;
3462
3463 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3464};
3465
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003466class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003467 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003468 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003469
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003470 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3471 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003472 void Advance() { ++index_; }
3473
3474 private:
3475 const HGraph& graph_;
3476 size_t index_;
3477
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003478 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003479};
3480
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003481class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003482 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003483 explicit HPostOrderIterator(const HGraph& graph)
3484 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003485
3486 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003487 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003488 void Advance() { --index_; }
3489
3490 private:
3491 const HGraph& graph_;
3492 size_t index_;
3493
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003494 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003495};
3496
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003497// Iterator over the blocks that art part of the loop. Includes blocks part
3498// of an inner loop. The order in which the blocks are iterated is on their
3499// block id.
3500class HBlocksInLoopIterator : public ValueObject {
3501 public:
3502 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3503 : blocks_in_loop_(info.GetBlocks()),
3504 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3505 index_(0) {
3506 if (!blocks_in_loop_.IsBitSet(index_)) {
3507 Advance();
3508 }
3509 }
3510
3511 bool Done() const { return index_ == blocks_.Size(); }
3512 HBasicBlock* Current() const { return blocks_.Get(index_); }
3513 void Advance() {
3514 ++index_;
3515 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3516 if (blocks_in_loop_.IsBitSet(index_)) {
3517 break;
3518 }
3519 }
3520 }
3521
3522 private:
3523 const BitVector& blocks_in_loop_;
3524 const GrowableArray<HBasicBlock*>& blocks_;
3525 size_t index_;
3526
3527 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3528};
3529
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003530inline int64_t Int64FromConstant(HConstant* constant) {
3531 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3532 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3533 : constant->AsLongConstant()->GetValue();
3534}
3535
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003536} // namespace art
3537
3538#endif // ART_COMPILER_OPTIMIZING_NODES_H_