blob: 08b16d99b6d8aff7ff4d4a7537b68badd18644c0 [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 Geoffraye53798a2014-12-01 10:31:54 +00002134 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002135 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002136 invoke_type_(invoke_type),
2137 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002138
Calin Juravle77520bc2015-01-12 18:45:46 +00002139 bool CanDoImplicitNullCheck() const OVERRIDE {
2140 // We access the method via the dex cache so we can't do an implicit null check.
2141 // TODO: for intrinsics we can generate implicit null checks.
2142 return false;
2143 }
2144
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002145 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002146 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002147 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002148
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002149 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002150
2151 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002152 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002153 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002154
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002155 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002156};
2157
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002158class HInvokeVirtual : public HInvoke {
2159 public:
2160 HInvokeVirtual(ArenaAllocator* arena,
2161 uint32_t number_of_arguments,
2162 Primitive::Type return_type,
2163 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002164 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002165 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002166 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002167 vtable_index_(vtable_index) {}
2168
Calin Juravle77520bc2015-01-12 18:45:46 +00002169 bool CanDoImplicitNullCheck() const OVERRIDE {
2170 // TODO: Add implicit null checks in intrinsics.
2171 return !GetLocations()->Intrinsified();
2172 }
2173
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002174 uint32_t GetVTableIndex() const { return vtable_index_; }
2175
2176 DECLARE_INSTRUCTION(InvokeVirtual);
2177
2178 private:
2179 const uint32_t vtable_index_;
2180
2181 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2182};
2183
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002184class HInvokeInterface : public HInvoke {
2185 public:
2186 HInvokeInterface(ArenaAllocator* arena,
2187 uint32_t number_of_arguments,
2188 Primitive::Type return_type,
2189 uint32_t dex_pc,
2190 uint32_t dex_method_index,
2191 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002192 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002193 imt_index_(imt_index) {}
2194
Calin Juravle77520bc2015-01-12 18:45:46 +00002195 bool CanDoImplicitNullCheck() const OVERRIDE {
2196 // TODO: Add implicit null checks in intrinsics.
2197 return !GetLocations()->Intrinsified();
2198 }
2199
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002200 uint32_t GetImtIndex() const { return imt_index_; }
2201 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2202
2203 DECLARE_INSTRUCTION(InvokeInterface);
2204
2205 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002206 const uint32_t imt_index_;
2207
2208 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2209};
2210
Dave Allison20dfc792014-06-16 20:44:29 -07002211class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002212 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002213 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002214 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2215 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002216 type_index_(type_index),
2217 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002218
2219 uint32_t GetDexPc() const { return dex_pc_; }
2220 uint16_t GetTypeIndex() const { return type_index_; }
2221
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002222 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002223 bool NeedsEnvironment() const OVERRIDE { return true; }
2224 // It may throw when called on:
2225 // - interfaces
2226 // - abstract/innaccessible/unknown classes
2227 // TODO: optimize when possible.
2228 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002229
Calin Juravle10e244f2015-01-26 18:54:32 +00002230 bool CanBeNull() const OVERRIDE { return false; }
2231
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002232 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2233
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002234 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002235
2236 private:
2237 const uint32_t dex_pc_;
2238 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002239 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002240
2241 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2242};
2243
Roland Levillain88cb1752014-10-20 16:36:47 +01002244class HNeg : public HUnaryOperation {
2245 public:
2246 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2247 : HUnaryOperation(result_type, input) {}
2248
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002249 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2250 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002251
Roland Levillain88cb1752014-10-20 16:36:47 +01002252 DECLARE_INSTRUCTION(Neg);
2253
2254 private:
2255 DISALLOW_COPY_AND_ASSIGN(HNeg);
2256};
2257
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002258class HNewArray : public HExpression<1> {
2259 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002260 HNewArray(HInstruction* length,
2261 uint32_t dex_pc,
2262 uint16_t type_index,
2263 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002264 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2265 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002266 type_index_(type_index),
2267 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002268 SetRawInputAt(0, length);
2269 }
2270
2271 uint32_t GetDexPc() const { return dex_pc_; }
2272 uint16_t GetTypeIndex() const { return type_index_; }
2273
2274 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002275 bool NeedsEnvironment() const OVERRIDE { return true; }
2276
2277 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002278
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002279 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2280
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002281 DECLARE_INSTRUCTION(NewArray);
2282
2283 private:
2284 const uint32_t dex_pc_;
2285 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002286 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002287
2288 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2289};
2290
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002291class HAdd : public HBinaryOperation {
2292 public:
2293 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2294 : HBinaryOperation(result_type, left, right) {}
2295
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002296 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002297
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002298 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002299 return x + y;
2300 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002301 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002302 return x + y;
2303 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002304
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002305 DECLARE_INSTRUCTION(Add);
2306
2307 private:
2308 DISALLOW_COPY_AND_ASSIGN(HAdd);
2309};
2310
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002311class HSub : public HBinaryOperation {
2312 public:
2313 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2314 : HBinaryOperation(result_type, left, right) {}
2315
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002316 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002317 return x - y;
2318 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002319 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002320 return x - y;
2321 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002322
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002323 DECLARE_INSTRUCTION(Sub);
2324
2325 private:
2326 DISALLOW_COPY_AND_ASSIGN(HSub);
2327};
2328
Calin Juravle34bacdf2014-10-07 20:23:36 +01002329class HMul : public HBinaryOperation {
2330 public:
2331 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2332 : HBinaryOperation(result_type, left, right) {}
2333
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002334 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002335
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002336 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2337 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002338
2339 DECLARE_INSTRUCTION(Mul);
2340
2341 private:
2342 DISALLOW_COPY_AND_ASSIGN(HMul);
2343};
2344
Calin Juravle7c4954d2014-10-28 16:57:40 +00002345class HDiv : public HBinaryOperation {
2346 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002347 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2348 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002349
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002350 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002351 // Our graph structure ensures we never have 0 for `y` during constant folding.
2352 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002353 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002354 return (y == -1) ? -x : x / y;
2355 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002356
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002357 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002358 DCHECK_NE(y, 0);
2359 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2360 return (y == -1) ? -x : x / y;
2361 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002362
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002363 uint32_t GetDexPc() const { return dex_pc_; }
2364
Calin Juravle7c4954d2014-10-28 16:57:40 +00002365 DECLARE_INSTRUCTION(Div);
2366
2367 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002368 const uint32_t dex_pc_;
2369
Calin Juravle7c4954d2014-10-28 16:57:40 +00002370 DISALLOW_COPY_AND_ASSIGN(HDiv);
2371};
2372
Calin Juravlebacfec32014-11-14 15:54:36 +00002373class HRem : public HBinaryOperation {
2374 public:
2375 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2376 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2377
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002378 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002379 DCHECK_NE(y, 0);
2380 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2381 return (y == -1) ? 0 : x % y;
2382 }
2383
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002384 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002385 DCHECK_NE(y, 0);
2386 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2387 return (y == -1) ? 0 : x % y;
2388 }
2389
2390 uint32_t GetDexPc() const { return dex_pc_; }
2391
2392 DECLARE_INSTRUCTION(Rem);
2393
2394 private:
2395 const uint32_t dex_pc_;
2396
2397 DISALLOW_COPY_AND_ASSIGN(HRem);
2398};
2399
Calin Juravled0d48522014-11-04 16:40:20 +00002400class HDivZeroCheck : public HExpression<1> {
2401 public:
2402 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2403 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2404 SetRawInputAt(0, value);
2405 }
2406
2407 bool CanBeMoved() const OVERRIDE { return true; }
2408
2409 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2410 UNUSED(other);
2411 return true;
2412 }
2413
2414 bool NeedsEnvironment() const OVERRIDE { return true; }
2415 bool CanThrow() const OVERRIDE { return true; }
2416
2417 uint32_t GetDexPc() const { return dex_pc_; }
2418
2419 DECLARE_INSTRUCTION(DivZeroCheck);
2420
2421 private:
2422 const uint32_t dex_pc_;
2423
2424 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2425};
2426
Calin Juravle9aec02f2014-11-18 23:06:35 +00002427class HShl : public HBinaryOperation {
2428 public:
2429 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2430 : HBinaryOperation(result_type, left, right) {}
2431
2432 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2433 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2434
2435 DECLARE_INSTRUCTION(Shl);
2436
2437 private:
2438 DISALLOW_COPY_AND_ASSIGN(HShl);
2439};
2440
2441class HShr : public HBinaryOperation {
2442 public:
2443 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2444 : HBinaryOperation(result_type, left, right) {}
2445
2446 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2447 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2448
2449 DECLARE_INSTRUCTION(Shr);
2450
2451 private:
2452 DISALLOW_COPY_AND_ASSIGN(HShr);
2453};
2454
2455class HUShr : public HBinaryOperation {
2456 public:
2457 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2458 : HBinaryOperation(result_type, left, right) {}
2459
2460 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2461 uint32_t ux = static_cast<uint32_t>(x);
2462 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2463 return static_cast<int32_t>(ux >> uy);
2464 }
2465
2466 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2467 uint64_t ux = static_cast<uint64_t>(x);
2468 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2469 return static_cast<int64_t>(ux >> uy);
2470 }
2471
2472 DECLARE_INSTRUCTION(UShr);
2473
2474 private:
2475 DISALLOW_COPY_AND_ASSIGN(HUShr);
2476};
2477
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002478class HAnd : public HBinaryOperation {
2479 public:
2480 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2481 : HBinaryOperation(result_type, left, right) {}
2482
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002483 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002484
2485 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2486 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2487
2488 DECLARE_INSTRUCTION(And);
2489
2490 private:
2491 DISALLOW_COPY_AND_ASSIGN(HAnd);
2492};
2493
2494class HOr : public HBinaryOperation {
2495 public:
2496 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2497 : HBinaryOperation(result_type, left, right) {}
2498
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002499 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002500
2501 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2502 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2503
2504 DECLARE_INSTRUCTION(Or);
2505
2506 private:
2507 DISALLOW_COPY_AND_ASSIGN(HOr);
2508};
2509
2510class HXor : public HBinaryOperation {
2511 public:
2512 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2513 : HBinaryOperation(result_type, left, right) {}
2514
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002515 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002516
2517 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2518 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2519
2520 DECLARE_INSTRUCTION(Xor);
2521
2522 private:
2523 DISALLOW_COPY_AND_ASSIGN(HXor);
2524};
2525
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002526// The value of a parameter in this method. Its location depends on
2527// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002528class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002529 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002530 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2531 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002532
2533 uint8_t GetIndex() const { return index_; }
2534
Calin Juravle10e244f2015-01-26 18:54:32 +00002535 bool CanBeNull() const OVERRIDE { return !is_this_; }
2536
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002537 DECLARE_INSTRUCTION(ParameterValue);
2538
2539 private:
2540 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002541 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002542 const uint8_t index_;
2543
Calin Juravle10e244f2015-01-26 18:54:32 +00002544 // Whether or not the parameter value corresponds to 'this' argument.
2545 const bool is_this_;
2546
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002547 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2548};
2549
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002550class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002551 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002552 explicit HNot(Primitive::Type result_type, HInstruction* input)
2553 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002554
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002555 bool CanBeMoved() const OVERRIDE { return true; }
2556 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002557 UNUSED(other);
2558 return true;
2559 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002560
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002561 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2562 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002563
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002564 DECLARE_INSTRUCTION(Not);
2565
2566 private:
2567 DISALLOW_COPY_AND_ASSIGN(HNot);
2568};
2569
Roland Levillaindff1f282014-11-05 14:15:05 +00002570class HTypeConversion : public HExpression<1> {
2571 public:
2572 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002573 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2574 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002575 SetRawInputAt(0, input);
2576 DCHECK_NE(input->GetType(), result_type);
2577 }
2578
2579 HInstruction* GetInput() const { return InputAt(0); }
2580 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2581 Primitive::Type GetResultType() const { return GetType(); }
2582
Roland Levillain624279f2014-12-04 11:54:28 +00002583 // Required by the x86 and ARM code generators when producing calls
2584 // to the runtime.
2585 uint32_t GetDexPc() const { return dex_pc_; }
2586
Roland Levillaindff1f282014-11-05 14:15:05 +00002587 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002588 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002589
2590 DECLARE_INSTRUCTION(TypeConversion);
2591
2592 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002593 const uint32_t dex_pc_;
2594
Roland Levillaindff1f282014-11-05 14:15:05 +00002595 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2596};
2597
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002598static constexpr uint32_t kNoRegNumber = -1;
2599
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002600class HPhi : public HInstruction {
2601 public:
2602 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002603 : HInstruction(SideEffects::None()),
2604 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002605 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002606 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002607 is_live_(false),
2608 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002609 inputs_.SetSize(number_of_inputs);
2610 }
2611
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002612 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2613 static Primitive::Type ToPhiType(Primitive::Type type) {
2614 switch (type) {
2615 case Primitive::kPrimBoolean:
2616 case Primitive::kPrimByte:
2617 case Primitive::kPrimShort:
2618 case Primitive::kPrimChar:
2619 return Primitive::kPrimInt;
2620 default:
2621 return type;
2622 }
2623 }
2624
Calin Juravle10e244f2015-01-26 18:54:32 +00002625 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002626
2627 void AddInput(HInstruction* input);
2628
Calin Juravle10e244f2015-01-26 18:54:32 +00002629 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002630 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002631
Calin Juravle10e244f2015-01-26 18:54:32 +00002632 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2633 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2634
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002635 uint32_t GetRegNumber() const { return reg_number_; }
2636
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002637 void SetDead() { is_live_ = false; }
2638 void SetLive() { is_live_ = true; }
2639 bool IsDead() const { return !is_live_; }
2640 bool IsLive() const { return is_live_; }
2641
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002642 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002643
David Brazdil1abb4192015-02-17 18:33:36 +00002644 protected:
2645 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2646
2647 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2648 inputs_.Put(index, input);
2649 }
2650
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002651 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002652 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002653 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002654 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002655 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002656 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002657
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002658 DISALLOW_COPY_AND_ASSIGN(HPhi);
2659};
2660
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002661class HNullCheck : public HExpression<1> {
2662 public:
2663 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002664 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002665 SetRawInputAt(0, value);
2666 }
2667
Calin Juravle10e244f2015-01-26 18:54:32 +00002668 bool CanBeMoved() const OVERRIDE { return true; }
2669 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002670 UNUSED(other);
2671 return true;
2672 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002673
Calin Juravle10e244f2015-01-26 18:54:32 +00002674 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002675
Calin Juravle10e244f2015-01-26 18:54:32 +00002676 bool CanThrow() const OVERRIDE { return true; }
2677
2678 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002679
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002680 uint32_t GetDexPc() const { return dex_pc_; }
2681
2682 DECLARE_INSTRUCTION(NullCheck);
2683
2684 private:
2685 const uint32_t dex_pc_;
2686
2687 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2688};
2689
2690class FieldInfo : public ValueObject {
2691 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002692 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2693 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002694
2695 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002696 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002697 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002698
2699 private:
2700 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002701 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002702 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002703};
2704
2705class HInstanceFieldGet : public HExpression<1> {
2706 public:
2707 HInstanceFieldGet(HInstruction* value,
2708 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002709 MemberOffset field_offset,
2710 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002711 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002712 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002713 SetRawInputAt(0, value);
2714 }
2715
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002716 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002717
2718 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2719 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2720 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002721 }
2722
Calin Juravle77520bc2015-01-12 18:45:46 +00002723 bool CanDoImplicitNullCheck() const OVERRIDE {
2724 return GetFieldOffset().Uint32Value() < kPageSize;
2725 }
2726
2727 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002728 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2729 }
2730
Calin Juravle52c48962014-12-16 17:02:57 +00002731 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002732 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002733 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002734 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002735
2736 DECLARE_INSTRUCTION(InstanceFieldGet);
2737
2738 private:
2739 const FieldInfo field_info_;
2740
2741 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2742};
2743
2744class HInstanceFieldSet : public HTemplateInstruction<2> {
2745 public:
2746 HInstanceFieldSet(HInstruction* object,
2747 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002748 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002749 MemberOffset field_offset,
2750 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002751 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002752 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002753 SetRawInputAt(0, object);
2754 SetRawInputAt(1, value);
2755 }
2756
Calin Juravle77520bc2015-01-12 18:45:46 +00002757 bool CanDoImplicitNullCheck() const OVERRIDE {
2758 return GetFieldOffset().Uint32Value() < kPageSize;
2759 }
2760
Calin Juravle52c48962014-12-16 17:02:57 +00002761 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002762 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002763 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002764 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002765 HInstruction* GetValue() const { return InputAt(1); }
2766
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002767 DECLARE_INSTRUCTION(InstanceFieldSet);
2768
2769 private:
2770 const FieldInfo field_info_;
2771
2772 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2773};
2774
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002775class HArrayGet : public HExpression<2> {
2776 public:
2777 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002778 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002779 SetRawInputAt(0, array);
2780 SetRawInputAt(1, index);
2781 }
2782
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002783 bool CanBeMoved() const OVERRIDE { return true; }
2784 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002785 UNUSED(other);
2786 return true;
2787 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002788 bool CanDoImplicitNullCheck() const OVERRIDE {
2789 // TODO: We can be smarter here.
2790 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2791 // which generates the implicit null check. There are cases when these can be removed
2792 // to produce better code. If we ever add optimizations to do so we should allow an
2793 // implicit check here (as long as the address falls in the first page).
2794 return false;
2795 }
2796
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002797 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002798
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002799 HInstruction* GetArray() const { return InputAt(0); }
2800 HInstruction* GetIndex() const { return InputAt(1); }
2801
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002802 DECLARE_INSTRUCTION(ArrayGet);
2803
2804 private:
2805 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2806};
2807
2808class HArraySet : public HTemplateInstruction<3> {
2809 public:
2810 HArraySet(HInstruction* array,
2811 HInstruction* index,
2812 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002813 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002814 uint32_t dex_pc)
2815 : HTemplateInstruction(SideEffects::ChangesSomething()),
2816 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002817 expected_component_type_(expected_component_type),
2818 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002819 SetRawInputAt(0, array);
2820 SetRawInputAt(1, index);
2821 SetRawInputAt(2, value);
2822 }
2823
Calin Juravle77520bc2015-01-12 18:45:46 +00002824 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002825 // We currently always call a runtime method to catch array store
2826 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002827 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002828 }
2829
Calin Juravle77520bc2015-01-12 18:45:46 +00002830 bool CanDoImplicitNullCheck() const OVERRIDE {
2831 // TODO: Same as for ArrayGet.
2832 return false;
2833 }
2834
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002835 void ClearNeedsTypeCheck() {
2836 needs_type_check_ = false;
2837 }
2838
2839 bool NeedsTypeCheck() const { return needs_type_check_; }
2840
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002841 uint32_t GetDexPc() const { return dex_pc_; }
2842
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002843 HInstruction* GetArray() const { return InputAt(0); }
2844 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002845 HInstruction* GetValue() const { return InputAt(2); }
2846
2847 Primitive::Type GetComponentType() const {
2848 // The Dex format does not type floating point index operations. Since the
2849 // `expected_component_type_` is set during building and can therefore not
2850 // be correct, we also check what is the value type. If it is a floating
2851 // point type, we must use that type.
2852 Primitive::Type value_type = GetValue()->GetType();
2853 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2854 ? value_type
2855 : expected_component_type_;
2856 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002857
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002858 DECLARE_INSTRUCTION(ArraySet);
2859
2860 private:
2861 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002862 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002863 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002864
2865 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2866};
2867
2868class HArrayLength : public HExpression<1> {
2869 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002870 explicit HArrayLength(HInstruction* array)
2871 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2872 // Note that arrays do not change length, so the instruction does not
2873 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002874 SetRawInputAt(0, array);
2875 }
2876
Calin Juravle77520bc2015-01-12 18:45:46 +00002877 bool CanBeMoved() const OVERRIDE { return true; }
2878 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002879 UNUSED(other);
2880 return true;
2881 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002882 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002883
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002884 DECLARE_INSTRUCTION(ArrayLength);
2885
2886 private:
2887 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2888};
2889
2890class HBoundsCheck : public HExpression<2> {
2891 public:
2892 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002893 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002894 DCHECK(index->GetType() == Primitive::kPrimInt);
2895 SetRawInputAt(0, index);
2896 SetRawInputAt(1, length);
2897 }
2898
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002899 bool CanBeMoved() const OVERRIDE { return true; }
2900 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002901 UNUSED(other);
2902 return true;
2903 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002904
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002905 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002906
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002907 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002908
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002909 uint32_t GetDexPc() const { return dex_pc_; }
2910
2911 DECLARE_INSTRUCTION(BoundsCheck);
2912
2913 private:
2914 const uint32_t dex_pc_;
2915
2916 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2917};
2918
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002919/**
2920 * Some DEX instructions are folded into multiple HInstructions that need
2921 * to stay live until the last HInstruction. This class
2922 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002923 * HInstruction stays live. `index` represents the stack location index of the
2924 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002925 */
2926class HTemporary : public HTemplateInstruction<0> {
2927 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002928 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002929
2930 size_t GetIndex() const { return index_; }
2931
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002932 Primitive::Type GetType() const OVERRIDE {
2933 // The previous instruction is the one that will be stored in the temporary location.
2934 DCHECK(GetPrevious() != nullptr);
2935 return GetPrevious()->GetType();
2936 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002937
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002938 DECLARE_INSTRUCTION(Temporary);
2939
2940 private:
2941 const size_t index_;
2942
2943 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2944};
2945
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002946class HSuspendCheck : public HTemplateInstruction<0> {
2947 public:
2948 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002949 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002950
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002951 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002952 return true;
2953 }
2954
2955 uint32_t GetDexPc() const { return dex_pc_; }
2956
2957 DECLARE_INSTRUCTION(SuspendCheck);
2958
2959 private:
2960 const uint32_t dex_pc_;
2961
2962 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2963};
2964
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002965/**
2966 * Instruction to load a Class object.
2967 */
2968class HLoadClass : public HExpression<0> {
2969 public:
2970 HLoadClass(uint16_t type_index,
2971 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002972 uint32_t dex_pc)
2973 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2974 type_index_(type_index),
2975 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002976 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00002977 generate_clinit_check_(false),
2978 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002979
2980 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002981
2982 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2983 return other->AsLoadClass()->type_index_ == type_index_;
2984 }
2985
2986 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2987
2988 uint32_t GetDexPc() const { return dex_pc_; }
2989 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002990 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002991
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002992 bool NeedsEnvironment() const OVERRIDE {
2993 // Will call runtime and load the class if the class is not loaded yet.
2994 // TODO: finer grain decision.
2995 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002996 }
2997
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002998 bool MustGenerateClinitCheck() const {
2999 return generate_clinit_check_;
3000 }
3001
3002 void SetMustGenerateClinitCheck() {
3003 generate_clinit_check_ = true;
3004 }
3005
3006 bool CanCallRuntime() const {
3007 return MustGenerateClinitCheck() || !is_referrers_class_;
3008 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003009
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003010 bool CanThrow() const OVERRIDE {
3011 // May call runtime and and therefore can throw.
3012 // TODO: finer grain decision.
3013 return !is_referrers_class_;
3014 }
3015
Calin Juravleacf735c2015-02-12 15:25:22 +00003016 ReferenceTypeInfo GetLoadedClassRTI() {
3017 return loaded_class_rti_;
3018 }
3019
3020 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3021 // Make sure we only set exact types (the loaded class should never be merged).
3022 DCHECK(rti.IsExact());
3023 loaded_class_rti_ = rti;
3024 }
3025
3026 bool IsResolved() {
3027 return loaded_class_rti_.IsExact();
3028 }
3029
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003030 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3031
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003032 DECLARE_INSTRUCTION(LoadClass);
3033
3034 private:
3035 const uint16_t type_index_;
3036 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003037 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003038 // Whether this instruction must generate the initialization check.
3039 // Used for code generation.
3040 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003041
Calin Juravleacf735c2015-02-12 15:25:22 +00003042 ReferenceTypeInfo loaded_class_rti_;
3043
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003044 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3045};
3046
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003047class HLoadString : public HExpression<0> {
3048 public:
3049 HLoadString(uint32_t string_index, uint32_t dex_pc)
3050 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3051 string_index_(string_index),
3052 dex_pc_(dex_pc) {}
3053
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003054 bool CanBeMoved() const OVERRIDE { return true; }
3055
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003056 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3057 return other->AsLoadString()->string_index_ == string_index_;
3058 }
3059
3060 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3061
3062 uint32_t GetDexPc() const { return dex_pc_; }
3063 uint32_t GetStringIndex() const { return string_index_; }
3064
3065 // TODO: Can we deopt or debug when we resolve a string?
3066 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003067 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003068
3069 DECLARE_INSTRUCTION(LoadString);
3070
3071 private:
3072 const uint32_t string_index_;
3073 const uint32_t dex_pc_;
3074
3075 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3076};
3077
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003078// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003079/**
3080 * Performs an initialization check on its Class object input.
3081 */
3082class HClinitCheck : public HExpression<1> {
3083 public:
3084 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
3085 : HExpression(Primitive::kPrimNot, SideEffects::All()),
3086 dex_pc_(dex_pc) {
3087 SetRawInputAt(0, constant);
3088 }
3089
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003090 bool CanBeMoved() const OVERRIDE { return true; }
3091 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3092 UNUSED(other);
3093 return true;
3094 }
3095
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003096 bool NeedsEnvironment() const OVERRIDE {
3097 // May call runtime to initialize the class.
3098 return true;
3099 }
3100
3101 uint32_t GetDexPc() const { return dex_pc_; }
3102
3103 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3104
3105 DECLARE_INSTRUCTION(ClinitCheck);
3106
3107 private:
3108 const uint32_t dex_pc_;
3109
3110 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3111};
3112
3113class HStaticFieldGet : public HExpression<1> {
3114 public:
3115 HStaticFieldGet(HInstruction* cls,
3116 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003117 MemberOffset field_offset,
3118 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003119 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003120 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003121 SetRawInputAt(0, cls);
3122 }
3123
Calin Juravle52c48962014-12-16 17:02:57 +00003124
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003125 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003126
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003127 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003128 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3129 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003130 }
3131
3132 size_t ComputeHashCode() const OVERRIDE {
3133 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3134 }
3135
Calin Juravle52c48962014-12-16 17:02:57 +00003136 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003137 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3138 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003139 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003140
3141 DECLARE_INSTRUCTION(StaticFieldGet);
3142
3143 private:
3144 const FieldInfo field_info_;
3145
3146 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3147};
3148
3149class HStaticFieldSet : public HTemplateInstruction<2> {
3150 public:
3151 HStaticFieldSet(HInstruction* cls,
3152 HInstruction* value,
3153 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003154 MemberOffset field_offset,
3155 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003156 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003157 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003158 SetRawInputAt(0, cls);
3159 SetRawInputAt(1, value);
3160 }
3161
Calin Juravle52c48962014-12-16 17:02:57 +00003162 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003163 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3164 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003165 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003166
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003167 HInstruction* GetValue() const { return InputAt(1); }
3168
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003169 DECLARE_INSTRUCTION(StaticFieldSet);
3170
3171 private:
3172 const FieldInfo field_info_;
3173
3174 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3175};
3176
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003177// Implement the move-exception DEX instruction.
3178class HLoadException : public HExpression<0> {
3179 public:
3180 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3181
3182 DECLARE_INSTRUCTION(LoadException);
3183
3184 private:
3185 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3186};
3187
3188class HThrow : public HTemplateInstruction<1> {
3189 public:
3190 HThrow(HInstruction* exception, uint32_t dex_pc)
3191 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3192 SetRawInputAt(0, exception);
3193 }
3194
3195 bool IsControlFlow() const OVERRIDE { return true; }
3196
3197 bool NeedsEnvironment() const OVERRIDE { return true; }
3198
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003199 bool CanThrow() const OVERRIDE { return true; }
3200
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003201 uint32_t GetDexPc() const { return dex_pc_; }
3202
3203 DECLARE_INSTRUCTION(Throw);
3204
3205 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003206 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003207
3208 DISALLOW_COPY_AND_ASSIGN(HThrow);
3209};
3210
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003211class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003212 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003213 HInstanceOf(HInstruction* object,
3214 HLoadClass* constant,
3215 bool class_is_final,
3216 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003217 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3218 class_is_final_(class_is_final),
3219 dex_pc_(dex_pc) {
3220 SetRawInputAt(0, object);
3221 SetRawInputAt(1, constant);
3222 }
3223
3224 bool CanBeMoved() const OVERRIDE { return true; }
3225
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003226 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003227 return true;
3228 }
3229
3230 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003231 return false;
3232 }
3233
3234 uint32_t GetDexPc() const { return dex_pc_; }
3235
3236 bool IsClassFinal() const { return class_is_final_; }
3237
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003238 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003239
3240 private:
3241 const bool class_is_final_;
3242 const uint32_t dex_pc_;
3243
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003244 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3245};
3246
Calin Juravleb1498f62015-02-16 13:13:29 +00003247class HBoundType : public HExpression<1> {
3248 public:
3249 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3250 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3251 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003252 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003253 SetRawInputAt(0, input);
3254 }
3255
3256 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3257
3258 bool CanBeNull() const OVERRIDE {
3259 // `null instanceof ClassX` always return false so we can't be null.
3260 return false;
3261 }
3262
3263 DECLARE_INSTRUCTION(BoundType);
3264
3265 private:
3266 // Encodes the most upper class that this instruction can have. In other words
3267 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3268 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3269 const ReferenceTypeInfo bound_type_;
3270
3271 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3272};
3273
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003274class HCheckCast : public HTemplateInstruction<2> {
3275 public:
3276 HCheckCast(HInstruction* object,
3277 HLoadClass* constant,
3278 bool class_is_final,
3279 uint32_t dex_pc)
3280 : HTemplateInstruction(SideEffects::None()),
3281 class_is_final_(class_is_final),
3282 dex_pc_(dex_pc) {
3283 SetRawInputAt(0, object);
3284 SetRawInputAt(1, constant);
3285 }
3286
3287 bool CanBeMoved() const OVERRIDE { return true; }
3288
3289 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3290 return true;
3291 }
3292
3293 bool NeedsEnvironment() const OVERRIDE {
3294 // Instruction may throw a CheckCastError.
3295 return true;
3296 }
3297
3298 bool CanThrow() const OVERRIDE { return true; }
3299
3300 uint32_t GetDexPc() const { return dex_pc_; }
3301
3302 bool IsClassFinal() const { return class_is_final_; }
3303
3304 DECLARE_INSTRUCTION(CheckCast);
3305
3306 private:
3307 const bool class_is_final_;
3308 const uint32_t dex_pc_;
3309
3310 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003311};
3312
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003313class HMonitorOperation : public HTemplateInstruction<1> {
3314 public:
3315 enum OperationKind {
3316 kEnter,
3317 kExit,
3318 };
3319
3320 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3321 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3322 SetRawInputAt(0, object);
3323 }
3324
3325 // Instruction may throw a Java exception, so we need an environment.
3326 bool NeedsEnvironment() const OVERRIDE { return true; }
3327 bool CanThrow() const OVERRIDE { return true; }
3328
3329 uint32_t GetDexPc() const { return dex_pc_; }
3330
3331 bool IsEnter() const { return kind_ == kEnter; }
3332
3333 DECLARE_INSTRUCTION(MonitorOperation);
3334
Calin Juravle52c48962014-12-16 17:02:57 +00003335 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003336 const OperationKind kind_;
3337 const uint32_t dex_pc_;
3338
3339 private:
3340 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3341};
3342
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003343class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003344 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003345 MoveOperands(Location source, Location destination, HInstruction* instruction)
3346 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003347
3348 Location GetSource() const { return source_; }
3349 Location GetDestination() const { return destination_; }
3350
3351 void SetSource(Location value) { source_ = value; }
3352 void SetDestination(Location value) { destination_ = value; }
3353
3354 // The parallel move resolver marks moves as "in-progress" by clearing the
3355 // destination (but not the source).
3356 Location MarkPending() {
3357 DCHECK(!IsPending());
3358 Location dest = destination_;
3359 destination_ = Location::NoLocation();
3360 return dest;
3361 }
3362
3363 void ClearPending(Location dest) {
3364 DCHECK(IsPending());
3365 destination_ = dest;
3366 }
3367
3368 bool IsPending() const {
3369 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3370 return destination_.IsInvalid() && !source_.IsInvalid();
3371 }
3372
3373 // True if this blocks a move from the given location.
3374 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003375 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003376 }
3377
3378 // A move is redundant if it's been eliminated, if its source and
3379 // destination are the same, or if its destination is unneeded.
3380 bool IsRedundant() const {
3381 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3382 }
3383
3384 // We clear both operands to indicate move that's been eliminated.
3385 void Eliminate() {
3386 source_ = destination_ = Location::NoLocation();
3387 }
3388
3389 bool IsEliminated() const {
3390 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3391 return source_.IsInvalid();
3392 }
3393
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003394 HInstruction* GetInstruction() const { return instruction_; }
3395
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003396 private:
3397 Location source_;
3398 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003399 // The instruction this move is assocatied with. Null when this move is
3400 // for moving an input in the expected locations of user (including a phi user).
3401 // This is only used in debug mode, to ensure we do not connect interval siblings
3402 // in the same parallel move.
3403 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003404};
3405
3406static constexpr size_t kDefaultNumberOfMoves = 4;
3407
3408class HParallelMove : public HTemplateInstruction<0> {
3409 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003410 explicit HParallelMove(ArenaAllocator* arena)
3411 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003412
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003413 void AddMove(Location source, Location destination, HInstruction* instruction) {
3414 DCHECK(source.IsValid());
3415 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003416 if (kIsDebugBuild) {
3417 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003418 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003419 if (moves_.Get(i).GetInstruction() == instruction) {
3420 // Special case the situation where the move is for the spill slot
3421 // of the instruction.
3422 if ((GetPrevious() == instruction)
3423 || ((GetPrevious() == nullptr)
3424 && instruction->IsPhi()
3425 && instruction->GetBlock() == GetBlock())) {
3426 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3427 << "Doing parallel moves for the same instruction.";
3428 } else {
3429 DCHECK(false) << "Doing parallel moves for the same instruction.";
3430 }
3431 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003432 }
3433 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003434 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3435 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3436 << "Same destination for two moves in a parallel move.";
3437 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003438 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003439 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003440 }
3441
3442 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003443 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003444 }
3445
3446 size_t NumMoves() const { return moves_.Size(); }
3447
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003448 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003449
3450 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003451 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003452
3453 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3454};
3455
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003456class HGraphVisitor : public ValueObject {
3457 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003458 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3459 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003460
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003461 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003462 virtual void VisitBasicBlock(HBasicBlock* block);
3463
Roland Levillain633021e2014-10-01 14:12:25 +01003464 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003465 void VisitInsertionOrder();
3466
Roland Levillain633021e2014-10-01 14:12:25 +01003467 // Visit the graph following dominator tree reverse post-order.
3468 void VisitReversePostOrder();
3469
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003470 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003471
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003472 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003473#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003474 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3475
3476 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3477
3478#undef DECLARE_VISIT_INSTRUCTION
3479
3480 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003481 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003482
3483 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3484};
3485
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003486class HGraphDelegateVisitor : public HGraphVisitor {
3487 public:
3488 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3489 virtual ~HGraphDelegateVisitor() {}
3490
3491 // Visit functions that delegate to to super class.
3492#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003493 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003494
3495 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3496
3497#undef DECLARE_VISIT_INSTRUCTION
3498
3499 private:
3500 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3501};
3502
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003503class HInsertionOrderIterator : public ValueObject {
3504 public:
3505 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3506
3507 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3508 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3509 void Advance() { ++index_; }
3510
3511 private:
3512 const HGraph& graph_;
3513 size_t index_;
3514
3515 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3516};
3517
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003518class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003519 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003520 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3521 // Check that reverse post order of the graph has been built.
3522 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3523 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003524
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003525 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3526 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003527 void Advance() { ++index_; }
3528
3529 private:
3530 const HGraph& graph_;
3531 size_t index_;
3532
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003533 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003534};
3535
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003536class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003537 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003538 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003539 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3540 // Check that reverse post order of the graph has been built.
3541 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3542 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003543
3544 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003545 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003546 void Advance() { --index_; }
3547
3548 private:
3549 const HGraph& graph_;
3550 size_t index_;
3551
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003552 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003553};
3554
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003555// Iterator over the blocks that art part of the loop. Includes blocks part
3556// of an inner loop. The order in which the blocks are iterated is on their
3557// block id.
3558class HBlocksInLoopIterator : public ValueObject {
3559 public:
3560 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3561 : blocks_in_loop_(info.GetBlocks()),
3562 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3563 index_(0) {
3564 if (!blocks_in_loop_.IsBitSet(index_)) {
3565 Advance();
3566 }
3567 }
3568
3569 bool Done() const { return index_ == blocks_.Size(); }
3570 HBasicBlock* Current() const { return blocks_.Get(index_); }
3571 void Advance() {
3572 ++index_;
3573 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3574 if (blocks_in_loop_.IsBitSet(index_)) {
3575 break;
3576 }
3577 }
3578 }
3579
3580 private:
3581 const BitVector& blocks_in_loop_;
3582 const GrowableArray<HBasicBlock*>& blocks_;
3583 size_t index_;
3584
3585 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3586};
3587
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003588inline int64_t Int64FromConstant(HConstant* constant) {
3589 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3590 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3591 : constant->AsLongConstant()->GetValue();
3592}
3593
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003594} // namespace art
3595
3596#endif // ART_COMPILER_OPTIMIZING_NODES_H_