blob: c4f6f6da145887e5ce46f0b43ef9d4270a4f2677 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Mathieu Chartierb666f482015-02-18 14:33:14 -080020#include "base/arena_object.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000021#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000022#include "handle.h"
23#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000024#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010025#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000026#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010027#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000029#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "utils/growable_array.h"
31
32namespace art {
33
David Brazdil1abb4192015-02-17 18:33:36 +000034class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010036class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000038class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000039class HInvoke;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040class HGraphVisitor;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000041class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010042class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010043class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010044class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000045class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046
47static const int kDefaultNumberOfBlocks = 8;
48static const int kDefaultNumberOfSuccessors = 2;
49static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010050static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000051static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
Calin Juravle9aec02f2014-11-18 23:06:35 +000053static constexpr uint32_t kMaxIntShiftValue = 0x1f;
54static constexpr uint64_t kMaxLongShiftValue = 0x3f;
55
Dave Allison20dfc792014-06-16 20:44:29 -070056enum IfCondition {
57 kCondEQ,
58 kCondNE,
59 kCondLT,
60 kCondLE,
61 kCondGT,
62 kCondGE,
63};
64
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010065class HInstructionList {
66 public:
67 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
68
69 void AddInstruction(HInstruction* instruction);
70 void RemoveInstruction(HInstruction* instruction);
71
Roland Levillain6b469232014-09-25 10:10:38 +010072 // Return true if this list contains `instruction`.
73 bool Contains(HInstruction* instruction) const;
74
Roland Levillainccc07a92014-09-16 14:48:16 +010075 // Return true if `instruction1` is found before `instruction2` in
76 // this instruction list and false otherwise. Abort if none
77 // of these instructions is found.
78 bool FoundBefore(const HInstruction* instruction1,
79 const HInstruction* instruction2) const;
80
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000081 bool IsEmpty() const { return first_instruction_ == nullptr; }
82 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
83
84 // Update the block of all instructions to be `block`.
85 void SetBlockOfInstructions(HBasicBlock* block) const;
86
87 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
88 void Add(const HInstructionList& instruction_list);
89
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010090 private:
91 HInstruction* first_instruction_;
92 HInstruction* last_instruction_;
93
94 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000095 friend class HGraph;
96 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010097 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010098 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010099
100 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
101};
102
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000103// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700104class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000105 public:
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000106 HGraph(ArenaAllocator* arena, bool debuggable = false, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000107 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000108 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100109 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700110 entry_block_(nullptr),
111 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100112 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100113 number_of_vregs_(0),
114 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000115 temporaries_vreg_slots_(0),
Mingyao Yange4335eb2015-03-02 15:14:13 -0800116 has_array_accesses_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000117 debuggable_(debuggable),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000118 current_instruction_id_(start_instruction_id) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000119
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000120 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100121 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100122 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000123
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000124 HBasicBlock* GetEntryBlock() const { return entry_block_; }
125 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000126
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000127 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
128 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000129
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000130 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100131
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000132 // Try building the SSA form of this graph, with dominance computation and loop
133 // recognition. Returns whether it was successful in doing all these steps.
134 bool TryBuildingSsa() {
135 BuildDominatorTree();
136 TransformToSsa();
137 return AnalyzeNaturalLoops();
138 }
139
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000140 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000141 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100142 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000143
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000144 // Analyze all natural loops in this graph. Returns false if one
145 // loop is not natural, that is the header does not dominate the
146 // back edge.
147 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100148
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000149 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
150 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
151
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100152 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
153 void SimplifyLoop(HBasicBlock* header);
154
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000155 int32_t GetNextInstructionId() {
156 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000157 return current_instruction_id_++;
158 }
159
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 int32_t GetCurrentInstructionId() const {
161 return current_instruction_id_;
162 }
163
164 void SetCurrentInstructionId(int32_t id) {
165 current_instruction_id_ = id;
166 }
167
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100168 uint16_t GetMaximumNumberOfOutVRegs() const {
169 return maximum_number_of_out_vregs_;
170 }
171
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000172 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
173 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100174 }
175
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000176 void UpdateTemporariesVRegSlots(size_t slots) {
177 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100178 }
179
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000180 size_t GetTemporariesVRegSlots() const {
181 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100182 }
183
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100184 void SetNumberOfVRegs(uint16_t number_of_vregs) {
185 number_of_vregs_ = number_of_vregs;
186 }
187
188 uint16_t GetNumberOfVRegs() const {
189 return number_of_vregs_;
190 }
191
192 void SetNumberOfInVRegs(uint16_t value) {
193 number_of_in_vregs_ = value;
194 }
195
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100196 uint16_t GetNumberOfLocalVRegs() const {
197 return number_of_vregs_ - number_of_in_vregs_;
198 }
199
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100200 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
201 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100202 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100203
Mingyao Yange4335eb2015-03-02 15:14:13 -0800204 bool HasArrayAccesses() const {
205 return has_array_accesses_;
206 }
207
208 void SetHasArrayAccesses(bool value) {
209 has_array_accesses_ = value;
210 }
211
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000212 bool IsDebuggable() const { return debuggable_; }
213
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000214 HNullConstant* GetNullConstant();
215
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000216 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000217 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
218 void VisitBlockForDominatorTree(HBasicBlock* block,
219 HBasicBlock* predecessor,
220 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100221 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000222 void VisitBlockForBackEdges(HBasicBlock* block,
223 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100224 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000225 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000226 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
227
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000228 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000229
230 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000231 GrowableArray<HBasicBlock*> blocks_;
232
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100233 // List of blocks to perform a reverse post order tree traversal.
234 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000235
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000236 HBasicBlock* entry_block_;
237 HBasicBlock* exit_block_;
238
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100239 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100240 uint16_t maximum_number_of_out_vregs_;
241
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100242 // The number of virtual registers in this method. Contains the parameters.
243 uint16_t number_of_vregs_;
244
245 // The number of virtual registers used by parameters of this method.
246 uint16_t number_of_in_vregs_;
247
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000248 // Number of vreg size slots that the temporaries use (used in baseline compiler).
249 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100250
Mingyao Yange4335eb2015-03-02 15:14:13 -0800251 // Has array accesses. We can totally skip BCE if it's false.
252 bool has_array_accesses_;
253
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000254 // Indicates whether the graph should be compiled in a way that
255 // ensures full debuggability. If false, we can apply more
256 // aggressive optimizations that may limit the level of debugging.
257 const bool debuggable_;
258
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000259 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000260 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000261
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000262 // Cached null constant that might be created when building SSA form.
263 HNullConstant* cached_null_constant_;
264
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000265 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000266 DISALLOW_COPY_AND_ASSIGN(HGraph);
267};
268
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700269class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270 public:
271 HLoopInformation(HBasicBlock* header, HGraph* graph)
272 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100273 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100274 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100275 // Make bit vector growable, as the number of blocks may change.
276 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100277
278 HBasicBlock* GetHeader() const {
279 return header_;
280 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000281
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000282 void SetHeader(HBasicBlock* block) {
283 header_ = block;
284 }
285
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100286 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
287 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
288 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
289
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000290 void AddBackEdge(HBasicBlock* back_edge) {
291 back_edges_.Add(back_edge);
292 }
293
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100294 void RemoveBackEdge(HBasicBlock* back_edge) {
295 back_edges_.Delete(back_edge);
296 }
297
298 bool IsBackEdge(HBasicBlock* block) {
299 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
300 if (back_edges_.Get(i) == block) return true;
301 }
302 return false;
303 }
304
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000305 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000306 return back_edges_.Size();
307 }
308
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100309 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100310
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100311 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
312 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100313 }
314
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100315 void ClearBackEdges() {
316 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100317 }
318
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100319 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
320 // that is the header dominates the back edge.
321 bool Populate();
322
323 // Returns whether this loop information contains `block`.
324 // Note that this loop information *must* be populated before entering this function.
325 bool Contains(const HBasicBlock& block) const;
326
327 // Returns whether this loop information is an inner loop of `other`.
328 // Note that `other` *must* be populated before entering this function.
329 bool IsIn(const HLoopInformation& other) const;
330
331 const ArenaBitVector& GetBlocks() const { return blocks_; }
332
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000333 void Add(HBasicBlock* block);
334
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000335 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100336 // Internal recursive implementation of `Populate`.
337 void PopulateRecursive(HBasicBlock* block);
338
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000339 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100340 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000341 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100342 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000343
344 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
345};
346
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100347static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100348static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100349
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000350// A block in a method. Contains the list of instructions represented
351// as a double linked list. Each block knows its predecessors and
352// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100353
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700354class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100356 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000357 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000358 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
359 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000360 loop_information_(nullptr),
361 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100362 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100363 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100364 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100365 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000366 lifetime_end_(kNoLifetime),
367 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000368
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100369 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
370 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000371 }
372
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100373 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
374 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000375 }
376
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100377 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
378 return dominated_blocks_;
379 }
380
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100381 bool IsEntryBlock() const {
382 return graph_->GetEntryBlock() == this;
383 }
384
385 bool IsExitBlock() const {
386 return graph_->GetExitBlock() == this;
387 }
388
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000389 void AddBackEdge(HBasicBlock* back_edge) {
390 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000391 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000392 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100393 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000394 loop_information_->AddBackEdge(back_edge);
395 }
396
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000397 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000398 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000399
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000400 int GetBlockId() const { return block_id_; }
401 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000402
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000403 HBasicBlock* GetDominator() const { return dominator_; }
404 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100405 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000406 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
407 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
408 if (dominated_blocks_.Get(i) == existing) {
409 dominated_blocks_.Put(i, new_block);
410 return;
411 }
412 }
413 LOG(FATAL) << "Unreachable";
414 UNREACHABLE();
415 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000416
417 int NumberOfBackEdges() const {
418 return loop_information_ == nullptr
419 ? 0
420 : loop_information_->NumberOfBackEdges();
421 }
422
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100423 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
424 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100425 const HInstructionList& GetInstructions() const { return instructions_; }
426 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100427 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000428
429 void AddSuccessor(HBasicBlock* block) {
430 successors_.Add(block);
431 block->predecessors_.Add(this);
432 }
433
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100434 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
435 size_t successor_index = GetSuccessorIndexOf(existing);
436 DCHECK_NE(successor_index, static_cast<size_t>(-1));
437 existing->RemovePredecessor(this);
438 new_block->predecessors_.Add(this);
439 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000440 }
441
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000442 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
443 size_t predecessor_index = GetPredecessorIndexOf(existing);
444 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
445 existing->RemoveSuccessor(this);
446 new_block->successors_.Add(this);
447 predecessors_.Put(predecessor_index, new_block);
448 }
449
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100450 void RemovePredecessor(HBasicBlock* block) {
451 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100452 }
453
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000454 void RemoveSuccessor(HBasicBlock* block) {
455 successors_.Delete(block);
456 }
457
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100458 void ClearAllPredecessors() {
459 predecessors_.Reset();
460 }
461
462 void AddPredecessor(HBasicBlock* block) {
463 predecessors_.Add(block);
464 block->successors_.Add(this);
465 }
466
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100467 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100468 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100469 HBasicBlock* temp = predecessors_.Get(0);
470 predecessors_.Put(0, predecessors_.Get(1));
471 predecessors_.Put(1, temp);
472 }
473
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100474 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
475 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
476 if (predecessors_.Get(i) == predecessor) {
477 return i;
478 }
479 }
480 return -1;
481 }
482
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100483 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
484 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
485 if (successors_.Get(i) == successor) {
486 return i;
487 }
488 }
489 return -1;
490 }
491
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000492 // Split the block into two blocks just after `cursor`. Returns the newly
493 // created block. Note that this method just updates raw block information,
494 // like predecessors, successors, dominators, and instruction list. It does not
495 // update the graph, reverse post order, loop information, nor make sure the
496 // blocks are consistent (for example ending with a control flow instruction).
497 HBasicBlock* SplitAfter(HInstruction* cursor);
498
499 // Merge `other` at the end of `this`. Successors and dominated blocks of
500 // `other` are changed to be successors and dominated blocks of `this`. Note
501 // that this method does not update the graph, reverse post order, loop
502 // information, nor make sure the blocks are consistent (for example ending
503 // with a control flow instruction).
504 void MergeWith(HBasicBlock* other);
505
506 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
507 // of `this` are moved to `other`.
508 // Note that this method does not update the graph, reverse post order, loop
509 // information, nor make sure the blocks are consistent (for example ending
510 void ReplaceWith(HBasicBlock* other);
511
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000512 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100513 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100514 // Replace instruction `initial` with `replacement` within this block.
515 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
516 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100517 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100518 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000519 // RemoveInstruction and RemovePhi delete a given instruction from the respective
520 // instruction list. With 'ensure_safety' set to true, it verifies that the
521 // instruction is not in use and removes it from the use lists of its inputs.
522 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
523 void RemovePhi(HPhi* phi, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100524
525 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100526 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100527 }
528
Roland Levillain6b879dd2014-09-22 17:13:44 +0100529 bool IsLoopPreHeaderFirstPredecessor() const {
530 DCHECK(IsLoopHeader());
531 DCHECK(!GetPredecessors().IsEmpty());
532 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
533 }
534
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100535 HLoopInformation* GetLoopInformation() const {
536 return loop_information_;
537 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000538
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000539 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100540 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000541 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100542 void SetInLoop(HLoopInformation* info) {
543 if (IsLoopHeader()) {
544 // Nothing to do. This just means `info` is an outer loop.
545 } else if (loop_information_ == nullptr) {
546 loop_information_ = info;
547 } else if (loop_information_->Contains(*info->GetHeader())) {
548 // Block is currently part of an outer loop. Make it part of this inner loop.
549 // Note that a non loop header having a loop information means this loop information
550 // has already been populated
551 loop_information_ = info;
552 } else {
553 // Block is part of an inner loop. Do not update the loop information.
554 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
555 // at this point, because this method is being called while populating `info`.
556 }
557 }
558
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000559 // Raw update of the loop information.
560 void SetLoopInformation(HLoopInformation* info) {
561 loop_information_ = info;
562 }
563
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100564 bool IsInLoop() const { return loop_information_ != nullptr; }
565
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100566 // Returns wheter this block dominates the blocked passed as parameter.
567 bool Dominates(HBasicBlock* block) const;
568
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100569 size_t GetLifetimeStart() const { return lifetime_start_; }
570 size_t GetLifetimeEnd() const { return lifetime_end_; }
571
572 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
573 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
574
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100575 uint32_t GetDexPc() const { return dex_pc_; }
576
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000577 bool IsCatchBlock() const { return is_catch_block_; }
578 void SetIsCatchBlock() { is_catch_block_ = true; }
579
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000580 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000581 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000582 GrowableArray<HBasicBlock*> predecessors_;
583 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100584 HInstructionList instructions_;
585 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000586 HLoopInformation* loop_information_;
587 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100588 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000589 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100590 // The dex program counter of the first instruction of this block.
591 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100592 size_t lifetime_start_;
593 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000594 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000595
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000596 friend class HGraph;
597 friend class HInstruction;
598
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000599 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
600};
601
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100602#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
603 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000604 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000605 M(ArrayGet, Instruction) \
606 M(ArrayLength, Instruction) \
607 M(ArraySet, Instruction) \
608 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000609 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000610 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100611 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000612 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100613 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000614 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000615 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000616 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100617 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000618 M(Exit, Instruction) \
619 M(FloatConstant, Constant) \
620 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100621 M(GreaterThan, Condition) \
622 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100623 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000624 M(InstanceFieldGet, Instruction) \
625 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000626 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100627 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000628 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000629 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100630 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000631 M(LessThan, Condition) \
632 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000633 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000634 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100635 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000636 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100637 M(Local, Instruction) \
638 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000639 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000640 M(Mul, BinaryOperation) \
641 M(Neg, UnaryOperation) \
642 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100643 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100644 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000645 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000646 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000647 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000648 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100649 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000650 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100651 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000652 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100653 M(Return, Instruction) \
654 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000655 M(Shl, BinaryOperation) \
656 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100657 M(StaticFieldGet, Instruction) \
658 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100659 M(StoreLocal, Instruction) \
660 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100661 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000662 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000663 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000664 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000665 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000666 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000667
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100668#define FOR_EACH_INSTRUCTION(M) \
669 FOR_EACH_CONCRETE_INSTRUCTION(M) \
670 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100671 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100672 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100673 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700674
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100675#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000676FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
677#undef FORWARD_DECLARATION
678
Roland Levillainccc07a92014-09-16 14:48:16 +0100679#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000680 InstructionKind GetKind() const OVERRIDE { return k##type; } \
681 const char* DebugName() const OVERRIDE { return #type; } \
682 const H##type* As##type() const OVERRIDE { return this; } \
683 H##type* As##type() OVERRIDE { return this; } \
684 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100685 return other->Is##type(); \
686 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000687 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000688
David Brazdiled596192015-01-23 10:39:45 +0000689template <typename T> class HUseList;
690
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100691template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700692class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000693 public:
David Brazdiled596192015-01-23 10:39:45 +0000694 HUseListNode* GetPrevious() const { return prev_; }
695 HUseListNode* GetNext() const { return next_; }
696 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100697 size_t GetIndex() const { return index_; }
698
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000699 private:
David Brazdiled596192015-01-23 10:39:45 +0000700 HUseListNode(T user, size_t index)
701 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
702
703 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100704 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000705 HUseListNode<T>* prev_;
706 HUseListNode<T>* next_;
707
708 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000709
710 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
711};
712
David Brazdiled596192015-01-23 10:39:45 +0000713template <typename T>
714class HUseList : public ValueObject {
715 public:
716 HUseList() : first_(nullptr) {}
717
718 void Clear() {
719 first_ = nullptr;
720 }
721
722 // Adds a new entry at the beginning of the use list and returns
723 // the newly created node.
724 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000725 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000726 if (IsEmpty()) {
727 first_ = new_node;
728 } else {
729 first_->prev_ = new_node;
730 new_node->next_ = first_;
731 first_ = new_node;
732 }
733 return new_node;
734 }
735
736 HUseListNode<T>* GetFirst() const {
737 return first_;
738 }
739
740 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000741 DCHECK(node != nullptr);
742 DCHECK(Contains(node));
743
David Brazdiled596192015-01-23 10:39:45 +0000744 if (node->prev_ != nullptr) {
745 node->prev_->next_ = node->next_;
746 }
747 if (node->next_ != nullptr) {
748 node->next_->prev_ = node->prev_;
749 }
750 if (node == first_) {
751 first_ = node->next_;
752 }
753 }
754
David Brazdil1abb4192015-02-17 18:33:36 +0000755 bool Contains(const HUseListNode<T>* node) const {
756 if (node == nullptr) {
757 return false;
758 }
759 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
760 if (current == node) {
761 return true;
762 }
763 }
764 return false;
765 }
766
David Brazdiled596192015-01-23 10:39:45 +0000767 bool IsEmpty() const {
768 return first_ == nullptr;
769 }
770
771 bool HasOnlyOneUse() const {
772 return first_ != nullptr && first_->next_ == nullptr;
773 }
774
775 private:
776 HUseListNode<T>* first_;
777};
778
779template<typename T>
780class HUseIterator : public ValueObject {
781 public:
782 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
783
784 bool Done() const { return current_ == nullptr; }
785
786 void Advance() {
787 DCHECK(!Done());
788 current_ = current_->GetNext();
789 }
790
791 HUseListNode<T>* Current() const {
792 DCHECK(!Done());
793 return current_;
794 }
795
796 private:
797 HUseListNode<T>* current_;
798
799 friend class HValue;
800};
801
David Brazdil1abb4192015-02-17 18:33:36 +0000802// This class is used by HEnvironment and HInstruction classes to record the
803// instructions they use and pointers to the corresponding HUseListNodes kept
804// by the used instructions.
805template <typename T>
806class HUserRecord : public ValueObject {
807 public:
808 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
809 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
810
811 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
812 : instruction_(old_record.instruction_), use_node_(use_node) {
813 DCHECK(instruction_ != nullptr);
814 DCHECK(use_node_ != nullptr);
815 DCHECK(old_record.use_node_ == nullptr);
816 }
817
818 HInstruction* GetInstruction() const { return instruction_; }
819 HUseListNode<T>* GetUseNode() const { return use_node_; }
820
821 private:
822 // Instruction used by the user.
823 HInstruction* instruction_;
824
825 // Corresponding entry in the use list kept by 'instruction_'.
826 HUseListNode<T>* use_node_;
827};
828
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100829// Represents the side effects an instruction may have.
830class SideEffects : public ValueObject {
831 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100832 SideEffects() : flags_(0) {}
833
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100834 static SideEffects None() {
835 return SideEffects(0);
836 }
837
838 static SideEffects All() {
839 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
840 }
841
842 static SideEffects ChangesSomething() {
843 return SideEffects((1 << kFlagChangesCount) - 1);
844 }
845
846 static SideEffects DependsOnSomething() {
847 int count = kFlagDependsOnCount - kFlagChangesCount;
848 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
849 }
850
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100851 SideEffects Union(SideEffects other) const {
852 return SideEffects(flags_ | other.flags_);
853 }
854
Roland Levillain72bceff2014-09-15 18:29:00 +0100855 bool HasSideEffects() const {
856 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
857 return (flags_ & all_bits_set) != 0;
858 }
859
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100860 bool HasAllSideEffects() const {
861 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
862 return all_bits_set == (flags_ & all_bits_set);
863 }
864
865 bool DependsOn(SideEffects other) const {
866 size_t depends_flags = other.ComputeDependsFlags();
867 return (flags_ & depends_flags) != 0;
868 }
869
870 bool HasDependencies() const {
871 int count = kFlagDependsOnCount - kFlagChangesCount;
872 size_t all_bits_set = (1 << count) - 1;
873 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
874 }
875
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100876 private:
877 static constexpr int kFlagChangesSomething = 0;
878 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
879
880 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
881 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
882
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100883 explicit SideEffects(size_t flags) : flags_(flags) {}
884
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100885 size_t ComputeDependsFlags() const {
886 return flags_ << kFlagChangesCount;
887 }
888
889 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100890};
891
David Brazdiled596192015-01-23 10:39:45 +0000892// A HEnvironment object contains the values of virtual registers at a given location.
893class HEnvironment : public ArenaObject<kArenaAllocMisc> {
894 public:
895 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
896 : vregs_(arena, number_of_vregs) {
897 vregs_.SetSize(number_of_vregs);
898 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000899 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000900 }
901 }
902
903 void CopyFrom(HEnvironment* env);
904
905 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +0000906 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +0000907 }
908
909 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +0000910 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +0000911 }
912
David Brazdil1abb4192015-02-17 18:33:36 +0000913 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +0000914
915 size_t Size() const { return vregs_.Size(); }
916
917 private:
David Brazdil1abb4192015-02-17 18:33:36 +0000918 // Record instructions' use entries of this environment for constant-time removal.
919 // It should only be called by HInstruction when a new environment use is added.
920 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
921 DCHECK(env_use->GetUser() == this);
922 size_t index = env_use->GetIndex();
923 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
924 }
David Brazdiled596192015-01-23 10:39:45 +0000925
David Brazdil1abb4192015-02-17 18:33:36 +0000926 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +0000927
David Brazdil1abb4192015-02-17 18:33:36 +0000928 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +0000929
930 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
931};
932
Calin Juravleacf735c2015-02-12 15:25:22 +0000933class ReferenceTypeInfo : ValueObject {
934 public:
Calin Juravleb1498f62015-02-16 13:13:29 +0000935 typedef Handle<mirror::Class> TypeHandle;
936
937 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
938 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
939 if (type_handle->IsObjectClass()) {
940 // Override the type handle to be consistent with the case when we get to
941 // Top but don't have the Object class available. It avoids having to guess
942 // what value the type_handle has when it's Top.
943 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
944 } else {
945 return ReferenceTypeInfo(type_handle, is_exact, false);
946 }
947 }
948
949 static ReferenceTypeInfo CreateTop(bool is_exact) {
950 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +0000951 }
952
953 bool IsExact() const { return is_exact_; }
954 bool IsTop() const { return is_top_; }
955
956 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
957
Calin Juravleb1498f62015-02-16 13:13:29 +0000958 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000959 if (IsTop()) {
960 // Top (equivalent for java.lang.Object) is supertype of anything.
961 return true;
962 }
963 if (rti.IsTop()) {
964 // If we get here `this` is not Top() so it can't be a supertype.
965 return false;
966 }
967 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
968 }
969
970 // Returns true if the type information provide the same amount of details.
971 // Note that it does not mean that the instructions have the same actual type
972 // (e.g. tops are equal but they can be the result of a merge).
973 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
974 if (IsExact() != rti.IsExact()) {
975 return false;
976 }
977 if (IsTop() && rti.IsTop()) {
978 // `Top` means java.lang.Object, so the types are equivalent.
979 return true;
980 }
981 if (IsTop() || rti.IsTop()) {
982 // If only one is top or object than they are not equivalent.
983 // NB: We need this extra check because the type_handle of `Top` is invalid
984 // and we cannot inspect its reference.
985 return false;
986 }
987
988 // Finally check the types.
989 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
990 }
991
992 private:
Calin Juravleb1498f62015-02-16 13:13:29 +0000993 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
994 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
995 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
996
Calin Juravleacf735c2015-02-12 15:25:22 +0000997 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +0000998 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +0000999 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001000 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001001 bool is_exact_;
1002 // A true value here means that the object type should be java.lang.Object.
1003 // We don't have access to the corresponding mirror object every time so this
1004 // flag acts as a substitute. When true, the TypeHandle refers to a null
1005 // pointer and should not be used.
1006 bool is_top_;
1007};
1008
1009std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1010
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001011class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001012 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001013 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001014 : previous_(nullptr),
1015 next_(nullptr),
1016 block_(nullptr),
1017 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001018 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001019 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001020 locations_(nullptr),
1021 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001022 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001023 side_effects_(side_effects),
1024 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001025
Dave Allison20dfc792014-06-16 20:44:29 -07001026 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001027
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001028#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001029 enum InstructionKind {
1030 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1031 };
1032#undef DECLARE_KIND
1033
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001034 HInstruction* GetNext() const { return next_; }
1035 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001036
Calin Juravle77520bc2015-01-12 18:45:46 +00001037 HInstruction* GetNextDisregardingMoves() const;
1038 HInstruction* GetPreviousDisregardingMoves() const;
1039
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001040 HBasicBlock* GetBlock() const { return block_; }
1041 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001042 bool IsInBlock() const { return block_ != nullptr; }
1043 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001044 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001045
Roland Levillain6b879dd2014-09-22 17:13:44 +01001046 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001047 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001048
1049 virtual void Accept(HGraphVisitor* visitor) = 0;
1050 virtual const char* DebugName() const = 0;
1051
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001052 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001053 void SetRawInputAt(size_t index, HInstruction* input) {
1054 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1055 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001056
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001057 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001058 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001059 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001060 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001061
Calin Juravle61d544b2015-02-23 16:46:57 +00001062 virtual bool ActAsNullConstant() const { return false; }
1063
Calin Juravle10e244f2015-01-26 18:54:32 +00001064 // Does not apply for all instructions, but having this at top level greatly
1065 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001066 virtual bool CanBeNull() const {
1067 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1068 return true;
1069 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001070
Calin Juravle77520bc2015-01-12 18:45:46 +00001071 virtual bool CanDoImplicitNullCheck() const { return false; }
1072
Calin Juravleacf735c2015-02-12 15:25:22 +00001073 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001074 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001075 reference_type_info_ = reference_type_info;
1076 }
1077
Calin Juravle61d544b2015-02-23 16:46:57 +00001078 ReferenceTypeInfo GetReferenceTypeInfo() const {
1079 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1080 return reference_type_info_;
1081 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001082
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001083 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001084 DCHECK(user != nullptr);
1085 HUseListNode<HInstruction*>* use =
1086 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1087 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001088 }
1089
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001090 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001091 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001092 HUseListNode<HEnvironment*>* env_use =
1093 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1094 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001095 }
1096
David Brazdil1abb4192015-02-17 18:33:36 +00001097 void RemoveAsUserOfInput(size_t input) {
1098 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1099 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1100 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001101
David Brazdil1abb4192015-02-17 18:33:36 +00001102 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1103 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001104
David Brazdiled596192015-01-23 10:39:45 +00001105 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1106 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001107
Roland Levillain6c82d402014-10-13 16:10:27 +01001108 // Does this instruction strictly dominate `other_instruction`?
1109 // Returns false if this instruction and `other_instruction` are the same.
1110 // Aborts if this instruction and `other_instruction` are both phis.
1111 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001112
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001113 int GetId() const { return id_; }
1114 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001115
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001116 int GetSsaIndex() const { return ssa_index_; }
1117 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1118 bool HasSsaIndex() const { return ssa_index_ != -1; }
1119
1120 bool HasEnvironment() const { return environment_ != nullptr; }
1121 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001122 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
1123
Nicolas Geoffray39468442014-09-02 15:17:15 +01001124 // Returns the number of entries in the environment. Typically, that is the
1125 // number of dex registers in a method. It could be more in case of inlining.
1126 size_t EnvironmentSize() const;
1127
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001128 LocationSummary* GetLocations() const { return locations_; }
1129 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001130
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001131 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001132 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001133
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001134 // Move `this` instruction before `cursor`.
1135 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001136
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001137#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001138 bool Is##type() const { return (As##type() != nullptr); } \
1139 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001140 virtual H##type* As##type() { return nullptr; }
1141
1142 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1143#undef INSTRUCTION_TYPE_CHECK
1144
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001145 // Returns whether the instruction can be moved within the graph.
1146 virtual bool CanBeMoved() const { return false; }
1147
1148 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001149 virtual bool InstructionTypeEquals(HInstruction* other) const {
1150 UNUSED(other);
1151 return false;
1152 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001153
1154 // Returns whether any data encoded in the two instructions is equal.
1155 // This method does not look at the inputs. Both instructions must be
1156 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001157 virtual bool InstructionDataEquals(HInstruction* other) const {
1158 UNUSED(other);
1159 return false;
1160 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001161
1162 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001163 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001164 // 2) Their inputs are identical.
1165 bool Equals(HInstruction* other) const;
1166
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001167 virtual InstructionKind GetKind() const = 0;
1168
1169 virtual size_t ComputeHashCode() const {
1170 size_t result = GetKind();
1171 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1172 result = (result * 31) + InputAt(i)->GetId();
1173 }
1174 return result;
1175 }
1176
1177 SideEffects GetSideEffects() const { return side_effects_; }
1178
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001179 size_t GetLifetimePosition() const { return lifetime_position_; }
1180 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1181 LiveInterval* GetLiveInterval() const { return live_interval_; }
1182 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1183 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1184
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001185 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1186
1187 // Returns whether the code generation of the instruction will require to have access
1188 // to the current method. Such instructions are:
1189 // (1): Instructions that require an environment, as calling the runtime requires
1190 // to walk the stack and have the current method stored at a specific stack address.
1191 // (2): Object literals like classes and strings, that are loaded from the dex cache
1192 // fields of the current method.
1193 bool NeedsCurrentMethod() const {
1194 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1195 }
1196
David Brazdil1abb4192015-02-17 18:33:36 +00001197 protected:
1198 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1199 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1200
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001201 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001202 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1203
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001204 HInstruction* previous_;
1205 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001206 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001207
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001208 // An instruction gets an id when it is added to the graph.
1209 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001210 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001211 int id_;
1212
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001213 // When doing liveness analysis, instructions that have uses get an SSA index.
1214 int ssa_index_;
1215
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001216 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001217 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001218
1219 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001220 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001221
Nicolas Geoffray39468442014-09-02 15:17:15 +01001222 // The environment associated with this instruction. Not null if the instruction
1223 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001224 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001225
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001226 // Set by the code generator.
1227 LocationSummary* locations_;
1228
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001229 // Set by the liveness analysis.
1230 LiveInterval* live_interval_;
1231
1232 // Set by the liveness analysis, this is the position in a linear
1233 // order of blocks where this instruction's live interval start.
1234 size_t lifetime_position_;
1235
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001236 const SideEffects side_effects_;
1237
Calin Juravleacf735c2015-02-12 15:25:22 +00001238 // TODO: for primitive types this should be marked as invalid.
1239 ReferenceTypeInfo reference_type_info_;
1240
David Brazdil1abb4192015-02-17 18:33:36 +00001241 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001242 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001243 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001244 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001245 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001246
1247 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1248};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001249std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001250
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001251class HInputIterator : public ValueObject {
1252 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001253 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001254
1255 bool Done() const { return index_ == instruction_->InputCount(); }
1256 HInstruction* Current() const { return instruction_->InputAt(index_); }
1257 void Advance() { index_++; }
1258
1259 private:
1260 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001261 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001262
1263 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1264};
1265
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001266class HInstructionIterator : public ValueObject {
1267 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001268 explicit HInstructionIterator(const HInstructionList& instructions)
1269 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001270 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001271 }
1272
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001273 bool Done() const { return instruction_ == nullptr; }
1274 HInstruction* Current() const { return instruction_; }
1275 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001276 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001277 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001278 }
1279
1280 private:
1281 HInstruction* instruction_;
1282 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001283
1284 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001285};
1286
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001287class HBackwardInstructionIterator : public ValueObject {
1288 public:
1289 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1290 : instruction_(instructions.last_instruction_) {
1291 next_ = Done() ? nullptr : instruction_->GetPrevious();
1292 }
1293
1294 bool Done() const { return instruction_ == nullptr; }
1295 HInstruction* Current() const { return instruction_; }
1296 void Advance() {
1297 instruction_ = next_;
1298 next_ = Done() ? nullptr : instruction_->GetPrevious();
1299 }
1300
1301 private:
1302 HInstruction* instruction_;
1303 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001304
1305 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001306};
1307
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001308// An embedded container with N elements of type T. Used (with partial
1309// specialization for N=0) because embedded arrays cannot have size 0.
1310template<typename T, intptr_t N>
1311class EmbeddedArray {
1312 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001313 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001314
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001315 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001316
1317 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001318 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001319 return elements_[i];
1320 }
1321
1322 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001323 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001324 return elements_[i];
1325 }
1326
1327 const T& At(intptr_t i) const {
1328 return (*this)[i];
1329 }
1330
1331 void SetAt(intptr_t i, const T& val) {
1332 (*this)[i] = val;
1333 }
1334
1335 private:
1336 T elements_[N];
1337};
1338
1339template<typename T>
1340class EmbeddedArray<T, 0> {
1341 public:
1342 intptr_t length() const { return 0; }
1343 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001344 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001345 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001346 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001347 }
1348 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001349 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001350 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001351 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001352 }
1353};
1354
1355template<intptr_t N>
1356class HTemplateInstruction: public HInstruction {
1357 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001358 HTemplateInstruction<N>(SideEffects side_effects)
1359 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001360 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001361
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001362 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001363
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001364 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001365 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1366
1367 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1368 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001369 }
1370
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001371 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001372 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001373
1374 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001375};
1376
Dave Allison20dfc792014-06-16 20:44:29 -07001377template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001378class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001379 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001380 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1381 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001382 virtual ~HExpression() {}
1383
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001384 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001385
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001386 protected:
1387 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001388};
1389
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001390// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1391// instruction that branches to the exit block.
1392class HReturnVoid : public HTemplateInstruction<0> {
1393 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001394 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001395
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001396 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001397
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001398 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001399
1400 private:
1401 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1402};
1403
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001404// Represents dex's RETURN opcodes. A HReturn is a control flow
1405// instruction that branches to the exit block.
1406class HReturn : public HTemplateInstruction<1> {
1407 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001408 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001409 SetRawInputAt(0, value);
1410 }
1411
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001412 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001413
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001414 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001415
1416 private:
1417 DISALLOW_COPY_AND_ASSIGN(HReturn);
1418};
1419
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001420// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001421// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001422// exit block.
1423class HExit : public HTemplateInstruction<0> {
1424 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001425 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001426
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001427 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001428
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001429 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001430
1431 private:
1432 DISALLOW_COPY_AND_ASSIGN(HExit);
1433};
1434
1435// Jumps from one block to another.
1436class HGoto : public HTemplateInstruction<0> {
1437 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001438 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1439
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001440 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001441
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001442 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001443 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001444 }
1445
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001446 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001447
1448 private:
1449 DISALLOW_COPY_AND_ASSIGN(HGoto);
1450};
1451
Dave Allison20dfc792014-06-16 20:44:29 -07001452
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001453// Conditional branch. A block ending with an HIf instruction must have
1454// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001455class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001456 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001457 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001458 SetRawInputAt(0, input);
1459 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001460
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001461 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001462
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001463 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001464 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001465 }
1466
1467 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001468 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001469 }
1470
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001471 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001472
Dave Allison20dfc792014-06-16 20:44:29 -07001473 virtual bool IsIfInstruction() const { return true; }
1474
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001475 private:
1476 DISALLOW_COPY_AND_ASSIGN(HIf);
1477};
1478
Roland Levillain88cb1752014-10-20 16:36:47 +01001479class HUnaryOperation : public HExpression<1> {
1480 public:
1481 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1482 : HExpression(result_type, SideEffects::None()) {
1483 SetRawInputAt(0, input);
1484 }
1485
1486 HInstruction* GetInput() const { return InputAt(0); }
1487 Primitive::Type GetResultType() const { return GetType(); }
1488
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001489 bool CanBeMoved() const OVERRIDE { return true; }
1490 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001491 UNUSED(other);
1492 return true;
1493 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001494
Roland Levillain9240d6a2014-10-20 16:47:04 +01001495 // Try to statically evaluate `operation` and return a HConstant
1496 // containing the result of this evaluation. If `operation` cannot
1497 // be evaluated as a constant, return nullptr.
1498 HConstant* TryStaticEvaluation() const;
1499
1500 // Apply this operation to `x`.
1501 virtual int32_t Evaluate(int32_t x) const = 0;
1502 virtual int64_t Evaluate(int64_t x) const = 0;
1503
Roland Levillain88cb1752014-10-20 16:36:47 +01001504 DECLARE_INSTRUCTION(UnaryOperation);
1505
1506 private:
1507 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1508};
1509
Dave Allison20dfc792014-06-16 20:44:29 -07001510class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001511 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001512 HBinaryOperation(Primitive::Type result_type,
1513 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001514 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001515 SetRawInputAt(0, left);
1516 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001517 }
1518
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001519 HInstruction* GetLeft() const { return InputAt(0); }
1520 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001521 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001522
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001523 virtual bool IsCommutative() const { return false; }
1524
1525 // Put constant on the right.
1526 // Returns whether order is changed.
1527 bool OrderInputsWithConstantOnTheRight() {
1528 HInstruction* left = InputAt(0);
1529 HInstruction* right = InputAt(1);
1530 if (left->IsConstant() && !right->IsConstant()) {
1531 ReplaceInput(right, 0);
1532 ReplaceInput(left, 1);
1533 return true;
1534 }
1535 return false;
1536 }
1537
1538 // Order inputs by instruction id, but favor constant on the right side.
1539 // This helps GVN for commutative ops.
1540 void OrderInputs() {
1541 DCHECK(IsCommutative());
1542 HInstruction* left = InputAt(0);
1543 HInstruction* right = InputAt(1);
1544 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1545 return;
1546 }
1547 if (OrderInputsWithConstantOnTheRight()) {
1548 return;
1549 }
1550 // Order according to instruction id.
1551 if (left->GetId() > right->GetId()) {
1552 ReplaceInput(right, 0);
1553 ReplaceInput(left, 1);
1554 }
1555 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001556
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001557 bool CanBeMoved() const OVERRIDE { return true; }
1558 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001559 UNUSED(other);
1560 return true;
1561 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001562
Roland Levillain9240d6a2014-10-20 16:47:04 +01001563 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001564 // containing the result of this evaluation. If `operation` cannot
1565 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001566 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001567
1568 // Apply this operation to `x` and `y`.
1569 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1570 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1571
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001572 // Returns an input that can legally be used as the right input and is
1573 // constant, or nullptr.
1574 HConstant* GetConstantRight() const;
1575
1576 // If `GetConstantRight()` returns one of the input, this returns the other
1577 // one. Otherwise it returns nullptr.
1578 HInstruction* GetLeastConstantLeft() const;
1579
Roland Levillainccc07a92014-09-16 14:48:16 +01001580 DECLARE_INSTRUCTION(BinaryOperation);
1581
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001582 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001583 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1584};
1585
Dave Allison20dfc792014-06-16 20:44:29 -07001586class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001587 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001588 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001589 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1590 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001591
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001592 bool NeedsMaterialization() const { return needs_materialization_; }
1593 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001594
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001595 // For code generation purposes, returns whether this instruction is just before
1596 // `if_`, and disregard moves in between.
1597 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1598
Dave Allison20dfc792014-06-16 20:44:29 -07001599 DECLARE_INSTRUCTION(Condition);
1600
1601 virtual IfCondition GetCondition() const = 0;
1602
1603 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001604 // For register allocation purposes, returns whether this instruction needs to be
1605 // materialized (that is, not just be in the processor flags).
1606 bool needs_materialization_;
1607
Dave Allison20dfc792014-06-16 20:44:29 -07001608 DISALLOW_COPY_AND_ASSIGN(HCondition);
1609};
1610
1611// Instruction to check if two inputs are equal to each other.
1612class HEqual : public HCondition {
1613 public:
1614 HEqual(HInstruction* first, HInstruction* second)
1615 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001616
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001617 bool IsCommutative() const OVERRIDE { return true; }
1618
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001619 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001620 return x == y ? 1 : 0;
1621 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001622 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001623 return x == y ? 1 : 0;
1624 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001625
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001626 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001627
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001628 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001629 return kCondEQ;
1630 }
1631
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001632 private:
1633 DISALLOW_COPY_AND_ASSIGN(HEqual);
1634};
1635
Dave Allison20dfc792014-06-16 20:44:29 -07001636class HNotEqual : public HCondition {
1637 public:
1638 HNotEqual(HInstruction* first, HInstruction* second)
1639 : HCondition(first, second) {}
1640
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001641 bool IsCommutative() const OVERRIDE { return true; }
1642
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001643 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001644 return x != y ? 1 : 0;
1645 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001646 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001647 return x != y ? 1 : 0;
1648 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001649
Dave Allison20dfc792014-06-16 20:44:29 -07001650 DECLARE_INSTRUCTION(NotEqual);
1651
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001652 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001653 return kCondNE;
1654 }
1655
1656 private:
1657 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1658};
1659
1660class HLessThan : public HCondition {
1661 public:
1662 HLessThan(HInstruction* first, HInstruction* second)
1663 : HCondition(first, second) {}
1664
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001665 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001666 return x < y ? 1 : 0;
1667 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001668 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001669 return x < y ? 1 : 0;
1670 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001671
Dave Allison20dfc792014-06-16 20:44:29 -07001672 DECLARE_INSTRUCTION(LessThan);
1673
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001674 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001675 return kCondLT;
1676 }
1677
1678 private:
1679 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1680};
1681
1682class HLessThanOrEqual : public HCondition {
1683 public:
1684 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1685 : HCondition(first, second) {}
1686
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001687 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001688 return x <= y ? 1 : 0;
1689 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001690 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001691 return x <= y ? 1 : 0;
1692 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001693
Dave Allison20dfc792014-06-16 20:44:29 -07001694 DECLARE_INSTRUCTION(LessThanOrEqual);
1695
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001696 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001697 return kCondLE;
1698 }
1699
1700 private:
1701 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1702};
1703
1704class HGreaterThan : public HCondition {
1705 public:
1706 HGreaterThan(HInstruction* first, HInstruction* second)
1707 : HCondition(first, second) {}
1708
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001709 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001710 return x > y ? 1 : 0;
1711 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001712 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001713 return x > y ? 1 : 0;
1714 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001715
Dave Allison20dfc792014-06-16 20:44:29 -07001716 DECLARE_INSTRUCTION(GreaterThan);
1717
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001718 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001719 return kCondGT;
1720 }
1721
1722 private:
1723 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1724};
1725
1726class HGreaterThanOrEqual : public HCondition {
1727 public:
1728 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1729 : HCondition(first, second) {}
1730
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001731 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001732 return x >= y ? 1 : 0;
1733 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001734 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001735 return x >= y ? 1 : 0;
1736 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001737
Dave Allison20dfc792014-06-16 20:44:29 -07001738 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1739
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001740 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001741 return kCondGE;
1742 }
1743
1744 private:
1745 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1746};
1747
1748
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001749// Instruction to check how two inputs compare to each other.
1750// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1751class HCompare : public HBinaryOperation {
1752 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001753 // The bias applies for floating point operations and indicates how NaN
1754 // comparisons are treated:
1755 enum Bias {
1756 kNoBias, // bias is not applicable (i.e. for long operation)
1757 kGtBias, // return 1 for NaN comparisons
1758 kLtBias, // return -1 for NaN comparisons
1759 };
1760
1761 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1762 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001763 DCHECK_EQ(type, first->GetType());
1764 DCHECK_EQ(type, second->GetType());
1765 }
1766
Calin Juravleddb7df22014-11-25 20:56:51 +00001767 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001768 return
1769 x == y ? 0 :
1770 x > y ? 1 :
1771 -1;
1772 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001773
1774 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001775 return
1776 x == y ? 0 :
1777 x > y ? 1 :
1778 -1;
1779 }
1780
Calin Juravleddb7df22014-11-25 20:56:51 +00001781 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1782 return bias_ == other->AsCompare()->bias_;
1783 }
1784
1785 bool IsGtBias() { return bias_ == kGtBias; }
1786
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001787 DECLARE_INSTRUCTION(Compare);
1788
1789 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001790 const Bias bias_;
1791
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001792 DISALLOW_COPY_AND_ASSIGN(HCompare);
1793};
1794
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001795// A local in the graph. Corresponds to a Dex register.
1796class HLocal : public HTemplateInstruction<0> {
1797 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001798 explicit HLocal(uint16_t reg_number)
1799 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001800
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001801 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001802
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001803 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001804
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001805 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001806 // The Dex register number.
1807 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001808
1809 DISALLOW_COPY_AND_ASSIGN(HLocal);
1810};
1811
1812// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001813class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001814 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001815 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001816 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001817 SetRawInputAt(0, local);
1818 }
1819
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001820 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1821
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001822 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001823
1824 private:
1825 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1826};
1827
1828// Store a value in a given local. This instruction has two inputs: the value
1829// and the local.
1830class HStoreLocal : public HTemplateInstruction<2> {
1831 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001832 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001833 SetRawInputAt(0, local);
1834 SetRawInputAt(1, value);
1835 }
1836
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001837 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1838
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001839 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001840
1841 private:
1842 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1843};
1844
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001845class HConstant : public HExpression<0> {
1846 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001847 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1848
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001849 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001850
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001851 virtual bool IsMinusOne() const { return false; }
1852 virtual bool IsZero() const { return false; }
1853 virtual bool IsOne() const { return false; }
1854
1855 static HConstant* NewConstant(ArenaAllocator* allocator, Primitive::Type type, int64_t val);
1856
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001857 DECLARE_INSTRUCTION(Constant);
1858
1859 private:
1860 DISALLOW_COPY_AND_ASSIGN(HConstant);
1861};
1862
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001863class HFloatConstant : public HConstant {
1864 public:
1865 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1866
1867 float GetValue() const { return value_; }
1868
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001869 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001870 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1871 bit_cast<float, int32_t>(value_);
1872 }
1873
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001874 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001875
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001876 bool IsMinusOne() const OVERRIDE {
1877 return bit_cast<uint32_t>(AsFloatConstant()->GetValue()) == bit_cast<uint32_t>((-1.0f));
1878 }
1879 bool IsZero() const OVERRIDE {
1880 return AsFloatConstant()->GetValue() == 0.0f;
1881 }
1882 bool IsOne() const OVERRIDE {
1883 return bit_cast<uint32_t>(AsFloatConstant()->GetValue()) == bit_cast<uint32_t>(1.0f);
1884 }
1885
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001886 DECLARE_INSTRUCTION(FloatConstant);
1887
1888 private:
1889 const float value_;
1890
1891 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1892};
1893
1894class HDoubleConstant : public HConstant {
1895 public:
1896 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1897
1898 double GetValue() const { return value_; }
1899
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001900 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001901 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1902 bit_cast<double, int64_t>(value_);
1903 }
1904
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001905 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001906
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001907 bool IsMinusOne() const OVERRIDE {
1908 return bit_cast<uint64_t>(AsDoubleConstant()->GetValue()) == bit_cast<uint64_t>((-1.0));
1909 }
1910 bool IsZero() const OVERRIDE {
1911 return AsDoubleConstant()->GetValue() == 0.0;
1912 }
1913 bool IsOne() const OVERRIDE {
1914 return bit_cast<uint64_t>(AsDoubleConstant()->GetValue()) == bit_cast<uint64_t>(1.0);
1915 }
1916
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001917 DECLARE_INSTRUCTION(DoubleConstant);
1918
1919 private:
1920 const double value_;
1921
1922 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1923};
1924
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001925class HNullConstant : public HConstant {
1926 public:
1927 HNullConstant() : HConstant(Primitive::kPrimNot) {}
1928
1929 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
1930 return true;
1931 }
1932
1933 size_t ComputeHashCode() const OVERRIDE { return 0; }
1934
Calin Juravle61d544b2015-02-23 16:46:57 +00001935 bool ActAsNullConstant() const OVERRIDE { return true; }
1936
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001937 DECLARE_INSTRUCTION(NullConstant);
1938
1939 private:
1940 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
1941};
1942
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001943// Constants of the type int. Those can be from Dex instructions, or
1944// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001945class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001946 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001947 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001948
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001949 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001950
Calin Juravle61d544b2015-02-23 16:46:57 +00001951 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001952 return other->AsIntConstant()->value_ == value_;
1953 }
1954
Calin Juravle61d544b2015-02-23 16:46:57 +00001955 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
1956
1957 // TODO: Null is represented by the `0` constant. In most cases we replace it
1958 // with a HNullConstant but we don't do it when comparing (a != null). This
1959 // method is an workaround until we fix the above.
1960 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001961
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001962 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
1963 bool IsZero() const OVERRIDE { return GetValue() == 0; }
1964 bool IsOne() const OVERRIDE { return GetValue() == 1; }
1965
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001966 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001967
1968 private:
1969 const int32_t value_;
1970
1971 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1972};
1973
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001974class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001975 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001976 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001977
1978 int64_t GetValue() const { return value_; }
1979
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001980 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001981 return other->AsLongConstant()->value_ == value_;
1982 }
1983
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001984 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001985
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001986 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
1987 bool IsZero() const OVERRIDE { return GetValue() == 0; }
1988 bool IsOne() const OVERRIDE { return GetValue() == 1; }
1989
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001990 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001991
1992 private:
1993 const int64_t value_;
1994
1995 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1996};
1997
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001998enum class Intrinsics {
1999#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2000#include "intrinsics_list.h"
2001 kNone,
2002 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2003#undef INTRINSICS_LIST
2004#undef OPTIMIZING_INTRINSICS
2005};
2006std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2007
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002008class HInvoke : public HInstruction {
2009 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002010 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002011
2012 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2013 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002014 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002015
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002016 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002017 SetRawInputAt(index, argument);
2018 }
2019
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002020 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002021
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002022 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002023
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002024 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2025
2026 Intrinsics GetIntrinsic() {
2027 return intrinsic_;
2028 }
2029
2030 void SetIntrinsic(Intrinsics intrinsic) {
2031 intrinsic_ = intrinsic;
2032 }
2033
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002034 DECLARE_INSTRUCTION(Invoke);
2035
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002036 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002037 HInvoke(ArenaAllocator* arena,
2038 uint32_t number_of_arguments,
2039 Primitive::Type return_type,
2040 uint32_t dex_pc,
2041 uint32_t dex_method_index)
2042 : HInstruction(SideEffects::All()),
2043 inputs_(arena, number_of_arguments),
2044 return_type_(return_type),
2045 dex_pc_(dex_pc),
2046 dex_method_index_(dex_method_index),
2047 intrinsic_(Intrinsics::kNone) {
2048 inputs_.SetSize(number_of_arguments);
2049 }
2050
David Brazdil1abb4192015-02-17 18:33:36 +00002051 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2052 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2053 inputs_.Put(index, input);
2054 }
2055
2056 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002057 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002058 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002059 const uint32_t dex_method_index_;
2060 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002061
2062 private:
2063 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2064};
2065
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002066class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002067 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002068 HInvokeStaticOrDirect(ArenaAllocator* arena,
2069 uint32_t number_of_arguments,
2070 Primitive::Type return_type,
2071 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002072 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002073 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002074 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002075 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002076 invoke_type_(invoke_type),
2077 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002078
Calin Juravle77520bc2015-01-12 18:45:46 +00002079 bool CanDoImplicitNullCheck() const OVERRIDE {
2080 // We access the method via the dex cache so we can't do an implicit null check.
2081 // TODO: for intrinsics we can generate implicit null checks.
2082 return false;
2083 }
2084
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002085 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002086 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002087
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002088 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002089
2090 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002091 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002092 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002093
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002094 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002095};
2096
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002097class HInvokeVirtual : public HInvoke {
2098 public:
2099 HInvokeVirtual(ArenaAllocator* arena,
2100 uint32_t number_of_arguments,
2101 Primitive::Type return_type,
2102 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002103 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002104 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002105 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002106 vtable_index_(vtable_index) {}
2107
Calin Juravle77520bc2015-01-12 18:45:46 +00002108 bool CanDoImplicitNullCheck() const OVERRIDE {
2109 // TODO: Add implicit null checks in intrinsics.
2110 return !GetLocations()->Intrinsified();
2111 }
2112
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002113 uint32_t GetVTableIndex() const { return vtable_index_; }
2114
2115 DECLARE_INSTRUCTION(InvokeVirtual);
2116
2117 private:
2118 const uint32_t vtable_index_;
2119
2120 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2121};
2122
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002123class HInvokeInterface : public HInvoke {
2124 public:
2125 HInvokeInterface(ArenaAllocator* arena,
2126 uint32_t number_of_arguments,
2127 Primitive::Type return_type,
2128 uint32_t dex_pc,
2129 uint32_t dex_method_index,
2130 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002131 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002132 imt_index_(imt_index) {}
2133
Calin Juravle77520bc2015-01-12 18:45:46 +00002134 bool CanDoImplicitNullCheck() const OVERRIDE {
2135 // TODO: Add implicit null checks in intrinsics.
2136 return !GetLocations()->Intrinsified();
2137 }
2138
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002139 uint32_t GetImtIndex() const { return imt_index_; }
2140 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2141
2142 DECLARE_INSTRUCTION(InvokeInterface);
2143
2144 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002145 const uint32_t imt_index_;
2146
2147 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2148};
2149
Dave Allison20dfc792014-06-16 20:44:29 -07002150class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002151 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002152 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002153 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2154 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002155 type_index_(type_index),
2156 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002157
2158 uint32_t GetDexPc() const { return dex_pc_; }
2159 uint16_t GetTypeIndex() const { return type_index_; }
2160
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002161 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002162 bool NeedsEnvironment() const OVERRIDE { return true; }
2163 // It may throw when called on:
2164 // - interfaces
2165 // - abstract/innaccessible/unknown classes
2166 // TODO: optimize when possible.
2167 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002168
Calin Juravle10e244f2015-01-26 18:54:32 +00002169 bool CanBeNull() const OVERRIDE { return false; }
2170
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002171 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2172
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002173 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002174
2175 private:
2176 const uint32_t dex_pc_;
2177 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002178 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002179
2180 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2181};
2182
Roland Levillain88cb1752014-10-20 16:36:47 +01002183class HNeg : public HUnaryOperation {
2184 public:
2185 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2186 : HUnaryOperation(result_type, input) {}
2187
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002188 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2189 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002190
Roland Levillain88cb1752014-10-20 16:36:47 +01002191 DECLARE_INSTRUCTION(Neg);
2192
2193 private:
2194 DISALLOW_COPY_AND_ASSIGN(HNeg);
2195};
2196
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002197class HNewArray : public HExpression<1> {
2198 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002199 HNewArray(HInstruction* length,
2200 uint32_t dex_pc,
2201 uint16_t type_index,
2202 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002203 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2204 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002205 type_index_(type_index),
2206 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002207 SetRawInputAt(0, length);
2208 }
2209
2210 uint32_t GetDexPc() const { return dex_pc_; }
2211 uint16_t GetTypeIndex() const { return type_index_; }
2212
2213 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002214 bool NeedsEnvironment() const OVERRIDE { return true; }
2215
2216 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002217
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002218 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2219
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002220 DECLARE_INSTRUCTION(NewArray);
2221
2222 private:
2223 const uint32_t dex_pc_;
2224 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002225 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002226
2227 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2228};
2229
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002230class HAdd : public HBinaryOperation {
2231 public:
2232 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2233 : HBinaryOperation(result_type, left, right) {}
2234
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002235 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002236
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002237 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002238 return x + y;
2239 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002240 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002241 return x + y;
2242 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002243
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002244 DECLARE_INSTRUCTION(Add);
2245
2246 private:
2247 DISALLOW_COPY_AND_ASSIGN(HAdd);
2248};
2249
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002250class HSub : public HBinaryOperation {
2251 public:
2252 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2253 : HBinaryOperation(result_type, left, right) {}
2254
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002255 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002256 return x - y;
2257 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002258 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002259 return x - y;
2260 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002261
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002262 DECLARE_INSTRUCTION(Sub);
2263
2264 private:
2265 DISALLOW_COPY_AND_ASSIGN(HSub);
2266};
2267
Calin Juravle34bacdf2014-10-07 20:23:36 +01002268class HMul : public HBinaryOperation {
2269 public:
2270 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2271 : HBinaryOperation(result_type, left, right) {}
2272
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002273 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002274
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002275 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2276 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002277
2278 DECLARE_INSTRUCTION(Mul);
2279
2280 private:
2281 DISALLOW_COPY_AND_ASSIGN(HMul);
2282};
2283
Calin Juravle7c4954d2014-10-28 16:57:40 +00002284class HDiv : public HBinaryOperation {
2285 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002286 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2287 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002288
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002289 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002290 // Our graph structure ensures we never have 0 for `y` during constant folding.
2291 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002292 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002293 return (y == -1) ? -x : x / y;
2294 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002295
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002296 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002297 DCHECK_NE(y, 0);
2298 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2299 return (y == -1) ? -x : x / y;
2300 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002301
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002302 uint32_t GetDexPc() const { return dex_pc_; }
2303
Calin Juravle7c4954d2014-10-28 16:57:40 +00002304 DECLARE_INSTRUCTION(Div);
2305
2306 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002307 const uint32_t dex_pc_;
2308
Calin Juravle7c4954d2014-10-28 16:57:40 +00002309 DISALLOW_COPY_AND_ASSIGN(HDiv);
2310};
2311
Calin Juravlebacfec32014-11-14 15:54:36 +00002312class HRem : public HBinaryOperation {
2313 public:
2314 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2315 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2316
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002317 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002318 DCHECK_NE(y, 0);
2319 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2320 return (y == -1) ? 0 : x % y;
2321 }
2322
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002323 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002324 DCHECK_NE(y, 0);
2325 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2326 return (y == -1) ? 0 : x % y;
2327 }
2328
2329 uint32_t GetDexPc() const { return dex_pc_; }
2330
2331 DECLARE_INSTRUCTION(Rem);
2332
2333 private:
2334 const uint32_t dex_pc_;
2335
2336 DISALLOW_COPY_AND_ASSIGN(HRem);
2337};
2338
Calin Juravled0d48522014-11-04 16:40:20 +00002339class HDivZeroCheck : public HExpression<1> {
2340 public:
2341 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2342 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2343 SetRawInputAt(0, value);
2344 }
2345
2346 bool CanBeMoved() const OVERRIDE { return true; }
2347
2348 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2349 UNUSED(other);
2350 return true;
2351 }
2352
2353 bool NeedsEnvironment() const OVERRIDE { return true; }
2354 bool CanThrow() const OVERRIDE { return true; }
2355
2356 uint32_t GetDexPc() const { return dex_pc_; }
2357
2358 DECLARE_INSTRUCTION(DivZeroCheck);
2359
2360 private:
2361 const uint32_t dex_pc_;
2362
2363 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2364};
2365
Calin Juravle9aec02f2014-11-18 23:06:35 +00002366class HShl : public HBinaryOperation {
2367 public:
2368 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2369 : HBinaryOperation(result_type, left, right) {}
2370
2371 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2372 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2373
2374 DECLARE_INSTRUCTION(Shl);
2375
2376 private:
2377 DISALLOW_COPY_AND_ASSIGN(HShl);
2378};
2379
2380class HShr : public HBinaryOperation {
2381 public:
2382 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2383 : HBinaryOperation(result_type, left, right) {}
2384
2385 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2386 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2387
2388 DECLARE_INSTRUCTION(Shr);
2389
2390 private:
2391 DISALLOW_COPY_AND_ASSIGN(HShr);
2392};
2393
2394class HUShr : public HBinaryOperation {
2395 public:
2396 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2397 : HBinaryOperation(result_type, left, right) {}
2398
2399 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2400 uint32_t ux = static_cast<uint32_t>(x);
2401 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2402 return static_cast<int32_t>(ux >> uy);
2403 }
2404
2405 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2406 uint64_t ux = static_cast<uint64_t>(x);
2407 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2408 return static_cast<int64_t>(ux >> uy);
2409 }
2410
2411 DECLARE_INSTRUCTION(UShr);
2412
2413 private:
2414 DISALLOW_COPY_AND_ASSIGN(HUShr);
2415};
2416
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002417class HAnd : public HBinaryOperation {
2418 public:
2419 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2420 : HBinaryOperation(result_type, left, right) {}
2421
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002422 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002423
2424 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2425 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2426
2427 DECLARE_INSTRUCTION(And);
2428
2429 private:
2430 DISALLOW_COPY_AND_ASSIGN(HAnd);
2431};
2432
2433class HOr : public HBinaryOperation {
2434 public:
2435 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2436 : HBinaryOperation(result_type, left, right) {}
2437
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002438 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002439
2440 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2441 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2442
2443 DECLARE_INSTRUCTION(Or);
2444
2445 private:
2446 DISALLOW_COPY_AND_ASSIGN(HOr);
2447};
2448
2449class HXor : public HBinaryOperation {
2450 public:
2451 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2452 : HBinaryOperation(result_type, left, right) {}
2453
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002454 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002455
2456 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2457 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2458
2459 DECLARE_INSTRUCTION(Xor);
2460
2461 private:
2462 DISALLOW_COPY_AND_ASSIGN(HXor);
2463};
2464
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002465// The value of a parameter in this method. Its location depends on
2466// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002467class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002468 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002469 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2470 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002471
2472 uint8_t GetIndex() const { return index_; }
2473
Calin Juravle10e244f2015-01-26 18:54:32 +00002474 bool CanBeNull() const OVERRIDE { return !is_this_; }
2475
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002476 DECLARE_INSTRUCTION(ParameterValue);
2477
2478 private:
2479 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002480 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002481 const uint8_t index_;
2482
Calin Juravle10e244f2015-01-26 18:54:32 +00002483 // Whether or not the parameter value corresponds to 'this' argument.
2484 const bool is_this_;
2485
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002486 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2487};
2488
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002489class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002490 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002491 explicit HNot(Primitive::Type result_type, HInstruction* input)
2492 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002493
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002494 bool CanBeMoved() const OVERRIDE { return true; }
2495 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002496 UNUSED(other);
2497 return true;
2498 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002499
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002500 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2501 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002502
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002503 DECLARE_INSTRUCTION(Not);
2504
2505 private:
2506 DISALLOW_COPY_AND_ASSIGN(HNot);
2507};
2508
Roland Levillaindff1f282014-11-05 14:15:05 +00002509class HTypeConversion : public HExpression<1> {
2510 public:
2511 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002512 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2513 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002514 SetRawInputAt(0, input);
2515 DCHECK_NE(input->GetType(), result_type);
2516 }
2517
2518 HInstruction* GetInput() const { return InputAt(0); }
2519 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2520 Primitive::Type GetResultType() const { return GetType(); }
2521
Roland Levillain624279f2014-12-04 11:54:28 +00002522 // Required by the x86 and ARM code generators when producing calls
2523 // to the runtime.
2524 uint32_t GetDexPc() const { return dex_pc_; }
2525
Roland Levillaindff1f282014-11-05 14:15:05 +00002526 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002527 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002528
2529 DECLARE_INSTRUCTION(TypeConversion);
2530
2531 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002532 const uint32_t dex_pc_;
2533
Roland Levillaindff1f282014-11-05 14:15:05 +00002534 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2535};
2536
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002537static constexpr uint32_t kNoRegNumber = -1;
2538
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002539class HPhi : public HInstruction {
2540 public:
2541 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002542 : HInstruction(SideEffects::None()),
2543 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002544 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002545 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002546 is_live_(false),
2547 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002548 inputs_.SetSize(number_of_inputs);
2549 }
2550
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002551 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2552 static Primitive::Type ToPhiType(Primitive::Type type) {
2553 switch (type) {
2554 case Primitive::kPrimBoolean:
2555 case Primitive::kPrimByte:
2556 case Primitive::kPrimShort:
2557 case Primitive::kPrimChar:
2558 return Primitive::kPrimInt;
2559 default:
2560 return type;
2561 }
2562 }
2563
Calin Juravle10e244f2015-01-26 18:54:32 +00002564 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002565
2566 void AddInput(HInstruction* input);
2567
Calin Juravle10e244f2015-01-26 18:54:32 +00002568 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002569 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002570
Calin Juravle10e244f2015-01-26 18:54:32 +00002571 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2572 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2573
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002574 uint32_t GetRegNumber() const { return reg_number_; }
2575
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002576 void SetDead() { is_live_ = false; }
2577 void SetLive() { is_live_ = true; }
2578 bool IsDead() const { return !is_live_; }
2579 bool IsLive() const { return is_live_; }
2580
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002581 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002582
David Brazdil1abb4192015-02-17 18:33:36 +00002583 protected:
2584 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2585
2586 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2587 inputs_.Put(index, input);
2588 }
2589
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002590 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002591 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002592 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002593 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002594 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002595 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002596
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002597 DISALLOW_COPY_AND_ASSIGN(HPhi);
2598};
2599
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002600class HNullCheck : public HExpression<1> {
2601 public:
2602 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002603 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002604 SetRawInputAt(0, value);
2605 }
2606
Calin Juravle10e244f2015-01-26 18:54:32 +00002607 bool CanBeMoved() const OVERRIDE { return true; }
2608 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002609 UNUSED(other);
2610 return true;
2611 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002612
Calin Juravle10e244f2015-01-26 18:54:32 +00002613 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002614
Calin Juravle10e244f2015-01-26 18:54:32 +00002615 bool CanThrow() const OVERRIDE { return true; }
2616
2617 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002618
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002619 uint32_t GetDexPc() const { return dex_pc_; }
2620
2621 DECLARE_INSTRUCTION(NullCheck);
2622
2623 private:
2624 const uint32_t dex_pc_;
2625
2626 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2627};
2628
2629class FieldInfo : public ValueObject {
2630 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002631 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2632 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002633
2634 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002635 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002636 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002637
2638 private:
2639 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002640 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002641 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002642};
2643
2644class HInstanceFieldGet : public HExpression<1> {
2645 public:
2646 HInstanceFieldGet(HInstruction* value,
2647 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002648 MemberOffset field_offset,
2649 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002650 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002651 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002652 SetRawInputAt(0, value);
2653 }
2654
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002655 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002656
2657 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2658 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2659 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002660 }
2661
Calin Juravle77520bc2015-01-12 18:45:46 +00002662 bool CanDoImplicitNullCheck() const OVERRIDE {
2663 return GetFieldOffset().Uint32Value() < kPageSize;
2664 }
2665
2666 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002667 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2668 }
2669
Calin Juravle52c48962014-12-16 17:02:57 +00002670 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002671 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002672 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002673 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002674
2675 DECLARE_INSTRUCTION(InstanceFieldGet);
2676
2677 private:
2678 const FieldInfo field_info_;
2679
2680 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2681};
2682
2683class HInstanceFieldSet : public HTemplateInstruction<2> {
2684 public:
2685 HInstanceFieldSet(HInstruction* object,
2686 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002687 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002688 MemberOffset field_offset,
2689 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002690 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002691 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002692 SetRawInputAt(0, object);
2693 SetRawInputAt(1, value);
2694 }
2695
Calin Juravle77520bc2015-01-12 18:45:46 +00002696 bool CanDoImplicitNullCheck() const OVERRIDE {
2697 return GetFieldOffset().Uint32Value() < kPageSize;
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 Geoffrayaf07bc12014-11-12 18:08:09 +00002704 HInstruction* GetValue() const { return InputAt(1); }
2705
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002706 DECLARE_INSTRUCTION(InstanceFieldSet);
2707
2708 private:
2709 const FieldInfo field_info_;
2710
2711 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2712};
2713
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002714class HArrayGet : public HExpression<2> {
2715 public:
2716 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002717 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002718 SetRawInputAt(0, array);
2719 SetRawInputAt(1, index);
2720 }
2721
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002722 bool CanBeMoved() const OVERRIDE { return true; }
2723 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002724 UNUSED(other);
2725 return true;
2726 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002727 bool CanDoImplicitNullCheck() const OVERRIDE {
2728 // TODO: We can be smarter here.
2729 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2730 // which generates the implicit null check. There are cases when these can be removed
2731 // to produce better code. If we ever add optimizations to do so we should allow an
2732 // implicit check here (as long as the address falls in the first page).
2733 return false;
2734 }
2735
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002736 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002737
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002738 HInstruction* GetArray() const { return InputAt(0); }
2739 HInstruction* GetIndex() const { return InputAt(1); }
2740
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002741 DECLARE_INSTRUCTION(ArrayGet);
2742
2743 private:
2744 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2745};
2746
2747class HArraySet : public HTemplateInstruction<3> {
2748 public:
2749 HArraySet(HInstruction* array,
2750 HInstruction* index,
2751 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002752 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002753 uint32_t dex_pc)
2754 : HTemplateInstruction(SideEffects::ChangesSomething()),
2755 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002756 expected_component_type_(expected_component_type),
2757 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002758 SetRawInputAt(0, array);
2759 SetRawInputAt(1, index);
2760 SetRawInputAt(2, value);
2761 }
2762
Calin Juravle77520bc2015-01-12 18:45:46 +00002763 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002764 // We currently always call a runtime method to catch array store
2765 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002766 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002767 }
2768
Calin Juravle77520bc2015-01-12 18:45:46 +00002769 bool CanDoImplicitNullCheck() const OVERRIDE {
2770 // TODO: Same as for ArrayGet.
2771 return false;
2772 }
2773
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002774 void ClearNeedsTypeCheck() {
2775 needs_type_check_ = false;
2776 }
2777
2778 bool NeedsTypeCheck() const { return needs_type_check_; }
2779
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002780 uint32_t GetDexPc() const { return dex_pc_; }
2781
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002782 HInstruction* GetArray() const { return InputAt(0); }
2783 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002784 HInstruction* GetValue() const { return InputAt(2); }
2785
2786 Primitive::Type GetComponentType() const {
2787 // The Dex format does not type floating point index operations. Since the
2788 // `expected_component_type_` is set during building and can therefore not
2789 // be correct, we also check what is the value type. If it is a floating
2790 // point type, we must use that type.
2791 Primitive::Type value_type = GetValue()->GetType();
2792 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2793 ? value_type
2794 : expected_component_type_;
2795 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002796
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002797 DECLARE_INSTRUCTION(ArraySet);
2798
2799 private:
2800 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002801 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002802 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002803
2804 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2805};
2806
2807class HArrayLength : public HExpression<1> {
2808 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002809 explicit HArrayLength(HInstruction* array)
2810 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2811 // Note that arrays do not change length, so the instruction does not
2812 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002813 SetRawInputAt(0, array);
2814 }
2815
Calin Juravle77520bc2015-01-12 18:45:46 +00002816 bool CanBeMoved() const OVERRIDE { return true; }
2817 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002818 UNUSED(other);
2819 return true;
2820 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002821 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002822
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002823 DECLARE_INSTRUCTION(ArrayLength);
2824
2825 private:
2826 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2827};
2828
2829class HBoundsCheck : public HExpression<2> {
2830 public:
2831 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002832 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002833 DCHECK(index->GetType() == Primitive::kPrimInt);
2834 SetRawInputAt(0, index);
2835 SetRawInputAt(1, length);
2836 }
2837
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002838 bool CanBeMoved() const OVERRIDE { return true; }
2839 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002840 UNUSED(other);
2841 return true;
2842 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002843
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002844 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002845
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002846 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002847
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002848 uint32_t GetDexPc() const { return dex_pc_; }
2849
2850 DECLARE_INSTRUCTION(BoundsCheck);
2851
2852 private:
2853 const uint32_t dex_pc_;
2854
2855 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2856};
2857
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002858/**
2859 * Some DEX instructions are folded into multiple HInstructions that need
2860 * to stay live until the last HInstruction. This class
2861 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002862 * HInstruction stays live. `index` represents the stack location index of the
2863 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002864 */
2865class HTemporary : public HTemplateInstruction<0> {
2866 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002867 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002868
2869 size_t GetIndex() const { return index_; }
2870
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002871 Primitive::Type GetType() const OVERRIDE {
2872 // The previous instruction is the one that will be stored in the temporary location.
2873 DCHECK(GetPrevious() != nullptr);
2874 return GetPrevious()->GetType();
2875 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002876
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002877 DECLARE_INSTRUCTION(Temporary);
2878
2879 private:
2880 const size_t index_;
2881
2882 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2883};
2884
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002885class HSuspendCheck : public HTemplateInstruction<0> {
2886 public:
2887 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002888 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002889
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002890 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002891 return true;
2892 }
2893
2894 uint32_t GetDexPc() const { return dex_pc_; }
2895
2896 DECLARE_INSTRUCTION(SuspendCheck);
2897
2898 private:
2899 const uint32_t dex_pc_;
2900
2901 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2902};
2903
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002904/**
2905 * Instruction to load a Class object.
2906 */
2907class HLoadClass : public HExpression<0> {
2908 public:
2909 HLoadClass(uint16_t type_index,
2910 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002911 uint32_t dex_pc)
2912 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2913 type_index_(type_index),
2914 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002915 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00002916 generate_clinit_check_(false),
2917 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002918
2919 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002920
2921 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2922 return other->AsLoadClass()->type_index_ == type_index_;
2923 }
2924
2925 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2926
2927 uint32_t GetDexPc() const { return dex_pc_; }
2928 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002929 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002930
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002931 bool NeedsEnvironment() const OVERRIDE {
2932 // Will call runtime and load the class if the class is not loaded yet.
2933 // TODO: finer grain decision.
2934 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002935 }
2936
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002937 bool MustGenerateClinitCheck() const {
2938 return generate_clinit_check_;
2939 }
2940
2941 void SetMustGenerateClinitCheck() {
2942 generate_clinit_check_ = true;
2943 }
2944
2945 bool CanCallRuntime() const {
2946 return MustGenerateClinitCheck() || !is_referrers_class_;
2947 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002948
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002949 bool CanThrow() const OVERRIDE {
2950 // May call runtime and and therefore can throw.
2951 // TODO: finer grain decision.
2952 return !is_referrers_class_;
2953 }
2954
Calin Juravleacf735c2015-02-12 15:25:22 +00002955 ReferenceTypeInfo GetLoadedClassRTI() {
2956 return loaded_class_rti_;
2957 }
2958
2959 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
2960 // Make sure we only set exact types (the loaded class should never be merged).
2961 DCHECK(rti.IsExact());
2962 loaded_class_rti_ = rti;
2963 }
2964
2965 bool IsResolved() {
2966 return loaded_class_rti_.IsExact();
2967 }
2968
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002969 DECLARE_INSTRUCTION(LoadClass);
2970
2971 private:
2972 const uint16_t type_index_;
2973 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002974 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002975 // Whether this instruction must generate the initialization check.
2976 // Used for code generation.
2977 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002978
Calin Juravleacf735c2015-02-12 15:25:22 +00002979 ReferenceTypeInfo loaded_class_rti_;
2980
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002981 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2982};
2983
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002984class HLoadString : public HExpression<0> {
2985 public:
2986 HLoadString(uint32_t string_index, uint32_t dex_pc)
2987 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2988 string_index_(string_index),
2989 dex_pc_(dex_pc) {}
2990
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002991 bool CanBeMoved() const OVERRIDE { return true; }
2992
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002993 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2994 return other->AsLoadString()->string_index_ == string_index_;
2995 }
2996
2997 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2998
2999 uint32_t GetDexPc() const { return dex_pc_; }
3000 uint32_t GetStringIndex() const { return string_index_; }
3001
3002 // TODO: Can we deopt or debug when we resolve a string?
3003 bool NeedsEnvironment() const OVERRIDE { return false; }
3004
3005 DECLARE_INSTRUCTION(LoadString);
3006
3007 private:
3008 const uint32_t string_index_;
3009 const uint32_t dex_pc_;
3010
3011 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3012};
3013
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003014// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003015/**
3016 * Performs an initialization check on its Class object input.
3017 */
3018class HClinitCheck : public HExpression<1> {
3019 public:
3020 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
3021 : HExpression(Primitive::kPrimNot, SideEffects::All()),
3022 dex_pc_(dex_pc) {
3023 SetRawInputAt(0, constant);
3024 }
3025
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003026 bool CanBeMoved() const OVERRIDE { return true; }
3027 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3028 UNUSED(other);
3029 return true;
3030 }
3031
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003032 bool NeedsEnvironment() const OVERRIDE {
3033 // May call runtime to initialize the class.
3034 return true;
3035 }
3036
3037 uint32_t GetDexPc() const { return dex_pc_; }
3038
3039 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3040
3041 DECLARE_INSTRUCTION(ClinitCheck);
3042
3043 private:
3044 const uint32_t dex_pc_;
3045
3046 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3047};
3048
3049class HStaticFieldGet : public HExpression<1> {
3050 public:
3051 HStaticFieldGet(HInstruction* cls,
3052 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003053 MemberOffset field_offset,
3054 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003055 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003056 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003057 SetRawInputAt(0, cls);
3058 }
3059
Calin Juravle52c48962014-12-16 17:02:57 +00003060
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003061 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003062
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003063 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003064 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3065 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003066 }
3067
3068 size_t ComputeHashCode() const OVERRIDE {
3069 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3070 }
3071
Calin Juravle52c48962014-12-16 17:02:57 +00003072 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003073 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3074 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003075 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003076
3077 DECLARE_INSTRUCTION(StaticFieldGet);
3078
3079 private:
3080 const FieldInfo field_info_;
3081
3082 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3083};
3084
3085class HStaticFieldSet : public HTemplateInstruction<2> {
3086 public:
3087 HStaticFieldSet(HInstruction* cls,
3088 HInstruction* value,
3089 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003090 MemberOffset field_offset,
3091 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003092 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003093 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003094 SetRawInputAt(0, cls);
3095 SetRawInputAt(1, value);
3096 }
3097
Calin Juravle52c48962014-12-16 17:02:57 +00003098 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003099 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3100 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003101 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003102
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003103 HInstruction* GetValue() const { return InputAt(1); }
3104
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003105 DECLARE_INSTRUCTION(StaticFieldSet);
3106
3107 private:
3108 const FieldInfo field_info_;
3109
3110 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3111};
3112
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003113// Implement the move-exception DEX instruction.
3114class HLoadException : public HExpression<0> {
3115 public:
3116 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3117
3118 DECLARE_INSTRUCTION(LoadException);
3119
3120 private:
3121 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3122};
3123
3124class HThrow : public HTemplateInstruction<1> {
3125 public:
3126 HThrow(HInstruction* exception, uint32_t dex_pc)
3127 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3128 SetRawInputAt(0, exception);
3129 }
3130
3131 bool IsControlFlow() const OVERRIDE { return true; }
3132
3133 bool NeedsEnvironment() const OVERRIDE { return true; }
3134
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003135 bool CanThrow() const OVERRIDE { return true; }
3136
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003137 uint32_t GetDexPc() const { return dex_pc_; }
3138
3139 DECLARE_INSTRUCTION(Throw);
3140
3141 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003142 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003143
3144 DISALLOW_COPY_AND_ASSIGN(HThrow);
3145};
3146
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003147class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003148 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003149 HInstanceOf(HInstruction* object,
3150 HLoadClass* constant,
3151 bool class_is_final,
3152 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003153 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3154 class_is_final_(class_is_final),
3155 dex_pc_(dex_pc) {
3156 SetRawInputAt(0, object);
3157 SetRawInputAt(1, constant);
3158 }
3159
3160 bool CanBeMoved() const OVERRIDE { return true; }
3161
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003162 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003163 return true;
3164 }
3165
3166 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003167 return false;
3168 }
3169
3170 uint32_t GetDexPc() const { return dex_pc_; }
3171
3172 bool IsClassFinal() const { return class_is_final_; }
3173
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003174 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003175
3176 private:
3177 const bool class_is_final_;
3178 const uint32_t dex_pc_;
3179
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003180 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3181};
3182
Calin Juravleb1498f62015-02-16 13:13:29 +00003183class HBoundType : public HExpression<1> {
3184 public:
3185 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3186 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3187 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003188 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003189 SetRawInputAt(0, input);
3190 }
3191
3192 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3193
3194 bool CanBeNull() const OVERRIDE {
3195 // `null instanceof ClassX` always return false so we can't be null.
3196 return false;
3197 }
3198
3199 DECLARE_INSTRUCTION(BoundType);
3200
3201 private:
3202 // Encodes the most upper class that this instruction can have. In other words
3203 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3204 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3205 const ReferenceTypeInfo bound_type_;
3206
3207 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3208};
3209
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003210class HCheckCast : public HTemplateInstruction<2> {
3211 public:
3212 HCheckCast(HInstruction* object,
3213 HLoadClass* constant,
3214 bool class_is_final,
3215 uint32_t dex_pc)
3216 : HTemplateInstruction(SideEffects::None()),
3217 class_is_final_(class_is_final),
3218 dex_pc_(dex_pc) {
3219 SetRawInputAt(0, object);
3220 SetRawInputAt(1, constant);
3221 }
3222
3223 bool CanBeMoved() const OVERRIDE { return true; }
3224
3225 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3226 return true;
3227 }
3228
3229 bool NeedsEnvironment() const OVERRIDE {
3230 // Instruction may throw a CheckCastError.
3231 return true;
3232 }
3233
3234 bool CanThrow() const OVERRIDE { return true; }
3235
3236 uint32_t GetDexPc() const { return dex_pc_; }
3237
3238 bool IsClassFinal() const { return class_is_final_; }
3239
3240 DECLARE_INSTRUCTION(CheckCast);
3241
3242 private:
3243 const bool class_is_final_;
3244 const uint32_t dex_pc_;
3245
3246 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003247};
3248
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003249class HMonitorOperation : public HTemplateInstruction<1> {
3250 public:
3251 enum OperationKind {
3252 kEnter,
3253 kExit,
3254 };
3255
3256 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3257 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3258 SetRawInputAt(0, object);
3259 }
3260
3261 // Instruction may throw a Java exception, so we need an environment.
3262 bool NeedsEnvironment() const OVERRIDE { return true; }
3263 bool CanThrow() const OVERRIDE { return true; }
3264
3265 uint32_t GetDexPc() const { return dex_pc_; }
3266
3267 bool IsEnter() const { return kind_ == kEnter; }
3268
3269 DECLARE_INSTRUCTION(MonitorOperation);
3270
Calin Juravle52c48962014-12-16 17:02:57 +00003271 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003272 const OperationKind kind_;
3273 const uint32_t dex_pc_;
3274
3275 private:
3276 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3277};
3278
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003279class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003280 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003281 MoveOperands(Location source, Location destination, HInstruction* instruction)
3282 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003283
3284 Location GetSource() const { return source_; }
3285 Location GetDestination() const { return destination_; }
3286
3287 void SetSource(Location value) { source_ = value; }
3288 void SetDestination(Location value) { destination_ = value; }
3289
3290 // The parallel move resolver marks moves as "in-progress" by clearing the
3291 // destination (but not the source).
3292 Location MarkPending() {
3293 DCHECK(!IsPending());
3294 Location dest = destination_;
3295 destination_ = Location::NoLocation();
3296 return dest;
3297 }
3298
3299 void ClearPending(Location dest) {
3300 DCHECK(IsPending());
3301 destination_ = dest;
3302 }
3303
3304 bool IsPending() const {
3305 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3306 return destination_.IsInvalid() && !source_.IsInvalid();
3307 }
3308
3309 // True if this blocks a move from the given location.
3310 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003311 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003312 }
3313
3314 // A move is redundant if it's been eliminated, if its source and
3315 // destination are the same, or if its destination is unneeded.
3316 bool IsRedundant() const {
3317 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3318 }
3319
3320 // We clear both operands to indicate move that's been eliminated.
3321 void Eliminate() {
3322 source_ = destination_ = Location::NoLocation();
3323 }
3324
3325 bool IsEliminated() const {
3326 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3327 return source_.IsInvalid();
3328 }
3329
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003330 HInstruction* GetInstruction() const { return instruction_; }
3331
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003332 private:
3333 Location source_;
3334 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003335 // The instruction this move is assocatied with. Null when this move is
3336 // for moving an input in the expected locations of user (including a phi user).
3337 // This is only used in debug mode, to ensure we do not connect interval siblings
3338 // in the same parallel move.
3339 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003340};
3341
3342static constexpr size_t kDefaultNumberOfMoves = 4;
3343
3344class HParallelMove : public HTemplateInstruction<0> {
3345 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003346 explicit HParallelMove(ArenaAllocator* arena)
3347 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003348
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003349 void AddMove(Location source, Location destination, HInstruction* instruction) {
3350 DCHECK(source.IsValid());
3351 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003352 if (kIsDebugBuild) {
3353 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003354 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003355 if (moves_.Get(i).GetInstruction() == instruction) {
3356 // Special case the situation where the move is for the spill slot
3357 // of the instruction.
3358 if ((GetPrevious() == instruction)
3359 || ((GetPrevious() == nullptr)
3360 && instruction->IsPhi()
3361 && instruction->GetBlock() == GetBlock())) {
3362 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3363 << "Doing parallel moves for the same instruction.";
3364 } else {
3365 DCHECK(false) << "Doing parallel moves for the same instruction.";
3366 }
3367 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003368 }
3369 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003370 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3371 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3372 << "Same destination for two moves in a parallel move.";
3373 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003374 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003375 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003376 }
3377
3378 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003379 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003380 }
3381
3382 size_t NumMoves() const { return moves_.Size(); }
3383
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003384 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003385
3386 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003387 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003388
3389 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3390};
3391
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003392class HGraphVisitor : public ValueObject {
3393 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003394 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3395 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003396
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003397 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003398 virtual void VisitBasicBlock(HBasicBlock* block);
3399
Roland Levillain633021e2014-10-01 14:12:25 +01003400 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003401 void VisitInsertionOrder();
3402
Roland Levillain633021e2014-10-01 14:12:25 +01003403 // Visit the graph following dominator tree reverse post-order.
3404 void VisitReversePostOrder();
3405
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003406 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003407
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003408 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003409#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003410 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3411
3412 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3413
3414#undef DECLARE_VISIT_INSTRUCTION
3415
3416 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003417 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003418
3419 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3420};
3421
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003422class HGraphDelegateVisitor : public HGraphVisitor {
3423 public:
3424 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3425 virtual ~HGraphDelegateVisitor() {}
3426
3427 // Visit functions that delegate to to super class.
3428#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003429 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003430
3431 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3432
3433#undef DECLARE_VISIT_INSTRUCTION
3434
3435 private:
3436 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3437};
3438
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003439class HInsertionOrderIterator : public ValueObject {
3440 public:
3441 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3442
3443 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3444 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3445 void Advance() { ++index_; }
3446
3447 private:
3448 const HGraph& graph_;
3449 size_t index_;
3450
3451 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3452};
3453
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003454class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003455 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003456 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003457
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003458 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3459 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003460 void Advance() { ++index_; }
3461
3462 private:
3463 const HGraph& graph_;
3464 size_t index_;
3465
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003466 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003467};
3468
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003469class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003470 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003471 explicit HPostOrderIterator(const HGraph& graph)
3472 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003473
3474 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003475 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003476 void Advance() { --index_; }
3477
3478 private:
3479 const HGraph& graph_;
3480 size_t index_;
3481
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003482 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003483};
3484
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003485// Iterator over the blocks that art part of the loop. Includes blocks part
3486// of an inner loop. The order in which the blocks are iterated is on their
3487// block id.
3488class HBlocksInLoopIterator : public ValueObject {
3489 public:
3490 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3491 : blocks_in_loop_(info.GetBlocks()),
3492 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3493 index_(0) {
3494 if (!blocks_in_loop_.IsBitSet(index_)) {
3495 Advance();
3496 }
3497 }
3498
3499 bool Done() const { return index_ == blocks_.Size(); }
3500 HBasicBlock* Current() const { return blocks_.Get(index_); }
3501 void Advance() {
3502 ++index_;
3503 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3504 if (blocks_in_loop_.IsBitSet(index_)) {
3505 break;
3506 }
3507 }
3508 }
3509
3510 private:
3511 const BitVector& blocks_in_loop_;
3512 const GrowableArray<HBasicBlock*>& blocks_;
3513 size_t index_;
3514
3515 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3516};
3517
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003518inline int64_t Int64FromConstant(HConstant* constant) {
3519 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3520 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3521 : constant->AsLongConstant()->GetValue();
3522}
3523
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003524} // namespace art
3525
3526#endif // ART_COMPILER_OPTIMIZING_NODES_H_