blob: 664cf18ad7b304713a4bbee88402acf6366a6f5c [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);
David Brazdil46e2a392015-03-16 17:31:52 +0000131 void AddConstant(HConstant* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100132
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000133 // Try building the SSA form of this graph, with dominance computation and loop
134 // recognition. Returns whether it was successful in doing all these steps.
135 bool TryBuildingSsa() {
136 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000137 // The SSA builder requires loops to all be natural. Specifically, the dead phi
138 // elimination phase checks the consistency of the graph when doing a post-order
139 // visit for eliminating dead phis: a dead phi can only have loop header phi
140 // users remaining when being visited.
141 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000142 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000143 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000144 }
145
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000146 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000147 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100148 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000149
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000150 // Analyze all natural loops in this graph. Returns false if one
151 // loop is not natural, that is the header does not dominate the
152 // back edge.
153 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100154
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000155 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
156 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
157
David Brazdil46e2a392015-03-16 17:31:52 +0000158 void MergeEmptyBranches(HBasicBlock* start_block, HBasicBlock* end_block);
159
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100160 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
161 void SimplifyLoop(HBasicBlock* header);
162
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000163 int32_t GetNextInstructionId() {
164 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000165 return current_instruction_id_++;
166 }
167
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000168 int32_t GetCurrentInstructionId() const {
169 return current_instruction_id_;
170 }
171
172 void SetCurrentInstructionId(int32_t id) {
173 current_instruction_id_ = id;
174 }
175
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100176 uint16_t GetMaximumNumberOfOutVRegs() const {
177 return maximum_number_of_out_vregs_;
178 }
179
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000180 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
181 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100182 }
183
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000184 void UpdateTemporariesVRegSlots(size_t slots) {
185 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100186 }
187
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000188 size_t GetTemporariesVRegSlots() const {
189 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100190 }
191
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100192 void SetNumberOfVRegs(uint16_t number_of_vregs) {
193 number_of_vregs_ = number_of_vregs;
194 }
195
196 uint16_t GetNumberOfVRegs() const {
197 return number_of_vregs_;
198 }
199
200 void SetNumberOfInVRegs(uint16_t value) {
201 number_of_in_vregs_ = value;
202 }
203
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100204 uint16_t GetNumberOfLocalVRegs() const {
205 return number_of_vregs_ - number_of_in_vregs_;
206 }
207
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100208 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
209 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100210 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100211
Mingyao Yange4335eb2015-03-02 15:14:13 -0800212 bool HasArrayAccesses() const {
213 return has_array_accesses_;
214 }
215
216 void SetHasArrayAccesses(bool value) {
217 has_array_accesses_ = value;
218 }
219
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000220 bool IsDebuggable() const { return debuggable_; }
221
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000222 HNullConstant* GetNullConstant();
David Brazdil46e2a392015-03-16 17:31:52 +0000223 HIntConstant* GetIntConstant0();
224 HIntConstant* GetIntConstant1();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000225
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000226 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000227 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
228 void VisitBlockForDominatorTree(HBasicBlock* block,
229 HBasicBlock* predecessor,
230 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100231 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000232 void VisitBlockForBackEdges(HBasicBlock* block,
233 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100234 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000235 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000236 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
237
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000238 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000239
240 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000241 GrowableArray<HBasicBlock*> blocks_;
242
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100243 // List of blocks to perform a reverse post order tree traversal.
244 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000245
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000246 HBasicBlock* entry_block_;
247 HBasicBlock* exit_block_;
248
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100249 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100250 uint16_t maximum_number_of_out_vregs_;
251
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100252 // The number of virtual registers in this method. Contains the parameters.
253 uint16_t number_of_vregs_;
254
255 // The number of virtual registers used by parameters of this method.
256 uint16_t number_of_in_vregs_;
257
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000258 // Number of vreg size slots that the temporaries use (used in baseline compiler).
259 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100260
Mingyao Yange4335eb2015-03-02 15:14:13 -0800261 // Has array accesses. We can totally skip BCE if it's false.
262 bool has_array_accesses_;
263
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000264 // Indicates whether the graph should be compiled in a way that
265 // ensures full debuggability. If false, we can apply more
266 // aggressive optimizations that may limit the level of debugging.
267 const bool debuggable_;
268
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000269 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000270 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000271
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000272 // Cached null constant that might be created when building SSA form.
273 HNullConstant* cached_null_constant_;
274
David Brazdil46e2a392015-03-16 17:31:52 +0000275 // Cached common constants often needed by optimization passes.
276 HIntConstant* cached_int_constant0_;
277 HIntConstant* cached_int_constant1_;
278
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000279 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000280 DISALLOW_COPY_AND_ASSIGN(HGraph);
281};
282
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700283class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000284 public:
285 HLoopInformation(HBasicBlock* header, HGraph* graph)
286 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100287 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100288 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100289 // Make bit vector growable, as the number of blocks may change.
290 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100291
292 HBasicBlock* GetHeader() const {
293 return header_;
294 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000295
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000296 void SetHeader(HBasicBlock* block) {
297 header_ = block;
298 }
299
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100300 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
301 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
302 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
303
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000304 void AddBackEdge(HBasicBlock* back_edge) {
305 back_edges_.Add(back_edge);
306 }
307
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100308 void RemoveBackEdge(HBasicBlock* back_edge) {
309 back_edges_.Delete(back_edge);
310 }
311
David Brazdil46e2a392015-03-16 17:31:52 +0000312 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100313 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000314 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100315 }
316 return false;
317 }
318
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000319 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000320 return back_edges_.Size();
321 }
322
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100323 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100324
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100325 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
326 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100327 }
328
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100329 void ClearBackEdges() {
330 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100331 }
332
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100333 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
334 // that is the header dominates the back edge.
335 bool Populate();
336
337 // Returns whether this loop information contains `block`.
338 // Note that this loop information *must* be populated before entering this function.
339 bool Contains(const HBasicBlock& block) const;
340
341 // Returns whether this loop information is an inner loop of `other`.
342 // Note that `other` *must* be populated before entering this function.
343 bool IsIn(const HLoopInformation& other) const;
344
345 const ArenaBitVector& GetBlocks() const { return blocks_; }
346
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000347 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000348 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000349
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000350 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100351 // Internal recursive implementation of `Populate`.
352 void PopulateRecursive(HBasicBlock* block);
353
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000354 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100355 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000356 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100357 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000358
359 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
360};
361
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100362static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100363static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100364
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000365// A block in a method. Contains the list of instructions represented
366// as a double linked list. Each block knows its predecessors and
367// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100368
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700369class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000370 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100371 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000372 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000373 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
374 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000375 loop_information_(nullptr),
376 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100377 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100378 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100379 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100380 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000381 lifetime_end_(kNoLifetime),
382 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000383
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100384 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
385 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000386 }
387
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100388 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
389 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000390 }
391
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100392 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
393 return dominated_blocks_;
394 }
395
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100396 bool IsEntryBlock() const {
397 return graph_->GetEntryBlock() == this;
398 }
399
400 bool IsExitBlock() const {
401 return graph_->GetExitBlock() == this;
402 }
403
David Brazdil46e2a392015-03-16 17:31:52 +0000404 bool IsSingleGoto() const;
405
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000406 void AddBackEdge(HBasicBlock* back_edge) {
407 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000408 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000409 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100410 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000411 loop_information_->AddBackEdge(back_edge);
412 }
413
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000414 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000415 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000416
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000417 int GetBlockId() const { return block_id_; }
418 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000419
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000420 HBasicBlock* GetDominator() const { return dominator_; }
421 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100422 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000423 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
424 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
425 if (dominated_blocks_.Get(i) == existing) {
426 dominated_blocks_.Put(i, new_block);
427 return;
428 }
429 }
430 LOG(FATAL) << "Unreachable";
431 UNREACHABLE();
432 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000433
434 int NumberOfBackEdges() const {
435 return loop_information_ == nullptr
436 ? 0
437 : loop_information_->NumberOfBackEdges();
438 }
439
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100440 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
441 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100442 const HInstructionList& GetInstructions() const { return instructions_; }
443 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100444 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000445
446 void AddSuccessor(HBasicBlock* block) {
447 successors_.Add(block);
448 block->predecessors_.Add(this);
449 }
450
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100451 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
452 size_t successor_index = GetSuccessorIndexOf(existing);
453 DCHECK_NE(successor_index, static_cast<size_t>(-1));
454 existing->RemovePredecessor(this);
455 new_block->predecessors_.Add(this);
456 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000457 }
458
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000459 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
460 size_t predecessor_index = GetPredecessorIndexOf(existing);
461 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
462 existing->RemoveSuccessor(this);
463 new_block->successors_.Add(this);
464 predecessors_.Put(predecessor_index, new_block);
465 }
466
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100467 void RemovePredecessor(HBasicBlock* block) {
468 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100469 }
470
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000471 void RemoveSuccessor(HBasicBlock* block) {
472 successors_.Delete(block);
473 }
474
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100475 void ClearAllPredecessors() {
476 predecessors_.Reset();
477 }
478
479 void AddPredecessor(HBasicBlock* block) {
480 predecessors_.Add(block);
481 block->successors_.Add(this);
482 }
483
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100484 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100485 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100486 HBasicBlock* temp = predecessors_.Get(0);
487 predecessors_.Put(0, predecessors_.Get(1));
488 predecessors_.Put(1, temp);
489 }
490
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100491 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
492 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
493 if (predecessors_.Get(i) == predecessor) {
494 return i;
495 }
496 }
497 return -1;
498 }
499
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100500 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
501 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
502 if (successors_.Get(i) == successor) {
503 return i;
504 }
505 }
506 return -1;
507 }
508
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000509 // Split the block into two blocks just after `cursor`. Returns the newly
510 // created block. Note that this method just updates raw block information,
511 // like predecessors, successors, dominators, and instruction list. It does not
512 // update the graph, reverse post order, loop information, nor make sure the
513 // blocks are consistent (for example ending with a control flow instruction).
514 HBasicBlock* SplitAfter(HInstruction* cursor);
515
516 // Merge `other` at the end of `this`. Successors and dominated blocks of
517 // `other` are changed to be successors and dominated blocks of `this`. Note
518 // that this method does not update the graph, reverse post order, loop
519 // information, nor make sure the blocks are consistent (for example ending
520 // with a control flow instruction).
521 void MergeWith(HBasicBlock* other);
522
523 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
524 // of `this` are moved to `other`.
525 // Note that this method does not update the graph, reverse post order, loop
526 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000527 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000528 void ReplaceWith(HBasicBlock* other);
529
David Brazdil46e2a392015-03-16 17:31:52 +0000530 // Disconnects `this` from all its predecessors, successors and the dominator.
531 // It assumes that `this` does not dominate any blocks.
532 // Note that this method does not update the graph, reverse post order, loop
533 // information, nor make sure the blocks are consistent (for example ending
534 // with a control flow instruction).
535 void DisconnectFromAll();
536
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000537 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100538 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100539 // Replace instruction `initial` with `replacement` within this block.
540 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
541 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100542 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100543 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000544 // RemoveInstruction and RemovePhi delete a given instruction from the respective
545 // instruction list. With 'ensure_safety' set to true, it verifies that the
546 // instruction is not in use and removes it from the use lists of its inputs.
547 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
548 void RemovePhi(HPhi* phi, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100549
550 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100551 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100552 }
553
Roland Levillain6b879dd2014-09-22 17:13:44 +0100554 bool IsLoopPreHeaderFirstPredecessor() const {
555 DCHECK(IsLoopHeader());
556 DCHECK(!GetPredecessors().IsEmpty());
557 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
558 }
559
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100560 HLoopInformation* GetLoopInformation() const {
561 return loop_information_;
562 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000563
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000564 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100565 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000566 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100567 void SetInLoop(HLoopInformation* info) {
568 if (IsLoopHeader()) {
569 // Nothing to do. This just means `info` is an outer loop.
570 } else if (loop_information_ == nullptr) {
571 loop_information_ = info;
572 } else if (loop_information_->Contains(*info->GetHeader())) {
573 // Block is currently part of an outer loop. Make it part of this inner loop.
574 // Note that a non loop header having a loop information means this loop information
575 // has already been populated
576 loop_information_ = info;
577 } else {
578 // Block is part of an inner loop. Do not update the loop information.
579 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
580 // at this point, because this method is being called while populating `info`.
581 }
582 }
583
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000584 // Raw update of the loop information.
585 void SetLoopInformation(HLoopInformation* info) {
586 loop_information_ = info;
587 }
588
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100589 bool IsInLoop() const { return loop_information_ != nullptr; }
590
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100591 // Returns wheter this block dominates the blocked passed as parameter.
592 bool Dominates(HBasicBlock* block) const;
593
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100594 size_t GetLifetimeStart() const { return lifetime_start_; }
595 size_t GetLifetimeEnd() const { return lifetime_end_; }
596
597 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
598 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
599
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100600 uint32_t GetDexPc() const { return dex_pc_; }
601
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000602 bool IsCatchBlock() const { return is_catch_block_; }
603 void SetIsCatchBlock() { is_catch_block_ = true; }
604
David Brazdilb2bd1c52015-03-25 11:17:37 +0000605 bool EndsWithIf() const;
606 bool HasSinglePhi() const;
607
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000608 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000609 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000610 GrowableArray<HBasicBlock*> predecessors_;
611 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100612 HInstructionList instructions_;
613 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000614 HLoopInformation* loop_information_;
615 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100616 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000617 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100618 // The dex program counter of the first instruction of this block.
619 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100620 size_t lifetime_start_;
621 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000622 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000623
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000624 friend class HGraph;
625 friend class HInstruction;
626
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000627 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
628};
629
David Brazdilb2bd1c52015-03-25 11:17:37 +0000630// Iterates over the LoopInformation of all loops which contain 'block'
631// from the innermost to the outermost.
632class HLoopInformationOutwardIterator : public ValueObject {
633 public:
634 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
635 : current_(block.GetLoopInformation()) {}
636
637 bool Done() const { return current_ == nullptr; }
638
639 void Advance() {
640 DCHECK(!Done());
641 current_ = current_->GetHeader()->GetDominator()->GetLoopInformation();
642 }
643
644 HLoopInformation* Current() const {
645 DCHECK(!Done());
646 return current_;
647 }
648
649 private:
650 HLoopInformation* current_;
651
652 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
653};
654
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100655#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
656 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000657 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000658 M(ArrayGet, Instruction) \
659 M(ArrayLength, Instruction) \
660 M(ArraySet, Instruction) \
661 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000662 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000663 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100664 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000665 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100666 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000667 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000668 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000669 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100670 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000671 M(Exit, Instruction) \
672 M(FloatConstant, Constant) \
673 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100674 M(GreaterThan, Condition) \
675 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100676 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000677 M(InstanceFieldGet, Instruction) \
678 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000679 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100680 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000681 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000682 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100683 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000684 M(LessThan, Condition) \
685 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000686 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000687 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100688 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000689 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100690 M(Local, Instruction) \
691 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000692 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000693 M(Mul, BinaryOperation) \
694 M(Neg, UnaryOperation) \
695 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100696 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100697 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000698 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000699 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000700 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000701 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100702 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000703 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100704 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000705 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100706 M(Return, Instruction) \
707 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000708 M(Shl, BinaryOperation) \
709 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100710 M(StaticFieldGet, Instruction) \
711 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100712 M(StoreLocal, Instruction) \
713 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100714 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000715 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000716 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000717 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000718 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000719 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000720
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100721#define FOR_EACH_INSTRUCTION(M) \
722 FOR_EACH_CONCRETE_INSTRUCTION(M) \
723 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100724 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100725 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100726 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700727
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100728#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000729FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
730#undef FORWARD_DECLARATION
731
Roland Levillainccc07a92014-09-16 14:48:16 +0100732#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000733 InstructionKind GetKind() const OVERRIDE { return k##type; } \
734 const char* DebugName() const OVERRIDE { return #type; } \
735 const H##type* As##type() const OVERRIDE { return this; } \
736 H##type* As##type() OVERRIDE { return this; } \
737 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100738 return other->Is##type(); \
739 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000740 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000741
David Brazdiled596192015-01-23 10:39:45 +0000742template <typename T> class HUseList;
743
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100744template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700745class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000746 public:
David Brazdiled596192015-01-23 10:39:45 +0000747 HUseListNode* GetPrevious() const { return prev_; }
748 HUseListNode* GetNext() const { return next_; }
749 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100750 size_t GetIndex() const { return index_; }
751
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000752 private:
David Brazdiled596192015-01-23 10:39:45 +0000753 HUseListNode(T user, size_t index)
754 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
755
756 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100757 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000758 HUseListNode<T>* prev_;
759 HUseListNode<T>* next_;
760
761 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000762
763 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
764};
765
David Brazdiled596192015-01-23 10:39:45 +0000766template <typename T>
767class HUseList : public ValueObject {
768 public:
769 HUseList() : first_(nullptr) {}
770
771 void Clear() {
772 first_ = nullptr;
773 }
774
775 // Adds a new entry at the beginning of the use list and returns
776 // the newly created node.
777 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000778 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000779 if (IsEmpty()) {
780 first_ = new_node;
781 } else {
782 first_->prev_ = new_node;
783 new_node->next_ = first_;
784 first_ = new_node;
785 }
786 return new_node;
787 }
788
789 HUseListNode<T>* GetFirst() const {
790 return first_;
791 }
792
793 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000794 DCHECK(node != nullptr);
795 DCHECK(Contains(node));
796
David Brazdiled596192015-01-23 10:39:45 +0000797 if (node->prev_ != nullptr) {
798 node->prev_->next_ = node->next_;
799 }
800 if (node->next_ != nullptr) {
801 node->next_->prev_ = node->prev_;
802 }
803 if (node == first_) {
804 first_ = node->next_;
805 }
806 }
807
David Brazdil1abb4192015-02-17 18:33:36 +0000808 bool Contains(const HUseListNode<T>* node) const {
809 if (node == nullptr) {
810 return false;
811 }
812 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
813 if (current == node) {
814 return true;
815 }
816 }
817 return false;
818 }
819
David Brazdiled596192015-01-23 10:39:45 +0000820 bool IsEmpty() const {
821 return first_ == nullptr;
822 }
823
824 bool HasOnlyOneUse() const {
825 return first_ != nullptr && first_->next_ == nullptr;
826 }
827
828 private:
829 HUseListNode<T>* first_;
830};
831
832template<typename T>
833class HUseIterator : public ValueObject {
834 public:
835 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
836
837 bool Done() const { return current_ == nullptr; }
838
839 void Advance() {
840 DCHECK(!Done());
841 current_ = current_->GetNext();
842 }
843
844 HUseListNode<T>* Current() const {
845 DCHECK(!Done());
846 return current_;
847 }
848
849 private:
850 HUseListNode<T>* current_;
851
852 friend class HValue;
853};
854
David Brazdil1abb4192015-02-17 18:33:36 +0000855// This class is used by HEnvironment and HInstruction classes to record the
856// instructions they use and pointers to the corresponding HUseListNodes kept
857// by the used instructions.
858template <typename T>
859class HUserRecord : public ValueObject {
860 public:
861 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
862 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
863
864 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
865 : instruction_(old_record.instruction_), use_node_(use_node) {
866 DCHECK(instruction_ != nullptr);
867 DCHECK(use_node_ != nullptr);
868 DCHECK(old_record.use_node_ == nullptr);
869 }
870
871 HInstruction* GetInstruction() const { return instruction_; }
872 HUseListNode<T>* GetUseNode() const { return use_node_; }
873
874 private:
875 // Instruction used by the user.
876 HInstruction* instruction_;
877
878 // Corresponding entry in the use list kept by 'instruction_'.
879 HUseListNode<T>* use_node_;
880};
881
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100882// Represents the side effects an instruction may have.
883class SideEffects : public ValueObject {
884 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100885 SideEffects() : flags_(0) {}
886
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100887 static SideEffects None() {
888 return SideEffects(0);
889 }
890
891 static SideEffects All() {
892 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
893 }
894
895 static SideEffects ChangesSomething() {
896 return SideEffects((1 << kFlagChangesCount) - 1);
897 }
898
899 static SideEffects DependsOnSomething() {
900 int count = kFlagDependsOnCount - kFlagChangesCount;
901 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
902 }
903
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100904 SideEffects Union(SideEffects other) const {
905 return SideEffects(flags_ | other.flags_);
906 }
907
Roland Levillain72bceff2014-09-15 18:29:00 +0100908 bool HasSideEffects() const {
909 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
910 return (flags_ & all_bits_set) != 0;
911 }
912
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100913 bool HasAllSideEffects() const {
914 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
915 return all_bits_set == (flags_ & all_bits_set);
916 }
917
918 bool DependsOn(SideEffects other) const {
919 size_t depends_flags = other.ComputeDependsFlags();
920 return (flags_ & depends_flags) != 0;
921 }
922
923 bool HasDependencies() const {
924 int count = kFlagDependsOnCount - kFlagChangesCount;
925 size_t all_bits_set = (1 << count) - 1;
926 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
927 }
928
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100929 private:
930 static constexpr int kFlagChangesSomething = 0;
931 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
932
933 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
934 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
935
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100936 explicit SideEffects(size_t flags) : flags_(flags) {}
937
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100938 size_t ComputeDependsFlags() const {
939 return flags_ << kFlagChangesCount;
940 }
941
942 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100943};
944
David Brazdiled596192015-01-23 10:39:45 +0000945// A HEnvironment object contains the values of virtual registers at a given location.
946class HEnvironment : public ArenaObject<kArenaAllocMisc> {
947 public:
948 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
949 : vregs_(arena, number_of_vregs) {
950 vregs_.SetSize(number_of_vregs);
951 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000952 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000953 }
954 }
955
956 void CopyFrom(HEnvironment* env);
957
958 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +0000959 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +0000960 }
961
962 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +0000963 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +0000964 }
965
David Brazdil1abb4192015-02-17 18:33:36 +0000966 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +0000967
968 size_t Size() const { return vregs_.Size(); }
969
970 private:
David Brazdil1abb4192015-02-17 18:33:36 +0000971 // Record instructions' use entries of this environment for constant-time removal.
972 // It should only be called by HInstruction when a new environment use is added.
973 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
974 DCHECK(env_use->GetUser() == this);
975 size_t index = env_use->GetIndex();
976 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
977 }
David Brazdiled596192015-01-23 10:39:45 +0000978
David Brazdil1abb4192015-02-17 18:33:36 +0000979 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +0000980
David Brazdil1abb4192015-02-17 18:33:36 +0000981 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +0000982
983 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
984};
985
Calin Juravleacf735c2015-02-12 15:25:22 +0000986class ReferenceTypeInfo : ValueObject {
987 public:
Calin Juravleb1498f62015-02-16 13:13:29 +0000988 typedef Handle<mirror::Class> TypeHandle;
989
990 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
991 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
992 if (type_handle->IsObjectClass()) {
993 // Override the type handle to be consistent with the case when we get to
994 // Top but don't have the Object class available. It avoids having to guess
995 // what value the type_handle has when it's Top.
996 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
997 } else {
998 return ReferenceTypeInfo(type_handle, is_exact, false);
999 }
1000 }
1001
1002 static ReferenceTypeInfo CreateTop(bool is_exact) {
1003 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001004 }
1005
1006 bool IsExact() const { return is_exact_; }
1007 bool IsTop() const { return is_top_; }
1008
1009 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1010
Calin Juravleb1498f62015-02-16 13:13:29 +00001011 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001012 if (IsTop()) {
1013 // Top (equivalent for java.lang.Object) is supertype of anything.
1014 return true;
1015 }
1016 if (rti.IsTop()) {
1017 // If we get here `this` is not Top() so it can't be a supertype.
1018 return false;
1019 }
1020 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1021 }
1022
1023 // Returns true if the type information provide the same amount of details.
1024 // Note that it does not mean that the instructions have the same actual type
1025 // (e.g. tops are equal but they can be the result of a merge).
1026 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1027 if (IsExact() != rti.IsExact()) {
1028 return false;
1029 }
1030 if (IsTop() && rti.IsTop()) {
1031 // `Top` means java.lang.Object, so the types are equivalent.
1032 return true;
1033 }
1034 if (IsTop() || rti.IsTop()) {
1035 // If only one is top or object than they are not equivalent.
1036 // NB: We need this extra check because the type_handle of `Top` is invalid
1037 // and we cannot inspect its reference.
1038 return false;
1039 }
1040
1041 // Finally check the types.
1042 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1043 }
1044
1045 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001046 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1047 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1048 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1049
Calin Juravleacf735c2015-02-12 15:25:22 +00001050 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001051 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001052 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001053 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001054 bool is_exact_;
1055 // A true value here means that the object type should be java.lang.Object.
1056 // We don't have access to the corresponding mirror object every time so this
1057 // flag acts as a substitute. When true, the TypeHandle refers to a null
1058 // pointer and should not be used.
1059 bool is_top_;
1060};
1061
1062std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1063
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001064class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001065 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001066 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001067 : previous_(nullptr),
1068 next_(nullptr),
1069 block_(nullptr),
1070 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001071 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001072 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001073 locations_(nullptr),
1074 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001075 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001076 side_effects_(side_effects),
1077 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001078
Dave Allison20dfc792014-06-16 20:44:29 -07001079 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001080
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001081#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001082 enum InstructionKind {
1083 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1084 };
1085#undef DECLARE_KIND
1086
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001087 HInstruction* GetNext() const { return next_; }
1088 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001089
Calin Juravle77520bc2015-01-12 18:45:46 +00001090 HInstruction* GetNextDisregardingMoves() const;
1091 HInstruction* GetPreviousDisregardingMoves() const;
1092
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001093 HBasicBlock* GetBlock() const { return block_; }
1094 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001095 bool IsInBlock() const { return block_ != nullptr; }
1096 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001097 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001098
Roland Levillain6b879dd2014-09-22 17:13:44 +01001099 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001100 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001101
1102 virtual void Accept(HGraphVisitor* visitor) = 0;
1103 virtual const char* DebugName() const = 0;
1104
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001105 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001106 void SetRawInputAt(size_t index, HInstruction* input) {
1107 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1108 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001109
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001110 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001111 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001112 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001113 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001114
Calin Juravle61d544b2015-02-23 16:46:57 +00001115 virtual bool ActAsNullConstant() const { return false; }
1116
Calin Juravle10e244f2015-01-26 18:54:32 +00001117 // Does not apply for all instructions, but having this at top level greatly
1118 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001119 virtual bool CanBeNull() const {
1120 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1121 return true;
1122 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001123
Calin Juravle77520bc2015-01-12 18:45:46 +00001124 virtual bool CanDoImplicitNullCheck() const { return false; }
1125
Calin Juravleacf735c2015-02-12 15:25:22 +00001126 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001127 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001128 reference_type_info_ = reference_type_info;
1129 }
1130
Calin Juravle61d544b2015-02-23 16:46:57 +00001131 ReferenceTypeInfo GetReferenceTypeInfo() const {
1132 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1133 return reference_type_info_;
1134 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001135
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001136 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001137 DCHECK(user != nullptr);
1138 HUseListNode<HInstruction*>* use =
1139 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1140 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001141 }
1142
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001143 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001144 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001145 HUseListNode<HEnvironment*>* env_use =
1146 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1147 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001148 }
1149
David Brazdil1abb4192015-02-17 18:33:36 +00001150 void RemoveAsUserOfInput(size_t input) {
1151 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1152 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1153 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001154
David Brazdil1abb4192015-02-17 18:33:36 +00001155 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1156 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001157
David Brazdiled596192015-01-23 10:39:45 +00001158 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1159 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001160 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001161
Roland Levillain6c82d402014-10-13 16:10:27 +01001162 // Does this instruction strictly dominate `other_instruction`?
1163 // Returns false if this instruction and `other_instruction` are the same.
1164 // Aborts if this instruction and `other_instruction` are both phis.
1165 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001166
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001167 int GetId() const { return id_; }
1168 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001169
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001170 int GetSsaIndex() const { return ssa_index_; }
1171 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1172 bool HasSsaIndex() const { return ssa_index_ != -1; }
1173
1174 bool HasEnvironment() const { return environment_ != nullptr; }
1175 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001176 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
1177
Nicolas Geoffray39468442014-09-02 15:17:15 +01001178 // Returns the number of entries in the environment. Typically, that is the
1179 // number of dex registers in a method. It could be more in case of inlining.
1180 size_t EnvironmentSize() const;
1181
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001182 LocationSummary* GetLocations() const { return locations_; }
1183 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001184
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001185 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001186 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001187
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001188 // Move `this` instruction before `cursor`.
1189 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001190
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001191#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001192 bool Is##type() const { return (As##type() != nullptr); } \
1193 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001194 virtual H##type* As##type() { return nullptr; }
1195
1196 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1197#undef INSTRUCTION_TYPE_CHECK
1198
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001199 // Returns whether the instruction can be moved within the graph.
1200 virtual bool CanBeMoved() const { return false; }
1201
1202 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001203 virtual bool InstructionTypeEquals(HInstruction* other) const {
1204 UNUSED(other);
1205 return false;
1206 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001207
1208 // Returns whether any data encoded in the two instructions is equal.
1209 // This method does not look at the inputs. Both instructions must be
1210 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001211 virtual bool InstructionDataEquals(HInstruction* other) const {
1212 UNUSED(other);
1213 return false;
1214 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001215
1216 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001217 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001218 // 2) Their inputs are identical.
1219 bool Equals(HInstruction* other) const;
1220
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001221 virtual InstructionKind GetKind() const = 0;
1222
1223 virtual size_t ComputeHashCode() const {
1224 size_t result = GetKind();
1225 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1226 result = (result * 31) + InputAt(i)->GetId();
1227 }
1228 return result;
1229 }
1230
1231 SideEffects GetSideEffects() const { return side_effects_; }
1232
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001233 size_t GetLifetimePosition() const { return lifetime_position_; }
1234 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1235 LiveInterval* GetLiveInterval() const { return live_interval_; }
1236 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1237 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1238
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001239 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1240
1241 // Returns whether the code generation of the instruction will require to have access
1242 // to the current method. Such instructions are:
1243 // (1): Instructions that require an environment, as calling the runtime requires
1244 // to walk the stack and have the current method stored at a specific stack address.
1245 // (2): Object literals like classes and strings, that are loaded from the dex cache
1246 // fields of the current method.
1247 bool NeedsCurrentMethod() const {
1248 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1249 }
1250
David Brazdil1abb4192015-02-17 18:33:36 +00001251 protected:
1252 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1253 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1254
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001255 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001256 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1257
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001258 HInstruction* previous_;
1259 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001260 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001261
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001262 // An instruction gets an id when it is added to the graph.
1263 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001264 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001265 int id_;
1266
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001267 // When doing liveness analysis, instructions that have uses get an SSA index.
1268 int ssa_index_;
1269
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001270 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001271 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001272
1273 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001274 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001275
Nicolas Geoffray39468442014-09-02 15:17:15 +01001276 // The environment associated with this instruction. Not null if the instruction
1277 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001278 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001279
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001280 // Set by the code generator.
1281 LocationSummary* locations_;
1282
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001283 // Set by the liveness analysis.
1284 LiveInterval* live_interval_;
1285
1286 // Set by the liveness analysis, this is the position in a linear
1287 // order of blocks where this instruction's live interval start.
1288 size_t lifetime_position_;
1289
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001290 const SideEffects side_effects_;
1291
Calin Juravleacf735c2015-02-12 15:25:22 +00001292 // TODO: for primitive types this should be marked as invalid.
1293 ReferenceTypeInfo reference_type_info_;
1294
David Brazdil1abb4192015-02-17 18:33:36 +00001295 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001296 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001297 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001298 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001299 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001300
1301 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1302};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001303std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001304
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001305class HInputIterator : public ValueObject {
1306 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001307 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001308
1309 bool Done() const { return index_ == instruction_->InputCount(); }
1310 HInstruction* Current() const { return instruction_->InputAt(index_); }
1311 void Advance() { index_++; }
1312
1313 private:
1314 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001315 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001316
1317 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1318};
1319
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001320class HInstructionIterator : public ValueObject {
1321 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001322 explicit HInstructionIterator(const HInstructionList& instructions)
1323 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001324 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001325 }
1326
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001327 bool Done() const { return instruction_ == nullptr; }
1328 HInstruction* Current() const { return instruction_; }
1329 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001330 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001331 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001332 }
1333
1334 private:
1335 HInstruction* instruction_;
1336 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001337
1338 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001339};
1340
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001341class HBackwardInstructionIterator : public ValueObject {
1342 public:
1343 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1344 : instruction_(instructions.last_instruction_) {
1345 next_ = Done() ? nullptr : instruction_->GetPrevious();
1346 }
1347
1348 bool Done() const { return instruction_ == nullptr; }
1349 HInstruction* Current() const { return instruction_; }
1350 void Advance() {
1351 instruction_ = next_;
1352 next_ = Done() ? nullptr : instruction_->GetPrevious();
1353 }
1354
1355 private:
1356 HInstruction* instruction_;
1357 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001358
1359 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001360};
1361
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001362// An embedded container with N elements of type T. Used (with partial
1363// specialization for N=0) because embedded arrays cannot have size 0.
1364template<typename T, intptr_t N>
1365class EmbeddedArray {
1366 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001367 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001368
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001369 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001370
1371 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001372 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001373 return elements_[i];
1374 }
1375
1376 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001377 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001378 return elements_[i];
1379 }
1380
1381 const T& At(intptr_t i) const {
1382 return (*this)[i];
1383 }
1384
1385 void SetAt(intptr_t i, const T& val) {
1386 (*this)[i] = val;
1387 }
1388
1389 private:
1390 T elements_[N];
1391};
1392
1393template<typename T>
1394class EmbeddedArray<T, 0> {
1395 public:
1396 intptr_t length() const { return 0; }
1397 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001398 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001399 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001400 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001401 }
1402 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001403 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001404 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001405 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001406 }
1407};
1408
1409template<intptr_t N>
1410class HTemplateInstruction: public HInstruction {
1411 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001412 HTemplateInstruction<N>(SideEffects side_effects)
1413 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001414 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001415
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001416 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001417
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001418 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001419 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1420
1421 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1422 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001423 }
1424
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001425 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001426 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001427
1428 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001429};
1430
Dave Allison20dfc792014-06-16 20:44:29 -07001431template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001432class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001433 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001434 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1435 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001436 virtual ~HExpression() {}
1437
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001438 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001439
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001440 protected:
1441 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001442};
1443
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001444// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1445// instruction that branches to the exit block.
1446class HReturnVoid : public HTemplateInstruction<0> {
1447 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001448 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001449
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001450 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001451
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001452 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001453
1454 private:
1455 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1456};
1457
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001458// Represents dex's RETURN opcodes. A HReturn is a control flow
1459// instruction that branches to the exit block.
1460class HReturn : public HTemplateInstruction<1> {
1461 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001462 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001463 SetRawInputAt(0, value);
1464 }
1465
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001466 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001467
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001468 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001469
1470 private:
1471 DISALLOW_COPY_AND_ASSIGN(HReturn);
1472};
1473
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001474// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001475// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001476// exit block.
1477class HExit : public HTemplateInstruction<0> {
1478 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001479 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001480
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001481 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001482
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001483 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001484
1485 private:
1486 DISALLOW_COPY_AND_ASSIGN(HExit);
1487};
1488
1489// Jumps from one block to another.
1490class HGoto : public HTemplateInstruction<0> {
1491 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001492 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1493
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001494 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001495
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001496 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001497 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001498 }
1499
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001500 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001501
1502 private:
1503 DISALLOW_COPY_AND_ASSIGN(HGoto);
1504};
1505
Dave Allison20dfc792014-06-16 20:44:29 -07001506
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001507// Conditional branch. A block ending with an HIf instruction must have
1508// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001509class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001510 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001511 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001512 SetRawInputAt(0, input);
1513 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001514
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001515 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001516
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001517 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001518 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001519 }
1520
1521 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001522 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001523 }
1524
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001525 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001526
Dave Allison20dfc792014-06-16 20:44:29 -07001527 virtual bool IsIfInstruction() const { return true; }
1528
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001529 private:
1530 DISALLOW_COPY_AND_ASSIGN(HIf);
1531};
1532
Roland Levillain88cb1752014-10-20 16:36:47 +01001533class HUnaryOperation : public HExpression<1> {
1534 public:
1535 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1536 : HExpression(result_type, SideEffects::None()) {
1537 SetRawInputAt(0, input);
1538 }
1539
1540 HInstruction* GetInput() const { return InputAt(0); }
1541 Primitive::Type GetResultType() const { return GetType(); }
1542
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001543 bool CanBeMoved() const OVERRIDE { return true; }
1544 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001545 UNUSED(other);
1546 return true;
1547 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001548
Roland Levillain9240d6a2014-10-20 16:47:04 +01001549 // Try to statically evaluate `operation` and return a HConstant
1550 // containing the result of this evaluation. If `operation` cannot
1551 // be evaluated as a constant, return nullptr.
1552 HConstant* TryStaticEvaluation() const;
1553
1554 // Apply this operation to `x`.
1555 virtual int32_t Evaluate(int32_t x) const = 0;
1556 virtual int64_t Evaluate(int64_t x) const = 0;
1557
Roland Levillain88cb1752014-10-20 16:36:47 +01001558 DECLARE_INSTRUCTION(UnaryOperation);
1559
1560 private:
1561 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1562};
1563
Dave Allison20dfc792014-06-16 20:44:29 -07001564class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001565 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001566 HBinaryOperation(Primitive::Type result_type,
1567 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001568 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001569 SetRawInputAt(0, left);
1570 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001571 }
1572
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001573 HInstruction* GetLeft() const { return InputAt(0); }
1574 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001575 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001576
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001577 virtual bool IsCommutative() const { return false; }
1578
1579 // Put constant on the right.
1580 // Returns whether order is changed.
1581 bool OrderInputsWithConstantOnTheRight() {
1582 HInstruction* left = InputAt(0);
1583 HInstruction* right = InputAt(1);
1584 if (left->IsConstant() && !right->IsConstant()) {
1585 ReplaceInput(right, 0);
1586 ReplaceInput(left, 1);
1587 return true;
1588 }
1589 return false;
1590 }
1591
1592 // Order inputs by instruction id, but favor constant on the right side.
1593 // This helps GVN for commutative ops.
1594 void OrderInputs() {
1595 DCHECK(IsCommutative());
1596 HInstruction* left = InputAt(0);
1597 HInstruction* right = InputAt(1);
1598 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1599 return;
1600 }
1601 if (OrderInputsWithConstantOnTheRight()) {
1602 return;
1603 }
1604 // Order according to instruction id.
1605 if (left->GetId() > right->GetId()) {
1606 ReplaceInput(right, 0);
1607 ReplaceInput(left, 1);
1608 }
1609 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001610
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001611 bool CanBeMoved() const OVERRIDE { return true; }
1612 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001613 UNUSED(other);
1614 return true;
1615 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001616
Roland Levillain9240d6a2014-10-20 16:47:04 +01001617 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001618 // containing the result of this evaluation. If `operation` cannot
1619 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001620 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001621
1622 // Apply this operation to `x` and `y`.
1623 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1624 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1625
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001626 // Returns an input that can legally be used as the right input and is
1627 // constant, or nullptr.
1628 HConstant* GetConstantRight() const;
1629
1630 // If `GetConstantRight()` returns one of the input, this returns the other
1631 // one. Otherwise it returns nullptr.
1632 HInstruction* GetLeastConstantLeft() const;
1633
Roland Levillainccc07a92014-09-16 14:48:16 +01001634 DECLARE_INSTRUCTION(BinaryOperation);
1635
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001636 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001637 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1638};
1639
Dave Allison20dfc792014-06-16 20:44:29 -07001640class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001641 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001642 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001643 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1644 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001645
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001646 bool NeedsMaterialization() const { return needs_materialization_; }
1647 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001648
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001649 // For code generation purposes, returns whether this instruction is just before
1650 // `if_`, and disregard moves in between.
1651 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1652
Dave Allison20dfc792014-06-16 20:44:29 -07001653 DECLARE_INSTRUCTION(Condition);
1654
1655 virtual IfCondition GetCondition() const = 0;
1656
1657 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001658 // For register allocation purposes, returns whether this instruction needs to be
1659 // materialized (that is, not just be in the processor flags).
1660 bool needs_materialization_;
1661
Dave Allison20dfc792014-06-16 20:44:29 -07001662 DISALLOW_COPY_AND_ASSIGN(HCondition);
1663};
1664
1665// Instruction to check if two inputs are equal to each other.
1666class HEqual : public HCondition {
1667 public:
1668 HEqual(HInstruction* first, HInstruction* second)
1669 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001670
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001671 bool IsCommutative() const OVERRIDE { return true; }
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
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001680 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001681
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001682 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001683 return kCondEQ;
1684 }
1685
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001686 private:
1687 DISALLOW_COPY_AND_ASSIGN(HEqual);
1688};
1689
Dave Allison20dfc792014-06-16 20:44:29 -07001690class HNotEqual : public HCondition {
1691 public:
1692 HNotEqual(HInstruction* first, HInstruction* second)
1693 : HCondition(first, second) {}
1694
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001695 bool IsCommutative() const OVERRIDE { return true; }
1696
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001697 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001698 return x != y ? 1 : 0;
1699 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001700 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001701 return x != y ? 1 : 0;
1702 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001703
Dave Allison20dfc792014-06-16 20:44:29 -07001704 DECLARE_INSTRUCTION(NotEqual);
1705
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001706 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001707 return kCondNE;
1708 }
1709
1710 private:
1711 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1712};
1713
1714class HLessThan : public HCondition {
1715 public:
1716 HLessThan(HInstruction* first, HInstruction* second)
1717 : HCondition(first, second) {}
1718
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001719 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001720 return x < y ? 1 : 0;
1721 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001722 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001723 return x < y ? 1 : 0;
1724 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001725
Dave Allison20dfc792014-06-16 20:44:29 -07001726 DECLARE_INSTRUCTION(LessThan);
1727
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001728 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001729 return kCondLT;
1730 }
1731
1732 private:
1733 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1734};
1735
1736class HLessThanOrEqual : public HCondition {
1737 public:
1738 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1739 : HCondition(first, second) {}
1740
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001741 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001742 return x <= y ? 1 : 0;
1743 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001744 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001745 return x <= y ? 1 : 0;
1746 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001747
Dave Allison20dfc792014-06-16 20:44:29 -07001748 DECLARE_INSTRUCTION(LessThanOrEqual);
1749
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001750 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001751 return kCondLE;
1752 }
1753
1754 private:
1755 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1756};
1757
1758class HGreaterThan : public HCondition {
1759 public:
1760 HGreaterThan(HInstruction* first, HInstruction* second)
1761 : HCondition(first, second) {}
1762
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001763 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001764 return x > y ? 1 : 0;
1765 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001766 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001767 return x > y ? 1 : 0;
1768 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001769
Dave Allison20dfc792014-06-16 20:44:29 -07001770 DECLARE_INSTRUCTION(GreaterThan);
1771
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001772 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001773 return kCondGT;
1774 }
1775
1776 private:
1777 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1778};
1779
1780class HGreaterThanOrEqual : public HCondition {
1781 public:
1782 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1783 : HCondition(first, second) {}
1784
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001785 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001786 return x >= y ? 1 : 0;
1787 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001788 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001789 return x >= y ? 1 : 0;
1790 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001791
Dave Allison20dfc792014-06-16 20:44:29 -07001792 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1793
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001794 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001795 return kCondGE;
1796 }
1797
1798 private:
1799 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1800};
1801
1802
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001803// Instruction to check how two inputs compare to each other.
1804// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1805class HCompare : public HBinaryOperation {
1806 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001807 // The bias applies for floating point operations and indicates how NaN
1808 // comparisons are treated:
1809 enum Bias {
1810 kNoBias, // bias is not applicable (i.e. for long operation)
1811 kGtBias, // return 1 for NaN comparisons
1812 kLtBias, // return -1 for NaN comparisons
1813 };
1814
1815 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1816 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001817 DCHECK_EQ(type, first->GetType());
1818 DCHECK_EQ(type, second->GetType());
1819 }
1820
Calin Juravleddb7df22014-11-25 20:56:51 +00001821 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001822 return
1823 x == y ? 0 :
1824 x > y ? 1 :
1825 -1;
1826 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001827
1828 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001829 return
1830 x == y ? 0 :
1831 x > y ? 1 :
1832 -1;
1833 }
1834
Calin Juravleddb7df22014-11-25 20:56:51 +00001835 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1836 return bias_ == other->AsCompare()->bias_;
1837 }
1838
1839 bool IsGtBias() { return bias_ == kGtBias; }
1840
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001841 DECLARE_INSTRUCTION(Compare);
1842
1843 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001844 const Bias bias_;
1845
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001846 DISALLOW_COPY_AND_ASSIGN(HCompare);
1847};
1848
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001849// A local in the graph. Corresponds to a Dex register.
1850class HLocal : public HTemplateInstruction<0> {
1851 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001852 explicit HLocal(uint16_t reg_number)
1853 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001854
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001855 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001856
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001857 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001858
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001859 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001860 // The Dex register number.
1861 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001862
1863 DISALLOW_COPY_AND_ASSIGN(HLocal);
1864};
1865
1866// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001867class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001868 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001869 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001870 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001871 SetRawInputAt(0, local);
1872 }
1873
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001874 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1875
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001876 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001877
1878 private:
1879 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1880};
1881
1882// Store a value in a given local. This instruction has two inputs: the value
1883// and the local.
1884class HStoreLocal : public HTemplateInstruction<2> {
1885 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001886 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001887 SetRawInputAt(0, local);
1888 SetRawInputAt(1, value);
1889 }
1890
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001891 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1892
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001893 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001894
1895 private:
1896 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1897};
1898
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001899class HConstant : public HExpression<0> {
1900 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001901 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1902
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001903 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001904
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001905 virtual bool IsMinusOne() const { return false; }
1906 virtual bool IsZero() const { return false; }
1907 virtual bool IsOne() const { return false; }
1908
1909 static HConstant* NewConstant(ArenaAllocator* allocator, Primitive::Type type, int64_t val);
1910
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001911 DECLARE_INSTRUCTION(Constant);
1912
1913 private:
1914 DISALLOW_COPY_AND_ASSIGN(HConstant);
1915};
1916
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001917class HFloatConstant : public HConstant {
1918 public:
1919 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1920
1921 float GetValue() const { return value_; }
1922
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001923 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001924 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
1925 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001926 }
1927
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001928 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001929
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001930 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001931 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
1932 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001933 }
1934 bool IsZero() const OVERRIDE {
1935 return AsFloatConstant()->GetValue() == 0.0f;
1936 }
1937 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001938 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
1939 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001940 }
1941
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001942 DECLARE_INSTRUCTION(FloatConstant);
1943
1944 private:
1945 const float value_;
1946
1947 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1948};
1949
1950class HDoubleConstant : public HConstant {
1951 public:
1952 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1953
1954 double GetValue() const { return value_; }
1955
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001956 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001957 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
1958 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001959 }
1960
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001961 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001962
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001963 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001964 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
1965 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001966 }
1967 bool IsZero() const OVERRIDE {
1968 return AsDoubleConstant()->GetValue() == 0.0;
1969 }
1970 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001971 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
1972 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001973 }
1974
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001975 DECLARE_INSTRUCTION(DoubleConstant);
1976
1977 private:
1978 const double value_;
1979
1980 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1981};
1982
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001983class HNullConstant : public HConstant {
1984 public:
1985 HNullConstant() : HConstant(Primitive::kPrimNot) {}
1986
1987 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
1988 return true;
1989 }
1990
1991 size_t ComputeHashCode() const OVERRIDE { return 0; }
1992
Calin Juravle61d544b2015-02-23 16:46:57 +00001993 bool ActAsNullConstant() const OVERRIDE { return true; }
1994
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001995 DECLARE_INSTRUCTION(NullConstant);
1996
1997 private:
1998 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
1999};
2000
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002001// Constants of the type int. Those can be from Dex instructions, or
2002// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002003class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002004 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002005 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002006
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002007 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002008
Calin Juravle61d544b2015-02-23 16:46:57 +00002009 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002010 return other->AsIntConstant()->value_ == value_;
2011 }
2012
Calin Juravle61d544b2015-02-23 16:46:57 +00002013 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2014
2015 // TODO: Null is represented by the `0` constant. In most cases we replace it
2016 // with a HNullConstant but we don't do it when comparing (a != null). This
2017 // method is an workaround until we fix the above.
2018 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002019
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002020 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2021 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2022 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2023
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002024 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002025
2026 private:
2027 const int32_t value_;
2028
2029 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2030};
2031
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002032class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002033 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002034 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002035
2036 int64_t GetValue() const { return value_; }
2037
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002038 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002039 return other->AsLongConstant()->value_ == value_;
2040 }
2041
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002042 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002043
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002044 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2045 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2046 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2047
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002048 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002049
2050 private:
2051 const int64_t value_;
2052
2053 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2054};
2055
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002056enum class Intrinsics {
2057#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2058#include "intrinsics_list.h"
2059 kNone,
2060 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2061#undef INTRINSICS_LIST
2062#undef OPTIMIZING_INTRINSICS
2063};
2064std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2065
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002066class HInvoke : public HInstruction {
2067 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002068 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002069
2070 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2071 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002072 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002073
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002074 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002075 SetRawInputAt(index, argument);
2076 }
2077
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002078 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002079
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002080 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002081
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002082 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2083
2084 Intrinsics GetIntrinsic() {
2085 return intrinsic_;
2086 }
2087
2088 void SetIntrinsic(Intrinsics intrinsic) {
2089 intrinsic_ = intrinsic;
2090 }
2091
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002092 DECLARE_INSTRUCTION(Invoke);
2093
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002094 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002095 HInvoke(ArenaAllocator* arena,
2096 uint32_t number_of_arguments,
2097 Primitive::Type return_type,
2098 uint32_t dex_pc,
2099 uint32_t dex_method_index)
2100 : HInstruction(SideEffects::All()),
2101 inputs_(arena, number_of_arguments),
2102 return_type_(return_type),
2103 dex_pc_(dex_pc),
2104 dex_method_index_(dex_method_index),
2105 intrinsic_(Intrinsics::kNone) {
2106 inputs_.SetSize(number_of_arguments);
2107 }
2108
David Brazdil1abb4192015-02-17 18:33:36 +00002109 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2110 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2111 inputs_.Put(index, input);
2112 }
2113
2114 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002115 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002116 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002117 const uint32_t dex_method_index_;
2118 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002119
2120 private:
2121 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2122};
2123
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002124class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002125 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002126 HInvokeStaticOrDirect(ArenaAllocator* arena,
2127 uint32_t number_of_arguments,
2128 Primitive::Type return_type,
2129 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002130 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002131 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002132 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002133 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002134 invoke_type_(invoke_type),
2135 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002136
Calin Juravle77520bc2015-01-12 18:45:46 +00002137 bool CanDoImplicitNullCheck() const OVERRIDE {
2138 // We access the method via the dex cache so we can't do an implicit null check.
2139 // TODO: for intrinsics we can generate implicit null checks.
2140 return false;
2141 }
2142
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002143 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002144 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002145
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002146 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002147
2148 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002149 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002150 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002151
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002152 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002153};
2154
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002155class HInvokeVirtual : public HInvoke {
2156 public:
2157 HInvokeVirtual(ArenaAllocator* arena,
2158 uint32_t number_of_arguments,
2159 Primitive::Type return_type,
2160 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002161 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002162 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002163 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002164 vtable_index_(vtable_index) {}
2165
Calin Juravle77520bc2015-01-12 18:45:46 +00002166 bool CanDoImplicitNullCheck() const OVERRIDE {
2167 // TODO: Add implicit null checks in intrinsics.
2168 return !GetLocations()->Intrinsified();
2169 }
2170
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002171 uint32_t GetVTableIndex() const { return vtable_index_; }
2172
2173 DECLARE_INSTRUCTION(InvokeVirtual);
2174
2175 private:
2176 const uint32_t vtable_index_;
2177
2178 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2179};
2180
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002181class HInvokeInterface : public HInvoke {
2182 public:
2183 HInvokeInterface(ArenaAllocator* arena,
2184 uint32_t number_of_arguments,
2185 Primitive::Type return_type,
2186 uint32_t dex_pc,
2187 uint32_t dex_method_index,
2188 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002189 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002190 imt_index_(imt_index) {}
2191
Calin Juravle77520bc2015-01-12 18:45:46 +00002192 bool CanDoImplicitNullCheck() const OVERRIDE {
2193 // TODO: Add implicit null checks in intrinsics.
2194 return !GetLocations()->Intrinsified();
2195 }
2196
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002197 uint32_t GetImtIndex() const { return imt_index_; }
2198 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2199
2200 DECLARE_INSTRUCTION(InvokeInterface);
2201
2202 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002203 const uint32_t imt_index_;
2204
2205 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2206};
2207
Dave Allison20dfc792014-06-16 20:44:29 -07002208class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002209 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002210 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002211 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2212 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002213 type_index_(type_index),
2214 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002215
2216 uint32_t GetDexPc() const { return dex_pc_; }
2217 uint16_t GetTypeIndex() const { return type_index_; }
2218
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002219 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002220 bool NeedsEnvironment() const OVERRIDE { return true; }
2221 // It may throw when called on:
2222 // - interfaces
2223 // - abstract/innaccessible/unknown classes
2224 // TODO: optimize when possible.
2225 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002226
Calin Juravle10e244f2015-01-26 18:54:32 +00002227 bool CanBeNull() const OVERRIDE { return false; }
2228
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002229 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2230
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002231 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002232
2233 private:
2234 const uint32_t dex_pc_;
2235 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002236 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002237
2238 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2239};
2240
Roland Levillain88cb1752014-10-20 16:36:47 +01002241class HNeg : public HUnaryOperation {
2242 public:
2243 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2244 : HUnaryOperation(result_type, input) {}
2245
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002246 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2247 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002248
Roland Levillain88cb1752014-10-20 16:36:47 +01002249 DECLARE_INSTRUCTION(Neg);
2250
2251 private:
2252 DISALLOW_COPY_AND_ASSIGN(HNeg);
2253};
2254
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002255class HNewArray : public HExpression<1> {
2256 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002257 HNewArray(HInstruction* length,
2258 uint32_t dex_pc,
2259 uint16_t type_index,
2260 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002261 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2262 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002263 type_index_(type_index),
2264 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002265 SetRawInputAt(0, length);
2266 }
2267
2268 uint32_t GetDexPc() const { return dex_pc_; }
2269 uint16_t GetTypeIndex() const { return type_index_; }
2270
2271 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002272 bool NeedsEnvironment() const OVERRIDE { return true; }
2273
2274 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002275
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002276 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2277
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002278 DECLARE_INSTRUCTION(NewArray);
2279
2280 private:
2281 const uint32_t dex_pc_;
2282 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002283 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002284
2285 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2286};
2287
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002288class HAdd : public HBinaryOperation {
2289 public:
2290 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2291 : HBinaryOperation(result_type, left, right) {}
2292
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002293 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002294
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002295 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002296 return x + y;
2297 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002298 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002299 return x + y;
2300 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002301
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002302 DECLARE_INSTRUCTION(Add);
2303
2304 private:
2305 DISALLOW_COPY_AND_ASSIGN(HAdd);
2306};
2307
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002308class HSub : public HBinaryOperation {
2309 public:
2310 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2311 : HBinaryOperation(result_type, left, right) {}
2312
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002313 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002314 return x - y;
2315 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002316 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002317 return x - y;
2318 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002319
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002320 DECLARE_INSTRUCTION(Sub);
2321
2322 private:
2323 DISALLOW_COPY_AND_ASSIGN(HSub);
2324};
2325
Calin Juravle34bacdf2014-10-07 20:23:36 +01002326class HMul : public HBinaryOperation {
2327 public:
2328 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2329 : HBinaryOperation(result_type, left, right) {}
2330
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002331 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002332
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002333 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2334 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002335
2336 DECLARE_INSTRUCTION(Mul);
2337
2338 private:
2339 DISALLOW_COPY_AND_ASSIGN(HMul);
2340};
2341
Calin Juravle7c4954d2014-10-28 16:57:40 +00002342class HDiv : public HBinaryOperation {
2343 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002344 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2345 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002346
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002347 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002348 // Our graph structure ensures we never have 0 for `y` during constant folding.
2349 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002350 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002351 return (y == -1) ? -x : x / y;
2352 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002353
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002354 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002355 DCHECK_NE(y, 0);
2356 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2357 return (y == -1) ? -x : x / y;
2358 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002359
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002360 uint32_t GetDexPc() const { return dex_pc_; }
2361
Calin Juravle7c4954d2014-10-28 16:57:40 +00002362 DECLARE_INSTRUCTION(Div);
2363
2364 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002365 const uint32_t dex_pc_;
2366
Calin Juravle7c4954d2014-10-28 16:57:40 +00002367 DISALLOW_COPY_AND_ASSIGN(HDiv);
2368};
2369
Calin Juravlebacfec32014-11-14 15:54:36 +00002370class HRem : public HBinaryOperation {
2371 public:
2372 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2373 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2374
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002375 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002376 DCHECK_NE(y, 0);
2377 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2378 return (y == -1) ? 0 : x % y;
2379 }
2380
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002381 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002382 DCHECK_NE(y, 0);
2383 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2384 return (y == -1) ? 0 : x % y;
2385 }
2386
2387 uint32_t GetDexPc() const { return dex_pc_; }
2388
2389 DECLARE_INSTRUCTION(Rem);
2390
2391 private:
2392 const uint32_t dex_pc_;
2393
2394 DISALLOW_COPY_AND_ASSIGN(HRem);
2395};
2396
Calin Juravled0d48522014-11-04 16:40:20 +00002397class HDivZeroCheck : public HExpression<1> {
2398 public:
2399 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2400 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2401 SetRawInputAt(0, value);
2402 }
2403
2404 bool CanBeMoved() const OVERRIDE { return true; }
2405
2406 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2407 UNUSED(other);
2408 return true;
2409 }
2410
2411 bool NeedsEnvironment() const OVERRIDE { return true; }
2412 bool CanThrow() const OVERRIDE { return true; }
2413
2414 uint32_t GetDexPc() const { return dex_pc_; }
2415
2416 DECLARE_INSTRUCTION(DivZeroCheck);
2417
2418 private:
2419 const uint32_t dex_pc_;
2420
2421 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2422};
2423
Calin Juravle9aec02f2014-11-18 23:06:35 +00002424class HShl : public HBinaryOperation {
2425 public:
2426 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2427 : HBinaryOperation(result_type, left, right) {}
2428
2429 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2430 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2431
2432 DECLARE_INSTRUCTION(Shl);
2433
2434 private:
2435 DISALLOW_COPY_AND_ASSIGN(HShl);
2436};
2437
2438class HShr : public HBinaryOperation {
2439 public:
2440 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2441 : HBinaryOperation(result_type, left, right) {}
2442
2443 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2444 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2445
2446 DECLARE_INSTRUCTION(Shr);
2447
2448 private:
2449 DISALLOW_COPY_AND_ASSIGN(HShr);
2450};
2451
2452class HUShr : public HBinaryOperation {
2453 public:
2454 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2455 : HBinaryOperation(result_type, left, right) {}
2456
2457 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2458 uint32_t ux = static_cast<uint32_t>(x);
2459 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2460 return static_cast<int32_t>(ux >> uy);
2461 }
2462
2463 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2464 uint64_t ux = static_cast<uint64_t>(x);
2465 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2466 return static_cast<int64_t>(ux >> uy);
2467 }
2468
2469 DECLARE_INSTRUCTION(UShr);
2470
2471 private:
2472 DISALLOW_COPY_AND_ASSIGN(HUShr);
2473};
2474
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002475class HAnd : public HBinaryOperation {
2476 public:
2477 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2478 : HBinaryOperation(result_type, left, right) {}
2479
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002480 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002481
2482 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2483 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2484
2485 DECLARE_INSTRUCTION(And);
2486
2487 private:
2488 DISALLOW_COPY_AND_ASSIGN(HAnd);
2489};
2490
2491class HOr : public HBinaryOperation {
2492 public:
2493 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2494 : HBinaryOperation(result_type, left, right) {}
2495
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002496 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002497
2498 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2499 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2500
2501 DECLARE_INSTRUCTION(Or);
2502
2503 private:
2504 DISALLOW_COPY_AND_ASSIGN(HOr);
2505};
2506
2507class HXor : public HBinaryOperation {
2508 public:
2509 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2510 : HBinaryOperation(result_type, left, right) {}
2511
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002512 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002513
2514 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2515 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2516
2517 DECLARE_INSTRUCTION(Xor);
2518
2519 private:
2520 DISALLOW_COPY_AND_ASSIGN(HXor);
2521};
2522
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002523// The value of a parameter in this method. Its location depends on
2524// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002525class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002526 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002527 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2528 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002529
2530 uint8_t GetIndex() const { return index_; }
2531
Calin Juravle10e244f2015-01-26 18:54:32 +00002532 bool CanBeNull() const OVERRIDE { return !is_this_; }
2533
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002534 DECLARE_INSTRUCTION(ParameterValue);
2535
2536 private:
2537 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002538 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002539 const uint8_t index_;
2540
Calin Juravle10e244f2015-01-26 18:54:32 +00002541 // Whether or not the parameter value corresponds to 'this' argument.
2542 const bool is_this_;
2543
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002544 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2545};
2546
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002547class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002548 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002549 explicit HNot(Primitive::Type result_type, HInstruction* input)
2550 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002551
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002552 bool CanBeMoved() const OVERRIDE { return true; }
2553 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002554 UNUSED(other);
2555 return true;
2556 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002557
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002558 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2559 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002560
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002561 DECLARE_INSTRUCTION(Not);
2562
2563 private:
2564 DISALLOW_COPY_AND_ASSIGN(HNot);
2565};
2566
Roland Levillaindff1f282014-11-05 14:15:05 +00002567class HTypeConversion : public HExpression<1> {
2568 public:
2569 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002570 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2571 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002572 SetRawInputAt(0, input);
2573 DCHECK_NE(input->GetType(), result_type);
2574 }
2575
2576 HInstruction* GetInput() const { return InputAt(0); }
2577 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2578 Primitive::Type GetResultType() const { return GetType(); }
2579
Roland Levillain624279f2014-12-04 11:54:28 +00002580 // Required by the x86 and ARM code generators when producing calls
2581 // to the runtime.
2582 uint32_t GetDexPc() const { return dex_pc_; }
2583
Roland Levillaindff1f282014-11-05 14:15:05 +00002584 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002585 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002586
2587 DECLARE_INSTRUCTION(TypeConversion);
2588
2589 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002590 const uint32_t dex_pc_;
2591
Roland Levillaindff1f282014-11-05 14:15:05 +00002592 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2593};
2594
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002595static constexpr uint32_t kNoRegNumber = -1;
2596
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002597class HPhi : public HInstruction {
2598 public:
2599 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002600 : HInstruction(SideEffects::None()),
2601 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002602 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002603 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002604 is_live_(false),
2605 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002606 inputs_.SetSize(number_of_inputs);
2607 }
2608
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002609 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2610 static Primitive::Type ToPhiType(Primitive::Type type) {
2611 switch (type) {
2612 case Primitive::kPrimBoolean:
2613 case Primitive::kPrimByte:
2614 case Primitive::kPrimShort:
2615 case Primitive::kPrimChar:
2616 return Primitive::kPrimInt;
2617 default:
2618 return type;
2619 }
2620 }
2621
Calin Juravle10e244f2015-01-26 18:54:32 +00002622 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002623
2624 void AddInput(HInstruction* input);
2625
Calin Juravle10e244f2015-01-26 18:54:32 +00002626 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002627 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002628
Calin Juravle10e244f2015-01-26 18:54:32 +00002629 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2630 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2631
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002632 uint32_t GetRegNumber() const { return reg_number_; }
2633
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002634 void SetDead() { is_live_ = false; }
2635 void SetLive() { is_live_ = true; }
2636 bool IsDead() const { return !is_live_; }
2637 bool IsLive() const { return is_live_; }
2638
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002639 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002640
David Brazdil1abb4192015-02-17 18:33:36 +00002641 protected:
2642 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2643
2644 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2645 inputs_.Put(index, input);
2646 }
2647
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002648 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002649 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002650 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002651 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002652 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002653 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002654
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002655 DISALLOW_COPY_AND_ASSIGN(HPhi);
2656};
2657
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002658class HNullCheck : public HExpression<1> {
2659 public:
2660 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002661 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002662 SetRawInputAt(0, value);
2663 }
2664
Calin Juravle10e244f2015-01-26 18:54:32 +00002665 bool CanBeMoved() const OVERRIDE { return true; }
2666 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002667 UNUSED(other);
2668 return true;
2669 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002670
Calin Juravle10e244f2015-01-26 18:54:32 +00002671 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002672
Calin Juravle10e244f2015-01-26 18:54:32 +00002673 bool CanThrow() const OVERRIDE { return true; }
2674
2675 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002676
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002677 uint32_t GetDexPc() const { return dex_pc_; }
2678
2679 DECLARE_INSTRUCTION(NullCheck);
2680
2681 private:
2682 const uint32_t dex_pc_;
2683
2684 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2685};
2686
2687class FieldInfo : public ValueObject {
2688 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002689 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2690 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002691
2692 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002693 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002694 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002695
2696 private:
2697 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002698 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002699 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002700};
2701
2702class HInstanceFieldGet : public HExpression<1> {
2703 public:
2704 HInstanceFieldGet(HInstruction* value,
2705 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002706 MemberOffset field_offset,
2707 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002708 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002709 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002710 SetRawInputAt(0, value);
2711 }
2712
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002713 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002714
2715 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2716 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2717 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002718 }
2719
Calin Juravle77520bc2015-01-12 18:45:46 +00002720 bool CanDoImplicitNullCheck() const OVERRIDE {
2721 return GetFieldOffset().Uint32Value() < kPageSize;
2722 }
2723
2724 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002725 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2726 }
2727
Calin Juravle52c48962014-12-16 17:02:57 +00002728 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002729 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002730 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002731 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002732
2733 DECLARE_INSTRUCTION(InstanceFieldGet);
2734
2735 private:
2736 const FieldInfo field_info_;
2737
2738 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2739};
2740
2741class HInstanceFieldSet : public HTemplateInstruction<2> {
2742 public:
2743 HInstanceFieldSet(HInstruction* object,
2744 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002745 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002746 MemberOffset field_offset,
2747 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002748 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002749 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002750 SetRawInputAt(0, object);
2751 SetRawInputAt(1, value);
2752 }
2753
Calin Juravle77520bc2015-01-12 18:45:46 +00002754 bool CanDoImplicitNullCheck() const OVERRIDE {
2755 return GetFieldOffset().Uint32Value() < kPageSize;
2756 }
2757
Calin Juravle52c48962014-12-16 17:02:57 +00002758 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002759 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002760 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002761 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002762 HInstruction* GetValue() const { return InputAt(1); }
2763
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002764 DECLARE_INSTRUCTION(InstanceFieldSet);
2765
2766 private:
2767 const FieldInfo field_info_;
2768
2769 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2770};
2771
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002772class HArrayGet : public HExpression<2> {
2773 public:
2774 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002775 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002776 SetRawInputAt(0, array);
2777 SetRawInputAt(1, index);
2778 }
2779
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002780 bool CanBeMoved() const OVERRIDE { return true; }
2781 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002782 UNUSED(other);
2783 return true;
2784 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002785 bool CanDoImplicitNullCheck() const OVERRIDE {
2786 // TODO: We can be smarter here.
2787 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2788 // which generates the implicit null check. There are cases when these can be removed
2789 // to produce better code. If we ever add optimizations to do so we should allow an
2790 // implicit check here (as long as the address falls in the first page).
2791 return false;
2792 }
2793
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002794 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002795
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002796 HInstruction* GetArray() const { return InputAt(0); }
2797 HInstruction* GetIndex() const { return InputAt(1); }
2798
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002799 DECLARE_INSTRUCTION(ArrayGet);
2800
2801 private:
2802 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2803};
2804
2805class HArraySet : public HTemplateInstruction<3> {
2806 public:
2807 HArraySet(HInstruction* array,
2808 HInstruction* index,
2809 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002810 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002811 uint32_t dex_pc)
2812 : HTemplateInstruction(SideEffects::ChangesSomething()),
2813 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002814 expected_component_type_(expected_component_type),
2815 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002816 SetRawInputAt(0, array);
2817 SetRawInputAt(1, index);
2818 SetRawInputAt(2, value);
2819 }
2820
Calin Juravle77520bc2015-01-12 18:45:46 +00002821 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002822 // We currently always call a runtime method to catch array store
2823 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002824 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002825 }
2826
Calin Juravle77520bc2015-01-12 18:45:46 +00002827 bool CanDoImplicitNullCheck() const OVERRIDE {
2828 // TODO: Same as for ArrayGet.
2829 return false;
2830 }
2831
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002832 void ClearNeedsTypeCheck() {
2833 needs_type_check_ = false;
2834 }
2835
2836 bool NeedsTypeCheck() const { return needs_type_check_; }
2837
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002838 uint32_t GetDexPc() const { return dex_pc_; }
2839
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002840 HInstruction* GetArray() const { return InputAt(0); }
2841 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002842 HInstruction* GetValue() const { return InputAt(2); }
2843
2844 Primitive::Type GetComponentType() const {
2845 // The Dex format does not type floating point index operations. Since the
2846 // `expected_component_type_` is set during building and can therefore not
2847 // be correct, we also check what is the value type. If it is a floating
2848 // point type, we must use that type.
2849 Primitive::Type value_type = GetValue()->GetType();
2850 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2851 ? value_type
2852 : expected_component_type_;
2853 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002854
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002855 DECLARE_INSTRUCTION(ArraySet);
2856
2857 private:
2858 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002859 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002860 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002861
2862 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2863};
2864
2865class HArrayLength : public HExpression<1> {
2866 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002867 explicit HArrayLength(HInstruction* array)
2868 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2869 // Note that arrays do not change length, so the instruction does not
2870 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002871 SetRawInputAt(0, array);
2872 }
2873
Calin Juravle77520bc2015-01-12 18:45:46 +00002874 bool CanBeMoved() const OVERRIDE { return true; }
2875 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002876 UNUSED(other);
2877 return true;
2878 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002879 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002880
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002881 DECLARE_INSTRUCTION(ArrayLength);
2882
2883 private:
2884 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2885};
2886
2887class HBoundsCheck : public HExpression<2> {
2888 public:
2889 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002890 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002891 DCHECK(index->GetType() == Primitive::kPrimInt);
2892 SetRawInputAt(0, index);
2893 SetRawInputAt(1, length);
2894 }
2895
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002896 bool CanBeMoved() const OVERRIDE { return true; }
2897 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002898 UNUSED(other);
2899 return true;
2900 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002901
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002902 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002903
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002904 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002905
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002906 uint32_t GetDexPc() const { return dex_pc_; }
2907
2908 DECLARE_INSTRUCTION(BoundsCheck);
2909
2910 private:
2911 const uint32_t dex_pc_;
2912
2913 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2914};
2915
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002916/**
2917 * Some DEX instructions are folded into multiple HInstructions that need
2918 * to stay live until the last HInstruction. This class
2919 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002920 * HInstruction stays live. `index` represents the stack location index of the
2921 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002922 */
2923class HTemporary : public HTemplateInstruction<0> {
2924 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002925 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002926
2927 size_t GetIndex() const { return index_; }
2928
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002929 Primitive::Type GetType() const OVERRIDE {
2930 // The previous instruction is the one that will be stored in the temporary location.
2931 DCHECK(GetPrevious() != nullptr);
2932 return GetPrevious()->GetType();
2933 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002934
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002935 DECLARE_INSTRUCTION(Temporary);
2936
2937 private:
2938 const size_t index_;
2939
2940 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2941};
2942
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002943class HSuspendCheck : public HTemplateInstruction<0> {
2944 public:
2945 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002946 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002947
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002948 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002949 return true;
2950 }
2951
2952 uint32_t GetDexPc() const { return dex_pc_; }
2953
2954 DECLARE_INSTRUCTION(SuspendCheck);
2955
2956 private:
2957 const uint32_t dex_pc_;
2958
2959 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2960};
2961
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002962/**
2963 * Instruction to load a Class object.
2964 */
2965class HLoadClass : public HExpression<0> {
2966 public:
2967 HLoadClass(uint16_t type_index,
2968 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002969 uint32_t dex_pc)
2970 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2971 type_index_(type_index),
2972 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002973 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00002974 generate_clinit_check_(false),
2975 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002976
2977 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002978
2979 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2980 return other->AsLoadClass()->type_index_ == type_index_;
2981 }
2982
2983 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2984
2985 uint32_t GetDexPc() const { return dex_pc_; }
2986 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002987 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002988
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002989 bool NeedsEnvironment() const OVERRIDE {
2990 // Will call runtime and load the class if the class is not loaded yet.
2991 // TODO: finer grain decision.
2992 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002993 }
2994
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002995 bool MustGenerateClinitCheck() const {
2996 return generate_clinit_check_;
2997 }
2998
2999 void SetMustGenerateClinitCheck() {
3000 generate_clinit_check_ = true;
3001 }
3002
3003 bool CanCallRuntime() const {
3004 return MustGenerateClinitCheck() || !is_referrers_class_;
3005 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003006
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003007 bool CanThrow() const OVERRIDE {
3008 // May call runtime and and therefore can throw.
3009 // TODO: finer grain decision.
3010 return !is_referrers_class_;
3011 }
3012
Calin Juravleacf735c2015-02-12 15:25:22 +00003013 ReferenceTypeInfo GetLoadedClassRTI() {
3014 return loaded_class_rti_;
3015 }
3016
3017 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3018 // Make sure we only set exact types (the loaded class should never be merged).
3019 DCHECK(rti.IsExact());
3020 loaded_class_rti_ = rti;
3021 }
3022
3023 bool IsResolved() {
3024 return loaded_class_rti_.IsExact();
3025 }
3026
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003027 DECLARE_INSTRUCTION(LoadClass);
3028
3029 private:
3030 const uint16_t type_index_;
3031 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003032 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003033 // Whether this instruction must generate the initialization check.
3034 // Used for code generation.
3035 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003036
Calin Juravleacf735c2015-02-12 15:25:22 +00003037 ReferenceTypeInfo loaded_class_rti_;
3038
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003039 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3040};
3041
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003042class HLoadString : public HExpression<0> {
3043 public:
3044 HLoadString(uint32_t string_index, uint32_t dex_pc)
3045 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3046 string_index_(string_index),
3047 dex_pc_(dex_pc) {}
3048
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003049 bool CanBeMoved() const OVERRIDE { return true; }
3050
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003051 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3052 return other->AsLoadString()->string_index_ == string_index_;
3053 }
3054
3055 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3056
3057 uint32_t GetDexPc() const { return dex_pc_; }
3058 uint32_t GetStringIndex() const { return string_index_; }
3059
3060 // TODO: Can we deopt or debug when we resolve a string?
3061 bool NeedsEnvironment() const OVERRIDE { return false; }
3062
3063 DECLARE_INSTRUCTION(LoadString);
3064
3065 private:
3066 const uint32_t string_index_;
3067 const uint32_t dex_pc_;
3068
3069 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3070};
3071
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003072// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003073/**
3074 * Performs an initialization check on its Class object input.
3075 */
3076class HClinitCheck : public HExpression<1> {
3077 public:
3078 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
3079 : HExpression(Primitive::kPrimNot, SideEffects::All()),
3080 dex_pc_(dex_pc) {
3081 SetRawInputAt(0, constant);
3082 }
3083
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003084 bool CanBeMoved() const OVERRIDE { return true; }
3085 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3086 UNUSED(other);
3087 return true;
3088 }
3089
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003090 bool NeedsEnvironment() const OVERRIDE {
3091 // May call runtime to initialize the class.
3092 return true;
3093 }
3094
3095 uint32_t GetDexPc() const { return dex_pc_; }
3096
3097 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3098
3099 DECLARE_INSTRUCTION(ClinitCheck);
3100
3101 private:
3102 const uint32_t dex_pc_;
3103
3104 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3105};
3106
3107class HStaticFieldGet : public HExpression<1> {
3108 public:
3109 HStaticFieldGet(HInstruction* cls,
3110 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003111 MemberOffset field_offset,
3112 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003113 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003114 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003115 SetRawInputAt(0, cls);
3116 }
3117
Calin Juravle52c48962014-12-16 17:02:57 +00003118
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003119 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003120
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003121 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003122 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3123 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003124 }
3125
3126 size_t ComputeHashCode() const OVERRIDE {
3127 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3128 }
3129
Calin Juravle52c48962014-12-16 17:02:57 +00003130 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003131 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3132 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003133 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003134
3135 DECLARE_INSTRUCTION(StaticFieldGet);
3136
3137 private:
3138 const FieldInfo field_info_;
3139
3140 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3141};
3142
3143class HStaticFieldSet : public HTemplateInstruction<2> {
3144 public:
3145 HStaticFieldSet(HInstruction* cls,
3146 HInstruction* value,
3147 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003148 MemberOffset field_offset,
3149 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003150 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003151 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003152 SetRawInputAt(0, cls);
3153 SetRawInputAt(1, value);
3154 }
3155
Calin Juravle52c48962014-12-16 17:02:57 +00003156 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003157 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3158 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003159 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003160
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003161 HInstruction* GetValue() const { return InputAt(1); }
3162
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003163 DECLARE_INSTRUCTION(StaticFieldSet);
3164
3165 private:
3166 const FieldInfo field_info_;
3167
3168 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3169};
3170
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003171// Implement the move-exception DEX instruction.
3172class HLoadException : public HExpression<0> {
3173 public:
3174 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3175
3176 DECLARE_INSTRUCTION(LoadException);
3177
3178 private:
3179 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3180};
3181
3182class HThrow : public HTemplateInstruction<1> {
3183 public:
3184 HThrow(HInstruction* exception, uint32_t dex_pc)
3185 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3186 SetRawInputAt(0, exception);
3187 }
3188
3189 bool IsControlFlow() const OVERRIDE { return true; }
3190
3191 bool NeedsEnvironment() const OVERRIDE { return true; }
3192
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003193 bool CanThrow() const OVERRIDE { return true; }
3194
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003195 uint32_t GetDexPc() const { return dex_pc_; }
3196
3197 DECLARE_INSTRUCTION(Throw);
3198
3199 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003200 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003201
3202 DISALLOW_COPY_AND_ASSIGN(HThrow);
3203};
3204
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003205class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003206 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003207 HInstanceOf(HInstruction* object,
3208 HLoadClass* constant,
3209 bool class_is_final,
3210 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003211 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3212 class_is_final_(class_is_final),
3213 dex_pc_(dex_pc) {
3214 SetRawInputAt(0, object);
3215 SetRawInputAt(1, constant);
3216 }
3217
3218 bool CanBeMoved() const OVERRIDE { return true; }
3219
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003220 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003221 return true;
3222 }
3223
3224 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003225 return false;
3226 }
3227
3228 uint32_t GetDexPc() const { return dex_pc_; }
3229
3230 bool IsClassFinal() const { return class_is_final_; }
3231
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003232 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003233
3234 private:
3235 const bool class_is_final_;
3236 const uint32_t dex_pc_;
3237
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003238 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3239};
3240
Calin Juravleb1498f62015-02-16 13:13:29 +00003241class HBoundType : public HExpression<1> {
3242 public:
3243 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3244 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3245 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003246 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003247 SetRawInputAt(0, input);
3248 }
3249
3250 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3251
3252 bool CanBeNull() const OVERRIDE {
3253 // `null instanceof ClassX` always return false so we can't be null.
3254 return false;
3255 }
3256
3257 DECLARE_INSTRUCTION(BoundType);
3258
3259 private:
3260 // Encodes the most upper class that this instruction can have. In other words
3261 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3262 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3263 const ReferenceTypeInfo bound_type_;
3264
3265 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3266};
3267
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003268class HCheckCast : public HTemplateInstruction<2> {
3269 public:
3270 HCheckCast(HInstruction* object,
3271 HLoadClass* constant,
3272 bool class_is_final,
3273 uint32_t dex_pc)
3274 : HTemplateInstruction(SideEffects::None()),
3275 class_is_final_(class_is_final),
3276 dex_pc_(dex_pc) {
3277 SetRawInputAt(0, object);
3278 SetRawInputAt(1, constant);
3279 }
3280
3281 bool CanBeMoved() const OVERRIDE { return true; }
3282
3283 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3284 return true;
3285 }
3286
3287 bool NeedsEnvironment() const OVERRIDE {
3288 // Instruction may throw a CheckCastError.
3289 return true;
3290 }
3291
3292 bool CanThrow() const OVERRIDE { return true; }
3293
3294 uint32_t GetDexPc() const { return dex_pc_; }
3295
3296 bool IsClassFinal() const { return class_is_final_; }
3297
3298 DECLARE_INSTRUCTION(CheckCast);
3299
3300 private:
3301 const bool class_is_final_;
3302 const uint32_t dex_pc_;
3303
3304 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003305};
3306
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003307class HMonitorOperation : public HTemplateInstruction<1> {
3308 public:
3309 enum OperationKind {
3310 kEnter,
3311 kExit,
3312 };
3313
3314 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3315 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3316 SetRawInputAt(0, object);
3317 }
3318
3319 // Instruction may throw a Java exception, so we need an environment.
3320 bool NeedsEnvironment() const OVERRIDE { return true; }
3321 bool CanThrow() const OVERRIDE { return true; }
3322
3323 uint32_t GetDexPc() const { return dex_pc_; }
3324
3325 bool IsEnter() const { return kind_ == kEnter; }
3326
3327 DECLARE_INSTRUCTION(MonitorOperation);
3328
Calin Juravle52c48962014-12-16 17:02:57 +00003329 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003330 const OperationKind kind_;
3331 const uint32_t dex_pc_;
3332
3333 private:
3334 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3335};
3336
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003337class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003338 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003339 MoveOperands(Location source, Location destination, HInstruction* instruction)
3340 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003341
3342 Location GetSource() const { return source_; }
3343 Location GetDestination() const { return destination_; }
3344
3345 void SetSource(Location value) { source_ = value; }
3346 void SetDestination(Location value) { destination_ = value; }
3347
3348 // The parallel move resolver marks moves as "in-progress" by clearing the
3349 // destination (but not the source).
3350 Location MarkPending() {
3351 DCHECK(!IsPending());
3352 Location dest = destination_;
3353 destination_ = Location::NoLocation();
3354 return dest;
3355 }
3356
3357 void ClearPending(Location dest) {
3358 DCHECK(IsPending());
3359 destination_ = dest;
3360 }
3361
3362 bool IsPending() const {
3363 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3364 return destination_.IsInvalid() && !source_.IsInvalid();
3365 }
3366
3367 // True if this blocks a move from the given location.
3368 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003369 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003370 }
3371
3372 // A move is redundant if it's been eliminated, if its source and
3373 // destination are the same, or if its destination is unneeded.
3374 bool IsRedundant() const {
3375 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3376 }
3377
3378 // We clear both operands to indicate move that's been eliminated.
3379 void Eliminate() {
3380 source_ = destination_ = Location::NoLocation();
3381 }
3382
3383 bool IsEliminated() const {
3384 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3385 return source_.IsInvalid();
3386 }
3387
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003388 HInstruction* GetInstruction() const { return instruction_; }
3389
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003390 private:
3391 Location source_;
3392 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003393 // The instruction this move is assocatied with. Null when this move is
3394 // for moving an input in the expected locations of user (including a phi user).
3395 // This is only used in debug mode, to ensure we do not connect interval siblings
3396 // in the same parallel move.
3397 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003398};
3399
3400static constexpr size_t kDefaultNumberOfMoves = 4;
3401
3402class HParallelMove : public HTemplateInstruction<0> {
3403 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003404 explicit HParallelMove(ArenaAllocator* arena)
3405 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003406
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003407 void AddMove(Location source, Location destination, HInstruction* instruction) {
3408 DCHECK(source.IsValid());
3409 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003410 if (kIsDebugBuild) {
3411 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003412 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003413 if (moves_.Get(i).GetInstruction() == instruction) {
3414 // Special case the situation where the move is for the spill slot
3415 // of the instruction.
3416 if ((GetPrevious() == instruction)
3417 || ((GetPrevious() == nullptr)
3418 && instruction->IsPhi()
3419 && instruction->GetBlock() == GetBlock())) {
3420 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3421 << "Doing parallel moves for the same instruction.";
3422 } else {
3423 DCHECK(false) << "Doing parallel moves for the same instruction.";
3424 }
3425 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003426 }
3427 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003428 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3429 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3430 << "Same destination for two moves in a parallel move.";
3431 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003432 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003433 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003434 }
3435
3436 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003437 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003438 }
3439
3440 size_t NumMoves() const { return moves_.Size(); }
3441
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003442 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003443
3444 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003445 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003446
3447 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3448};
3449
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003450class HGraphVisitor : public ValueObject {
3451 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003452 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3453 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003454
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003455 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003456 virtual void VisitBasicBlock(HBasicBlock* block);
3457
Roland Levillain633021e2014-10-01 14:12:25 +01003458 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003459 void VisitInsertionOrder();
3460
Roland Levillain633021e2014-10-01 14:12:25 +01003461 // Visit the graph following dominator tree reverse post-order.
3462 void VisitReversePostOrder();
3463
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003464 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003465
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003466 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003467#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003468 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3469
3470 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3471
3472#undef DECLARE_VISIT_INSTRUCTION
3473
3474 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003475 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003476
3477 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3478};
3479
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003480class HGraphDelegateVisitor : public HGraphVisitor {
3481 public:
3482 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3483 virtual ~HGraphDelegateVisitor() {}
3484
3485 // Visit functions that delegate to to super class.
3486#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003487 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003488
3489 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3490
3491#undef DECLARE_VISIT_INSTRUCTION
3492
3493 private:
3494 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3495};
3496
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003497class HInsertionOrderIterator : public ValueObject {
3498 public:
3499 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3500
3501 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3502 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3503 void Advance() { ++index_; }
3504
3505 private:
3506 const HGraph& graph_;
3507 size_t index_;
3508
3509 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3510};
3511
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003512class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003513 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003514 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3515 // Check that reverse post order of the graph has been built.
3516 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3517 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003518
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003519 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3520 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003521 void Advance() { ++index_; }
3522
3523 private:
3524 const HGraph& graph_;
3525 size_t index_;
3526
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003527 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003528};
3529
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003530class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003531 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003532 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003533 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3534 // Check that reverse post order of the graph has been built.
3535 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3536 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003537
3538 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003539 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003540 void Advance() { --index_; }
3541
3542 private:
3543 const HGraph& graph_;
3544 size_t index_;
3545
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003546 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003547};
3548
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003549// Iterator over the blocks that art part of the loop. Includes blocks part
3550// of an inner loop. The order in which the blocks are iterated is on their
3551// block id.
3552class HBlocksInLoopIterator : public ValueObject {
3553 public:
3554 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3555 : blocks_in_loop_(info.GetBlocks()),
3556 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3557 index_(0) {
3558 if (!blocks_in_loop_.IsBitSet(index_)) {
3559 Advance();
3560 }
3561 }
3562
3563 bool Done() const { return index_ == blocks_.Size(); }
3564 HBasicBlock* Current() const { return blocks_.Get(index_); }
3565 void Advance() {
3566 ++index_;
3567 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3568 if (blocks_in_loop_.IsBitSet(index_)) {
3569 break;
3570 }
3571 }
3572 }
3573
3574 private:
3575 const BitVector& blocks_in_loop_;
3576 const GrowableArray<HBasicBlock*>& blocks_;
3577 size_t index_;
3578
3579 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3580};
3581
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003582inline int64_t Int64FromConstant(HConstant* constant) {
3583 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3584 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3585 : constant->AsLongConstant()->GetValue();
3586}
3587
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003588} // namespace art
3589
3590#endif // ART_COMPILER_OPTIMIZING_NODES_H_