blob: db7873b14e665a7e95527c911fdae121348bbacc [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
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000605 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000606 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000607 GrowableArray<HBasicBlock*> predecessors_;
608 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100609 HInstructionList instructions_;
610 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000611 HLoopInformation* loop_information_;
612 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100613 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000614 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100615 // The dex program counter of the first instruction of this block.
616 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100617 size_t lifetime_start_;
618 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000619 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000620
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000621 friend class HGraph;
622 friend class HInstruction;
623
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000624 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
625};
626
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100627#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
628 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000629 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000630 M(ArrayGet, Instruction) \
631 M(ArrayLength, Instruction) \
632 M(ArraySet, Instruction) \
633 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000634 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000635 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100636 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000637 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100638 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000639 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000640 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000641 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100642 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000643 M(Exit, Instruction) \
644 M(FloatConstant, Constant) \
645 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100646 M(GreaterThan, Condition) \
647 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100648 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000649 M(InstanceFieldGet, Instruction) \
650 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000651 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100652 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000653 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000654 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100655 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000656 M(LessThan, Condition) \
657 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000658 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000659 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100660 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000661 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100662 M(Local, Instruction) \
663 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000664 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000665 M(Mul, BinaryOperation) \
666 M(Neg, UnaryOperation) \
667 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100668 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100669 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000670 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000671 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000672 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000673 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100674 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000675 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100676 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000677 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100678 M(Return, Instruction) \
679 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000680 M(Shl, BinaryOperation) \
681 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100682 M(StaticFieldGet, Instruction) \
683 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100684 M(StoreLocal, Instruction) \
685 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100686 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000687 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000688 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000689 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000690 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000691 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000692
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100693#define FOR_EACH_INSTRUCTION(M) \
694 FOR_EACH_CONCRETE_INSTRUCTION(M) \
695 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100696 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100697 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100698 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700699
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100700#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000701FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
702#undef FORWARD_DECLARATION
703
Roland Levillainccc07a92014-09-16 14:48:16 +0100704#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000705 InstructionKind GetKind() const OVERRIDE { return k##type; } \
706 const char* DebugName() const OVERRIDE { return #type; } \
707 const H##type* As##type() const OVERRIDE { return this; } \
708 H##type* As##type() OVERRIDE { return this; } \
709 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100710 return other->Is##type(); \
711 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000712 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000713
David Brazdiled596192015-01-23 10:39:45 +0000714template <typename T> class HUseList;
715
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100716template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700717class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000718 public:
David Brazdiled596192015-01-23 10:39:45 +0000719 HUseListNode* GetPrevious() const { return prev_; }
720 HUseListNode* GetNext() const { return next_; }
721 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100722 size_t GetIndex() const { return index_; }
723
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000724 private:
David Brazdiled596192015-01-23 10:39:45 +0000725 HUseListNode(T user, size_t index)
726 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
727
728 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100729 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000730 HUseListNode<T>* prev_;
731 HUseListNode<T>* next_;
732
733 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000734
735 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
736};
737
David Brazdiled596192015-01-23 10:39:45 +0000738template <typename T>
739class HUseList : public ValueObject {
740 public:
741 HUseList() : first_(nullptr) {}
742
743 void Clear() {
744 first_ = nullptr;
745 }
746
747 // Adds a new entry at the beginning of the use list and returns
748 // the newly created node.
749 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000750 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000751 if (IsEmpty()) {
752 first_ = new_node;
753 } else {
754 first_->prev_ = new_node;
755 new_node->next_ = first_;
756 first_ = new_node;
757 }
758 return new_node;
759 }
760
761 HUseListNode<T>* GetFirst() const {
762 return first_;
763 }
764
765 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000766 DCHECK(node != nullptr);
767 DCHECK(Contains(node));
768
David Brazdiled596192015-01-23 10:39:45 +0000769 if (node->prev_ != nullptr) {
770 node->prev_->next_ = node->next_;
771 }
772 if (node->next_ != nullptr) {
773 node->next_->prev_ = node->prev_;
774 }
775 if (node == first_) {
776 first_ = node->next_;
777 }
778 }
779
David Brazdil1abb4192015-02-17 18:33:36 +0000780 bool Contains(const HUseListNode<T>* node) const {
781 if (node == nullptr) {
782 return false;
783 }
784 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
785 if (current == node) {
786 return true;
787 }
788 }
789 return false;
790 }
791
David Brazdiled596192015-01-23 10:39:45 +0000792 bool IsEmpty() const {
793 return first_ == nullptr;
794 }
795
796 bool HasOnlyOneUse() const {
797 return first_ != nullptr && first_->next_ == nullptr;
798 }
799
800 private:
801 HUseListNode<T>* first_;
802};
803
804template<typename T>
805class HUseIterator : public ValueObject {
806 public:
807 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
808
809 bool Done() const { return current_ == nullptr; }
810
811 void Advance() {
812 DCHECK(!Done());
813 current_ = current_->GetNext();
814 }
815
816 HUseListNode<T>* Current() const {
817 DCHECK(!Done());
818 return current_;
819 }
820
821 private:
822 HUseListNode<T>* current_;
823
824 friend class HValue;
825};
826
David Brazdil1abb4192015-02-17 18:33:36 +0000827// This class is used by HEnvironment and HInstruction classes to record the
828// instructions they use and pointers to the corresponding HUseListNodes kept
829// by the used instructions.
830template <typename T>
831class HUserRecord : public ValueObject {
832 public:
833 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
834 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
835
836 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
837 : instruction_(old_record.instruction_), use_node_(use_node) {
838 DCHECK(instruction_ != nullptr);
839 DCHECK(use_node_ != nullptr);
840 DCHECK(old_record.use_node_ == nullptr);
841 }
842
843 HInstruction* GetInstruction() const { return instruction_; }
844 HUseListNode<T>* GetUseNode() const { return use_node_; }
845
846 private:
847 // Instruction used by the user.
848 HInstruction* instruction_;
849
850 // Corresponding entry in the use list kept by 'instruction_'.
851 HUseListNode<T>* use_node_;
852};
853
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100854// Represents the side effects an instruction may have.
855class SideEffects : public ValueObject {
856 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100857 SideEffects() : flags_(0) {}
858
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100859 static SideEffects None() {
860 return SideEffects(0);
861 }
862
863 static SideEffects All() {
864 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
865 }
866
867 static SideEffects ChangesSomething() {
868 return SideEffects((1 << kFlagChangesCount) - 1);
869 }
870
871 static SideEffects DependsOnSomething() {
872 int count = kFlagDependsOnCount - kFlagChangesCount;
873 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
874 }
875
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100876 SideEffects Union(SideEffects other) const {
877 return SideEffects(flags_ | other.flags_);
878 }
879
Roland Levillain72bceff2014-09-15 18:29:00 +0100880 bool HasSideEffects() const {
881 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
882 return (flags_ & all_bits_set) != 0;
883 }
884
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100885 bool HasAllSideEffects() const {
886 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
887 return all_bits_set == (flags_ & all_bits_set);
888 }
889
890 bool DependsOn(SideEffects other) const {
891 size_t depends_flags = other.ComputeDependsFlags();
892 return (flags_ & depends_flags) != 0;
893 }
894
895 bool HasDependencies() const {
896 int count = kFlagDependsOnCount - kFlagChangesCount;
897 size_t all_bits_set = (1 << count) - 1;
898 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
899 }
900
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100901 private:
902 static constexpr int kFlagChangesSomething = 0;
903 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
904
905 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
906 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
907
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100908 explicit SideEffects(size_t flags) : flags_(flags) {}
909
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100910 size_t ComputeDependsFlags() const {
911 return flags_ << kFlagChangesCount;
912 }
913
914 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100915};
916
David Brazdiled596192015-01-23 10:39:45 +0000917// A HEnvironment object contains the values of virtual registers at a given location.
918class HEnvironment : public ArenaObject<kArenaAllocMisc> {
919 public:
920 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
921 : vregs_(arena, number_of_vregs) {
922 vregs_.SetSize(number_of_vregs);
923 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000924 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000925 }
926 }
927
928 void CopyFrom(HEnvironment* env);
929
930 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +0000931 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +0000932 }
933
934 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +0000935 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +0000936 }
937
David Brazdil1abb4192015-02-17 18:33:36 +0000938 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +0000939
940 size_t Size() const { return vregs_.Size(); }
941
942 private:
David Brazdil1abb4192015-02-17 18:33:36 +0000943 // Record instructions' use entries of this environment for constant-time removal.
944 // It should only be called by HInstruction when a new environment use is added.
945 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
946 DCHECK(env_use->GetUser() == this);
947 size_t index = env_use->GetIndex();
948 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
949 }
David Brazdiled596192015-01-23 10:39:45 +0000950
David Brazdil1abb4192015-02-17 18:33:36 +0000951 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +0000952
David Brazdil1abb4192015-02-17 18:33:36 +0000953 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +0000954
955 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
956};
957
Calin Juravleacf735c2015-02-12 15:25:22 +0000958class ReferenceTypeInfo : ValueObject {
959 public:
Calin Juravleb1498f62015-02-16 13:13:29 +0000960 typedef Handle<mirror::Class> TypeHandle;
961
962 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
963 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
964 if (type_handle->IsObjectClass()) {
965 // Override the type handle to be consistent with the case when we get to
966 // Top but don't have the Object class available. It avoids having to guess
967 // what value the type_handle has when it's Top.
968 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
969 } else {
970 return ReferenceTypeInfo(type_handle, is_exact, false);
971 }
972 }
973
974 static ReferenceTypeInfo CreateTop(bool is_exact) {
975 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +0000976 }
977
978 bool IsExact() const { return is_exact_; }
979 bool IsTop() const { return is_top_; }
980
981 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
982
Calin Juravleb1498f62015-02-16 13:13:29 +0000983 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000984 if (IsTop()) {
985 // Top (equivalent for java.lang.Object) is supertype of anything.
986 return true;
987 }
988 if (rti.IsTop()) {
989 // If we get here `this` is not Top() so it can't be a supertype.
990 return false;
991 }
992 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
993 }
994
995 // Returns true if the type information provide the same amount of details.
996 // Note that it does not mean that the instructions have the same actual type
997 // (e.g. tops are equal but they can be the result of a merge).
998 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
999 if (IsExact() != rti.IsExact()) {
1000 return false;
1001 }
1002 if (IsTop() && rti.IsTop()) {
1003 // `Top` means java.lang.Object, so the types are equivalent.
1004 return true;
1005 }
1006 if (IsTop() || rti.IsTop()) {
1007 // If only one is top or object than they are not equivalent.
1008 // NB: We need this extra check because the type_handle of `Top` is invalid
1009 // and we cannot inspect its reference.
1010 return false;
1011 }
1012
1013 // Finally check the types.
1014 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1015 }
1016
1017 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001018 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1019 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1020 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1021
Calin Juravleacf735c2015-02-12 15:25:22 +00001022 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001023 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001024 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001025 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001026 bool is_exact_;
1027 // A true value here means that the object type should be java.lang.Object.
1028 // We don't have access to the corresponding mirror object every time so this
1029 // flag acts as a substitute. When true, the TypeHandle refers to a null
1030 // pointer and should not be used.
1031 bool is_top_;
1032};
1033
1034std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1035
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001036class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001037 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001038 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001039 : previous_(nullptr),
1040 next_(nullptr),
1041 block_(nullptr),
1042 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001043 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001044 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001045 locations_(nullptr),
1046 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001047 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001048 side_effects_(side_effects),
1049 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001050
Dave Allison20dfc792014-06-16 20:44:29 -07001051 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001052
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001053#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001054 enum InstructionKind {
1055 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1056 };
1057#undef DECLARE_KIND
1058
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001059 HInstruction* GetNext() const { return next_; }
1060 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001061
Calin Juravle77520bc2015-01-12 18:45:46 +00001062 HInstruction* GetNextDisregardingMoves() const;
1063 HInstruction* GetPreviousDisregardingMoves() const;
1064
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001065 HBasicBlock* GetBlock() const { return block_; }
1066 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001067 bool IsInBlock() const { return block_ != nullptr; }
1068 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001069 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001070
Roland Levillain6b879dd2014-09-22 17:13:44 +01001071 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001072 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001073
1074 virtual void Accept(HGraphVisitor* visitor) = 0;
1075 virtual const char* DebugName() const = 0;
1076
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001077 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001078 void SetRawInputAt(size_t index, HInstruction* input) {
1079 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1080 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001081
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001082 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001083 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001084 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001085 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001086
Calin Juravle61d544b2015-02-23 16:46:57 +00001087 virtual bool ActAsNullConstant() const { return false; }
1088
Calin Juravle10e244f2015-01-26 18:54:32 +00001089 // Does not apply for all instructions, but having this at top level greatly
1090 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001091 virtual bool CanBeNull() const {
1092 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1093 return true;
1094 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001095
Calin Juravle77520bc2015-01-12 18:45:46 +00001096 virtual bool CanDoImplicitNullCheck() const { return false; }
1097
Calin Juravleacf735c2015-02-12 15:25:22 +00001098 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001099 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001100 reference_type_info_ = reference_type_info;
1101 }
1102
Calin Juravle61d544b2015-02-23 16:46:57 +00001103 ReferenceTypeInfo GetReferenceTypeInfo() const {
1104 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1105 return reference_type_info_;
1106 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001107
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001108 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001109 DCHECK(user != nullptr);
1110 HUseListNode<HInstruction*>* use =
1111 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1112 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001113 }
1114
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001115 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001116 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001117 HUseListNode<HEnvironment*>* env_use =
1118 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1119 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001120 }
1121
David Brazdil1abb4192015-02-17 18:33:36 +00001122 void RemoveAsUserOfInput(size_t input) {
1123 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1124 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1125 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001126
David Brazdil1abb4192015-02-17 18:33:36 +00001127 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1128 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001129
David Brazdiled596192015-01-23 10:39:45 +00001130 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1131 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001132 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001133
Roland Levillain6c82d402014-10-13 16:10:27 +01001134 // Does this instruction strictly dominate `other_instruction`?
1135 // Returns false if this instruction and `other_instruction` are the same.
1136 // Aborts if this instruction and `other_instruction` are both phis.
1137 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001138
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001139 int GetId() const { return id_; }
1140 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001141
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001142 int GetSsaIndex() const { return ssa_index_; }
1143 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1144 bool HasSsaIndex() const { return ssa_index_ != -1; }
1145
1146 bool HasEnvironment() const { return environment_ != nullptr; }
1147 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001148 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
1149
Nicolas Geoffray39468442014-09-02 15:17:15 +01001150 // Returns the number of entries in the environment. Typically, that is the
1151 // number of dex registers in a method. It could be more in case of inlining.
1152 size_t EnvironmentSize() const;
1153
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001154 LocationSummary* GetLocations() const { return locations_; }
1155 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001156
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001157 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001158 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001159
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001160 // Move `this` instruction before `cursor`.
1161 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001162
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001163#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001164 bool Is##type() const { return (As##type() != nullptr); } \
1165 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001166 virtual H##type* As##type() { return nullptr; }
1167
1168 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1169#undef INSTRUCTION_TYPE_CHECK
1170
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001171 // Returns whether the instruction can be moved within the graph.
1172 virtual bool CanBeMoved() const { return false; }
1173
1174 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001175 virtual bool InstructionTypeEquals(HInstruction* other) const {
1176 UNUSED(other);
1177 return false;
1178 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001179
1180 // Returns whether any data encoded in the two instructions is equal.
1181 // This method does not look at the inputs. Both instructions must be
1182 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001183 virtual bool InstructionDataEquals(HInstruction* other) const {
1184 UNUSED(other);
1185 return false;
1186 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001187
1188 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001189 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001190 // 2) Their inputs are identical.
1191 bool Equals(HInstruction* other) const;
1192
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001193 virtual InstructionKind GetKind() const = 0;
1194
1195 virtual size_t ComputeHashCode() const {
1196 size_t result = GetKind();
1197 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1198 result = (result * 31) + InputAt(i)->GetId();
1199 }
1200 return result;
1201 }
1202
1203 SideEffects GetSideEffects() const { return side_effects_; }
1204
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001205 size_t GetLifetimePosition() const { return lifetime_position_; }
1206 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1207 LiveInterval* GetLiveInterval() const { return live_interval_; }
1208 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1209 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1210
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001211 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1212
1213 // Returns whether the code generation of the instruction will require to have access
1214 // to the current method. Such instructions are:
1215 // (1): Instructions that require an environment, as calling the runtime requires
1216 // to walk the stack and have the current method stored at a specific stack address.
1217 // (2): Object literals like classes and strings, that are loaded from the dex cache
1218 // fields of the current method.
1219 bool NeedsCurrentMethod() const {
1220 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1221 }
1222
David Brazdil1abb4192015-02-17 18:33:36 +00001223 protected:
1224 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1225 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1226
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001227 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001228 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1229
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001230 HInstruction* previous_;
1231 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001232 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001233
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001234 // An instruction gets an id when it is added to the graph.
1235 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001236 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001237 int id_;
1238
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001239 // When doing liveness analysis, instructions that have uses get an SSA index.
1240 int ssa_index_;
1241
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001242 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001243 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001244
1245 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001246 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001247
Nicolas Geoffray39468442014-09-02 15:17:15 +01001248 // The environment associated with this instruction. Not null if the instruction
1249 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001250 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001251
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001252 // Set by the code generator.
1253 LocationSummary* locations_;
1254
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001255 // Set by the liveness analysis.
1256 LiveInterval* live_interval_;
1257
1258 // Set by the liveness analysis, this is the position in a linear
1259 // order of blocks where this instruction's live interval start.
1260 size_t lifetime_position_;
1261
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001262 const SideEffects side_effects_;
1263
Calin Juravleacf735c2015-02-12 15:25:22 +00001264 // TODO: for primitive types this should be marked as invalid.
1265 ReferenceTypeInfo reference_type_info_;
1266
David Brazdil1abb4192015-02-17 18:33:36 +00001267 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001268 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001269 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001270 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001271 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001272
1273 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1274};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001275std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001276
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001277class HInputIterator : public ValueObject {
1278 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001279 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001280
1281 bool Done() const { return index_ == instruction_->InputCount(); }
1282 HInstruction* Current() const { return instruction_->InputAt(index_); }
1283 void Advance() { index_++; }
1284
1285 private:
1286 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001287 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001288
1289 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1290};
1291
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001292class HInstructionIterator : public ValueObject {
1293 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001294 explicit HInstructionIterator(const HInstructionList& instructions)
1295 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001296 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001297 }
1298
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001299 bool Done() const { return instruction_ == nullptr; }
1300 HInstruction* Current() const { return instruction_; }
1301 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001302 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001303 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001304 }
1305
1306 private:
1307 HInstruction* instruction_;
1308 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001309
1310 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001311};
1312
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001313class HBackwardInstructionIterator : public ValueObject {
1314 public:
1315 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1316 : instruction_(instructions.last_instruction_) {
1317 next_ = Done() ? nullptr : instruction_->GetPrevious();
1318 }
1319
1320 bool Done() const { return instruction_ == nullptr; }
1321 HInstruction* Current() const { return instruction_; }
1322 void Advance() {
1323 instruction_ = next_;
1324 next_ = Done() ? nullptr : instruction_->GetPrevious();
1325 }
1326
1327 private:
1328 HInstruction* instruction_;
1329 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001330
1331 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001332};
1333
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001334// An embedded container with N elements of type T. Used (with partial
1335// specialization for N=0) because embedded arrays cannot have size 0.
1336template<typename T, intptr_t N>
1337class EmbeddedArray {
1338 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001339 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001340
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001341 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001342
1343 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001344 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001345 return elements_[i];
1346 }
1347
1348 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001349 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001350 return elements_[i];
1351 }
1352
1353 const T& At(intptr_t i) const {
1354 return (*this)[i];
1355 }
1356
1357 void SetAt(intptr_t i, const T& val) {
1358 (*this)[i] = val;
1359 }
1360
1361 private:
1362 T elements_[N];
1363};
1364
1365template<typename T>
1366class EmbeddedArray<T, 0> {
1367 public:
1368 intptr_t length() const { return 0; }
1369 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001370 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001371 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001372 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001373 }
1374 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001375 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001376 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001377 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001378 }
1379};
1380
1381template<intptr_t N>
1382class HTemplateInstruction: public HInstruction {
1383 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001384 HTemplateInstruction<N>(SideEffects side_effects)
1385 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001386 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001387
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001388 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001389
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001390 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001391 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1392
1393 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1394 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001395 }
1396
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001397 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001398 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001399
1400 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001401};
1402
Dave Allison20dfc792014-06-16 20:44:29 -07001403template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001404class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001405 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001406 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1407 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001408 virtual ~HExpression() {}
1409
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001410 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001411
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001412 protected:
1413 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001414};
1415
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001416// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1417// instruction that branches to the exit block.
1418class HReturnVoid : public HTemplateInstruction<0> {
1419 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001420 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001421
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001422 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001423
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001424 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001425
1426 private:
1427 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1428};
1429
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001430// Represents dex's RETURN opcodes. A HReturn is a control flow
1431// instruction that branches to the exit block.
1432class HReturn : public HTemplateInstruction<1> {
1433 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001434 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001435 SetRawInputAt(0, value);
1436 }
1437
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001438 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001439
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001440 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001441
1442 private:
1443 DISALLOW_COPY_AND_ASSIGN(HReturn);
1444};
1445
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001446// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001447// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001448// exit block.
1449class HExit : public HTemplateInstruction<0> {
1450 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001451 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001452
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001453 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001454
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001455 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001456
1457 private:
1458 DISALLOW_COPY_AND_ASSIGN(HExit);
1459};
1460
1461// Jumps from one block to another.
1462class HGoto : public HTemplateInstruction<0> {
1463 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001464 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1465
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001466 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001467
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001468 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001469 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001470 }
1471
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001472 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001473
1474 private:
1475 DISALLOW_COPY_AND_ASSIGN(HGoto);
1476};
1477
Dave Allison20dfc792014-06-16 20:44:29 -07001478
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001479// Conditional branch. A block ending with an HIf instruction must have
1480// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001481class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001482 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001483 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001484 SetRawInputAt(0, input);
1485 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001486
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001487 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001488
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001489 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001490 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001491 }
1492
1493 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001494 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001495 }
1496
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001497 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001498
Dave Allison20dfc792014-06-16 20:44:29 -07001499 virtual bool IsIfInstruction() const { return true; }
1500
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001501 private:
1502 DISALLOW_COPY_AND_ASSIGN(HIf);
1503};
1504
Roland Levillain88cb1752014-10-20 16:36:47 +01001505class HUnaryOperation : public HExpression<1> {
1506 public:
1507 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1508 : HExpression(result_type, SideEffects::None()) {
1509 SetRawInputAt(0, input);
1510 }
1511
1512 HInstruction* GetInput() const { return InputAt(0); }
1513 Primitive::Type GetResultType() const { return GetType(); }
1514
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001515 bool CanBeMoved() const OVERRIDE { return true; }
1516 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001517 UNUSED(other);
1518 return true;
1519 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001520
Roland Levillain9240d6a2014-10-20 16:47:04 +01001521 // Try to statically evaluate `operation` and return a HConstant
1522 // containing the result of this evaluation. If `operation` cannot
1523 // be evaluated as a constant, return nullptr.
1524 HConstant* TryStaticEvaluation() const;
1525
1526 // Apply this operation to `x`.
1527 virtual int32_t Evaluate(int32_t x) const = 0;
1528 virtual int64_t Evaluate(int64_t x) const = 0;
1529
Roland Levillain88cb1752014-10-20 16:36:47 +01001530 DECLARE_INSTRUCTION(UnaryOperation);
1531
1532 private:
1533 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1534};
1535
Dave Allison20dfc792014-06-16 20:44:29 -07001536class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001537 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001538 HBinaryOperation(Primitive::Type result_type,
1539 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001540 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001541 SetRawInputAt(0, left);
1542 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001543 }
1544
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001545 HInstruction* GetLeft() const { return InputAt(0); }
1546 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001547 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001548
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001549 virtual bool IsCommutative() const { return false; }
1550
1551 // Put constant on the right.
1552 // Returns whether order is changed.
1553 bool OrderInputsWithConstantOnTheRight() {
1554 HInstruction* left = InputAt(0);
1555 HInstruction* right = InputAt(1);
1556 if (left->IsConstant() && !right->IsConstant()) {
1557 ReplaceInput(right, 0);
1558 ReplaceInput(left, 1);
1559 return true;
1560 }
1561 return false;
1562 }
1563
1564 // Order inputs by instruction id, but favor constant on the right side.
1565 // This helps GVN for commutative ops.
1566 void OrderInputs() {
1567 DCHECK(IsCommutative());
1568 HInstruction* left = InputAt(0);
1569 HInstruction* right = InputAt(1);
1570 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1571 return;
1572 }
1573 if (OrderInputsWithConstantOnTheRight()) {
1574 return;
1575 }
1576 // Order according to instruction id.
1577 if (left->GetId() > right->GetId()) {
1578 ReplaceInput(right, 0);
1579 ReplaceInput(left, 1);
1580 }
1581 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001582
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001583 bool CanBeMoved() const OVERRIDE { return true; }
1584 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001585 UNUSED(other);
1586 return true;
1587 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001588
Roland Levillain9240d6a2014-10-20 16:47:04 +01001589 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001590 // containing the result of this evaluation. If `operation` cannot
1591 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001592 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001593
1594 // Apply this operation to `x` and `y`.
1595 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1596 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1597
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001598 // Returns an input that can legally be used as the right input and is
1599 // constant, or nullptr.
1600 HConstant* GetConstantRight() const;
1601
1602 // If `GetConstantRight()` returns one of the input, this returns the other
1603 // one. Otherwise it returns nullptr.
1604 HInstruction* GetLeastConstantLeft() const;
1605
Roland Levillainccc07a92014-09-16 14:48:16 +01001606 DECLARE_INSTRUCTION(BinaryOperation);
1607
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001608 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001609 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1610};
1611
Dave Allison20dfc792014-06-16 20:44:29 -07001612class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001613 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001614 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001615 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1616 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001617
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001618 bool NeedsMaterialization() const { return needs_materialization_; }
1619 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001620
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001621 // For code generation purposes, returns whether this instruction is just before
1622 // `if_`, and disregard moves in between.
1623 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1624
Dave Allison20dfc792014-06-16 20:44:29 -07001625 DECLARE_INSTRUCTION(Condition);
1626
1627 virtual IfCondition GetCondition() const = 0;
1628
1629 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001630 // For register allocation purposes, returns whether this instruction needs to be
1631 // materialized (that is, not just be in the processor flags).
1632 bool needs_materialization_;
1633
Dave Allison20dfc792014-06-16 20:44:29 -07001634 DISALLOW_COPY_AND_ASSIGN(HCondition);
1635};
1636
1637// Instruction to check if two inputs are equal to each other.
1638class HEqual : public HCondition {
1639 public:
1640 HEqual(HInstruction* first, HInstruction* second)
1641 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001642
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001643 bool IsCommutative() const OVERRIDE { return true; }
1644
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001645 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001646 return x == y ? 1 : 0;
1647 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001648 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001649 return x == y ? 1 : 0;
1650 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001651
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001652 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001653
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001654 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001655 return kCondEQ;
1656 }
1657
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001658 private:
1659 DISALLOW_COPY_AND_ASSIGN(HEqual);
1660};
1661
Dave Allison20dfc792014-06-16 20:44:29 -07001662class HNotEqual : public HCondition {
1663 public:
1664 HNotEqual(HInstruction* first, HInstruction* second)
1665 : HCondition(first, second) {}
1666
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001667 bool IsCommutative() const OVERRIDE { return true; }
1668
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001669 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001670 return x != y ? 1 : 0;
1671 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001672 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001673 return x != y ? 1 : 0;
1674 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001675
Dave Allison20dfc792014-06-16 20:44:29 -07001676 DECLARE_INSTRUCTION(NotEqual);
1677
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001678 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001679 return kCondNE;
1680 }
1681
1682 private:
1683 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1684};
1685
1686class HLessThan : public HCondition {
1687 public:
1688 HLessThan(HInstruction* first, HInstruction* second)
1689 : HCondition(first, second) {}
1690
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001691 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001692 return x < y ? 1 : 0;
1693 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001694 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001695 return x < y ? 1 : 0;
1696 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001697
Dave Allison20dfc792014-06-16 20:44:29 -07001698 DECLARE_INSTRUCTION(LessThan);
1699
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001700 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001701 return kCondLT;
1702 }
1703
1704 private:
1705 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1706};
1707
1708class HLessThanOrEqual : public HCondition {
1709 public:
1710 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1711 : HCondition(first, second) {}
1712
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001713 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001714 return x <= y ? 1 : 0;
1715 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001716 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001717 return x <= y ? 1 : 0;
1718 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001719
Dave Allison20dfc792014-06-16 20:44:29 -07001720 DECLARE_INSTRUCTION(LessThanOrEqual);
1721
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001722 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001723 return kCondLE;
1724 }
1725
1726 private:
1727 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1728};
1729
1730class HGreaterThan : public HCondition {
1731 public:
1732 HGreaterThan(HInstruction* first, HInstruction* second)
1733 : HCondition(first, second) {}
1734
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001735 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001736 return x > y ? 1 : 0;
1737 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001738 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001739 return x > y ? 1 : 0;
1740 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001741
Dave Allison20dfc792014-06-16 20:44:29 -07001742 DECLARE_INSTRUCTION(GreaterThan);
1743
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001744 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001745 return kCondGT;
1746 }
1747
1748 private:
1749 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1750};
1751
1752class HGreaterThanOrEqual : public HCondition {
1753 public:
1754 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1755 : HCondition(first, second) {}
1756
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001757 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001758 return x >= y ? 1 : 0;
1759 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001760 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001761 return x >= y ? 1 : 0;
1762 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001763
Dave Allison20dfc792014-06-16 20:44:29 -07001764 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1765
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001766 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001767 return kCondGE;
1768 }
1769
1770 private:
1771 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1772};
1773
1774
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001775// Instruction to check how two inputs compare to each other.
1776// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1777class HCompare : public HBinaryOperation {
1778 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001779 // The bias applies for floating point operations and indicates how NaN
1780 // comparisons are treated:
1781 enum Bias {
1782 kNoBias, // bias is not applicable (i.e. for long operation)
1783 kGtBias, // return 1 for NaN comparisons
1784 kLtBias, // return -1 for NaN comparisons
1785 };
1786
1787 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1788 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001789 DCHECK_EQ(type, first->GetType());
1790 DCHECK_EQ(type, second->GetType());
1791 }
1792
Calin Juravleddb7df22014-11-25 20:56:51 +00001793 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001794 return
1795 x == y ? 0 :
1796 x > y ? 1 :
1797 -1;
1798 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001799
1800 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001801 return
1802 x == y ? 0 :
1803 x > y ? 1 :
1804 -1;
1805 }
1806
Calin Juravleddb7df22014-11-25 20:56:51 +00001807 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1808 return bias_ == other->AsCompare()->bias_;
1809 }
1810
1811 bool IsGtBias() { return bias_ == kGtBias; }
1812
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001813 DECLARE_INSTRUCTION(Compare);
1814
1815 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001816 const Bias bias_;
1817
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001818 DISALLOW_COPY_AND_ASSIGN(HCompare);
1819};
1820
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001821// A local in the graph. Corresponds to a Dex register.
1822class HLocal : public HTemplateInstruction<0> {
1823 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001824 explicit HLocal(uint16_t reg_number)
1825 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001826
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001827 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001828
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001829 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001830
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001831 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001832 // The Dex register number.
1833 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001834
1835 DISALLOW_COPY_AND_ASSIGN(HLocal);
1836};
1837
1838// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001839class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001840 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001841 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001842 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001843 SetRawInputAt(0, local);
1844 }
1845
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001846 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1847
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001848 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001849
1850 private:
1851 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1852};
1853
1854// Store a value in a given local. This instruction has two inputs: the value
1855// and the local.
1856class HStoreLocal : public HTemplateInstruction<2> {
1857 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001858 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001859 SetRawInputAt(0, local);
1860 SetRawInputAt(1, value);
1861 }
1862
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001863 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1864
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001865 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001866
1867 private:
1868 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1869};
1870
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001871class HConstant : public HExpression<0> {
1872 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001873 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1874
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001875 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001876
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001877 virtual bool IsMinusOne() const { return false; }
1878 virtual bool IsZero() const { return false; }
1879 virtual bool IsOne() const { return false; }
1880
1881 static HConstant* NewConstant(ArenaAllocator* allocator, Primitive::Type type, int64_t val);
1882
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001883 DECLARE_INSTRUCTION(Constant);
1884
1885 private:
1886 DISALLOW_COPY_AND_ASSIGN(HConstant);
1887};
1888
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001889class HFloatConstant : public HConstant {
1890 public:
1891 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1892
1893 float GetValue() const { return value_; }
1894
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001895 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001896 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
1897 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001898 }
1899
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001900 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001901
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001902 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001903 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
1904 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001905 }
1906 bool IsZero() const OVERRIDE {
1907 return AsFloatConstant()->GetValue() == 0.0f;
1908 }
1909 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001910 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
1911 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001912 }
1913
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001914 DECLARE_INSTRUCTION(FloatConstant);
1915
1916 private:
1917 const float value_;
1918
1919 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1920};
1921
1922class HDoubleConstant : public HConstant {
1923 public:
1924 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1925
1926 double GetValue() const { return value_; }
1927
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001928 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001929 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
1930 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001931 }
1932
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001933 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001934
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001935 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001936 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
1937 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001938 }
1939 bool IsZero() const OVERRIDE {
1940 return AsDoubleConstant()->GetValue() == 0.0;
1941 }
1942 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001943 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
1944 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001945 }
1946
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001947 DECLARE_INSTRUCTION(DoubleConstant);
1948
1949 private:
1950 const double value_;
1951
1952 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1953};
1954
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001955class HNullConstant : public HConstant {
1956 public:
1957 HNullConstant() : HConstant(Primitive::kPrimNot) {}
1958
1959 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
1960 return true;
1961 }
1962
1963 size_t ComputeHashCode() const OVERRIDE { return 0; }
1964
Calin Juravle61d544b2015-02-23 16:46:57 +00001965 bool ActAsNullConstant() const OVERRIDE { return true; }
1966
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001967 DECLARE_INSTRUCTION(NullConstant);
1968
1969 private:
1970 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
1971};
1972
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001973// Constants of the type int. Those can be from Dex instructions, or
1974// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001975class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001976 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001977 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001978
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001979 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001980
Calin Juravle61d544b2015-02-23 16:46:57 +00001981 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001982 return other->AsIntConstant()->value_ == value_;
1983 }
1984
Calin Juravle61d544b2015-02-23 16:46:57 +00001985 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
1986
1987 // TODO: Null is represented by the `0` constant. In most cases we replace it
1988 // with a HNullConstant but we don't do it when comparing (a != null). This
1989 // method is an workaround until we fix the above.
1990 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001991
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001992 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
1993 bool IsZero() const OVERRIDE { return GetValue() == 0; }
1994 bool IsOne() const OVERRIDE { return GetValue() == 1; }
1995
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001996 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001997
1998 private:
1999 const int32_t value_;
2000
2001 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2002};
2003
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002004class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002005 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002006 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002007
2008 int64_t GetValue() const { return value_; }
2009
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002010 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002011 return other->AsLongConstant()->value_ == value_;
2012 }
2013
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002014 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002015
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002016 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2017 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2018 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2019
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002020 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002021
2022 private:
2023 const int64_t value_;
2024
2025 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2026};
2027
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002028enum class Intrinsics {
2029#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2030#include "intrinsics_list.h"
2031 kNone,
2032 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2033#undef INTRINSICS_LIST
2034#undef OPTIMIZING_INTRINSICS
2035};
2036std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2037
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002038class HInvoke : public HInstruction {
2039 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002040 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002041
2042 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2043 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002044 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002045
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002046 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002047 SetRawInputAt(index, argument);
2048 }
2049
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002050 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002051
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002052 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002053
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002054 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2055
2056 Intrinsics GetIntrinsic() {
2057 return intrinsic_;
2058 }
2059
2060 void SetIntrinsic(Intrinsics intrinsic) {
2061 intrinsic_ = intrinsic;
2062 }
2063
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002064 DECLARE_INSTRUCTION(Invoke);
2065
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002066 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002067 HInvoke(ArenaAllocator* arena,
2068 uint32_t number_of_arguments,
2069 Primitive::Type return_type,
2070 uint32_t dex_pc,
2071 uint32_t dex_method_index)
2072 : HInstruction(SideEffects::All()),
2073 inputs_(arena, number_of_arguments),
2074 return_type_(return_type),
2075 dex_pc_(dex_pc),
2076 dex_method_index_(dex_method_index),
2077 intrinsic_(Intrinsics::kNone) {
2078 inputs_.SetSize(number_of_arguments);
2079 }
2080
David Brazdil1abb4192015-02-17 18:33:36 +00002081 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2082 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2083 inputs_.Put(index, input);
2084 }
2085
2086 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002087 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002088 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002089 const uint32_t dex_method_index_;
2090 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002091
2092 private:
2093 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2094};
2095
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002096class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002097 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002098 HInvokeStaticOrDirect(ArenaAllocator* arena,
2099 uint32_t number_of_arguments,
2100 Primitive::Type return_type,
2101 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002102 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002103 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002104 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002105 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002106 invoke_type_(invoke_type),
2107 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002108
Calin Juravle77520bc2015-01-12 18:45:46 +00002109 bool CanDoImplicitNullCheck() const OVERRIDE {
2110 // We access the method via the dex cache so we can't do an implicit null check.
2111 // TODO: for intrinsics we can generate implicit null checks.
2112 return false;
2113 }
2114
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002115 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002116 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002117
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002118 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002119
2120 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002121 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002122 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002123
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002124 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002125};
2126
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002127class HInvokeVirtual : public HInvoke {
2128 public:
2129 HInvokeVirtual(ArenaAllocator* arena,
2130 uint32_t number_of_arguments,
2131 Primitive::Type return_type,
2132 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002133 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002134 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002135 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002136 vtable_index_(vtable_index) {}
2137
Calin Juravle77520bc2015-01-12 18:45:46 +00002138 bool CanDoImplicitNullCheck() const OVERRIDE {
2139 // TODO: Add implicit null checks in intrinsics.
2140 return !GetLocations()->Intrinsified();
2141 }
2142
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002143 uint32_t GetVTableIndex() const { return vtable_index_; }
2144
2145 DECLARE_INSTRUCTION(InvokeVirtual);
2146
2147 private:
2148 const uint32_t vtable_index_;
2149
2150 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2151};
2152
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002153class HInvokeInterface : public HInvoke {
2154 public:
2155 HInvokeInterface(ArenaAllocator* arena,
2156 uint32_t number_of_arguments,
2157 Primitive::Type return_type,
2158 uint32_t dex_pc,
2159 uint32_t dex_method_index,
2160 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002161 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002162 imt_index_(imt_index) {}
2163
Calin Juravle77520bc2015-01-12 18:45:46 +00002164 bool CanDoImplicitNullCheck() const OVERRIDE {
2165 // TODO: Add implicit null checks in intrinsics.
2166 return !GetLocations()->Intrinsified();
2167 }
2168
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002169 uint32_t GetImtIndex() const { return imt_index_; }
2170 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2171
2172 DECLARE_INSTRUCTION(InvokeInterface);
2173
2174 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002175 const uint32_t imt_index_;
2176
2177 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2178};
2179
Dave Allison20dfc792014-06-16 20:44:29 -07002180class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002181 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002182 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002183 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2184 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002185 type_index_(type_index),
2186 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002187
2188 uint32_t GetDexPc() const { return dex_pc_; }
2189 uint16_t GetTypeIndex() const { return type_index_; }
2190
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002191 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002192 bool NeedsEnvironment() const OVERRIDE { return true; }
2193 // It may throw when called on:
2194 // - interfaces
2195 // - abstract/innaccessible/unknown classes
2196 // TODO: optimize when possible.
2197 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002198
Calin Juravle10e244f2015-01-26 18:54:32 +00002199 bool CanBeNull() const OVERRIDE { return false; }
2200
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002201 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2202
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002203 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002204
2205 private:
2206 const uint32_t dex_pc_;
2207 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002208 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002209
2210 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2211};
2212
Roland Levillain88cb1752014-10-20 16:36:47 +01002213class HNeg : public HUnaryOperation {
2214 public:
2215 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2216 : HUnaryOperation(result_type, input) {}
2217
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002218 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2219 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002220
Roland Levillain88cb1752014-10-20 16:36:47 +01002221 DECLARE_INSTRUCTION(Neg);
2222
2223 private:
2224 DISALLOW_COPY_AND_ASSIGN(HNeg);
2225};
2226
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002227class HNewArray : public HExpression<1> {
2228 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002229 HNewArray(HInstruction* length,
2230 uint32_t dex_pc,
2231 uint16_t type_index,
2232 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002233 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2234 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002235 type_index_(type_index),
2236 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002237 SetRawInputAt(0, length);
2238 }
2239
2240 uint32_t GetDexPc() const { return dex_pc_; }
2241 uint16_t GetTypeIndex() const { return type_index_; }
2242
2243 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002244 bool NeedsEnvironment() const OVERRIDE { return true; }
2245
2246 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002247
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002248 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2249
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002250 DECLARE_INSTRUCTION(NewArray);
2251
2252 private:
2253 const uint32_t dex_pc_;
2254 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002255 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002256
2257 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2258};
2259
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002260class HAdd : public HBinaryOperation {
2261 public:
2262 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2263 : HBinaryOperation(result_type, left, right) {}
2264
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002265 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002266
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002267 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002268 return x + y;
2269 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002270 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002271 return x + y;
2272 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002273
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002274 DECLARE_INSTRUCTION(Add);
2275
2276 private:
2277 DISALLOW_COPY_AND_ASSIGN(HAdd);
2278};
2279
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002280class HSub : public HBinaryOperation {
2281 public:
2282 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2283 : HBinaryOperation(result_type, left, right) {}
2284
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002285 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002286 return x - y;
2287 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002288 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002289 return x - y;
2290 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002291
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002292 DECLARE_INSTRUCTION(Sub);
2293
2294 private:
2295 DISALLOW_COPY_AND_ASSIGN(HSub);
2296};
2297
Calin Juravle34bacdf2014-10-07 20:23:36 +01002298class HMul : public HBinaryOperation {
2299 public:
2300 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2301 : HBinaryOperation(result_type, left, right) {}
2302
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002303 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002304
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002305 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2306 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002307
2308 DECLARE_INSTRUCTION(Mul);
2309
2310 private:
2311 DISALLOW_COPY_AND_ASSIGN(HMul);
2312};
2313
Calin Juravle7c4954d2014-10-28 16:57:40 +00002314class HDiv : public HBinaryOperation {
2315 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002316 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2317 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002318
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002319 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002320 // Our graph structure ensures we never have 0 for `y` during constant folding.
2321 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002322 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002323 return (y == -1) ? -x : x / y;
2324 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002325
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002326 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002327 DCHECK_NE(y, 0);
2328 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2329 return (y == -1) ? -x : x / y;
2330 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002331
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002332 uint32_t GetDexPc() const { return dex_pc_; }
2333
Calin Juravle7c4954d2014-10-28 16:57:40 +00002334 DECLARE_INSTRUCTION(Div);
2335
2336 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002337 const uint32_t dex_pc_;
2338
Calin Juravle7c4954d2014-10-28 16:57:40 +00002339 DISALLOW_COPY_AND_ASSIGN(HDiv);
2340};
2341
Calin Juravlebacfec32014-11-14 15:54:36 +00002342class HRem : public HBinaryOperation {
2343 public:
2344 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2345 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2346
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002347 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002348 DCHECK_NE(y, 0);
2349 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2350 return (y == -1) ? 0 : x % y;
2351 }
2352
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002353 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002354 DCHECK_NE(y, 0);
2355 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2356 return (y == -1) ? 0 : x % y;
2357 }
2358
2359 uint32_t GetDexPc() const { return dex_pc_; }
2360
2361 DECLARE_INSTRUCTION(Rem);
2362
2363 private:
2364 const uint32_t dex_pc_;
2365
2366 DISALLOW_COPY_AND_ASSIGN(HRem);
2367};
2368
Calin Juravled0d48522014-11-04 16:40:20 +00002369class HDivZeroCheck : public HExpression<1> {
2370 public:
2371 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2372 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2373 SetRawInputAt(0, value);
2374 }
2375
2376 bool CanBeMoved() const OVERRIDE { return true; }
2377
2378 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2379 UNUSED(other);
2380 return true;
2381 }
2382
2383 bool NeedsEnvironment() const OVERRIDE { return true; }
2384 bool CanThrow() const OVERRIDE { return true; }
2385
2386 uint32_t GetDexPc() const { return dex_pc_; }
2387
2388 DECLARE_INSTRUCTION(DivZeroCheck);
2389
2390 private:
2391 const uint32_t dex_pc_;
2392
2393 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2394};
2395
Calin Juravle9aec02f2014-11-18 23:06:35 +00002396class HShl : public HBinaryOperation {
2397 public:
2398 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2399 : HBinaryOperation(result_type, left, right) {}
2400
2401 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2402 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2403
2404 DECLARE_INSTRUCTION(Shl);
2405
2406 private:
2407 DISALLOW_COPY_AND_ASSIGN(HShl);
2408};
2409
2410class HShr : public HBinaryOperation {
2411 public:
2412 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2413 : HBinaryOperation(result_type, left, right) {}
2414
2415 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2416 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2417
2418 DECLARE_INSTRUCTION(Shr);
2419
2420 private:
2421 DISALLOW_COPY_AND_ASSIGN(HShr);
2422};
2423
2424class HUShr : public HBinaryOperation {
2425 public:
2426 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2427 : HBinaryOperation(result_type, left, right) {}
2428
2429 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2430 uint32_t ux = static_cast<uint32_t>(x);
2431 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2432 return static_cast<int32_t>(ux >> uy);
2433 }
2434
2435 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2436 uint64_t ux = static_cast<uint64_t>(x);
2437 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2438 return static_cast<int64_t>(ux >> uy);
2439 }
2440
2441 DECLARE_INSTRUCTION(UShr);
2442
2443 private:
2444 DISALLOW_COPY_AND_ASSIGN(HUShr);
2445};
2446
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002447class HAnd : public HBinaryOperation {
2448 public:
2449 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2450 : HBinaryOperation(result_type, left, right) {}
2451
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002452 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002453
2454 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2455 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2456
2457 DECLARE_INSTRUCTION(And);
2458
2459 private:
2460 DISALLOW_COPY_AND_ASSIGN(HAnd);
2461};
2462
2463class HOr : public HBinaryOperation {
2464 public:
2465 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2466 : HBinaryOperation(result_type, left, right) {}
2467
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002468 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002469
2470 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2471 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2472
2473 DECLARE_INSTRUCTION(Or);
2474
2475 private:
2476 DISALLOW_COPY_AND_ASSIGN(HOr);
2477};
2478
2479class HXor : public HBinaryOperation {
2480 public:
2481 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2482 : HBinaryOperation(result_type, left, right) {}
2483
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002484 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002485
2486 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2487 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2488
2489 DECLARE_INSTRUCTION(Xor);
2490
2491 private:
2492 DISALLOW_COPY_AND_ASSIGN(HXor);
2493};
2494
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002495// The value of a parameter in this method. Its location depends on
2496// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002497class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002498 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002499 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2500 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002501
2502 uint8_t GetIndex() const { return index_; }
2503
Calin Juravle10e244f2015-01-26 18:54:32 +00002504 bool CanBeNull() const OVERRIDE { return !is_this_; }
2505
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002506 DECLARE_INSTRUCTION(ParameterValue);
2507
2508 private:
2509 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002510 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002511 const uint8_t index_;
2512
Calin Juravle10e244f2015-01-26 18:54:32 +00002513 // Whether or not the parameter value corresponds to 'this' argument.
2514 const bool is_this_;
2515
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002516 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2517};
2518
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002519class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002520 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002521 explicit HNot(Primitive::Type result_type, HInstruction* input)
2522 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002523
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002524 bool CanBeMoved() const OVERRIDE { return true; }
2525 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002526 UNUSED(other);
2527 return true;
2528 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002529
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002530 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2531 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002532
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002533 DECLARE_INSTRUCTION(Not);
2534
2535 private:
2536 DISALLOW_COPY_AND_ASSIGN(HNot);
2537};
2538
Roland Levillaindff1f282014-11-05 14:15:05 +00002539class HTypeConversion : public HExpression<1> {
2540 public:
2541 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002542 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2543 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002544 SetRawInputAt(0, input);
2545 DCHECK_NE(input->GetType(), result_type);
2546 }
2547
2548 HInstruction* GetInput() const { return InputAt(0); }
2549 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2550 Primitive::Type GetResultType() const { return GetType(); }
2551
Roland Levillain624279f2014-12-04 11:54:28 +00002552 // Required by the x86 and ARM code generators when producing calls
2553 // to the runtime.
2554 uint32_t GetDexPc() const { return dex_pc_; }
2555
Roland Levillaindff1f282014-11-05 14:15:05 +00002556 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002557 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002558
2559 DECLARE_INSTRUCTION(TypeConversion);
2560
2561 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002562 const uint32_t dex_pc_;
2563
Roland Levillaindff1f282014-11-05 14:15:05 +00002564 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2565};
2566
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002567static constexpr uint32_t kNoRegNumber = -1;
2568
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002569class HPhi : public HInstruction {
2570 public:
2571 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002572 : HInstruction(SideEffects::None()),
2573 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002574 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002575 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002576 is_live_(false),
2577 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002578 inputs_.SetSize(number_of_inputs);
2579 }
2580
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002581 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2582 static Primitive::Type ToPhiType(Primitive::Type type) {
2583 switch (type) {
2584 case Primitive::kPrimBoolean:
2585 case Primitive::kPrimByte:
2586 case Primitive::kPrimShort:
2587 case Primitive::kPrimChar:
2588 return Primitive::kPrimInt;
2589 default:
2590 return type;
2591 }
2592 }
2593
Calin Juravle10e244f2015-01-26 18:54:32 +00002594 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002595
2596 void AddInput(HInstruction* input);
2597
Calin Juravle10e244f2015-01-26 18:54:32 +00002598 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002599 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002600
Calin Juravle10e244f2015-01-26 18:54:32 +00002601 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2602 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2603
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002604 uint32_t GetRegNumber() const { return reg_number_; }
2605
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002606 void SetDead() { is_live_ = false; }
2607 void SetLive() { is_live_ = true; }
2608 bool IsDead() const { return !is_live_; }
2609 bool IsLive() const { return is_live_; }
2610
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002611 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002612
David Brazdil1abb4192015-02-17 18:33:36 +00002613 protected:
2614 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2615
2616 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2617 inputs_.Put(index, input);
2618 }
2619
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002620 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002621 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002622 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002623 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002624 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002625 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002626
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002627 DISALLOW_COPY_AND_ASSIGN(HPhi);
2628};
2629
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002630class HNullCheck : public HExpression<1> {
2631 public:
2632 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002633 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002634 SetRawInputAt(0, value);
2635 }
2636
Calin Juravle10e244f2015-01-26 18:54:32 +00002637 bool CanBeMoved() const OVERRIDE { return true; }
2638 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002639 UNUSED(other);
2640 return true;
2641 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002642
Calin Juravle10e244f2015-01-26 18:54:32 +00002643 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002644
Calin Juravle10e244f2015-01-26 18:54:32 +00002645 bool CanThrow() const OVERRIDE { return true; }
2646
2647 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002648
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002649 uint32_t GetDexPc() const { return dex_pc_; }
2650
2651 DECLARE_INSTRUCTION(NullCheck);
2652
2653 private:
2654 const uint32_t dex_pc_;
2655
2656 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2657};
2658
2659class FieldInfo : public ValueObject {
2660 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002661 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2662 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002663
2664 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002665 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002666 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002667
2668 private:
2669 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002670 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002671 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002672};
2673
2674class HInstanceFieldGet : public HExpression<1> {
2675 public:
2676 HInstanceFieldGet(HInstruction* value,
2677 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002678 MemberOffset field_offset,
2679 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002680 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002681 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002682 SetRawInputAt(0, value);
2683 }
2684
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002685 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002686
2687 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2688 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2689 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002690 }
2691
Calin Juravle77520bc2015-01-12 18:45:46 +00002692 bool CanDoImplicitNullCheck() const OVERRIDE {
2693 return GetFieldOffset().Uint32Value() < kPageSize;
2694 }
2695
2696 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002697 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2698 }
2699
Calin Juravle52c48962014-12-16 17:02:57 +00002700 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002701 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002702 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002703 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002704
2705 DECLARE_INSTRUCTION(InstanceFieldGet);
2706
2707 private:
2708 const FieldInfo field_info_;
2709
2710 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2711};
2712
2713class HInstanceFieldSet : public HTemplateInstruction<2> {
2714 public:
2715 HInstanceFieldSet(HInstruction* object,
2716 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002717 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002718 MemberOffset field_offset,
2719 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002720 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002721 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002722 SetRawInputAt(0, object);
2723 SetRawInputAt(1, value);
2724 }
2725
Calin Juravle77520bc2015-01-12 18:45:46 +00002726 bool CanDoImplicitNullCheck() const OVERRIDE {
2727 return GetFieldOffset().Uint32Value() < kPageSize;
2728 }
2729
Calin Juravle52c48962014-12-16 17:02:57 +00002730 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002731 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002732 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002733 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002734 HInstruction* GetValue() const { return InputAt(1); }
2735
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002736 DECLARE_INSTRUCTION(InstanceFieldSet);
2737
2738 private:
2739 const FieldInfo field_info_;
2740
2741 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2742};
2743
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002744class HArrayGet : public HExpression<2> {
2745 public:
2746 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002747 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002748 SetRawInputAt(0, array);
2749 SetRawInputAt(1, index);
2750 }
2751
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002752 bool CanBeMoved() const OVERRIDE { return true; }
2753 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002754 UNUSED(other);
2755 return true;
2756 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002757 bool CanDoImplicitNullCheck() const OVERRIDE {
2758 // TODO: We can be smarter here.
2759 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2760 // which generates the implicit null check. There are cases when these can be removed
2761 // to produce better code. If we ever add optimizations to do so we should allow an
2762 // implicit check here (as long as the address falls in the first page).
2763 return false;
2764 }
2765
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002766 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002767
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002768 HInstruction* GetArray() const { return InputAt(0); }
2769 HInstruction* GetIndex() const { return InputAt(1); }
2770
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002771 DECLARE_INSTRUCTION(ArrayGet);
2772
2773 private:
2774 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2775};
2776
2777class HArraySet : public HTemplateInstruction<3> {
2778 public:
2779 HArraySet(HInstruction* array,
2780 HInstruction* index,
2781 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002782 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002783 uint32_t dex_pc)
2784 : HTemplateInstruction(SideEffects::ChangesSomething()),
2785 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002786 expected_component_type_(expected_component_type),
2787 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002788 SetRawInputAt(0, array);
2789 SetRawInputAt(1, index);
2790 SetRawInputAt(2, value);
2791 }
2792
Calin Juravle77520bc2015-01-12 18:45:46 +00002793 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002794 // We currently always call a runtime method to catch array store
2795 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002796 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002797 }
2798
Calin Juravle77520bc2015-01-12 18:45:46 +00002799 bool CanDoImplicitNullCheck() const OVERRIDE {
2800 // TODO: Same as for ArrayGet.
2801 return false;
2802 }
2803
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002804 void ClearNeedsTypeCheck() {
2805 needs_type_check_ = false;
2806 }
2807
2808 bool NeedsTypeCheck() const { return needs_type_check_; }
2809
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002810 uint32_t GetDexPc() const { return dex_pc_; }
2811
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002812 HInstruction* GetArray() const { return InputAt(0); }
2813 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002814 HInstruction* GetValue() const { return InputAt(2); }
2815
2816 Primitive::Type GetComponentType() const {
2817 // The Dex format does not type floating point index operations. Since the
2818 // `expected_component_type_` is set during building and can therefore not
2819 // be correct, we also check what is the value type. If it is a floating
2820 // point type, we must use that type.
2821 Primitive::Type value_type = GetValue()->GetType();
2822 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2823 ? value_type
2824 : expected_component_type_;
2825 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002826
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002827 DECLARE_INSTRUCTION(ArraySet);
2828
2829 private:
2830 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002831 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002832 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002833
2834 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2835};
2836
2837class HArrayLength : public HExpression<1> {
2838 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002839 explicit HArrayLength(HInstruction* array)
2840 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2841 // Note that arrays do not change length, so the instruction does not
2842 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002843 SetRawInputAt(0, array);
2844 }
2845
Calin Juravle77520bc2015-01-12 18:45:46 +00002846 bool CanBeMoved() const OVERRIDE { return true; }
2847 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002848 UNUSED(other);
2849 return true;
2850 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002851 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002852
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002853 DECLARE_INSTRUCTION(ArrayLength);
2854
2855 private:
2856 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2857};
2858
2859class HBoundsCheck : public HExpression<2> {
2860 public:
2861 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002862 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002863 DCHECK(index->GetType() == Primitive::kPrimInt);
2864 SetRawInputAt(0, index);
2865 SetRawInputAt(1, length);
2866 }
2867
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002868 bool CanBeMoved() const OVERRIDE { return true; }
2869 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002870 UNUSED(other);
2871 return true;
2872 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002873
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002874 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002875
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002876 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002877
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002878 uint32_t GetDexPc() const { return dex_pc_; }
2879
2880 DECLARE_INSTRUCTION(BoundsCheck);
2881
2882 private:
2883 const uint32_t dex_pc_;
2884
2885 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2886};
2887
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002888/**
2889 * Some DEX instructions are folded into multiple HInstructions that need
2890 * to stay live until the last HInstruction. This class
2891 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002892 * HInstruction stays live. `index` represents the stack location index of the
2893 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002894 */
2895class HTemporary : public HTemplateInstruction<0> {
2896 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002897 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002898
2899 size_t GetIndex() const { return index_; }
2900
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002901 Primitive::Type GetType() const OVERRIDE {
2902 // The previous instruction is the one that will be stored in the temporary location.
2903 DCHECK(GetPrevious() != nullptr);
2904 return GetPrevious()->GetType();
2905 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002906
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002907 DECLARE_INSTRUCTION(Temporary);
2908
2909 private:
2910 const size_t index_;
2911
2912 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2913};
2914
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002915class HSuspendCheck : public HTemplateInstruction<0> {
2916 public:
2917 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002918 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002919
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002920 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002921 return true;
2922 }
2923
2924 uint32_t GetDexPc() const { return dex_pc_; }
2925
2926 DECLARE_INSTRUCTION(SuspendCheck);
2927
2928 private:
2929 const uint32_t dex_pc_;
2930
2931 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2932};
2933
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002934/**
2935 * Instruction to load a Class object.
2936 */
2937class HLoadClass : public HExpression<0> {
2938 public:
2939 HLoadClass(uint16_t type_index,
2940 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002941 uint32_t dex_pc)
2942 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2943 type_index_(type_index),
2944 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002945 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00002946 generate_clinit_check_(false),
2947 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002948
2949 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002950
2951 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2952 return other->AsLoadClass()->type_index_ == type_index_;
2953 }
2954
2955 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2956
2957 uint32_t GetDexPc() const { return dex_pc_; }
2958 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002959 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002960
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002961 bool NeedsEnvironment() const OVERRIDE {
2962 // Will call runtime and load the class if the class is not loaded yet.
2963 // TODO: finer grain decision.
2964 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002965 }
2966
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002967 bool MustGenerateClinitCheck() const {
2968 return generate_clinit_check_;
2969 }
2970
2971 void SetMustGenerateClinitCheck() {
2972 generate_clinit_check_ = true;
2973 }
2974
2975 bool CanCallRuntime() const {
2976 return MustGenerateClinitCheck() || !is_referrers_class_;
2977 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002978
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002979 bool CanThrow() const OVERRIDE {
2980 // May call runtime and and therefore can throw.
2981 // TODO: finer grain decision.
2982 return !is_referrers_class_;
2983 }
2984
Calin Juravleacf735c2015-02-12 15:25:22 +00002985 ReferenceTypeInfo GetLoadedClassRTI() {
2986 return loaded_class_rti_;
2987 }
2988
2989 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
2990 // Make sure we only set exact types (the loaded class should never be merged).
2991 DCHECK(rti.IsExact());
2992 loaded_class_rti_ = rti;
2993 }
2994
2995 bool IsResolved() {
2996 return loaded_class_rti_.IsExact();
2997 }
2998
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002999 DECLARE_INSTRUCTION(LoadClass);
3000
3001 private:
3002 const uint16_t type_index_;
3003 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003004 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003005 // Whether this instruction must generate the initialization check.
3006 // Used for code generation.
3007 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003008
Calin Juravleacf735c2015-02-12 15:25:22 +00003009 ReferenceTypeInfo loaded_class_rti_;
3010
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003011 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3012};
3013
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003014class HLoadString : public HExpression<0> {
3015 public:
3016 HLoadString(uint32_t string_index, uint32_t dex_pc)
3017 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3018 string_index_(string_index),
3019 dex_pc_(dex_pc) {}
3020
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003021 bool CanBeMoved() const OVERRIDE { return true; }
3022
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003023 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3024 return other->AsLoadString()->string_index_ == string_index_;
3025 }
3026
3027 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3028
3029 uint32_t GetDexPc() const { return dex_pc_; }
3030 uint32_t GetStringIndex() const { return string_index_; }
3031
3032 // TODO: Can we deopt or debug when we resolve a string?
3033 bool NeedsEnvironment() const OVERRIDE { return false; }
3034
3035 DECLARE_INSTRUCTION(LoadString);
3036
3037 private:
3038 const uint32_t string_index_;
3039 const uint32_t dex_pc_;
3040
3041 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3042};
3043
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003044// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003045/**
3046 * Performs an initialization check on its Class object input.
3047 */
3048class HClinitCheck : public HExpression<1> {
3049 public:
3050 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
3051 : HExpression(Primitive::kPrimNot, SideEffects::All()),
3052 dex_pc_(dex_pc) {
3053 SetRawInputAt(0, constant);
3054 }
3055
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003056 bool CanBeMoved() const OVERRIDE { return true; }
3057 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3058 UNUSED(other);
3059 return true;
3060 }
3061
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003062 bool NeedsEnvironment() const OVERRIDE {
3063 // May call runtime to initialize the class.
3064 return true;
3065 }
3066
3067 uint32_t GetDexPc() const { return dex_pc_; }
3068
3069 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3070
3071 DECLARE_INSTRUCTION(ClinitCheck);
3072
3073 private:
3074 const uint32_t dex_pc_;
3075
3076 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3077};
3078
3079class HStaticFieldGet : public HExpression<1> {
3080 public:
3081 HStaticFieldGet(HInstruction* cls,
3082 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003083 MemberOffset field_offset,
3084 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003085 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003086 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003087 SetRawInputAt(0, cls);
3088 }
3089
Calin Juravle52c48962014-12-16 17:02:57 +00003090
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003091 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003092
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003093 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003094 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3095 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003096 }
3097
3098 size_t ComputeHashCode() const OVERRIDE {
3099 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3100 }
3101
Calin Juravle52c48962014-12-16 17:02:57 +00003102 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003103 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3104 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003105 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003106
3107 DECLARE_INSTRUCTION(StaticFieldGet);
3108
3109 private:
3110 const FieldInfo field_info_;
3111
3112 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3113};
3114
3115class HStaticFieldSet : public HTemplateInstruction<2> {
3116 public:
3117 HStaticFieldSet(HInstruction* cls,
3118 HInstruction* value,
3119 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003120 MemberOffset field_offset,
3121 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003122 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003123 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003124 SetRawInputAt(0, cls);
3125 SetRawInputAt(1, value);
3126 }
3127
Calin Juravle52c48962014-12-16 17:02:57 +00003128 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003129 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3130 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003131 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003132
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003133 HInstruction* GetValue() const { return InputAt(1); }
3134
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003135 DECLARE_INSTRUCTION(StaticFieldSet);
3136
3137 private:
3138 const FieldInfo field_info_;
3139
3140 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3141};
3142
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003143// Implement the move-exception DEX instruction.
3144class HLoadException : public HExpression<0> {
3145 public:
3146 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3147
3148 DECLARE_INSTRUCTION(LoadException);
3149
3150 private:
3151 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3152};
3153
3154class HThrow : public HTemplateInstruction<1> {
3155 public:
3156 HThrow(HInstruction* exception, uint32_t dex_pc)
3157 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3158 SetRawInputAt(0, exception);
3159 }
3160
3161 bool IsControlFlow() const OVERRIDE { return true; }
3162
3163 bool NeedsEnvironment() const OVERRIDE { return true; }
3164
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003165 bool CanThrow() const OVERRIDE { return true; }
3166
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003167 uint32_t GetDexPc() const { return dex_pc_; }
3168
3169 DECLARE_INSTRUCTION(Throw);
3170
3171 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003172 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003173
3174 DISALLOW_COPY_AND_ASSIGN(HThrow);
3175};
3176
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003177class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003178 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003179 HInstanceOf(HInstruction* object,
3180 HLoadClass* constant,
3181 bool class_is_final,
3182 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003183 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3184 class_is_final_(class_is_final),
3185 dex_pc_(dex_pc) {
3186 SetRawInputAt(0, object);
3187 SetRawInputAt(1, constant);
3188 }
3189
3190 bool CanBeMoved() const OVERRIDE { return true; }
3191
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003192 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003193 return true;
3194 }
3195
3196 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003197 return false;
3198 }
3199
3200 uint32_t GetDexPc() const { return dex_pc_; }
3201
3202 bool IsClassFinal() const { return class_is_final_; }
3203
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003204 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003205
3206 private:
3207 const bool class_is_final_;
3208 const uint32_t dex_pc_;
3209
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003210 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3211};
3212
Calin Juravleb1498f62015-02-16 13:13:29 +00003213class HBoundType : public HExpression<1> {
3214 public:
3215 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3216 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3217 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003218 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003219 SetRawInputAt(0, input);
3220 }
3221
3222 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3223
3224 bool CanBeNull() const OVERRIDE {
3225 // `null instanceof ClassX` always return false so we can't be null.
3226 return false;
3227 }
3228
3229 DECLARE_INSTRUCTION(BoundType);
3230
3231 private:
3232 // Encodes the most upper class that this instruction can have. In other words
3233 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3234 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3235 const ReferenceTypeInfo bound_type_;
3236
3237 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3238};
3239
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003240class HCheckCast : public HTemplateInstruction<2> {
3241 public:
3242 HCheckCast(HInstruction* object,
3243 HLoadClass* constant,
3244 bool class_is_final,
3245 uint32_t dex_pc)
3246 : HTemplateInstruction(SideEffects::None()),
3247 class_is_final_(class_is_final),
3248 dex_pc_(dex_pc) {
3249 SetRawInputAt(0, object);
3250 SetRawInputAt(1, constant);
3251 }
3252
3253 bool CanBeMoved() const OVERRIDE { return true; }
3254
3255 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3256 return true;
3257 }
3258
3259 bool NeedsEnvironment() const OVERRIDE {
3260 // Instruction may throw a CheckCastError.
3261 return true;
3262 }
3263
3264 bool CanThrow() const OVERRIDE { return true; }
3265
3266 uint32_t GetDexPc() const { return dex_pc_; }
3267
3268 bool IsClassFinal() const { return class_is_final_; }
3269
3270 DECLARE_INSTRUCTION(CheckCast);
3271
3272 private:
3273 const bool class_is_final_;
3274 const uint32_t dex_pc_;
3275
3276 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003277};
3278
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003279class HMonitorOperation : public HTemplateInstruction<1> {
3280 public:
3281 enum OperationKind {
3282 kEnter,
3283 kExit,
3284 };
3285
3286 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3287 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3288 SetRawInputAt(0, object);
3289 }
3290
3291 // Instruction may throw a Java exception, so we need an environment.
3292 bool NeedsEnvironment() const OVERRIDE { return true; }
3293 bool CanThrow() const OVERRIDE { return true; }
3294
3295 uint32_t GetDexPc() const { return dex_pc_; }
3296
3297 bool IsEnter() const { return kind_ == kEnter; }
3298
3299 DECLARE_INSTRUCTION(MonitorOperation);
3300
Calin Juravle52c48962014-12-16 17:02:57 +00003301 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003302 const OperationKind kind_;
3303 const uint32_t dex_pc_;
3304
3305 private:
3306 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3307};
3308
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003309class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003310 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003311 MoveOperands(Location source, Location destination, HInstruction* instruction)
3312 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003313
3314 Location GetSource() const { return source_; }
3315 Location GetDestination() const { return destination_; }
3316
3317 void SetSource(Location value) { source_ = value; }
3318 void SetDestination(Location value) { destination_ = value; }
3319
3320 // The parallel move resolver marks moves as "in-progress" by clearing the
3321 // destination (but not the source).
3322 Location MarkPending() {
3323 DCHECK(!IsPending());
3324 Location dest = destination_;
3325 destination_ = Location::NoLocation();
3326 return dest;
3327 }
3328
3329 void ClearPending(Location dest) {
3330 DCHECK(IsPending());
3331 destination_ = dest;
3332 }
3333
3334 bool IsPending() const {
3335 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3336 return destination_.IsInvalid() && !source_.IsInvalid();
3337 }
3338
3339 // True if this blocks a move from the given location.
3340 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003341 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003342 }
3343
3344 // A move is redundant if it's been eliminated, if its source and
3345 // destination are the same, or if its destination is unneeded.
3346 bool IsRedundant() const {
3347 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3348 }
3349
3350 // We clear both operands to indicate move that's been eliminated.
3351 void Eliminate() {
3352 source_ = destination_ = Location::NoLocation();
3353 }
3354
3355 bool IsEliminated() const {
3356 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3357 return source_.IsInvalid();
3358 }
3359
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003360 HInstruction* GetInstruction() const { return instruction_; }
3361
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003362 private:
3363 Location source_;
3364 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003365 // The instruction this move is assocatied with. Null when this move is
3366 // for moving an input in the expected locations of user (including a phi user).
3367 // This is only used in debug mode, to ensure we do not connect interval siblings
3368 // in the same parallel move.
3369 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003370};
3371
3372static constexpr size_t kDefaultNumberOfMoves = 4;
3373
3374class HParallelMove : public HTemplateInstruction<0> {
3375 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003376 explicit HParallelMove(ArenaAllocator* arena)
3377 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003378
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003379 void AddMove(Location source, Location destination, HInstruction* instruction) {
3380 DCHECK(source.IsValid());
3381 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003382 if (kIsDebugBuild) {
3383 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003384 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003385 if (moves_.Get(i).GetInstruction() == instruction) {
3386 // Special case the situation where the move is for the spill slot
3387 // of the instruction.
3388 if ((GetPrevious() == instruction)
3389 || ((GetPrevious() == nullptr)
3390 && instruction->IsPhi()
3391 && instruction->GetBlock() == GetBlock())) {
3392 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3393 << "Doing parallel moves for the same instruction.";
3394 } else {
3395 DCHECK(false) << "Doing parallel moves for the same instruction.";
3396 }
3397 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003398 }
3399 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003400 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3401 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3402 << "Same destination for two moves in a parallel move.";
3403 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003404 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003405 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003406 }
3407
3408 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003409 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003410 }
3411
3412 size_t NumMoves() const { return moves_.Size(); }
3413
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003414 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003415
3416 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003417 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003418
3419 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3420};
3421
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003422class HGraphVisitor : public ValueObject {
3423 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003424 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3425 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003426
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003427 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003428 virtual void VisitBasicBlock(HBasicBlock* block);
3429
Roland Levillain633021e2014-10-01 14:12:25 +01003430 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003431 void VisitInsertionOrder();
3432
Roland Levillain633021e2014-10-01 14:12:25 +01003433 // Visit the graph following dominator tree reverse post-order.
3434 void VisitReversePostOrder();
3435
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003436 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003437
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003438 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003439#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003440 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3441
3442 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3443
3444#undef DECLARE_VISIT_INSTRUCTION
3445
3446 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003447 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003448
3449 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3450};
3451
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003452class HGraphDelegateVisitor : public HGraphVisitor {
3453 public:
3454 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3455 virtual ~HGraphDelegateVisitor() {}
3456
3457 // Visit functions that delegate to to super class.
3458#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003459 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003460
3461 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3462
3463#undef DECLARE_VISIT_INSTRUCTION
3464
3465 private:
3466 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3467};
3468
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003469class HInsertionOrderIterator : public ValueObject {
3470 public:
3471 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3472
3473 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3474 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3475 void Advance() { ++index_; }
3476
3477 private:
3478 const HGraph& graph_;
3479 size_t index_;
3480
3481 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3482};
3483
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003484class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003485 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003486 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3487 // Check that reverse post order of the graph has been built.
3488 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3489 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003490
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003491 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3492 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003493 void Advance() { ++index_; }
3494
3495 private:
3496 const HGraph& graph_;
3497 size_t index_;
3498
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003499 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003500};
3501
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003502class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003503 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003504 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003505 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3506 // Check that reverse post order of the graph has been built.
3507 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3508 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003509
3510 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003511 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003512 void Advance() { --index_; }
3513
3514 private:
3515 const HGraph& graph_;
3516 size_t index_;
3517
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003518 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003519};
3520
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003521// Iterator over the blocks that art part of the loop. Includes blocks part
3522// of an inner loop. The order in which the blocks are iterated is on their
3523// block id.
3524class HBlocksInLoopIterator : public ValueObject {
3525 public:
3526 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3527 : blocks_in_loop_(info.GetBlocks()),
3528 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3529 index_(0) {
3530 if (!blocks_in_loop_.IsBitSet(index_)) {
3531 Advance();
3532 }
3533 }
3534
3535 bool Done() const { return index_ == blocks_.Size(); }
3536 HBasicBlock* Current() const { return blocks_.Get(index_); }
3537 void Advance() {
3538 ++index_;
3539 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3540 if (blocks_in_loop_.IsBitSet(index_)) {
3541 break;
3542 }
3543 }
3544 }
3545
3546 private:
3547 const BitVector& blocks_in_loop_;
3548 const GrowableArray<HBasicBlock*>& blocks_;
3549 size_t index_;
3550
3551 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3552};
3553
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003554inline int64_t Int64FromConstant(HConstant* constant) {
3555 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3556 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3557 : constant->AsLongConstant()->GetValue();
3558}
3559
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003560} // namespace art
3561
3562#endif // ART_COMPILER_OPTIMIZING_NODES_H_