blob: 9c751fb9c5bf0fc213be8d5ba327f2fa6ff3ff49 [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
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001251 virtual bool NeedsDexCache() const { return false; }
1252
David Brazdil1abb4192015-02-17 18:33:36 +00001253 protected:
1254 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1255 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1256
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001257 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001258 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1259
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001260 HInstruction* previous_;
1261 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001262 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001263
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001264 // An instruction gets an id when it is added to the graph.
1265 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001266 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001267 int id_;
1268
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001269 // When doing liveness analysis, instructions that have uses get an SSA index.
1270 int ssa_index_;
1271
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001272 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001273 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001274
1275 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001276 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001277
Nicolas Geoffray39468442014-09-02 15:17:15 +01001278 // The environment associated with this instruction. Not null if the instruction
1279 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001280 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001281
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001282 // Set by the code generator.
1283 LocationSummary* locations_;
1284
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001285 // Set by the liveness analysis.
1286 LiveInterval* live_interval_;
1287
1288 // Set by the liveness analysis, this is the position in a linear
1289 // order of blocks where this instruction's live interval start.
1290 size_t lifetime_position_;
1291
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001292 const SideEffects side_effects_;
1293
Calin Juravleacf735c2015-02-12 15:25:22 +00001294 // TODO: for primitive types this should be marked as invalid.
1295 ReferenceTypeInfo reference_type_info_;
1296
David Brazdil1abb4192015-02-17 18:33:36 +00001297 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001298 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001299 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001300 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001301 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001302
1303 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1304};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001305std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001306
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001307class HInputIterator : public ValueObject {
1308 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001309 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001310
1311 bool Done() const { return index_ == instruction_->InputCount(); }
1312 HInstruction* Current() const { return instruction_->InputAt(index_); }
1313 void Advance() { index_++; }
1314
1315 private:
1316 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001317 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001318
1319 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1320};
1321
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001322class HInstructionIterator : public ValueObject {
1323 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001324 explicit HInstructionIterator(const HInstructionList& instructions)
1325 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001326 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001327 }
1328
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001329 bool Done() const { return instruction_ == nullptr; }
1330 HInstruction* Current() const { return instruction_; }
1331 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001332 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001333 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001334 }
1335
1336 private:
1337 HInstruction* instruction_;
1338 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001339
1340 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001341};
1342
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001343class HBackwardInstructionIterator : public ValueObject {
1344 public:
1345 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1346 : instruction_(instructions.last_instruction_) {
1347 next_ = Done() ? nullptr : instruction_->GetPrevious();
1348 }
1349
1350 bool Done() const { return instruction_ == nullptr; }
1351 HInstruction* Current() const { return instruction_; }
1352 void Advance() {
1353 instruction_ = next_;
1354 next_ = Done() ? nullptr : instruction_->GetPrevious();
1355 }
1356
1357 private:
1358 HInstruction* instruction_;
1359 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001360
1361 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001362};
1363
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001364// An embedded container with N elements of type T. Used (with partial
1365// specialization for N=0) because embedded arrays cannot have size 0.
1366template<typename T, intptr_t N>
1367class EmbeddedArray {
1368 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001369 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001370
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001371 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001372
1373 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001374 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001375 return elements_[i];
1376 }
1377
1378 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001379 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001380 return elements_[i];
1381 }
1382
1383 const T& At(intptr_t i) const {
1384 return (*this)[i];
1385 }
1386
1387 void SetAt(intptr_t i, const T& val) {
1388 (*this)[i] = val;
1389 }
1390
1391 private:
1392 T elements_[N];
1393};
1394
1395template<typename T>
1396class EmbeddedArray<T, 0> {
1397 public:
1398 intptr_t length() const { return 0; }
1399 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001400 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001401 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001402 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001403 }
1404 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001405 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001406 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001407 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001408 }
1409};
1410
1411template<intptr_t N>
1412class HTemplateInstruction: public HInstruction {
1413 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001414 HTemplateInstruction<N>(SideEffects side_effects)
1415 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001416 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001417
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001418 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001419
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001420 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001421 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1422
1423 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1424 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001425 }
1426
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001427 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001428 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001429
1430 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001431};
1432
Dave Allison20dfc792014-06-16 20:44:29 -07001433template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001434class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001435 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001436 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1437 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001438 virtual ~HExpression() {}
1439
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001440 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001441
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001442 protected:
1443 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001444};
1445
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001446// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1447// instruction that branches to the exit block.
1448class HReturnVoid : public HTemplateInstruction<0> {
1449 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001450 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001451
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001452 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001453
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001454 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001455
1456 private:
1457 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1458};
1459
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001460// Represents dex's RETURN opcodes. A HReturn is a control flow
1461// instruction that branches to the exit block.
1462class HReturn : public HTemplateInstruction<1> {
1463 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001464 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001465 SetRawInputAt(0, value);
1466 }
1467
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001468 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001469
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001470 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001471
1472 private:
1473 DISALLOW_COPY_AND_ASSIGN(HReturn);
1474};
1475
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001476// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001477// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001478// exit block.
1479class HExit : public HTemplateInstruction<0> {
1480 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001481 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001482
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001483 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001484
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001485 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001486
1487 private:
1488 DISALLOW_COPY_AND_ASSIGN(HExit);
1489};
1490
1491// Jumps from one block to another.
1492class HGoto : public HTemplateInstruction<0> {
1493 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001494 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1495
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001496 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001497
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001498 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001499 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001500 }
1501
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001502 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001503
1504 private:
1505 DISALLOW_COPY_AND_ASSIGN(HGoto);
1506};
1507
Dave Allison20dfc792014-06-16 20:44:29 -07001508
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001509// Conditional branch. A block ending with an HIf instruction must have
1510// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001511class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001512 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001513 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001514 SetRawInputAt(0, input);
1515 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001516
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001517 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001518
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001519 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001520 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001521 }
1522
1523 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001524 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001525 }
1526
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001527 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001528
Dave Allison20dfc792014-06-16 20:44:29 -07001529 virtual bool IsIfInstruction() const { return true; }
1530
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001531 private:
1532 DISALLOW_COPY_AND_ASSIGN(HIf);
1533};
1534
Roland Levillain88cb1752014-10-20 16:36:47 +01001535class HUnaryOperation : public HExpression<1> {
1536 public:
1537 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1538 : HExpression(result_type, SideEffects::None()) {
1539 SetRawInputAt(0, input);
1540 }
1541
1542 HInstruction* GetInput() const { return InputAt(0); }
1543 Primitive::Type GetResultType() const { return GetType(); }
1544
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001545 bool CanBeMoved() const OVERRIDE { return true; }
1546 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001547 UNUSED(other);
1548 return true;
1549 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001550
Roland Levillain9240d6a2014-10-20 16:47:04 +01001551 // Try to statically evaluate `operation` and return a HConstant
1552 // containing the result of this evaluation. If `operation` cannot
1553 // be evaluated as a constant, return nullptr.
1554 HConstant* TryStaticEvaluation() const;
1555
1556 // Apply this operation to `x`.
1557 virtual int32_t Evaluate(int32_t x) const = 0;
1558 virtual int64_t Evaluate(int64_t x) const = 0;
1559
Roland Levillain88cb1752014-10-20 16:36:47 +01001560 DECLARE_INSTRUCTION(UnaryOperation);
1561
1562 private:
1563 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1564};
1565
Dave Allison20dfc792014-06-16 20:44:29 -07001566class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001567 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001568 HBinaryOperation(Primitive::Type result_type,
1569 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001570 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001571 SetRawInputAt(0, left);
1572 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001573 }
1574
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001575 HInstruction* GetLeft() const { return InputAt(0); }
1576 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001577 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001578
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001579 virtual bool IsCommutative() const { return false; }
1580
1581 // Put constant on the right.
1582 // Returns whether order is changed.
1583 bool OrderInputsWithConstantOnTheRight() {
1584 HInstruction* left = InputAt(0);
1585 HInstruction* right = InputAt(1);
1586 if (left->IsConstant() && !right->IsConstant()) {
1587 ReplaceInput(right, 0);
1588 ReplaceInput(left, 1);
1589 return true;
1590 }
1591 return false;
1592 }
1593
1594 // Order inputs by instruction id, but favor constant on the right side.
1595 // This helps GVN for commutative ops.
1596 void OrderInputs() {
1597 DCHECK(IsCommutative());
1598 HInstruction* left = InputAt(0);
1599 HInstruction* right = InputAt(1);
1600 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1601 return;
1602 }
1603 if (OrderInputsWithConstantOnTheRight()) {
1604 return;
1605 }
1606 // Order according to instruction id.
1607 if (left->GetId() > right->GetId()) {
1608 ReplaceInput(right, 0);
1609 ReplaceInput(left, 1);
1610 }
1611 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001612
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001613 bool CanBeMoved() const OVERRIDE { return true; }
1614 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001615 UNUSED(other);
1616 return true;
1617 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001618
Roland Levillain9240d6a2014-10-20 16:47:04 +01001619 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001620 // containing the result of this evaluation. If `operation` cannot
1621 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001622 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001623
1624 // Apply this operation to `x` and `y`.
1625 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1626 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1627
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001628 // Returns an input that can legally be used as the right input and is
1629 // constant, or nullptr.
1630 HConstant* GetConstantRight() const;
1631
1632 // If `GetConstantRight()` returns one of the input, this returns the other
1633 // one. Otherwise it returns nullptr.
1634 HInstruction* GetLeastConstantLeft() const;
1635
Roland Levillainccc07a92014-09-16 14:48:16 +01001636 DECLARE_INSTRUCTION(BinaryOperation);
1637
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001638 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001639 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1640};
1641
Dave Allison20dfc792014-06-16 20:44:29 -07001642class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001643 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001644 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001645 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1646 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001647
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001648 bool NeedsMaterialization() const { return needs_materialization_; }
1649 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001650
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001651 // For code generation purposes, returns whether this instruction is just before
1652 // `if_`, and disregard moves in between.
1653 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1654
Dave Allison20dfc792014-06-16 20:44:29 -07001655 DECLARE_INSTRUCTION(Condition);
1656
1657 virtual IfCondition GetCondition() const = 0;
1658
1659 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001660 // For register allocation purposes, returns whether this instruction needs to be
1661 // materialized (that is, not just be in the processor flags).
1662 bool needs_materialization_;
1663
Dave Allison20dfc792014-06-16 20:44:29 -07001664 DISALLOW_COPY_AND_ASSIGN(HCondition);
1665};
1666
1667// Instruction to check if two inputs are equal to each other.
1668class HEqual : public HCondition {
1669 public:
1670 HEqual(HInstruction* first, HInstruction* second)
1671 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001672
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001673 bool IsCommutative() const OVERRIDE { return true; }
1674
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001675 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001676 return x == y ? 1 : 0;
1677 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001678 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001679 return x == y ? 1 : 0;
1680 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001681
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001682 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001683
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001684 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001685 return kCondEQ;
1686 }
1687
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001688 private:
1689 DISALLOW_COPY_AND_ASSIGN(HEqual);
1690};
1691
Dave Allison20dfc792014-06-16 20:44:29 -07001692class HNotEqual : public HCondition {
1693 public:
1694 HNotEqual(HInstruction* first, HInstruction* second)
1695 : HCondition(first, second) {}
1696
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001697 bool IsCommutative() const OVERRIDE { return true; }
1698
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001699 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001700 return x != y ? 1 : 0;
1701 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001702 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001703 return x != y ? 1 : 0;
1704 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001705
Dave Allison20dfc792014-06-16 20:44:29 -07001706 DECLARE_INSTRUCTION(NotEqual);
1707
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001708 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001709 return kCondNE;
1710 }
1711
1712 private:
1713 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1714};
1715
1716class HLessThan : public HCondition {
1717 public:
1718 HLessThan(HInstruction* first, HInstruction* second)
1719 : HCondition(first, second) {}
1720
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001721 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001722 return x < y ? 1 : 0;
1723 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001724 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001725 return x < y ? 1 : 0;
1726 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001727
Dave Allison20dfc792014-06-16 20:44:29 -07001728 DECLARE_INSTRUCTION(LessThan);
1729
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001730 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001731 return kCondLT;
1732 }
1733
1734 private:
1735 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1736};
1737
1738class HLessThanOrEqual : public HCondition {
1739 public:
1740 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1741 : HCondition(first, second) {}
1742
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001743 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001744 return x <= y ? 1 : 0;
1745 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001746 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001747 return x <= y ? 1 : 0;
1748 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001749
Dave Allison20dfc792014-06-16 20:44:29 -07001750 DECLARE_INSTRUCTION(LessThanOrEqual);
1751
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001752 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001753 return kCondLE;
1754 }
1755
1756 private:
1757 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1758};
1759
1760class HGreaterThan : public HCondition {
1761 public:
1762 HGreaterThan(HInstruction* first, HInstruction* second)
1763 : HCondition(first, second) {}
1764
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001765 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001766 return x > y ? 1 : 0;
1767 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001768 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001769 return x > y ? 1 : 0;
1770 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001771
Dave Allison20dfc792014-06-16 20:44:29 -07001772 DECLARE_INSTRUCTION(GreaterThan);
1773
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001774 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001775 return kCondGT;
1776 }
1777
1778 private:
1779 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1780};
1781
1782class HGreaterThanOrEqual : public HCondition {
1783 public:
1784 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1785 : HCondition(first, second) {}
1786
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001787 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001788 return x >= y ? 1 : 0;
1789 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001790 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001791 return x >= y ? 1 : 0;
1792 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001793
Dave Allison20dfc792014-06-16 20:44:29 -07001794 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1795
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001796 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001797 return kCondGE;
1798 }
1799
1800 private:
1801 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1802};
1803
1804
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001805// Instruction to check how two inputs compare to each other.
1806// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1807class HCompare : public HBinaryOperation {
1808 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001809 // The bias applies for floating point operations and indicates how NaN
1810 // comparisons are treated:
1811 enum Bias {
1812 kNoBias, // bias is not applicable (i.e. for long operation)
1813 kGtBias, // return 1 for NaN comparisons
1814 kLtBias, // return -1 for NaN comparisons
1815 };
1816
1817 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1818 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001819 DCHECK_EQ(type, first->GetType());
1820 DCHECK_EQ(type, second->GetType());
1821 }
1822
Calin Juravleddb7df22014-11-25 20:56:51 +00001823 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001824 return
1825 x == y ? 0 :
1826 x > y ? 1 :
1827 -1;
1828 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001829
1830 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001831 return
1832 x == y ? 0 :
1833 x > y ? 1 :
1834 -1;
1835 }
1836
Calin Juravleddb7df22014-11-25 20:56:51 +00001837 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1838 return bias_ == other->AsCompare()->bias_;
1839 }
1840
1841 bool IsGtBias() { return bias_ == kGtBias; }
1842
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001843 DECLARE_INSTRUCTION(Compare);
1844
1845 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001846 const Bias bias_;
1847
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001848 DISALLOW_COPY_AND_ASSIGN(HCompare);
1849};
1850
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001851// A local in the graph. Corresponds to a Dex register.
1852class HLocal : public HTemplateInstruction<0> {
1853 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001854 explicit HLocal(uint16_t reg_number)
1855 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001856
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001857 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001858
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001859 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001860
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001861 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001862 // The Dex register number.
1863 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001864
1865 DISALLOW_COPY_AND_ASSIGN(HLocal);
1866};
1867
1868// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001869class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001870 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001871 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001872 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001873 SetRawInputAt(0, local);
1874 }
1875
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001876 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1877
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001878 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001879
1880 private:
1881 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1882};
1883
1884// Store a value in a given local. This instruction has two inputs: the value
1885// and the local.
1886class HStoreLocal : public HTemplateInstruction<2> {
1887 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001888 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001889 SetRawInputAt(0, local);
1890 SetRawInputAt(1, value);
1891 }
1892
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001893 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1894
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001895 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001896
1897 private:
1898 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1899};
1900
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001901class HConstant : public HExpression<0> {
1902 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001903 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1904
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001905 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001906
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001907 virtual bool IsMinusOne() const { return false; }
1908 virtual bool IsZero() const { return false; }
1909 virtual bool IsOne() const { return false; }
1910
1911 static HConstant* NewConstant(ArenaAllocator* allocator, Primitive::Type type, int64_t val);
1912
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001913 DECLARE_INSTRUCTION(Constant);
1914
1915 private:
1916 DISALLOW_COPY_AND_ASSIGN(HConstant);
1917};
1918
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001919class HFloatConstant : public HConstant {
1920 public:
1921 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1922
1923 float GetValue() const { return value_; }
1924
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001925 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001926 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
1927 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001928 }
1929
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001930 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001931
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001932 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001933 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
1934 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001935 }
1936 bool IsZero() const OVERRIDE {
1937 return AsFloatConstant()->GetValue() == 0.0f;
1938 }
1939 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001940 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
1941 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001942 }
1943
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001944 DECLARE_INSTRUCTION(FloatConstant);
1945
1946 private:
1947 const float value_;
1948
1949 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1950};
1951
1952class HDoubleConstant : public HConstant {
1953 public:
1954 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1955
1956 double GetValue() const { return value_; }
1957
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001958 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001959 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
1960 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001961 }
1962
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001963 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001964
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001965 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001966 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
1967 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001968 }
1969 bool IsZero() const OVERRIDE {
1970 return AsDoubleConstant()->GetValue() == 0.0;
1971 }
1972 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001973 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
1974 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001975 }
1976
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001977 DECLARE_INSTRUCTION(DoubleConstant);
1978
1979 private:
1980 const double value_;
1981
1982 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1983};
1984
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001985class HNullConstant : public HConstant {
1986 public:
1987 HNullConstant() : HConstant(Primitive::kPrimNot) {}
1988
1989 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
1990 return true;
1991 }
1992
1993 size_t ComputeHashCode() const OVERRIDE { return 0; }
1994
Calin Juravle61d544b2015-02-23 16:46:57 +00001995 bool ActAsNullConstant() const OVERRIDE { return true; }
1996
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001997 DECLARE_INSTRUCTION(NullConstant);
1998
1999 private:
2000 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2001};
2002
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002003// Constants of the type int. Those can be from Dex instructions, or
2004// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002005class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002006 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002007 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002008
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002009 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002010
Calin Juravle61d544b2015-02-23 16:46:57 +00002011 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002012 return other->AsIntConstant()->value_ == value_;
2013 }
2014
Calin Juravle61d544b2015-02-23 16:46:57 +00002015 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2016
2017 // TODO: Null is represented by the `0` constant. In most cases we replace it
2018 // with a HNullConstant but we don't do it when comparing (a != null). This
2019 // method is an workaround until we fix the above.
2020 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002021
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002022 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2023 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2024 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2025
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002026 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002027
2028 private:
2029 const int32_t value_;
2030
2031 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2032};
2033
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002034class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002035 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002036 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002037
2038 int64_t GetValue() const { return value_; }
2039
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002040 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002041 return other->AsLongConstant()->value_ == value_;
2042 }
2043
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002044 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002045
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002046 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2047 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2048 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2049
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002050 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002051
2052 private:
2053 const int64_t value_;
2054
2055 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2056};
2057
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002058enum class Intrinsics {
2059#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2060#include "intrinsics_list.h"
2061 kNone,
2062 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2063#undef INTRINSICS_LIST
2064#undef OPTIMIZING_INTRINSICS
2065};
2066std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2067
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002068class HInvoke : public HInstruction {
2069 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002070 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002071
2072 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2073 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002074 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002075
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002076 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002077 SetRawInputAt(index, argument);
2078 }
2079
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002080 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002081
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002082 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002083
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002084 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2085
2086 Intrinsics GetIntrinsic() {
2087 return intrinsic_;
2088 }
2089
2090 void SetIntrinsic(Intrinsics intrinsic) {
2091 intrinsic_ = intrinsic;
2092 }
2093
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002094 DECLARE_INSTRUCTION(Invoke);
2095
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002096 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002097 HInvoke(ArenaAllocator* arena,
2098 uint32_t number_of_arguments,
2099 Primitive::Type return_type,
2100 uint32_t dex_pc,
2101 uint32_t dex_method_index)
2102 : HInstruction(SideEffects::All()),
2103 inputs_(arena, number_of_arguments),
2104 return_type_(return_type),
2105 dex_pc_(dex_pc),
2106 dex_method_index_(dex_method_index),
2107 intrinsic_(Intrinsics::kNone) {
2108 inputs_.SetSize(number_of_arguments);
2109 }
2110
David Brazdil1abb4192015-02-17 18:33:36 +00002111 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2112 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2113 inputs_.Put(index, input);
2114 }
2115
2116 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002117 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002118 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002119 const uint32_t dex_method_index_;
2120 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002121
2122 private:
2123 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2124};
2125
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002126class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002127 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002128 HInvokeStaticOrDirect(ArenaAllocator* arena,
2129 uint32_t number_of_arguments,
2130 Primitive::Type return_type,
2131 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002132 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002133 bool is_recursive,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002134 InvokeType original_invoke_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002135 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002136 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002137 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002138 invoke_type_(invoke_type),
2139 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002140
Calin Juravle77520bc2015-01-12 18:45:46 +00002141 bool CanDoImplicitNullCheck() const OVERRIDE {
2142 // We access the method via the dex cache so we can't do an implicit null check.
2143 // TODO: for intrinsics we can generate implicit null checks.
2144 return false;
2145 }
2146
Nicolas Geoffray79041292015-03-26 10:05:54 +00002147 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002148 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002149 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002150 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002151
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002152 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002153
2154 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002155 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002156 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002157 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002158
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002159 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002160};
2161
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002162class HInvokeVirtual : public HInvoke {
2163 public:
2164 HInvokeVirtual(ArenaAllocator* arena,
2165 uint32_t number_of_arguments,
2166 Primitive::Type return_type,
2167 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002168 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002169 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002170 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002171 vtable_index_(vtable_index) {}
2172
Calin Juravle77520bc2015-01-12 18:45:46 +00002173 bool CanDoImplicitNullCheck() const OVERRIDE {
2174 // TODO: Add implicit null checks in intrinsics.
2175 return !GetLocations()->Intrinsified();
2176 }
2177
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002178 uint32_t GetVTableIndex() const { return vtable_index_; }
2179
2180 DECLARE_INSTRUCTION(InvokeVirtual);
2181
2182 private:
2183 const uint32_t vtable_index_;
2184
2185 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2186};
2187
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002188class HInvokeInterface : public HInvoke {
2189 public:
2190 HInvokeInterface(ArenaAllocator* arena,
2191 uint32_t number_of_arguments,
2192 Primitive::Type return_type,
2193 uint32_t dex_pc,
2194 uint32_t dex_method_index,
2195 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002196 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002197 imt_index_(imt_index) {}
2198
Calin Juravle77520bc2015-01-12 18:45:46 +00002199 bool CanDoImplicitNullCheck() const OVERRIDE {
2200 // TODO: Add implicit null checks in intrinsics.
2201 return !GetLocations()->Intrinsified();
2202 }
2203
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002204 uint32_t GetImtIndex() const { return imt_index_; }
2205 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2206
2207 DECLARE_INSTRUCTION(InvokeInterface);
2208
2209 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002210 const uint32_t imt_index_;
2211
2212 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2213};
2214
Dave Allison20dfc792014-06-16 20:44:29 -07002215class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002216 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002217 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002218 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2219 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002220 type_index_(type_index),
2221 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002222
2223 uint32_t GetDexPc() const { return dex_pc_; }
2224 uint16_t GetTypeIndex() const { return type_index_; }
2225
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002226 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002227 bool NeedsEnvironment() const OVERRIDE { return true; }
2228 // It may throw when called on:
2229 // - interfaces
2230 // - abstract/innaccessible/unknown classes
2231 // TODO: optimize when possible.
2232 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002233
Calin Juravle10e244f2015-01-26 18:54:32 +00002234 bool CanBeNull() const OVERRIDE { return false; }
2235
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002236 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2237
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002238 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002239
2240 private:
2241 const uint32_t dex_pc_;
2242 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002243 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002244
2245 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2246};
2247
Roland Levillain88cb1752014-10-20 16:36:47 +01002248class HNeg : public HUnaryOperation {
2249 public:
2250 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2251 : HUnaryOperation(result_type, input) {}
2252
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002253 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2254 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002255
Roland Levillain88cb1752014-10-20 16:36:47 +01002256 DECLARE_INSTRUCTION(Neg);
2257
2258 private:
2259 DISALLOW_COPY_AND_ASSIGN(HNeg);
2260};
2261
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002262class HNewArray : public HExpression<1> {
2263 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002264 HNewArray(HInstruction* length,
2265 uint32_t dex_pc,
2266 uint16_t type_index,
2267 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002268 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2269 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002270 type_index_(type_index),
2271 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002272 SetRawInputAt(0, length);
2273 }
2274
2275 uint32_t GetDexPc() const { return dex_pc_; }
2276 uint16_t GetTypeIndex() const { return type_index_; }
2277
2278 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002279 bool NeedsEnvironment() const OVERRIDE { return true; }
2280
2281 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002282
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002283 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2284
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002285 DECLARE_INSTRUCTION(NewArray);
2286
2287 private:
2288 const uint32_t dex_pc_;
2289 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002290 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002291
2292 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2293};
2294
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002295class HAdd : public HBinaryOperation {
2296 public:
2297 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2298 : HBinaryOperation(result_type, left, right) {}
2299
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002300 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002301
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002302 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002303 return x + y;
2304 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002305 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002306 return x + y;
2307 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002308
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002309 DECLARE_INSTRUCTION(Add);
2310
2311 private:
2312 DISALLOW_COPY_AND_ASSIGN(HAdd);
2313};
2314
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002315class HSub : public HBinaryOperation {
2316 public:
2317 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2318 : HBinaryOperation(result_type, left, right) {}
2319
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002320 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002321 return x - y;
2322 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002323 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002324 return x - y;
2325 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002326
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002327 DECLARE_INSTRUCTION(Sub);
2328
2329 private:
2330 DISALLOW_COPY_AND_ASSIGN(HSub);
2331};
2332
Calin Juravle34bacdf2014-10-07 20:23:36 +01002333class HMul : public HBinaryOperation {
2334 public:
2335 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2336 : HBinaryOperation(result_type, left, right) {}
2337
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002338 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002339
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002340 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2341 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002342
2343 DECLARE_INSTRUCTION(Mul);
2344
2345 private:
2346 DISALLOW_COPY_AND_ASSIGN(HMul);
2347};
2348
Calin Juravle7c4954d2014-10-28 16:57:40 +00002349class HDiv : public HBinaryOperation {
2350 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002351 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2352 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002353
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002354 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002355 // Our graph structure ensures we never have 0 for `y` during constant folding.
2356 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002357 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002358 return (y == -1) ? -x : x / y;
2359 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002360
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002361 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002362 DCHECK_NE(y, 0);
2363 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2364 return (y == -1) ? -x : x / y;
2365 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002366
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002367 uint32_t GetDexPc() const { return dex_pc_; }
2368
Calin Juravle7c4954d2014-10-28 16:57:40 +00002369 DECLARE_INSTRUCTION(Div);
2370
2371 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002372 const uint32_t dex_pc_;
2373
Calin Juravle7c4954d2014-10-28 16:57:40 +00002374 DISALLOW_COPY_AND_ASSIGN(HDiv);
2375};
2376
Calin Juravlebacfec32014-11-14 15:54:36 +00002377class HRem : public HBinaryOperation {
2378 public:
2379 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2380 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2381
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002382 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002383 DCHECK_NE(y, 0);
2384 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2385 return (y == -1) ? 0 : x % y;
2386 }
2387
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002388 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002389 DCHECK_NE(y, 0);
2390 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2391 return (y == -1) ? 0 : x % y;
2392 }
2393
2394 uint32_t GetDexPc() const { return dex_pc_; }
2395
2396 DECLARE_INSTRUCTION(Rem);
2397
2398 private:
2399 const uint32_t dex_pc_;
2400
2401 DISALLOW_COPY_AND_ASSIGN(HRem);
2402};
2403
Calin Juravled0d48522014-11-04 16:40:20 +00002404class HDivZeroCheck : public HExpression<1> {
2405 public:
2406 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2407 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2408 SetRawInputAt(0, value);
2409 }
2410
2411 bool CanBeMoved() const OVERRIDE { return true; }
2412
2413 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2414 UNUSED(other);
2415 return true;
2416 }
2417
2418 bool NeedsEnvironment() const OVERRIDE { return true; }
2419 bool CanThrow() const OVERRIDE { return true; }
2420
2421 uint32_t GetDexPc() const { return dex_pc_; }
2422
2423 DECLARE_INSTRUCTION(DivZeroCheck);
2424
2425 private:
2426 const uint32_t dex_pc_;
2427
2428 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2429};
2430
Calin Juravle9aec02f2014-11-18 23:06:35 +00002431class HShl : public HBinaryOperation {
2432 public:
2433 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2434 : HBinaryOperation(result_type, left, right) {}
2435
2436 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2437 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2438
2439 DECLARE_INSTRUCTION(Shl);
2440
2441 private:
2442 DISALLOW_COPY_AND_ASSIGN(HShl);
2443};
2444
2445class HShr : public HBinaryOperation {
2446 public:
2447 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2448 : HBinaryOperation(result_type, left, right) {}
2449
2450 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2451 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2452
2453 DECLARE_INSTRUCTION(Shr);
2454
2455 private:
2456 DISALLOW_COPY_AND_ASSIGN(HShr);
2457};
2458
2459class HUShr : public HBinaryOperation {
2460 public:
2461 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2462 : HBinaryOperation(result_type, left, right) {}
2463
2464 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2465 uint32_t ux = static_cast<uint32_t>(x);
2466 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2467 return static_cast<int32_t>(ux >> uy);
2468 }
2469
2470 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2471 uint64_t ux = static_cast<uint64_t>(x);
2472 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2473 return static_cast<int64_t>(ux >> uy);
2474 }
2475
2476 DECLARE_INSTRUCTION(UShr);
2477
2478 private:
2479 DISALLOW_COPY_AND_ASSIGN(HUShr);
2480};
2481
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002482class HAnd : public HBinaryOperation {
2483 public:
2484 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2485 : HBinaryOperation(result_type, left, right) {}
2486
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002487 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002488
2489 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2490 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2491
2492 DECLARE_INSTRUCTION(And);
2493
2494 private:
2495 DISALLOW_COPY_AND_ASSIGN(HAnd);
2496};
2497
2498class HOr : public HBinaryOperation {
2499 public:
2500 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2501 : HBinaryOperation(result_type, left, right) {}
2502
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002503 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002504
2505 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2506 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2507
2508 DECLARE_INSTRUCTION(Or);
2509
2510 private:
2511 DISALLOW_COPY_AND_ASSIGN(HOr);
2512};
2513
2514class HXor : public HBinaryOperation {
2515 public:
2516 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2517 : HBinaryOperation(result_type, left, right) {}
2518
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002519 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002520
2521 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2522 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2523
2524 DECLARE_INSTRUCTION(Xor);
2525
2526 private:
2527 DISALLOW_COPY_AND_ASSIGN(HXor);
2528};
2529
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002530// The value of a parameter in this method. Its location depends on
2531// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002532class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002533 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002534 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2535 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002536
2537 uint8_t GetIndex() const { return index_; }
2538
Calin Juravle10e244f2015-01-26 18:54:32 +00002539 bool CanBeNull() const OVERRIDE { return !is_this_; }
2540
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002541 DECLARE_INSTRUCTION(ParameterValue);
2542
2543 private:
2544 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002545 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002546 const uint8_t index_;
2547
Calin Juravle10e244f2015-01-26 18:54:32 +00002548 // Whether or not the parameter value corresponds to 'this' argument.
2549 const bool is_this_;
2550
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002551 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2552};
2553
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002554class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002555 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002556 explicit HNot(Primitive::Type result_type, HInstruction* input)
2557 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002558
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002559 bool CanBeMoved() const OVERRIDE { return true; }
2560 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002561 UNUSED(other);
2562 return true;
2563 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002564
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002565 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2566 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002567
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002568 DECLARE_INSTRUCTION(Not);
2569
2570 private:
2571 DISALLOW_COPY_AND_ASSIGN(HNot);
2572};
2573
Roland Levillaindff1f282014-11-05 14:15:05 +00002574class HTypeConversion : public HExpression<1> {
2575 public:
2576 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002577 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2578 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002579 SetRawInputAt(0, input);
2580 DCHECK_NE(input->GetType(), result_type);
2581 }
2582
2583 HInstruction* GetInput() const { return InputAt(0); }
2584 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2585 Primitive::Type GetResultType() const { return GetType(); }
2586
Roland Levillain624279f2014-12-04 11:54:28 +00002587 // Required by the x86 and ARM code generators when producing calls
2588 // to the runtime.
2589 uint32_t GetDexPc() const { return dex_pc_; }
2590
Roland Levillaindff1f282014-11-05 14:15:05 +00002591 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002592 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002593
2594 DECLARE_INSTRUCTION(TypeConversion);
2595
2596 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002597 const uint32_t dex_pc_;
2598
Roland Levillaindff1f282014-11-05 14:15:05 +00002599 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2600};
2601
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002602static constexpr uint32_t kNoRegNumber = -1;
2603
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002604class HPhi : public HInstruction {
2605 public:
2606 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002607 : HInstruction(SideEffects::None()),
2608 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002609 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002610 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002611 is_live_(false),
2612 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002613 inputs_.SetSize(number_of_inputs);
2614 }
2615
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002616 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2617 static Primitive::Type ToPhiType(Primitive::Type type) {
2618 switch (type) {
2619 case Primitive::kPrimBoolean:
2620 case Primitive::kPrimByte:
2621 case Primitive::kPrimShort:
2622 case Primitive::kPrimChar:
2623 return Primitive::kPrimInt;
2624 default:
2625 return type;
2626 }
2627 }
2628
Calin Juravle10e244f2015-01-26 18:54:32 +00002629 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002630
2631 void AddInput(HInstruction* input);
2632
Calin Juravle10e244f2015-01-26 18:54:32 +00002633 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002634 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002635
Calin Juravle10e244f2015-01-26 18:54:32 +00002636 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2637 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2638
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002639 uint32_t GetRegNumber() const { return reg_number_; }
2640
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002641 void SetDead() { is_live_ = false; }
2642 void SetLive() { is_live_ = true; }
2643 bool IsDead() const { return !is_live_; }
2644 bool IsLive() const { return is_live_; }
2645
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002646 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002647
David Brazdil1abb4192015-02-17 18:33:36 +00002648 protected:
2649 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2650
2651 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2652 inputs_.Put(index, input);
2653 }
2654
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002655 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002656 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002657 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002658 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002659 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002660 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002661
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002662 DISALLOW_COPY_AND_ASSIGN(HPhi);
2663};
2664
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002665class HNullCheck : public HExpression<1> {
2666 public:
2667 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002668 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002669 SetRawInputAt(0, value);
2670 }
2671
Calin Juravle10e244f2015-01-26 18:54:32 +00002672 bool CanBeMoved() const OVERRIDE { return true; }
2673 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002674 UNUSED(other);
2675 return true;
2676 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002677
Calin Juravle10e244f2015-01-26 18:54:32 +00002678 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002679
Calin Juravle10e244f2015-01-26 18:54:32 +00002680 bool CanThrow() const OVERRIDE { return true; }
2681
2682 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002683
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002684 uint32_t GetDexPc() const { return dex_pc_; }
2685
2686 DECLARE_INSTRUCTION(NullCheck);
2687
2688 private:
2689 const uint32_t dex_pc_;
2690
2691 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2692};
2693
2694class FieldInfo : public ValueObject {
2695 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002696 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2697 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002698
2699 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002700 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002701 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002702
2703 private:
2704 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002705 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002706 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002707};
2708
2709class HInstanceFieldGet : public HExpression<1> {
2710 public:
2711 HInstanceFieldGet(HInstruction* value,
2712 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002713 MemberOffset field_offset,
2714 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002715 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002716 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002717 SetRawInputAt(0, value);
2718 }
2719
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002720 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002721
2722 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2723 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2724 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002725 }
2726
Calin Juravle77520bc2015-01-12 18:45:46 +00002727 bool CanDoImplicitNullCheck() const OVERRIDE {
2728 return GetFieldOffset().Uint32Value() < kPageSize;
2729 }
2730
2731 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002732 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2733 }
2734
Calin Juravle52c48962014-12-16 17:02:57 +00002735 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002736 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002737 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002738 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002739
2740 DECLARE_INSTRUCTION(InstanceFieldGet);
2741
2742 private:
2743 const FieldInfo field_info_;
2744
2745 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2746};
2747
2748class HInstanceFieldSet : public HTemplateInstruction<2> {
2749 public:
2750 HInstanceFieldSet(HInstruction* object,
2751 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002752 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002753 MemberOffset field_offset,
2754 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002755 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002756 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002757 SetRawInputAt(0, object);
2758 SetRawInputAt(1, value);
2759 }
2760
Calin Juravle77520bc2015-01-12 18:45:46 +00002761 bool CanDoImplicitNullCheck() const OVERRIDE {
2762 return GetFieldOffset().Uint32Value() < kPageSize;
2763 }
2764
Calin Juravle52c48962014-12-16 17:02:57 +00002765 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002766 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002767 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002768 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002769 HInstruction* GetValue() const { return InputAt(1); }
2770
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002771 DECLARE_INSTRUCTION(InstanceFieldSet);
2772
2773 private:
2774 const FieldInfo field_info_;
2775
2776 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2777};
2778
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002779class HArrayGet : public HExpression<2> {
2780 public:
2781 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002782 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002783 SetRawInputAt(0, array);
2784 SetRawInputAt(1, index);
2785 }
2786
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002787 bool CanBeMoved() const OVERRIDE { return true; }
2788 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002789 UNUSED(other);
2790 return true;
2791 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002792 bool CanDoImplicitNullCheck() const OVERRIDE {
2793 // TODO: We can be smarter here.
2794 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2795 // which generates the implicit null check. There are cases when these can be removed
2796 // to produce better code. If we ever add optimizations to do so we should allow an
2797 // implicit check here (as long as the address falls in the first page).
2798 return false;
2799 }
2800
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002801 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002802
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002803 HInstruction* GetArray() const { return InputAt(0); }
2804 HInstruction* GetIndex() const { return InputAt(1); }
2805
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002806 DECLARE_INSTRUCTION(ArrayGet);
2807
2808 private:
2809 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2810};
2811
2812class HArraySet : public HTemplateInstruction<3> {
2813 public:
2814 HArraySet(HInstruction* array,
2815 HInstruction* index,
2816 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002817 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002818 uint32_t dex_pc)
2819 : HTemplateInstruction(SideEffects::ChangesSomething()),
2820 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002821 expected_component_type_(expected_component_type),
2822 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002823 SetRawInputAt(0, array);
2824 SetRawInputAt(1, index);
2825 SetRawInputAt(2, value);
2826 }
2827
Calin Juravle77520bc2015-01-12 18:45:46 +00002828 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002829 // We currently always call a runtime method to catch array store
2830 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002831 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002832 }
2833
Calin Juravle77520bc2015-01-12 18:45:46 +00002834 bool CanDoImplicitNullCheck() const OVERRIDE {
2835 // TODO: Same as for ArrayGet.
2836 return false;
2837 }
2838
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002839 void ClearNeedsTypeCheck() {
2840 needs_type_check_ = false;
2841 }
2842
2843 bool NeedsTypeCheck() const { return needs_type_check_; }
2844
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002845 uint32_t GetDexPc() const { return dex_pc_; }
2846
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002847 HInstruction* GetArray() const { return InputAt(0); }
2848 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002849 HInstruction* GetValue() const { return InputAt(2); }
2850
2851 Primitive::Type GetComponentType() const {
2852 // The Dex format does not type floating point index operations. Since the
2853 // `expected_component_type_` is set during building and can therefore not
2854 // be correct, we also check what is the value type. If it is a floating
2855 // point type, we must use that type.
2856 Primitive::Type value_type = GetValue()->GetType();
2857 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2858 ? value_type
2859 : expected_component_type_;
2860 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002861
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002862 DECLARE_INSTRUCTION(ArraySet);
2863
2864 private:
2865 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002866 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002867 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002868
2869 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2870};
2871
2872class HArrayLength : public HExpression<1> {
2873 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002874 explicit HArrayLength(HInstruction* array)
2875 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2876 // Note that arrays do not change length, so the instruction does not
2877 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002878 SetRawInputAt(0, array);
2879 }
2880
Calin Juravle77520bc2015-01-12 18:45:46 +00002881 bool CanBeMoved() const OVERRIDE { return true; }
2882 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002883 UNUSED(other);
2884 return true;
2885 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002886 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002887
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002888 DECLARE_INSTRUCTION(ArrayLength);
2889
2890 private:
2891 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2892};
2893
2894class HBoundsCheck : public HExpression<2> {
2895 public:
2896 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002897 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002898 DCHECK(index->GetType() == Primitive::kPrimInt);
2899 SetRawInputAt(0, index);
2900 SetRawInputAt(1, length);
2901 }
2902
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002903 bool CanBeMoved() const OVERRIDE { return true; }
2904 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002905 UNUSED(other);
2906 return true;
2907 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002908
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002909 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002910
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002911 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002912
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002913 uint32_t GetDexPc() const { return dex_pc_; }
2914
2915 DECLARE_INSTRUCTION(BoundsCheck);
2916
2917 private:
2918 const uint32_t dex_pc_;
2919
2920 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2921};
2922
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002923/**
2924 * Some DEX instructions are folded into multiple HInstructions that need
2925 * to stay live until the last HInstruction. This class
2926 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002927 * HInstruction stays live. `index` represents the stack location index of the
2928 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002929 */
2930class HTemporary : public HTemplateInstruction<0> {
2931 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002932 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002933
2934 size_t GetIndex() const { return index_; }
2935
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002936 Primitive::Type GetType() const OVERRIDE {
2937 // The previous instruction is the one that will be stored in the temporary location.
2938 DCHECK(GetPrevious() != nullptr);
2939 return GetPrevious()->GetType();
2940 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002941
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002942 DECLARE_INSTRUCTION(Temporary);
2943
2944 private:
2945 const size_t index_;
2946
2947 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2948};
2949
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002950class HSuspendCheck : public HTemplateInstruction<0> {
2951 public:
2952 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002953 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002954
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002955 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002956 return true;
2957 }
2958
2959 uint32_t GetDexPc() const { return dex_pc_; }
2960
2961 DECLARE_INSTRUCTION(SuspendCheck);
2962
2963 private:
2964 const uint32_t dex_pc_;
2965
2966 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2967};
2968
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002969/**
2970 * Instruction to load a Class object.
2971 */
2972class HLoadClass : public HExpression<0> {
2973 public:
2974 HLoadClass(uint16_t type_index,
2975 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002976 uint32_t dex_pc)
2977 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2978 type_index_(type_index),
2979 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002980 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00002981 generate_clinit_check_(false),
2982 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002983
2984 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002985
2986 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2987 return other->AsLoadClass()->type_index_ == type_index_;
2988 }
2989
2990 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2991
2992 uint32_t GetDexPc() const { return dex_pc_; }
2993 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002994 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002995
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002996 bool NeedsEnvironment() const OVERRIDE {
2997 // Will call runtime and load the class if the class is not loaded yet.
2998 // TODO: finer grain decision.
2999 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003000 }
3001
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003002 bool MustGenerateClinitCheck() const {
3003 return generate_clinit_check_;
3004 }
3005
3006 void SetMustGenerateClinitCheck() {
3007 generate_clinit_check_ = true;
3008 }
3009
3010 bool CanCallRuntime() const {
3011 return MustGenerateClinitCheck() || !is_referrers_class_;
3012 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003013
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003014 bool CanThrow() const OVERRIDE {
3015 // May call runtime and and therefore can throw.
3016 // TODO: finer grain decision.
3017 return !is_referrers_class_;
3018 }
3019
Calin Juravleacf735c2015-02-12 15:25:22 +00003020 ReferenceTypeInfo GetLoadedClassRTI() {
3021 return loaded_class_rti_;
3022 }
3023
3024 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3025 // Make sure we only set exact types (the loaded class should never be merged).
3026 DCHECK(rti.IsExact());
3027 loaded_class_rti_ = rti;
3028 }
3029
3030 bool IsResolved() {
3031 return loaded_class_rti_.IsExact();
3032 }
3033
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003034 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3035
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003036 DECLARE_INSTRUCTION(LoadClass);
3037
3038 private:
3039 const uint16_t type_index_;
3040 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003041 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003042 // Whether this instruction must generate the initialization check.
3043 // Used for code generation.
3044 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003045
Calin Juravleacf735c2015-02-12 15:25:22 +00003046 ReferenceTypeInfo loaded_class_rti_;
3047
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003048 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3049};
3050
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003051class HLoadString : public HExpression<0> {
3052 public:
3053 HLoadString(uint32_t string_index, uint32_t dex_pc)
3054 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3055 string_index_(string_index),
3056 dex_pc_(dex_pc) {}
3057
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003058 bool CanBeMoved() const OVERRIDE { return true; }
3059
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003060 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3061 return other->AsLoadString()->string_index_ == string_index_;
3062 }
3063
3064 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3065
3066 uint32_t GetDexPc() const { return dex_pc_; }
3067 uint32_t GetStringIndex() const { return string_index_; }
3068
3069 // TODO: Can we deopt or debug when we resolve a string?
3070 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003071 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003072
3073 DECLARE_INSTRUCTION(LoadString);
3074
3075 private:
3076 const uint32_t string_index_;
3077 const uint32_t dex_pc_;
3078
3079 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3080};
3081
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003082// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003083/**
3084 * Performs an initialization check on its Class object input.
3085 */
3086class HClinitCheck : public HExpression<1> {
3087 public:
3088 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
3089 : HExpression(Primitive::kPrimNot, SideEffects::All()),
3090 dex_pc_(dex_pc) {
3091 SetRawInputAt(0, constant);
3092 }
3093
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003094 bool CanBeMoved() const OVERRIDE { return true; }
3095 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3096 UNUSED(other);
3097 return true;
3098 }
3099
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003100 bool NeedsEnvironment() const OVERRIDE {
3101 // May call runtime to initialize the class.
3102 return true;
3103 }
3104
3105 uint32_t GetDexPc() const { return dex_pc_; }
3106
3107 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3108
3109 DECLARE_INSTRUCTION(ClinitCheck);
3110
3111 private:
3112 const uint32_t dex_pc_;
3113
3114 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3115};
3116
3117class HStaticFieldGet : public HExpression<1> {
3118 public:
3119 HStaticFieldGet(HInstruction* cls,
3120 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003121 MemberOffset field_offset,
3122 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003123 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003124 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003125 SetRawInputAt(0, cls);
3126 }
3127
Calin Juravle52c48962014-12-16 17:02:57 +00003128
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003129 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003130
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003131 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003132 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3133 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003134 }
3135
3136 size_t ComputeHashCode() const OVERRIDE {
3137 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3138 }
3139
Calin Juravle52c48962014-12-16 17:02:57 +00003140 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003141 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3142 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003143 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003144
3145 DECLARE_INSTRUCTION(StaticFieldGet);
3146
3147 private:
3148 const FieldInfo field_info_;
3149
3150 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3151};
3152
3153class HStaticFieldSet : public HTemplateInstruction<2> {
3154 public:
3155 HStaticFieldSet(HInstruction* cls,
3156 HInstruction* value,
3157 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003158 MemberOffset field_offset,
3159 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003160 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003161 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003162 SetRawInputAt(0, cls);
3163 SetRawInputAt(1, value);
3164 }
3165
Calin Juravle52c48962014-12-16 17:02:57 +00003166 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003167 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3168 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003169 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003170
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003171 HInstruction* GetValue() const { return InputAt(1); }
3172
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003173 DECLARE_INSTRUCTION(StaticFieldSet);
3174
3175 private:
3176 const FieldInfo field_info_;
3177
3178 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3179};
3180
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003181// Implement the move-exception DEX instruction.
3182class HLoadException : public HExpression<0> {
3183 public:
3184 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3185
3186 DECLARE_INSTRUCTION(LoadException);
3187
3188 private:
3189 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3190};
3191
3192class HThrow : public HTemplateInstruction<1> {
3193 public:
3194 HThrow(HInstruction* exception, uint32_t dex_pc)
3195 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3196 SetRawInputAt(0, exception);
3197 }
3198
3199 bool IsControlFlow() const OVERRIDE { return true; }
3200
3201 bool NeedsEnvironment() const OVERRIDE { return true; }
3202
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003203 bool CanThrow() const OVERRIDE { return true; }
3204
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003205 uint32_t GetDexPc() const { return dex_pc_; }
3206
3207 DECLARE_INSTRUCTION(Throw);
3208
3209 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003210 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003211
3212 DISALLOW_COPY_AND_ASSIGN(HThrow);
3213};
3214
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003215class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003216 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003217 HInstanceOf(HInstruction* object,
3218 HLoadClass* constant,
3219 bool class_is_final,
3220 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003221 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3222 class_is_final_(class_is_final),
3223 dex_pc_(dex_pc) {
3224 SetRawInputAt(0, object);
3225 SetRawInputAt(1, constant);
3226 }
3227
3228 bool CanBeMoved() const OVERRIDE { return true; }
3229
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003230 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003231 return true;
3232 }
3233
3234 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003235 return false;
3236 }
3237
3238 uint32_t GetDexPc() const { return dex_pc_; }
3239
3240 bool IsClassFinal() const { return class_is_final_; }
3241
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003242 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003243
3244 private:
3245 const bool class_is_final_;
3246 const uint32_t dex_pc_;
3247
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003248 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3249};
3250
Calin Juravleb1498f62015-02-16 13:13:29 +00003251class HBoundType : public HExpression<1> {
3252 public:
3253 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3254 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3255 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003256 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003257 SetRawInputAt(0, input);
3258 }
3259
3260 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3261
3262 bool CanBeNull() const OVERRIDE {
3263 // `null instanceof ClassX` always return false so we can't be null.
3264 return false;
3265 }
3266
3267 DECLARE_INSTRUCTION(BoundType);
3268
3269 private:
3270 // Encodes the most upper class that this instruction can have. In other words
3271 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3272 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3273 const ReferenceTypeInfo bound_type_;
3274
3275 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3276};
3277
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003278class HCheckCast : public HTemplateInstruction<2> {
3279 public:
3280 HCheckCast(HInstruction* object,
3281 HLoadClass* constant,
3282 bool class_is_final,
3283 uint32_t dex_pc)
3284 : HTemplateInstruction(SideEffects::None()),
3285 class_is_final_(class_is_final),
3286 dex_pc_(dex_pc) {
3287 SetRawInputAt(0, object);
3288 SetRawInputAt(1, constant);
3289 }
3290
3291 bool CanBeMoved() const OVERRIDE { return true; }
3292
3293 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3294 return true;
3295 }
3296
3297 bool NeedsEnvironment() const OVERRIDE {
3298 // Instruction may throw a CheckCastError.
3299 return true;
3300 }
3301
3302 bool CanThrow() const OVERRIDE { return true; }
3303
3304 uint32_t GetDexPc() const { return dex_pc_; }
3305
3306 bool IsClassFinal() const { return class_is_final_; }
3307
3308 DECLARE_INSTRUCTION(CheckCast);
3309
3310 private:
3311 const bool class_is_final_;
3312 const uint32_t dex_pc_;
3313
3314 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003315};
3316
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003317class HMonitorOperation : public HTemplateInstruction<1> {
3318 public:
3319 enum OperationKind {
3320 kEnter,
3321 kExit,
3322 };
3323
3324 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3325 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3326 SetRawInputAt(0, object);
3327 }
3328
3329 // Instruction may throw a Java exception, so we need an environment.
3330 bool NeedsEnvironment() const OVERRIDE { return true; }
3331 bool CanThrow() const OVERRIDE { return true; }
3332
3333 uint32_t GetDexPc() const { return dex_pc_; }
3334
3335 bool IsEnter() const { return kind_ == kEnter; }
3336
3337 DECLARE_INSTRUCTION(MonitorOperation);
3338
Calin Juravle52c48962014-12-16 17:02:57 +00003339 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003340 const OperationKind kind_;
3341 const uint32_t dex_pc_;
3342
3343 private:
3344 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3345};
3346
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003347class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003348 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003349 MoveOperands(Location source, Location destination, HInstruction* instruction)
3350 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003351
3352 Location GetSource() const { return source_; }
3353 Location GetDestination() const { return destination_; }
3354
3355 void SetSource(Location value) { source_ = value; }
3356 void SetDestination(Location value) { destination_ = value; }
3357
3358 // The parallel move resolver marks moves as "in-progress" by clearing the
3359 // destination (but not the source).
3360 Location MarkPending() {
3361 DCHECK(!IsPending());
3362 Location dest = destination_;
3363 destination_ = Location::NoLocation();
3364 return dest;
3365 }
3366
3367 void ClearPending(Location dest) {
3368 DCHECK(IsPending());
3369 destination_ = dest;
3370 }
3371
3372 bool IsPending() const {
3373 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3374 return destination_.IsInvalid() && !source_.IsInvalid();
3375 }
3376
3377 // True if this blocks a move from the given location.
3378 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003379 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003380 }
3381
3382 // A move is redundant if it's been eliminated, if its source and
3383 // destination are the same, or if its destination is unneeded.
3384 bool IsRedundant() const {
3385 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3386 }
3387
3388 // We clear both operands to indicate move that's been eliminated.
3389 void Eliminate() {
3390 source_ = destination_ = Location::NoLocation();
3391 }
3392
3393 bool IsEliminated() const {
3394 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3395 return source_.IsInvalid();
3396 }
3397
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003398 HInstruction* GetInstruction() const { return instruction_; }
3399
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003400 private:
3401 Location source_;
3402 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003403 // The instruction this move is assocatied with. Null when this move is
3404 // for moving an input in the expected locations of user (including a phi user).
3405 // This is only used in debug mode, to ensure we do not connect interval siblings
3406 // in the same parallel move.
3407 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003408};
3409
3410static constexpr size_t kDefaultNumberOfMoves = 4;
3411
3412class HParallelMove : public HTemplateInstruction<0> {
3413 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003414 explicit HParallelMove(ArenaAllocator* arena)
3415 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003416
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003417 void AddMove(Location source, Location destination, HInstruction* instruction) {
3418 DCHECK(source.IsValid());
3419 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003420 if (kIsDebugBuild) {
3421 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003422 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003423 if (moves_.Get(i).GetInstruction() == instruction) {
3424 // Special case the situation where the move is for the spill slot
3425 // of the instruction.
3426 if ((GetPrevious() == instruction)
3427 || ((GetPrevious() == nullptr)
3428 && instruction->IsPhi()
3429 && instruction->GetBlock() == GetBlock())) {
3430 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3431 << "Doing parallel moves for the same instruction.";
3432 } else {
3433 DCHECK(false) << "Doing parallel moves for the same instruction.";
3434 }
3435 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003436 }
3437 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003438 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3439 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3440 << "Same destination for two moves in a parallel move.";
3441 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003442 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003443 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003444 }
3445
3446 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003447 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003448 }
3449
3450 size_t NumMoves() const { return moves_.Size(); }
3451
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003452 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003453
3454 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003455 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003456
3457 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3458};
3459
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003460class HGraphVisitor : public ValueObject {
3461 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003462 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3463 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003464
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003465 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003466 virtual void VisitBasicBlock(HBasicBlock* block);
3467
Roland Levillain633021e2014-10-01 14:12:25 +01003468 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003469 void VisitInsertionOrder();
3470
Roland Levillain633021e2014-10-01 14:12:25 +01003471 // Visit the graph following dominator tree reverse post-order.
3472 void VisitReversePostOrder();
3473
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003474 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003475
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003476 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003477#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003478 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3479
3480 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3481
3482#undef DECLARE_VISIT_INSTRUCTION
3483
3484 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003485 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003486
3487 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3488};
3489
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003490class HGraphDelegateVisitor : public HGraphVisitor {
3491 public:
3492 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3493 virtual ~HGraphDelegateVisitor() {}
3494
3495 // Visit functions that delegate to to super class.
3496#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003497 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003498
3499 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3500
3501#undef DECLARE_VISIT_INSTRUCTION
3502
3503 private:
3504 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3505};
3506
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003507class HInsertionOrderIterator : public ValueObject {
3508 public:
3509 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3510
3511 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3512 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3513 void Advance() { ++index_; }
3514
3515 private:
3516 const HGraph& graph_;
3517 size_t index_;
3518
3519 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3520};
3521
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003522class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003523 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003524 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3525 // Check that reverse post order of the graph has been built.
3526 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3527 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003528
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003529 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3530 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003531 void Advance() { ++index_; }
3532
3533 private:
3534 const HGraph& graph_;
3535 size_t index_;
3536
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003537 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003538};
3539
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003540class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003541 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003542 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003543 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3544 // Check that reverse post order of the graph has been built.
3545 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3546 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003547
3548 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003549 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003550 void Advance() { --index_; }
3551
3552 private:
3553 const HGraph& graph_;
3554 size_t index_;
3555
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003556 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003557};
3558
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003559// Iterator over the blocks that art part of the loop. Includes blocks part
3560// of an inner loop. The order in which the blocks are iterated is on their
3561// block id.
3562class HBlocksInLoopIterator : public ValueObject {
3563 public:
3564 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3565 : blocks_in_loop_(info.GetBlocks()),
3566 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3567 index_(0) {
3568 if (!blocks_in_loop_.IsBitSet(index_)) {
3569 Advance();
3570 }
3571 }
3572
3573 bool Done() const { return index_ == blocks_.Size(); }
3574 HBasicBlock* Current() const { return blocks_.Get(index_); }
3575 void Advance() {
3576 ++index_;
3577 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3578 if (blocks_in_loop_.IsBitSet(index_)) {
3579 break;
3580 }
3581 }
3582 }
3583
3584 private:
3585 const BitVector& blocks_in_loop_;
3586 const GrowableArray<HBasicBlock*>& blocks_;
3587 size_t index_;
3588
3589 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3590};
3591
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003592inline int64_t Int64FromConstant(HConstant* constant) {
3593 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3594 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3595 : constant->AsLongConstant()->GetValue();
3596}
3597
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003598} // namespace art
3599
3600#endif // ART_COMPILER_OPTIMIZING_NODES_H_