blob: 50eecffc57caa4ef79ef6fc049a831893f60f2e0 [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
David Brazdil8d5b8b22015-03-24 10:51:52 +000020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010022#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "handle.h"
25#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000028#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000031#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "utils/growable_array.h"
33
34namespace art {
35
David Brazdil1abb4192015-02-17 18:33:36 +000036class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HBasicBlock;
David Brazdil8d5b8b22015-03-24 10:51:52 +000038class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010039class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000040class HFloatConstant;
41class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000043class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000044class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000046class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010047class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010048class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010049class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000050class LocationSummary;
David Brazdil8d5b8b22015-03-24 10:51:52 +000051class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
53static const int kDefaultNumberOfBlocks = 8;
54static const int kDefaultNumberOfSuccessors = 2;
55static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010056static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000057static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000058
Calin Juravle9aec02f2014-11-18 23:06:35 +000059static constexpr uint32_t kMaxIntShiftValue = 0x1f;
60static constexpr uint64_t kMaxLongShiftValue = 0x3f;
61
Dave Allison20dfc792014-06-16 20:44:29 -070062enum IfCondition {
63 kCondEQ,
64 kCondNE,
65 kCondLT,
66 kCondLE,
67 kCondGT,
68 kCondGE,
69};
70
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010071class HInstructionList {
72 public:
73 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
74
75 void AddInstruction(HInstruction* instruction);
76 void RemoveInstruction(HInstruction* instruction);
77
David Brazdilc3d743f2015-04-22 13:40:50 +010078 // Insert `instruction` before/after an existing instruction `cursor`.
79 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
80 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
81
Roland Levillain6b469232014-09-25 10:10:38 +010082 // Return true if this list contains `instruction`.
83 bool Contains(HInstruction* instruction) const;
84
Roland Levillainccc07a92014-09-16 14:48:16 +010085 // Return true if `instruction1` is found before `instruction2` in
86 // this instruction list and false otherwise. Abort if none
87 // of these instructions is found.
88 bool FoundBefore(const HInstruction* instruction1,
89 const HInstruction* instruction2) const;
90
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000091 bool IsEmpty() const { return first_instruction_ == nullptr; }
92 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
93
94 // Update the block of all instructions to be `block`.
95 void SetBlockOfInstructions(HBasicBlock* block) const;
96
97 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
98 void Add(const HInstructionList& instruction_list);
99
David Brazdil2d7352b2015-04-20 14:52:42 +0100100 // Return the number of instructions in the list. This is an expensive operation.
101 size_t CountSize() const;
102
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100103 private:
104 HInstruction* first_instruction_;
105 HInstruction* last_instruction_;
106
107 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000108 friend class HGraph;
109 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100110 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100111 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100112
113 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
114};
115
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000116// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700117class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000118 public:
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000119 HGraph(ArenaAllocator* arena, bool debuggable = false, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000120 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000121 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100122 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100123 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700124 entry_block_(nullptr),
125 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100126 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100127 number_of_vregs_(0),
128 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000129 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400130 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000131 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000132 current_instruction_id_(start_instruction_id),
133 cached_null_constant_(nullptr),
134 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000135 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
136 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
137 cached_double_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000138
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000139 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100140 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100141 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000142
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000143 HBasicBlock* GetEntryBlock() const { return entry_block_; }
144 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000145
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000146 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
147 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000148
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000149 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100150
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000151 // Try building the SSA form of this graph, with dominance computation and loop
152 // recognition. Returns whether it was successful in doing all these steps.
153 bool TryBuildingSsa() {
154 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000155 // The SSA builder requires loops to all be natural. Specifically, the dead phi
156 // elimination phase checks the consistency of the graph when doing a post-order
157 // visit for eliminating dead phis: a dead phi can only have loop header phi
158 // users remaining when being visited.
159 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000161 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000162 }
163
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000164 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000165 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100166 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000167
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000168 // Analyze all natural loops in this graph. Returns false if one
169 // loop is not natural, that is the header does not dominate the
170 // back edge.
171 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100172
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000173 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
174 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
175
David Brazdil2d7352b2015-04-20 14:52:42 +0100176 // Removes `block` from the graph.
177 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000178
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100179 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
180 void SimplifyLoop(HBasicBlock* header);
181
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000182 int32_t GetNextInstructionId() {
183 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000184 return current_instruction_id_++;
185 }
186
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000187 int32_t GetCurrentInstructionId() const {
188 return current_instruction_id_;
189 }
190
191 void SetCurrentInstructionId(int32_t id) {
192 current_instruction_id_ = id;
193 }
194
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100195 uint16_t GetMaximumNumberOfOutVRegs() const {
196 return maximum_number_of_out_vregs_;
197 }
198
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
200 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100201 }
202
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000203 void UpdateTemporariesVRegSlots(size_t slots) {
204 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100205 }
206
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000207 size_t GetTemporariesVRegSlots() const {
208 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100209 }
210
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100211 void SetNumberOfVRegs(uint16_t number_of_vregs) {
212 number_of_vregs_ = number_of_vregs;
213 }
214
215 uint16_t GetNumberOfVRegs() const {
216 return number_of_vregs_;
217 }
218
219 void SetNumberOfInVRegs(uint16_t value) {
220 number_of_in_vregs_ = value;
221 }
222
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100223 uint16_t GetNumberOfLocalVRegs() const {
224 return number_of_vregs_ - number_of_in_vregs_;
225 }
226
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100227 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
228 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100229 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100230
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100231 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
232 return linear_order_;
233 }
234
Mark Mendell1152c922015-04-24 17:06:35 -0400235 bool HasBoundsChecks() const {
236 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800237 }
238
Mark Mendell1152c922015-04-24 17:06:35 -0400239 void SetHasBoundsChecks(bool value) {
240 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800241 }
242
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000243 bool IsDebuggable() const { return debuggable_; }
244
David Brazdil8d5b8b22015-03-24 10:51:52 +0000245 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000246 // already, it is created and inserted into the graph. This method is only for
247 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000248 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000249 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000250 HIntConstant* GetIntConstant(int32_t value) {
251 return CreateConstant(value, &cached_int_constants_);
252 }
253 HLongConstant* GetLongConstant(int64_t value) {
254 return CreateConstant(value, &cached_long_constants_);
255 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000256 HFloatConstant* GetFloatConstant(float value) {
257 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
258 }
259 HDoubleConstant* GetDoubleConstant(double value) {
260 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
261 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000262
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000263 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100264
265 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000266 void VisitBlockForDominatorTree(HBasicBlock* block,
267 HBasicBlock* predecessor,
268 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100269 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270 void VisitBlockForBackEdges(HBasicBlock* block,
271 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100272 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000273 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100274 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000275
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000276 template <class InstructionType, typename ValueType>
277 InstructionType* CreateConstant(ValueType value,
278 ArenaSafeMap<ValueType, InstructionType*>* cache) {
279 // Try to find an existing constant of the given value.
280 InstructionType* constant = nullptr;
281 auto cached_constant = cache->find(value);
282 if (cached_constant != cache->end()) {
283 constant = cached_constant->second;
284 }
285
286 // If not found or previously deleted, create and cache a new instruction.
287 if (constant == nullptr || constant->GetBlock() == nullptr) {
288 constant = new (arena_) InstructionType(value);
289 cache->Overwrite(value, constant);
290 InsertConstant(constant);
291 }
292 return constant;
293 }
294
David Brazdil8d5b8b22015-03-24 10:51:52 +0000295 void InsertConstant(HConstant* instruction);
296
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000297 // Cache a float constant into the graph. This method should only be
298 // called by the SsaBuilder when creating "equivalent" instructions.
299 void CacheFloatConstant(HFloatConstant* constant);
300
301 // See CacheFloatConstant comment.
302 void CacheDoubleConstant(HDoubleConstant* constant);
303
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000305
306 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307 GrowableArray<HBasicBlock*> blocks_;
308
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100309 // List of blocks to perform a reverse post order tree traversal.
310 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000311
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100312 // List of blocks to perform a linear order tree traversal.
313 GrowableArray<HBasicBlock*> linear_order_;
314
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000315 HBasicBlock* entry_block_;
316 HBasicBlock* exit_block_;
317
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100318 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100319 uint16_t maximum_number_of_out_vregs_;
320
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100321 // The number of virtual registers in this method. Contains the parameters.
322 uint16_t number_of_vregs_;
323
324 // The number of virtual registers used by parameters of this method.
325 uint16_t number_of_in_vregs_;
326
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000327 // Number of vreg size slots that the temporaries use (used in baseline compiler).
328 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100329
Mark Mendell1152c922015-04-24 17:06:35 -0400330 // Has bounds checks. We can totally skip BCE if it's false.
331 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800332
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000333 // Indicates whether the graph should be compiled in a way that
334 // ensures full debuggability. If false, we can apply more
335 // aggressive optimizations that may limit the level of debugging.
336 const bool debuggable_;
337
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000338 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000339 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000340
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000341 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 HNullConstant* cached_null_constant_;
343 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000344 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000345 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000346 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000347
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000348 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100349 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000350 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000351 DISALLOW_COPY_AND_ASSIGN(HGraph);
352};
353
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700354class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000355 public:
356 HLoopInformation(HBasicBlock* header, HGraph* graph)
357 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100358 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100359 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100360 // Make bit vector growable, as the number of blocks may change.
361 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100362
363 HBasicBlock* GetHeader() const {
364 return header_;
365 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000366
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000367 void SetHeader(HBasicBlock* block) {
368 header_ = block;
369 }
370
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100371 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
372 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
373 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
374
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000375 void AddBackEdge(HBasicBlock* back_edge) {
376 back_edges_.Add(back_edge);
377 }
378
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100379 void RemoveBackEdge(HBasicBlock* back_edge) {
380 back_edges_.Delete(back_edge);
381 }
382
David Brazdil46e2a392015-03-16 17:31:52 +0000383 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100384 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000385 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100386 }
387 return false;
388 }
389
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000390 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000391 return back_edges_.Size();
392 }
393
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100394 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100395
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100396 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
397 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100398 }
399
Nicolas Geoffray57902602015-04-21 14:28:41 +0100400 HBasicBlock* GetSingleBackEdge() const {
401 DCHECK_EQ(back_edges_.Size(), 1u);
402 return back_edges_.Get(0);
403 }
404
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100405 void ClearBackEdges() {
406 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100407 }
408
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100409 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
410 // that is the header dominates the back edge.
411 bool Populate();
412
413 // Returns whether this loop information contains `block`.
414 // Note that this loop information *must* be populated before entering this function.
415 bool Contains(const HBasicBlock& block) const;
416
417 // Returns whether this loop information is an inner loop of `other`.
418 // Note that `other` *must* be populated before entering this function.
419 bool IsIn(const HLoopInformation& other) const;
420
421 const ArenaBitVector& GetBlocks() const { return blocks_; }
422
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000423 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000424 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000425
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000426 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100427 // Internal recursive implementation of `Populate`.
428 void PopulateRecursive(HBasicBlock* block);
429
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000430 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100431 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000432 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100433 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000434
435 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
436};
437
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100438static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100439static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100440
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000441// A block in a method. Contains the list of instructions represented
442// as a double linked list. Each block knows its predecessors and
443// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100444
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700445class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000446 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100447 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000448 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000449 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
450 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000451 loop_information_(nullptr),
452 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100453 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100454 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100455 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100456 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000457 lifetime_end_(kNoLifetime),
458 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000459
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100460 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
461 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000462 }
463
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100464 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
465 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000466 }
467
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100468 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
469 return dominated_blocks_;
470 }
471
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100472 bool IsEntryBlock() const {
473 return graph_->GetEntryBlock() == this;
474 }
475
476 bool IsExitBlock() const {
477 return graph_->GetExitBlock() == this;
478 }
479
David Brazdil46e2a392015-03-16 17:31:52 +0000480 bool IsSingleGoto() const;
481
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000482 void AddBackEdge(HBasicBlock* back_edge) {
483 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000484 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000485 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100486 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000487 loop_information_->AddBackEdge(back_edge);
488 }
489
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000490 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000491 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000492
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000493 int GetBlockId() const { return block_id_; }
494 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000495
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000496 HBasicBlock* GetDominator() const { return dominator_; }
497 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100498 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100499 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000500 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
501 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
502 if (dominated_blocks_.Get(i) == existing) {
503 dominated_blocks_.Put(i, new_block);
504 return;
505 }
506 }
507 LOG(FATAL) << "Unreachable";
508 UNREACHABLE();
509 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000510
511 int NumberOfBackEdges() const {
512 return loop_information_ == nullptr
513 ? 0
514 : loop_information_->NumberOfBackEdges();
515 }
516
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100517 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
518 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100519 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100520 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100521 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
522 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000523
524 void AddSuccessor(HBasicBlock* block) {
525 successors_.Add(block);
526 block->predecessors_.Add(this);
527 }
528
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100529 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
530 size_t successor_index = GetSuccessorIndexOf(existing);
531 DCHECK_NE(successor_index, static_cast<size_t>(-1));
532 existing->RemovePredecessor(this);
533 new_block->predecessors_.Add(this);
534 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000535 }
536
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000537 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
538 size_t predecessor_index = GetPredecessorIndexOf(existing);
539 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
540 existing->RemoveSuccessor(this);
541 new_block->successors_.Add(this);
542 predecessors_.Put(predecessor_index, new_block);
543 }
544
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100545 void RemovePredecessor(HBasicBlock* block) {
546 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100547 }
548
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000549 void RemoveSuccessor(HBasicBlock* block) {
550 successors_.Delete(block);
551 }
552
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100553 void ClearAllPredecessors() {
554 predecessors_.Reset();
555 }
556
557 void AddPredecessor(HBasicBlock* block) {
558 predecessors_.Add(block);
559 block->successors_.Add(this);
560 }
561
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100562 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100563 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100564 HBasicBlock* temp = predecessors_.Get(0);
565 predecessors_.Put(0, predecessors_.Get(1));
566 predecessors_.Put(1, temp);
567 }
568
David Brazdil769c9e52015-04-27 13:54:09 +0100569 void SwapSuccessors() {
570 DCHECK_EQ(successors_.Size(), 2u);
571 HBasicBlock* temp = successors_.Get(0);
572 successors_.Put(0, successors_.Get(1));
573 successors_.Put(1, temp);
574 }
575
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100576 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
577 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
578 if (predecessors_.Get(i) == predecessor) {
579 return i;
580 }
581 }
582 return -1;
583 }
584
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100585 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
586 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
587 if (successors_.Get(i) == successor) {
588 return i;
589 }
590 }
591 return -1;
592 }
593
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000594 // Split the block into two blocks just after `cursor`. Returns the newly
595 // created block. Note that this method just updates raw block information,
596 // like predecessors, successors, dominators, and instruction list. It does not
597 // update the graph, reverse post order, loop information, nor make sure the
598 // blocks are consistent (for example ending with a control flow instruction).
599 HBasicBlock* SplitAfter(HInstruction* cursor);
600
601 // Merge `other` at the end of `this`. Successors and dominated blocks of
602 // `other` are changed to be successors and dominated blocks of `this`. Note
603 // that this method does not update the graph, reverse post order, loop
604 // information, nor make sure the blocks are consistent (for example ending
605 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100606 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000607
608 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
609 // of `this` are moved to `other`.
610 // Note that this method does not update the graph, reverse post order, loop
611 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000612 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000613 void ReplaceWith(HBasicBlock* other);
614
David Brazdil2d7352b2015-04-20 14:52:42 +0100615 // Merge `other` at the end of `this`. This method updates loops, reverse post
616 // order, links to predecessors, successors, dominators and deletes the block
617 // from the graph. The two blocks must be successive, i.e. `this` the only
618 // predecessor of `other` and vice versa.
619 void MergeWith(HBasicBlock* other);
620
621 // Disconnects `this` from all its predecessors, successors and dominator,
622 // removes it from all loops it is included in and eventually from the graph.
623 // The block must not dominate any other block. Predecessors and successors
624 // are safely updated.
625 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000626
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000627 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100628 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100629 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100630 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100631 // Replace instruction `initial` with `replacement` within this block.
632 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
633 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100634 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100635 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000636 // RemoveInstruction and RemovePhi delete a given instruction from the respective
637 // instruction list. With 'ensure_safety' set to true, it verifies that the
638 // instruction is not in use and removes it from the use lists of its inputs.
639 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
640 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100641 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100642
643 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100644 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100645 }
646
Roland Levillain6b879dd2014-09-22 17:13:44 +0100647 bool IsLoopPreHeaderFirstPredecessor() const {
648 DCHECK(IsLoopHeader());
649 DCHECK(!GetPredecessors().IsEmpty());
650 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
651 }
652
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100653 HLoopInformation* GetLoopInformation() const {
654 return loop_information_;
655 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000656
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000657 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100658 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000659 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100660 void SetInLoop(HLoopInformation* info) {
661 if (IsLoopHeader()) {
662 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100663 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100664 loop_information_ = info;
665 } else if (loop_information_->Contains(*info->GetHeader())) {
666 // Block is currently part of an outer loop. Make it part of this inner loop.
667 // Note that a non loop header having a loop information means this loop information
668 // has already been populated
669 loop_information_ = info;
670 } else {
671 // Block is part of an inner loop. Do not update the loop information.
672 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
673 // at this point, because this method is being called while populating `info`.
674 }
675 }
676
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000677 // Raw update of the loop information.
678 void SetLoopInformation(HLoopInformation* info) {
679 loop_information_ = info;
680 }
681
David Brazdil69a28042015-04-29 17:16:07 +0100682 // Checks if the loop information points to a valid loop. If the loop has been
683 // dismantled (does not have a back edge any more), loop information is
684 // removed or replaced with the information of the first valid outer loop.
685 void UpdateLoopInformation();
686
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100687 bool IsInLoop() const { return loop_information_ != nullptr; }
688
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100689 // Returns wheter this block dominates the blocked passed as parameter.
690 bool Dominates(HBasicBlock* block) const;
691
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100692 size_t GetLifetimeStart() const { return lifetime_start_; }
693 size_t GetLifetimeEnd() const { return lifetime_end_; }
694
695 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
696 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
697
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100698 uint32_t GetDexPc() const { return dex_pc_; }
699
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000700 bool IsCatchBlock() const { return is_catch_block_; }
701 void SetIsCatchBlock() { is_catch_block_ = true; }
702
David Brazdil8d5b8b22015-03-24 10:51:52 +0000703 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000704 bool EndsWithIf() const;
705 bool HasSinglePhi() const;
706
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000707 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000708 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000709 GrowableArray<HBasicBlock*> predecessors_;
710 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100711 HInstructionList instructions_;
712 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000713 HLoopInformation* loop_information_;
714 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100715 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000716 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100717 // The dex program counter of the first instruction of this block.
718 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100719 size_t lifetime_start_;
720 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000721 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000722
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000723 friend class HGraph;
724 friend class HInstruction;
725
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000726 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
727};
728
David Brazdilb2bd1c52015-03-25 11:17:37 +0000729// Iterates over the LoopInformation of all loops which contain 'block'
730// from the innermost to the outermost.
731class HLoopInformationOutwardIterator : public ValueObject {
732 public:
733 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
734 : current_(block.GetLoopInformation()) {}
735
736 bool Done() const { return current_ == nullptr; }
737
738 void Advance() {
739 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100740 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000741 }
742
743 HLoopInformation* Current() const {
744 DCHECK(!Done());
745 return current_;
746 }
747
748 private:
749 HLoopInformation* current_;
750
751 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
752};
753
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100754#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
755 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000756 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000757 M(ArrayGet, Instruction) \
758 M(ArrayLength, Instruction) \
759 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100760 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000761 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000762 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000763 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100764 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000765 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100766 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700767 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000768 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000769 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000770 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100771 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000772 M(Exit, Instruction) \
773 M(FloatConstant, Constant) \
774 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100775 M(GreaterThan, Condition) \
776 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100777 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000778 M(InstanceFieldGet, Instruction) \
779 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000780 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100781 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000782 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000783 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100784 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000785 M(LessThan, Condition) \
786 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000787 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000788 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100789 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000790 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100791 M(Local, Instruction) \
792 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100793 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000794 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000795 M(Mul, BinaryOperation) \
796 M(Neg, UnaryOperation) \
797 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100798 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100799 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000800 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000801 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000802 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000803 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100804 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000805 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100806 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000807 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100808 M(Return, Instruction) \
809 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000810 M(Shl, BinaryOperation) \
811 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100812 M(StaticFieldGet, Instruction) \
813 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100814 M(StoreLocal, Instruction) \
815 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100816 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000817 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000818 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000819 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000820 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000821 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000822
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100823#define FOR_EACH_INSTRUCTION(M) \
824 FOR_EACH_CONCRETE_INSTRUCTION(M) \
825 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100826 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100827 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100828 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700829
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100830#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000831FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
832#undef FORWARD_DECLARATION
833
Roland Levillainccc07a92014-09-16 14:48:16 +0100834#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000835 InstructionKind GetKind() const OVERRIDE { return k##type; } \
836 const char* DebugName() const OVERRIDE { return #type; } \
837 const H##type* As##type() const OVERRIDE { return this; } \
838 H##type* As##type() OVERRIDE { return this; } \
839 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100840 return other->Is##type(); \
841 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000842 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000843
David Brazdiled596192015-01-23 10:39:45 +0000844template <typename T> class HUseList;
845
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100846template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700847class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000848 public:
David Brazdiled596192015-01-23 10:39:45 +0000849 HUseListNode* GetPrevious() const { return prev_; }
850 HUseListNode* GetNext() const { return next_; }
851 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100852 size_t GetIndex() const { return index_; }
853
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000854 private:
David Brazdiled596192015-01-23 10:39:45 +0000855 HUseListNode(T user, size_t index)
856 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
857
858 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100859 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000860 HUseListNode<T>* prev_;
861 HUseListNode<T>* next_;
862
863 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000864
865 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
866};
867
David Brazdiled596192015-01-23 10:39:45 +0000868template <typename T>
869class HUseList : public ValueObject {
870 public:
871 HUseList() : first_(nullptr) {}
872
873 void Clear() {
874 first_ = nullptr;
875 }
876
877 // Adds a new entry at the beginning of the use list and returns
878 // the newly created node.
879 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000880 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000881 if (IsEmpty()) {
882 first_ = new_node;
883 } else {
884 first_->prev_ = new_node;
885 new_node->next_ = first_;
886 first_ = new_node;
887 }
888 return new_node;
889 }
890
891 HUseListNode<T>* GetFirst() const {
892 return first_;
893 }
894
895 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000896 DCHECK(node != nullptr);
897 DCHECK(Contains(node));
898
David Brazdiled596192015-01-23 10:39:45 +0000899 if (node->prev_ != nullptr) {
900 node->prev_->next_ = node->next_;
901 }
902 if (node->next_ != nullptr) {
903 node->next_->prev_ = node->prev_;
904 }
905 if (node == first_) {
906 first_ = node->next_;
907 }
908 }
909
David Brazdil1abb4192015-02-17 18:33:36 +0000910 bool Contains(const HUseListNode<T>* node) const {
911 if (node == nullptr) {
912 return false;
913 }
914 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
915 if (current == node) {
916 return true;
917 }
918 }
919 return false;
920 }
921
David Brazdiled596192015-01-23 10:39:45 +0000922 bool IsEmpty() const {
923 return first_ == nullptr;
924 }
925
926 bool HasOnlyOneUse() const {
927 return first_ != nullptr && first_->next_ == nullptr;
928 }
929
930 private:
931 HUseListNode<T>* first_;
932};
933
934template<typename T>
935class HUseIterator : public ValueObject {
936 public:
937 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
938
939 bool Done() const { return current_ == nullptr; }
940
941 void Advance() {
942 DCHECK(!Done());
943 current_ = current_->GetNext();
944 }
945
946 HUseListNode<T>* Current() const {
947 DCHECK(!Done());
948 return current_;
949 }
950
951 private:
952 HUseListNode<T>* current_;
953
954 friend class HValue;
955};
956
David Brazdil1abb4192015-02-17 18:33:36 +0000957// This class is used by HEnvironment and HInstruction classes to record the
958// instructions they use and pointers to the corresponding HUseListNodes kept
959// by the used instructions.
960template <typename T>
961class HUserRecord : public ValueObject {
962 public:
963 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
964 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
965
966 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
967 : instruction_(old_record.instruction_), use_node_(use_node) {
968 DCHECK(instruction_ != nullptr);
969 DCHECK(use_node_ != nullptr);
970 DCHECK(old_record.use_node_ == nullptr);
971 }
972
973 HInstruction* GetInstruction() const { return instruction_; }
974 HUseListNode<T>* GetUseNode() const { return use_node_; }
975
976 private:
977 // Instruction used by the user.
978 HInstruction* instruction_;
979
980 // Corresponding entry in the use list kept by 'instruction_'.
981 HUseListNode<T>* use_node_;
982};
983
Calin Juravle27df7582015-04-17 19:12:31 +0100984// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
985// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
986// flag is consider.
987// - DependsOn suggests that there is a real dependency between side effects but it only
988// checks DependendsOnSomething flag.
989//
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100990// Represents the side effects an instruction may have.
991class SideEffects : public ValueObject {
992 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100993 SideEffects() : flags_(0) {}
994
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100995 static SideEffects None() {
996 return SideEffects(0);
997 }
998
999 static SideEffects All() {
1000 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1001 }
1002
1003 static SideEffects ChangesSomething() {
1004 return SideEffects((1 << kFlagChangesCount) - 1);
1005 }
1006
1007 static SideEffects DependsOnSomething() {
1008 int count = kFlagDependsOnCount - kFlagChangesCount;
1009 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1010 }
1011
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001012 SideEffects Union(SideEffects other) const {
1013 return SideEffects(flags_ | other.flags_);
1014 }
1015
Roland Levillain72bceff2014-09-15 18:29:00 +01001016 bool HasSideEffects() const {
1017 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1018 return (flags_ & all_bits_set) != 0;
1019 }
1020
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001021 bool HasAllSideEffects() const {
1022 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1023 return all_bits_set == (flags_ & all_bits_set);
1024 }
1025
1026 bool DependsOn(SideEffects other) const {
1027 size_t depends_flags = other.ComputeDependsFlags();
1028 return (flags_ & depends_flags) != 0;
1029 }
1030
1031 bool HasDependencies() const {
1032 int count = kFlagDependsOnCount - kFlagChangesCount;
1033 size_t all_bits_set = (1 << count) - 1;
1034 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1035 }
1036
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001037 private:
1038 static constexpr int kFlagChangesSomething = 0;
1039 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1040
1041 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1042 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1043
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001044 explicit SideEffects(size_t flags) : flags_(flags) {}
1045
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001046 size_t ComputeDependsFlags() const {
1047 return flags_ << kFlagChangesCount;
1048 }
1049
1050 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001051};
1052
David Brazdiled596192015-01-23 10:39:45 +00001053// A HEnvironment object contains the values of virtual registers at a given location.
1054class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1055 public:
1056 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
1057 : vregs_(arena, number_of_vregs) {
1058 vregs_.SetSize(number_of_vregs);
1059 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001060 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001061 }
1062 }
1063
1064 void CopyFrom(HEnvironment* env);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001065 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1066 // input to the loop phi instead. This is for inserting instructions that
1067 // require an environment (like HDeoptimization) in the loop pre-header.
1068 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001069
1070 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001071 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001072 }
1073
1074 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001075 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001076 }
1077
David Brazdil1abb4192015-02-17 18:33:36 +00001078 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001079
1080 size_t Size() const { return vregs_.Size(); }
1081
1082 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001083 // Record instructions' use entries of this environment for constant-time removal.
1084 // It should only be called by HInstruction when a new environment use is added.
1085 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1086 DCHECK(env_use->GetUser() == this);
1087 size_t index = env_use->GetIndex();
1088 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1089 }
David Brazdiled596192015-01-23 10:39:45 +00001090
David Brazdil1abb4192015-02-17 18:33:36 +00001091 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +00001092
David Brazdil1abb4192015-02-17 18:33:36 +00001093 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001094
1095 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1096};
1097
Calin Juravleacf735c2015-02-12 15:25:22 +00001098class ReferenceTypeInfo : ValueObject {
1099 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001100 typedef Handle<mirror::Class> TypeHandle;
1101
1102 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1103 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1104 if (type_handle->IsObjectClass()) {
1105 // Override the type handle to be consistent with the case when we get to
1106 // Top but don't have the Object class available. It avoids having to guess
1107 // what value the type_handle has when it's Top.
1108 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1109 } else {
1110 return ReferenceTypeInfo(type_handle, is_exact, false);
1111 }
1112 }
1113
1114 static ReferenceTypeInfo CreateTop(bool is_exact) {
1115 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001116 }
1117
1118 bool IsExact() const { return is_exact_; }
1119 bool IsTop() const { return is_top_; }
1120
1121 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1122
Calin Juravleb1498f62015-02-16 13:13:29 +00001123 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001124 if (IsTop()) {
1125 // Top (equivalent for java.lang.Object) is supertype of anything.
1126 return true;
1127 }
1128 if (rti.IsTop()) {
1129 // If we get here `this` is not Top() so it can't be a supertype.
1130 return false;
1131 }
1132 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1133 }
1134
1135 // Returns true if the type information provide the same amount of details.
1136 // Note that it does not mean that the instructions have the same actual type
1137 // (e.g. tops are equal but they can be the result of a merge).
1138 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1139 if (IsExact() != rti.IsExact()) {
1140 return false;
1141 }
1142 if (IsTop() && rti.IsTop()) {
1143 // `Top` means java.lang.Object, so the types are equivalent.
1144 return true;
1145 }
1146 if (IsTop() || rti.IsTop()) {
1147 // If only one is top or object than they are not equivalent.
1148 // NB: We need this extra check because the type_handle of `Top` is invalid
1149 // and we cannot inspect its reference.
1150 return false;
1151 }
1152
1153 // Finally check the types.
1154 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1155 }
1156
1157 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001158 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1159 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1160 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1161
Calin Juravleacf735c2015-02-12 15:25:22 +00001162 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001163 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001164 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001165 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001166 bool is_exact_;
1167 // A true value here means that the object type should be java.lang.Object.
1168 // We don't have access to the corresponding mirror object every time so this
1169 // flag acts as a substitute. When true, the TypeHandle refers to a null
1170 // pointer and should not be used.
1171 bool is_top_;
1172};
1173
1174std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1175
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001176class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001177 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001178 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001179 : previous_(nullptr),
1180 next_(nullptr),
1181 block_(nullptr),
1182 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001183 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001184 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001185 locations_(nullptr),
1186 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001187 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001188 side_effects_(side_effects),
1189 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001190
Dave Allison20dfc792014-06-16 20:44:29 -07001191 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001192
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001193#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001194 enum InstructionKind {
1195 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1196 };
1197#undef DECLARE_KIND
1198
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001199 HInstruction* GetNext() const { return next_; }
1200 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001201
Calin Juravle77520bc2015-01-12 18:45:46 +00001202 HInstruction* GetNextDisregardingMoves() const;
1203 HInstruction* GetPreviousDisregardingMoves() const;
1204
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001205 HBasicBlock* GetBlock() const { return block_; }
1206 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001207 bool IsInBlock() const { return block_ != nullptr; }
1208 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001209 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001210
Roland Levillain6b879dd2014-09-22 17:13:44 +01001211 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001212 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001213
1214 virtual void Accept(HGraphVisitor* visitor) = 0;
1215 virtual const char* DebugName() const = 0;
1216
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001217 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001218 void SetRawInputAt(size_t index, HInstruction* input) {
1219 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1220 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001221
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001222 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001223 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001224 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001225 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001226
Calin Juravle10e244f2015-01-26 18:54:32 +00001227 // Does not apply for all instructions, but having this at top level greatly
1228 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001229 virtual bool CanBeNull() const {
1230 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1231 return true;
1232 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001233
Calin Juravle641547a2015-04-21 22:08:51 +01001234 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1235 UNUSED(obj);
1236 return false;
1237 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001238
Calin Juravleacf735c2015-02-12 15:25:22 +00001239 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001240 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001241 reference_type_info_ = reference_type_info;
1242 }
1243
Calin Juravle61d544b2015-02-23 16:46:57 +00001244 ReferenceTypeInfo GetReferenceTypeInfo() const {
1245 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1246 return reference_type_info_;
1247 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001248
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001249 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001250 DCHECK(user != nullptr);
1251 HUseListNode<HInstruction*>* use =
1252 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1253 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001254 }
1255
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001256 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001257 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001258 HUseListNode<HEnvironment*>* env_use =
1259 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1260 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001261 }
1262
David Brazdil1abb4192015-02-17 18:33:36 +00001263 void RemoveAsUserOfInput(size_t input) {
1264 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1265 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1266 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001267
David Brazdil1abb4192015-02-17 18:33:36 +00001268 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1269 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001270
David Brazdiled596192015-01-23 10:39:45 +00001271 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1272 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001273 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001274 bool HasOnlyOneNonEnvironmentUse() const {
1275 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1276 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001277
Roland Levillain6c82d402014-10-13 16:10:27 +01001278 // Does this instruction strictly dominate `other_instruction`?
1279 // Returns false if this instruction and `other_instruction` are the same.
1280 // Aborts if this instruction and `other_instruction` are both phis.
1281 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001282
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001283 int GetId() const { return id_; }
1284 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001285
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001286 int GetSsaIndex() const { return ssa_index_; }
1287 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1288 bool HasSsaIndex() const { return ssa_index_ != -1; }
1289
1290 bool HasEnvironment() const { return environment_ != nullptr; }
1291 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001292 // Set the `environment_` field. Raw because this method does not
1293 // update the uses lists.
1294 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1295
1296 // Set the environment of this instruction, copying it from `environment`. While
1297 // copying, the uses lists are being updated.
1298 void CopyEnvironmentFrom(HEnvironment* environment) {
1299 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1300 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1301 environment_->CopyFrom(environment);
1302 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001303
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001304 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1305 HBasicBlock* block) {
1306 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1307 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1308 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
1309 }
1310
Nicolas Geoffray39468442014-09-02 15:17:15 +01001311 // Returns the number of entries in the environment. Typically, that is the
1312 // number of dex registers in a method. It could be more in case of inlining.
1313 size_t EnvironmentSize() const;
1314
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001315 LocationSummary* GetLocations() const { return locations_; }
1316 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001317
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001318 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001319 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001320
Alexandre Rames188d4312015-04-09 18:30:21 +01001321 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1322 // uses of this instruction by `other` are *not* updated.
1323 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1324 ReplaceWith(other);
1325 other->ReplaceInput(this, use_index);
1326 }
1327
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001328 // Move `this` instruction before `cursor`.
1329 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001330
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001331#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001332 bool Is##type() const { return (As##type() != nullptr); } \
1333 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001334 virtual H##type* As##type() { return nullptr; }
1335
1336 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1337#undef INSTRUCTION_TYPE_CHECK
1338
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001339 // Returns whether the instruction can be moved within the graph.
1340 virtual bool CanBeMoved() const { return false; }
1341
1342 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001343 virtual bool InstructionTypeEquals(HInstruction* other) const {
1344 UNUSED(other);
1345 return false;
1346 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001347
1348 // Returns whether any data encoded in the two instructions is equal.
1349 // This method does not look at the inputs. Both instructions must be
1350 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001351 virtual bool InstructionDataEquals(HInstruction* other) const {
1352 UNUSED(other);
1353 return false;
1354 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001355
1356 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001357 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001358 // 2) Their inputs are identical.
1359 bool Equals(HInstruction* other) const;
1360
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001361 virtual InstructionKind GetKind() const = 0;
1362
1363 virtual size_t ComputeHashCode() const {
1364 size_t result = GetKind();
1365 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1366 result = (result * 31) + InputAt(i)->GetId();
1367 }
1368 return result;
1369 }
1370
1371 SideEffects GetSideEffects() const { return side_effects_; }
1372
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001373 size_t GetLifetimePosition() const { return lifetime_position_; }
1374 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1375 LiveInterval* GetLiveInterval() const { return live_interval_; }
1376 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1377 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1378
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001379 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1380
1381 // Returns whether the code generation of the instruction will require to have access
1382 // to the current method. Such instructions are:
1383 // (1): Instructions that require an environment, as calling the runtime requires
1384 // to walk the stack and have the current method stored at a specific stack address.
1385 // (2): Object literals like classes and strings, that are loaded from the dex cache
1386 // fields of the current method.
1387 bool NeedsCurrentMethod() const {
1388 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1389 }
1390
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001391 virtual bool NeedsDexCache() const { return false; }
1392
David Brazdil1abb4192015-02-17 18:33:36 +00001393 protected:
1394 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1395 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1396
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001397 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001398 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1399
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001400 HInstruction* previous_;
1401 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001402 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001403
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001404 // An instruction gets an id when it is added to the graph.
1405 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001406 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001407 int id_;
1408
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001409 // When doing liveness analysis, instructions that have uses get an SSA index.
1410 int ssa_index_;
1411
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001412 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001413 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001414
1415 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001416 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001417
Nicolas Geoffray39468442014-09-02 15:17:15 +01001418 // The environment associated with this instruction. Not null if the instruction
1419 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001420 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001421
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001422 // Set by the code generator.
1423 LocationSummary* locations_;
1424
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001425 // Set by the liveness analysis.
1426 LiveInterval* live_interval_;
1427
1428 // Set by the liveness analysis, this is the position in a linear
1429 // order of blocks where this instruction's live interval start.
1430 size_t lifetime_position_;
1431
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001432 const SideEffects side_effects_;
1433
Calin Juravleacf735c2015-02-12 15:25:22 +00001434 // TODO: for primitive types this should be marked as invalid.
1435 ReferenceTypeInfo reference_type_info_;
1436
David Brazdil1abb4192015-02-17 18:33:36 +00001437 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001438 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001439 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001440 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001441 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001442
1443 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1444};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001445std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001446
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001447class HInputIterator : public ValueObject {
1448 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001449 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001450
1451 bool Done() const { return index_ == instruction_->InputCount(); }
1452 HInstruction* Current() const { return instruction_->InputAt(index_); }
1453 void Advance() { index_++; }
1454
1455 private:
1456 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001457 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001458
1459 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1460};
1461
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001462class HInstructionIterator : public ValueObject {
1463 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001464 explicit HInstructionIterator(const HInstructionList& instructions)
1465 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001466 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001467 }
1468
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001469 bool Done() const { return instruction_ == nullptr; }
1470 HInstruction* Current() const { return instruction_; }
1471 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001472 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001473 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001474 }
1475
1476 private:
1477 HInstruction* instruction_;
1478 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001479
1480 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001481};
1482
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001483class HBackwardInstructionIterator : public ValueObject {
1484 public:
1485 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1486 : instruction_(instructions.last_instruction_) {
1487 next_ = Done() ? nullptr : instruction_->GetPrevious();
1488 }
1489
1490 bool Done() const { return instruction_ == nullptr; }
1491 HInstruction* Current() const { return instruction_; }
1492 void Advance() {
1493 instruction_ = next_;
1494 next_ = Done() ? nullptr : instruction_->GetPrevious();
1495 }
1496
1497 private:
1498 HInstruction* instruction_;
1499 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001500
1501 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001502};
1503
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001504// An embedded container with N elements of type T. Used (with partial
1505// specialization for N=0) because embedded arrays cannot have size 0.
1506template<typename T, intptr_t N>
1507class EmbeddedArray {
1508 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001509 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001510
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001511 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001512
1513 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001514 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001515 return elements_[i];
1516 }
1517
1518 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001519 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001520 return elements_[i];
1521 }
1522
1523 const T& At(intptr_t i) const {
1524 return (*this)[i];
1525 }
1526
1527 void SetAt(intptr_t i, const T& val) {
1528 (*this)[i] = val;
1529 }
1530
1531 private:
1532 T elements_[N];
1533};
1534
1535template<typename T>
1536class EmbeddedArray<T, 0> {
1537 public:
1538 intptr_t length() const { return 0; }
1539 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001540 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001541 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001542 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001543 }
1544 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001545 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001546 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001547 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001548 }
1549};
1550
1551template<intptr_t N>
1552class HTemplateInstruction: public HInstruction {
1553 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001554 HTemplateInstruction<N>(SideEffects side_effects)
1555 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001556 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001557
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001558 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001559
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001560 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001561 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1562
1563 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1564 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001565 }
1566
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001567 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001568 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001569
1570 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001571};
1572
Dave Allison20dfc792014-06-16 20:44:29 -07001573template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001574class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001575 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001576 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1577 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001578 virtual ~HExpression() {}
1579
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001580 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001581
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001582 protected:
1583 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001584};
1585
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001586// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1587// instruction that branches to the exit block.
1588class HReturnVoid : public HTemplateInstruction<0> {
1589 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001590 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001591
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001592 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001593
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001594 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001595
1596 private:
1597 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1598};
1599
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001600// Represents dex's RETURN opcodes. A HReturn is a control flow
1601// instruction that branches to the exit block.
1602class HReturn : public HTemplateInstruction<1> {
1603 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001604 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001605 SetRawInputAt(0, value);
1606 }
1607
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001608 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001609
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001610 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001611
1612 private:
1613 DISALLOW_COPY_AND_ASSIGN(HReturn);
1614};
1615
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001616// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001617// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001618// exit block.
1619class HExit : public HTemplateInstruction<0> {
1620 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001621 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001622
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001623 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001624
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001625 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001626
1627 private:
1628 DISALLOW_COPY_AND_ASSIGN(HExit);
1629};
1630
1631// Jumps from one block to another.
1632class HGoto : public HTemplateInstruction<0> {
1633 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001634 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1635
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001636 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001637
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001638 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001639 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001640 }
1641
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001642 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001643
1644 private:
1645 DISALLOW_COPY_AND_ASSIGN(HGoto);
1646};
1647
Dave Allison20dfc792014-06-16 20:44:29 -07001648
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001649// Conditional branch. A block ending with an HIf instruction must have
1650// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001651class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001652 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001653 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001654 SetRawInputAt(0, input);
1655 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001656
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001657 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001658
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001659 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001660 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001661 }
1662
1663 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001664 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001665 }
1666
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001667 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001668
1669 private:
1670 DISALLOW_COPY_AND_ASSIGN(HIf);
1671};
1672
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001673// Deoptimize to interpreter, upon checking a condition.
1674class HDeoptimize : public HTemplateInstruction<1> {
1675 public:
1676 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1677 : HTemplateInstruction(SideEffects::None()),
1678 dex_pc_(dex_pc) {
1679 SetRawInputAt(0, cond);
1680 }
1681
1682 bool NeedsEnvironment() const OVERRIDE { return true; }
1683 bool CanThrow() const OVERRIDE { return true; }
1684 uint32_t GetDexPc() const { return dex_pc_; }
1685
1686 DECLARE_INSTRUCTION(Deoptimize);
1687
1688 private:
1689 uint32_t dex_pc_;
1690
1691 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1692};
1693
Roland Levillain88cb1752014-10-20 16:36:47 +01001694class HUnaryOperation : public HExpression<1> {
1695 public:
1696 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1697 : HExpression(result_type, SideEffects::None()) {
1698 SetRawInputAt(0, input);
1699 }
1700
1701 HInstruction* GetInput() const { return InputAt(0); }
1702 Primitive::Type GetResultType() const { return GetType(); }
1703
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001704 bool CanBeMoved() const OVERRIDE { return true; }
1705 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001706 UNUSED(other);
1707 return true;
1708 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001709
Roland Levillain9240d6a2014-10-20 16:47:04 +01001710 // Try to statically evaluate `operation` and return a HConstant
1711 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001712 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001713 HConstant* TryStaticEvaluation() const;
1714
1715 // Apply this operation to `x`.
1716 virtual int32_t Evaluate(int32_t x) const = 0;
1717 virtual int64_t Evaluate(int64_t x) const = 0;
1718
Roland Levillain88cb1752014-10-20 16:36:47 +01001719 DECLARE_INSTRUCTION(UnaryOperation);
1720
1721 private:
1722 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1723};
1724
Dave Allison20dfc792014-06-16 20:44:29 -07001725class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001726 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001727 HBinaryOperation(Primitive::Type result_type,
1728 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001729 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001730 SetRawInputAt(0, left);
1731 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001732 }
1733
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001734 HInstruction* GetLeft() const { return InputAt(0); }
1735 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001736 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001737
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001738 virtual bool IsCommutative() const { return false; }
1739
1740 // Put constant on the right.
1741 // Returns whether order is changed.
1742 bool OrderInputsWithConstantOnTheRight() {
1743 HInstruction* left = InputAt(0);
1744 HInstruction* right = InputAt(1);
1745 if (left->IsConstant() && !right->IsConstant()) {
1746 ReplaceInput(right, 0);
1747 ReplaceInput(left, 1);
1748 return true;
1749 }
1750 return false;
1751 }
1752
1753 // Order inputs by instruction id, but favor constant on the right side.
1754 // This helps GVN for commutative ops.
1755 void OrderInputs() {
1756 DCHECK(IsCommutative());
1757 HInstruction* left = InputAt(0);
1758 HInstruction* right = InputAt(1);
1759 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1760 return;
1761 }
1762 if (OrderInputsWithConstantOnTheRight()) {
1763 return;
1764 }
1765 // Order according to instruction id.
1766 if (left->GetId() > right->GetId()) {
1767 ReplaceInput(right, 0);
1768 ReplaceInput(left, 1);
1769 }
1770 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001771
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001772 bool CanBeMoved() const OVERRIDE { return true; }
1773 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001774 UNUSED(other);
1775 return true;
1776 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001777
Roland Levillain9240d6a2014-10-20 16:47:04 +01001778 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001779 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001780 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001781 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001782
1783 // Apply this operation to `x` and `y`.
1784 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1785 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1786
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001787 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001788 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001789 HConstant* GetConstantRight() const;
1790
1791 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001792 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001793 HInstruction* GetLeastConstantLeft() const;
1794
Roland Levillainccc07a92014-09-16 14:48:16 +01001795 DECLARE_INSTRUCTION(BinaryOperation);
1796
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001797 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001798 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1799};
1800
Dave Allison20dfc792014-06-16 20:44:29 -07001801class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001802 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001803 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001804 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1805 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001806
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001807 bool NeedsMaterialization() const { return needs_materialization_; }
1808 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001809
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001810 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001811 // `instruction`, and disregard moves in between.
1812 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001813
Dave Allison20dfc792014-06-16 20:44:29 -07001814 DECLARE_INSTRUCTION(Condition);
1815
1816 virtual IfCondition GetCondition() const = 0;
1817
1818 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001819 // For register allocation purposes, returns whether this instruction needs to be
1820 // materialized (that is, not just be in the processor flags).
1821 bool needs_materialization_;
1822
Dave Allison20dfc792014-06-16 20:44:29 -07001823 DISALLOW_COPY_AND_ASSIGN(HCondition);
1824};
1825
1826// Instruction to check if two inputs are equal to each other.
1827class HEqual : public HCondition {
1828 public:
1829 HEqual(HInstruction* first, HInstruction* second)
1830 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001831
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001832 bool IsCommutative() const OVERRIDE { return true; }
1833
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001834 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001835 return x == y ? 1 : 0;
1836 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001837 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001838 return x == y ? 1 : 0;
1839 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001840
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001841 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001842
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001843 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001844 return kCondEQ;
1845 }
1846
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001847 private:
1848 DISALLOW_COPY_AND_ASSIGN(HEqual);
1849};
1850
Dave Allison20dfc792014-06-16 20:44:29 -07001851class HNotEqual : public HCondition {
1852 public:
1853 HNotEqual(HInstruction* first, HInstruction* second)
1854 : HCondition(first, second) {}
1855
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001856 bool IsCommutative() const OVERRIDE { return true; }
1857
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001858 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001859 return x != y ? 1 : 0;
1860 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001861 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001862 return x != y ? 1 : 0;
1863 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001864
Dave Allison20dfc792014-06-16 20:44:29 -07001865 DECLARE_INSTRUCTION(NotEqual);
1866
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001867 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001868 return kCondNE;
1869 }
1870
1871 private:
1872 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1873};
1874
1875class HLessThan : public HCondition {
1876 public:
1877 HLessThan(HInstruction* first, HInstruction* second)
1878 : HCondition(first, second) {}
1879
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001880 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001881 return x < y ? 1 : 0;
1882 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001883 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001884 return x < y ? 1 : 0;
1885 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001886
Dave Allison20dfc792014-06-16 20:44:29 -07001887 DECLARE_INSTRUCTION(LessThan);
1888
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001889 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001890 return kCondLT;
1891 }
1892
1893 private:
1894 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1895};
1896
1897class HLessThanOrEqual : public HCondition {
1898 public:
1899 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1900 : HCondition(first, second) {}
1901
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001902 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001903 return x <= y ? 1 : 0;
1904 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001905 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001906 return x <= y ? 1 : 0;
1907 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001908
Dave Allison20dfc792014-06-16 20:44:29 -07001909 DECLARE_INSTRUCTION(LessThanOrEqual);
1910
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001911 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001912 return kCondLE;
1913 }
1914
1915 private:
1916 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1917};
1918
1919class HGreaterThan : public HCondition {
1920 public:
1921 HGreaterThan(HInstruction* first, HInstruction* second)
1922 : HCondition(first, second) {}
1923
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001924 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001925 return x > y ? 1 : 0;
1926 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001927 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001928 return x > y ? 1 : 0;
1929 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001930
Dave Allison20dfc792014-06-16 20:44:29 -07001931 DECLARE_INSTRUCTION(GreaterThan);
1932
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001933 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001934 return kCondGT;
1935 }
1936
1937 private:
1938 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1939};
1940
1941class HGreaterThanOrEqual : public HCondition {
1942 public:
1943 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1944 : HCondition(first, second) {}
1945
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001946 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001947 return x >= y ? 1 : 0;
1948 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001949 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001950 return x >= y ? 1 : 0;
1951 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001952
Dave Allison20dfc792014-06-16 20:44:29 -07001953 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1954
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001955 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001956 return kCondGE;
1957 }
1958
1959 private:
1960 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1961};
1962
1963
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001964// Instruction to check how two inputs compare to each other.
1965// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1966class HCompare : public HBinaryOperation {
1967 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001968 // The bias applies for floating point operations and indicates how NaN
1969 // comparisons are treated:
1970 enum Bias {
1971 kNoBias, // bias is not applicable (i.e. for long operation)
1972 kGtBias, // return 1 for NaN comparisons
1973 kLtBias, // return -1 for NaN comparisons
1974 };
1975
1976 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1977 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001978 DCHECK_EQ(type, first->GetType());
1979 DCHECK_EQ(type, second->GetType());
1980 }
1981
Calin Juravleddb7df22014-11-25 20:56:51 +00001982 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001983 return
1984 x == y ? 0 :
1985 x > y ? 1 :
1986 -1;
1987 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001988
1989 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001990 return
1991 x == y ? 0 :
1992 x > y ? 1 :
1993 -1;
1994 }
1995
Calin Juravleddb7df22014-11-25 20:56:51 +00001996 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1997 return bias_ == other->AsCompare()->bias_;
1998 }
1999
2000 bool IsGtBias() { return bias_ == kGtBias; }
2001
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002002 DECLARE_INSTRUCTION(Compare);
2003
2004 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002005 const Bias bias_;
2006
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002007 DISALLOW_COPY_AND_ASSIGN(HCompare);
2008};
2009
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002010// A local in the graph. Corresponds to a Dex register.
2011class HLocal : public HTemplateInstruction<0> {
2012 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002013 explicit HLocal(uint16_t reg_number)
2014 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002015
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002016 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002017
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002018 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002019
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002020 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002021 // The Dex register number.
2022 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002023
2024 DISALLOW_COPY_AND_ASSIGN(HLocal);
2025};
2026
2027// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002028class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002029 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002030 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002031 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002032 SetRawInputAt(0, local);
2033 }
2034
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002035 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2036
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002037 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002038
2039 private:
2040 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2041};
2042
2043// Store a value in a given local. This instruction has two inputs: the value
2044// and the local.
2045class HStoreLocal : public HTemplateInstruction<2> {
2046 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002047 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002048 SetRawInputAt(0, local);
2049 SetRawInputAt(1, value);
2050 }
2051
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002052 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2053
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002054 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002055
2056 private:
2057 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2058};
2059
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002060class HConstant : public HExpression<0> {
2061 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002062 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2063
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002064 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002065
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002066 virtual bool IsMinusOne() const { return false; }
2067 virtual bool IsZero() const { return false; }
2068 virtual bool IsOne() const { return false; }
2069
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002070 DECLARE_INSTRUCTION(Constant);
2071
2072 private:
2073 DISALLOW_COPY_AND_ASSIGN(HConstant);
2074};
2075
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002076class HFloatConstant : public HConstant {
2077 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002078 float GetValue() const { return value_; }
2079
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002080 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002081 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2082 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002083 }
2084
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002085 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002086
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002087 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002088 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2089 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002090 }
2091 bool IsZero() const OVERRIDE {
2092 return AsFloatConstant()->GetValue() == 0.0f;
2093 }
2094 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002095 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2096 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002097 }
2098
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002099 DECLARE_INSTRUCTION(FloatConstant);
2100
2101 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002102 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002103 explicit HFloatConstant(int32_t value)
2104 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002105
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002106 const float value_;
2107
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002108 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002109 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002110 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002111 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2112};
2113
2114class HDoubleConstant : public HConstant {
2115 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002116 double GetValue() const { return value_; }
2117
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002118 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002119 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2120 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002121 }
2122
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002123 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002124
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002125 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002126 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2127 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002128 }
2129 bool IsZero() const OVERRIDE {
2130 return AsDoubleConstant()->GetValue() == 0.0;
2131 }
2132 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002133 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2134 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002135 }
2136
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002137 DECLARE_INSTRUCTION(DoubleConstant);
2138
2139 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002140 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002141 explicit HDoubleConstant(int64_t value)
2142 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002143
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002144 const double value_;
2145
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002146 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002147 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002148 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002149 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2150};
2151
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002152class HNullConstant : public HConstant {
2153 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002154 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2155 return true;
2156 }
2157
2158 size_t ComputeHashCode() const OVERRIDE { return 0; }
2159
2160 DECLARE_INSTRUCTION(NullConstant);
2161
2162 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002163 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2164
2165 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002166 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2167};
2168
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002169// Constants of the type int. Those can be from Dex instructions, or
2170// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002171class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002172 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002173 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002174
Calin Juravle61d544b2015-02-23 16:46:57 +00002175 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002176 return other->AsIntConstant()->value_ == value_;
2177 }
2178
Calin Juravle61d544b2015-02-23 16:46:57 +00002179 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2180
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002181 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2182 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2183 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2184
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002185 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002186
2187 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002188 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2189
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002190 const int32_t value_;
2191
David Brazdil8d5b8b22015-03-24 10:51:52 +00002192 friend class HGraph;
2193 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002194 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002195 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2196};
2197
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002198class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002199 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002200 int64_t GetValue() const { return value_; }
2201
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002202 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002203 return other->AsLongConstant()->value_ == value_;
2204 }
2205
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002206 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002207
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002208 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2209 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2210 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2211
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002212 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002213
2214 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002215 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2216
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002217 const int64_t value_;
2218
David Brazdil8d5b8b22015-03-24 10:51:52 +00002219 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002220 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2221};
2222
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002223enum class Intrinsics {
2224#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2225#include "intrinsics_list.h"
2226 kNone,
2227 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2228#undef INTRINSICS_LIST
2229#undef OPTIMIZING_INTRINSICS
2230};
2231std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2232
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002233class HInvoke : public HInstruction {
2234 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002235 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002236
2237 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2238 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002239 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002240
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002241 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002242 SetRawInputAt(index, argument);
2243 }
2244
Roland Levillain3e3d7332015-04-28 11:00:54 +01002245 // Return the number of arguments. This number can be lower than
2246 // the number of inputs returned by InputCount(), as some invoke
2247 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2248 // inputs at the end of their list of inputs.
2249 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2250
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002251 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002252
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002253 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002254
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002255 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2256
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002257 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002258 return intrinsic_;
2259 }
2260
2261 void SetIntrinsic(Intrinsics intrinsic) {
2262 intrinsic_ = intrinsic;
2263 }
2264
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002265 DECLARE_INSTRUCTION(Invoke);
2266
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002267 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002268 HInvoke(ArenaAllocator* arena,
2269 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002270 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002271 Primitive::Type return_type,
2272 uint32_t dex_pc,
2273 uint32_t dex_method_index)
2274 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002275 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002276 inputs_(arena, number_of_arguments),
2277 return_type_(return_type),
2278 dex_pc_(dex_pc),
2279 dex_method_index_(dex_method_index),
2280 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002281 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2282 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002283 }
2284
David Brazdil1abb4192015-02-17 18:33:36 +00002285 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2286 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2287 inputs_.Put(index, input);
2288 }
2289
Roland Levillain3e3d7332015-04-28 11:00:54 +01002290 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002291 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002292 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002293 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002294 const uint32_t dex_method_index_;
2295 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002296
2297 private:
2298 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2299};
2300
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002301class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002302 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002303 // Requirements of this method call regarding the class
2304 // initialization (clinit) check of its declaring class.
2305 enum class ClinitCheckRequirement {
2306 kNone, // Class already initialized.
2307 kExplicit, // Static call having explicit clinit check as last input.
2308 kImplicit, // Static call implicitly requiring a clinit check.
2309 };
2310
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002311 HInvokeStaticOrDirect(ArenaAllocator* arena,
2312 uint32_t number_of_arguments,
2313 Primitive::Type return_type,
2314 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002315 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002316 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002317 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002318 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002319 InvokeType invoke_type,
2320 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002321 : HInvoke(arena,
2322 number_of_arguments,
2323 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u,
2324 return_type,
2325 dex_pc,
2326 dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002327 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002328 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002329 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002330 clinit_check_requirement_(clinit_check_requirement),
2331 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002332
Calin Juravle641547a2015-04-21 22:08:51 +01002333 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2334 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002335 // We access the method via the dex cache so we can't do an implicit null check.
2336 // TODO: for intrinsics we can generate implicit null checks.
2337 return false;
2338 }
2339
Nicolas Geoffray79041292015-03-26 10:05:54 +00002340 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002341 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002342 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002343 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002344 bool IsStringInit() const { return string_init_offset_ != 0; }
2345 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002346
Roland Levillain4c0eb422015-04-24 16:43:49 +01002347 // Is this instruction a call to a static method?
2348 bool IsStatic() const {
2349 return GetInvokeType() == kStatic;
2350 }
2351
Roland Levillain3e3d7332015-04-28 11:00:54 +01002352 // Remove the art::HLoadClass instruction set as last input by
2353 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2354 // the initial art::HClinitCheck instruction (only relevant for
2355 // static calls with explicit clinit check).
2356 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002357 DCHECK(IsStaticWithExplicitClinitCheck());
2358 size_t last_input_index = InputCount() - 1;
2359 HInstruction* last_input = InputAt(last_input_index);
2360 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002361 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002362 RemoveAsUserOfInput(last_input_index);
2363 inputs_.DeleteAt(last_input_index);
2364 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2365 DCHECK(IsStaticWithImplicitClinitCheck());
2366 }
2367
2368 // Is this a call to a static method whose declaring class has an
2369 // explicit intialization check in the graph?
2370 bool IsStaticWithExplicitClinitCheck() const {
2371 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2372 }
2373
2374 // Is this a call to a static method whose declaring class has an
2375 // implicit intialization check requirement?
2376 bool IsStaticWithImplicitClinitCheck() const {
2377 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2378 }
2379
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002380 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002381
Roland Levillain4c0eb422015-04-24 16:43:49 +01002382 protected:
2383 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2384 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2385 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2386 HInstruction* input = input_record.GetInstruction();
2387 // `input` is the last input of a static invoke marked as having
2388 // an explicit clinit check. It must either be:
2389 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2390 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2391 DCHECK(input != nullptr);
2392 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2393 }
2394 return input_record;
2395 }
2396
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002397 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002398 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002399 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002400 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002401 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002402 // Thread entrypoint offset for string init method if this is a string init invoke.
2403 // Note that there are multiple string init methods, each having its own offset.
2404 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002405
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002406 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002407};
2408
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002409class HInvokeVirtual : public HInvoke {
2410 public:
2411 HInvokeVirtual(ArenaAllocator* arena,
2412 uint32_t number_of_arguments,
2413 Primitive::Type return_type,
2414 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002415 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002416 uint32_t vtable_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002417 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002418 vtable_index_(vtable_index) {}
2419
Calin Juravle641547a2015-04-21 22:08:51 +01002420 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002421 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002422 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002423 }
2424
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002425 uint32_t GetVTableIndex() const { return vtable_index_; }
2426
2427 DECLARE_INSTRUCTION(InvokeVirtual);
2428
2429 private:
2430 const uint32_t vtable_index_;
2431
2432 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2433};
2434
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002435class HInvokeInterface : public HInvoke {
2436 public:
2437 HInvokeInterface(ArenaAllocator* arena,
2438 uint32_t number_of_arguments,
2439 Primitive::Type return_type,
2440 uint32_t dex_pc,
2441 uint32_t dex_method_index,
2442 uint32_t imt_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002443 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002444 imt_index_(imt_index) {}
2445
Calin Juravle641547a2015-04-21 22:08:51 +01002446 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002447 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002448 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002449 }
2450
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002451 uint32_t GetImtIndex() const { return imt_index_; }
2452 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2453
2454 DECLARE_INSTRUCTION(InvokeInterface);
2455
2456 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002457 const uint32_t imt_index_;
2458
2459 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2460};
2461
Dave Allison20dfc792014-06-16 20:44:29 -07002462class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002463 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002464 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002465 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2466 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002467 type_index_(type_index),
2468 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002469
2470 uint32_t GetDexPc() const { return dex_pc_; }
2471 uint16_t GetTypeIndex() const { return type_index_; }
2472
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002473 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002474 bool NeedsEnvironment() const OVERRIDE { return true; }
2475 // It may throw when called on:
2476 // - interfaces
2477 // - abstract/innaccessible/unknown classes
2478 // TODO: optimize when possible.
2479 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002480
Calin Juravle10e244f2015-01-26 18:54:32 +00002481 bool CanBeNull() const OVERRIDE { return false; }
2482
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002483 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2484
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002485 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002486
2487 private:
2488 const uint32_t dex_pc_;
2489 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002490 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002491
2492 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2493};
2494
Roland Levillain88cb1752014-10-20 16:36:47 +01002495class HNeg : public HUnaryOperation {
2496 public:
2497 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2498 : HUnaryOperation(result_type, input) {}
2499
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 Levillain9240d6a2014-10-20 16:47:04 +01002502
Roland Levillain88cb1752014-10-20 16:36:47 +01002503 DECLARE_INSTRUCTION(Neg);
2504
2505 private:
2506 DISALLOW_COPY_AND_ASSIGN(HNeg);
2507};
2508
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002509class HNewArray : public HExpression<1> {
2510 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002511 HNewArray(HInstruction* length,
2512 uint32_t dex_pc,
2513 uint16_t type_index,
2514 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002515 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2516 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002517 type_index_(type_index),
2518 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002519 SetRawInputAt(0, length);
2520 }
2521
2522 uint32_t GetDexPc() const { return dex_pc_; }
2523 uint16_t GetTypeIndex() const { return type_index_; }
2524
2525 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002526 bool NeedsEnvironment() const OVERRIDE { return true; }
2527
Mingyao Yang0c365e62015-03-31 15:09:29 -07002528 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2529 bool CanThrow() const OVERRIDE { return true; }
2530
Calin Juravle10e244f2015-01-26 18:54:32 +00002531 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002532
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002533 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2534
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002535 DECLARE_INSTRUCTION(NewArray);
2536
2537 private:
2538 const uint32_t dex_pc_;
2539 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002540 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002541
2542 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2543};
2544
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002545class HAdd : public HBinaryOperation {
2546 public:
2547 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2548 : HBinaryOperation(result_type, left, right) {}
2549
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002550 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002551
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002552 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002553 return x + y;
2554 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002555 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002556 return x + y;
2557 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002558
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002559 DECLARE_INSTRUCTION(Add);
2560
2561 private:
2562 DISALLOW_COPY_AND_ASSIGN(HAdd);
2563};
2564
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002565class HSub : public HBinaryOperation {
2566 public:
2567 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2568 : HBinaryOperation(result_type, left, right) {}
2569
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002570 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002571 return x - y;
2572 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002573 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002574 return x - y;
2575 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002576
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002577 DECLARE_INSTRUCTION(Sub);
2578
2579 private:
2580 DISALLOW_COPY_AND_ASSIGN(HSub);
2581};
2582
Calin Juravle34bacdf2014-10-07 20:23:36 +01002583class HMul : public HBinaryOperation {
2584 public:
2585 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2586 : HBinaryOperation(result_type, left, right) {}
2587
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002588 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002589
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002590 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2591 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002592
2593 DECLARE_INSTRUCTION(Mul);
2594
2595 private:
2596 DISALLOW_COPY_AND_ASSIGN(HMul);
2597};
2598
Calin Juravle7c4954d2014-10-28 16:57:40 +00002599class HDiv : public HBinaryOperation {
2600 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002601 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2602 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002603
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002604 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002605 // Our graph structure ensures we never have 0 for `y` during constant folding.
2606 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002607 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002608 return (y == -1) ? -x : x / y;
2609 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002610
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002611 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002612 DCHECK_NE(y, 0);
2613 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2614 return (y == -1) ? -x : x / y;
2615 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002616
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002617 uint32_t GetDexPc() const { return dex_pc_; }
2618
Calin Juravle7c4954d2014-10-28 16:57:40 +00002619 DECLARE_INSTRUCTION(Div);
2620
2621 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002622 const uint32_t dex_pc_;
2623
Calin Juravle7c4954d2014-10-28 16:57:40 +00002624 DISALLOW_COPY_AND_ASSIGN(HDiv);
2625};
2626
Calin Juravlebacfec32014-11-14 15:54:36 +00002627class HRem : public HBinaryOperation {
2628 public:
2629 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2630 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2631
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002632 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002633 DCHECK_NE(y, 0);
2634 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2635 return (y == -1) ? 0 : x % y;
2636 }
2637
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002638 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002639 DCHECK_NE(y, 0);
2640 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2641 return (y == -1) ? 0 : x % y;
2642 }
2643
2644 uint32_t GetDexPc() const { return dex_pc_; }
2645
2646 DECLARE_INSTRUCTION(Rem);
2647
2648 private:
2649 const uint32_t dex_pc_;
2650
2651 DISALLOW_COPY_AND_ASSIGN(HRem);
2652};
2653
Calin Juravled0d48522014-11-04 16:40:20 +00002654class HDivZeroCheck : public HExpression<1> {
2655 public:
2656 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2657 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2658 SetRawInputAt(0, value);
2659 }
2660
2661 bool CanBeMoved() const OVERRIDE { return true; }
2662
2663 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2664 UNUSED(other);
2665 return true;
2666 }
2667
2668 bool NeedsEnvironment() const OVERRIDE { return true; }
2669 bool CanThrow() const OVERRIDE { return true; }
2670
2671 uint32_t GetDexPc() const { return dex_pc_; }
2672
2673 DECLARE_INSTRUCTION(DivZeroCheck);
2674
2675 private:
2676 const uint32_t dex_pc_;
2677
2678 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2679};
2680
Calin Juravle9aec02f2014-11-18 23:06:35 +00002681class HShl : public HBinaryOperation {
2682 public:
2683 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2684 : HBinaryOperation(result_type, left, right) {}
2685
2686 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2687 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2688
2689 DECLARE_INSTRUCTION(Shl);
2690
2691 private:
2692 DISALLOW_COPY_AND_ASSIGN(HShl);
2693};
2694
2695class HShr : public HBinaryOperation {
2696 public:
2697 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2698 : HBinaryOperation(result_type, left, right) {}
2699
2700 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2701 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2702
2703 DECLARE_INSTRUCTION(Shr);
2704
2705 private:
2706 DISALLOW_COPY_AND_ASSIGN(HShr);
2707};
2708
2709class HUShr : public HBinaryOperation {
2710 public:
2711 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2712 : HBinaryOperation(result_type, left, right) {}
2713
2714 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2715 uint32_t ux = static_cast<uint32_t>(x);
2716 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2717 return static_cast<int32_t>(ux >> uy);
2718 }
2719
2720 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2721 uint64_t ux = static_cast<uint64_t>(x);
2722 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2723 return static_cast<int64_t>(ux >> uy);
2724 }
2725
2726 DECLARE_INSTRUCTION(UShr);
2727
2728 private:
2729 DISALLOW_COPY_AND_ASSIGN(HUShr);
2730};
2731
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002732class HAnd : public HBinaryOperation {
2733 public:
2734 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2735 : HBinaryOperation(result_type, left, right) {}
2736
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002737 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002738
2739 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2740 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2741
2742 DECLARE_INSTRUCTION(And);
2743
2744 private:
2745 DISALLOW_COPY_AND_ASSIGN(HAnd);
2746};
2747
2748class HOr : public HBinaryOperation {
2749 public:
2750 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2751 : HBinaryOperation(result_type, left, right) {}
2752
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002753 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002754
2755 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2756 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2757
2758 DECLARE_INSTRUCTION(Or);
2759
2760 private:
2761 DISALLOW_COPY_AND_ASSIGN(HOr);
2762};
2763
2764class HXor : public HBinaryOperation {
2765 public:
2766 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2767 : HBinaryOperation(result_type, left, right) {}
2768
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002769 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002770
2771 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2772 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2773
2774 DECLARE_INSTRUCTION(Xor);
2775
2776 private:
2777 DISALLOW_COPY_AND_ASSIGN(HXor);
2778};
2779
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002780// The value of a parameter in this method. Its location depends on
2781// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002782class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002783 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002784 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2785 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002786
2787 uint8_t GetIndex() const { return index_; }
2788
Calin Juravle10e244f2015-01-26 18:54:32 +00002789 bool CanBeNull() const OVERRIDE { return !is_this_; }
2790
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002791 DECLARE_INSTRUCTION(ParameterValue);
2792
2793 private:
2794 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002795 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002796 const uint8_t index_;
2797
Calin Juravle10e244f2015-01-26 18:54:32 +00002798 // Whether or not the parameter value corresponds to 'this' argument.
2799 const bool is_this_;
2800
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002801 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2802};
2803
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002804class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002805 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002806 explicit HNot(Primitive::Type result_type, HInstruction* input)
2807 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002808
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002809 bool CanBeMoved() const OVERRIDE { return true; }
2810 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002811 UNUSED(other);
2812 return true;
2813 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002814
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002815 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2816 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002817
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002818 DECLARE_INSTRUCTION(Not);
2819
2820 private:
2821 DISALLOW_COPY_AND_ASSIGN(HNot);
2822};
2823
David Brazdil66d126e2015-04-03 16:02:44 +01002824class HBooleanNot : public HUnaryOperation {
2825 public:
2826 explicit HBooleanNot(HInstruction* input)
2827 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2828
2829 bool CanBeMoved() const OVERRIDE { return true; }
2830 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2831 UNUSED(other);
2832 return true;
2833 }
2834
2835 int32_t Evaluate(int32_t x) const OVERRIDE {
2836 DCHECK(IsUint<1>(x));
2837 return !x;
2838 }
2839
2840 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2841 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2842 UNREACHABLE();
2843 }
2844
2845 DECLARE_INSTRUCTION(BooleanNot);
2846
2847 private:
2848 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2849};
2850
Roland Levillaindff1f282014-11-05 14:15:05 +00002851class HTypeConversion : public HExpression<1> {
2852 public:
2853 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002854 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2855 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002856 SetRawInputAt(0, input);
2857 DCHECK_NE(input->GetType(), result_type);
2858 }
2859
2860 HInstruction* GetInput() const { return InputAt(0); }
2861 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2862 Primitive::Type GetResultType() const { return GetType(); }
2863
Roland Levillain624279f2014-12-04 11:54:28 +00002864 // Required by the x86 and ARM code generators when producing calls
2865 // to the runtime.
2866 uint32_t GetDexPc() const { return dex_pc_; }
2867
Roland Levillaindff1f282014-11-05 14:15:05 +00002868 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002869 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002870
2871 DECLARE_INSTRUCTION(TypeConversion);
2872
2873 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002874 const uint32_t dex_pc_;
2875
Roland Levillaindff1f282014-11-05 14:15:05 +00002876 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2877};
2878
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002879static constexpr uint32_t kNoRegNumber = -1;
2880
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002881class HPhi : public HInstruction {
2882 public:
2883 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002884 : HInstruction(SideEffects::None()),
2885 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002886 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002887 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002888 is_live_(false),
2889 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002890 inputs_.SetSize(number_of_inputs);
2891 }
2892
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002893 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2894 static Primitive::Type ToPhiType(Primitive::Type type) {
2895 switch (type) {
2896 case Primitive::kPrimBoolean:
2897 case Primitive::kPrimByte:
2898 case Primitive::kPrimShort:
2899 case Primitive::kPrimChar:
2900 return Primitive::kPrimInt;
2901 default:
2902 return type;
2903 }
2904 }
2905
Calin Juravle10e244f2015-01-26 18:54:32 +00002906 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002907
2908 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01002909 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002910
Calin Juravle10e244f2015-01-26 18:54:32 +00002911 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002912 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002913
Calin Juravle10e244f2015-01-26 18:54:32 +00002914 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2915 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2916
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002917 uint32_t GetRegNumber() const { return reg_number_; }
2918
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002919 void SetDead() { is_live_ = false; }
2920 void SetLive() { is_live_ = true; }
2921 bool IsDead() const { return !is_live_; }
2922 bool IsLive() const { return is_live_; }
2923
Calin Juravlea4f88312015-04-16 12:57:19 +01002924 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2925 // An equivalent phi is a phi having the same dex register and type.
2926 // It assumes that phis with the same dex register are adjacent.
2927 HPhi* GetNextEquivalentPhiWithSameType() {
2928 HInstruction* next = GetNext();
2929 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2930 if (next->GetType() == GetType()) {
2931 return next->AsPhi();
2932 }
2933 next = next->GetNext();
2934 }
2935 return nullptr;
2936 }
2937
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002938 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002939
David Brazdil1abb4192015-02-17 18:33:36 +00002940 protected:
2941 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2942
2943 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2944 inputs_.Put(index, input);
2945 }
2946
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002947 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002948 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002949 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002950 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002951 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002952 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002953
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002954 DISALLOW_COPY_AND_ASSIGN(HPhi);
2955};
2956
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002957class HNullCheck : public HExpression<1> {
2958 public:
2959 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002960 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002961 SetRawInputAt(0, value);
2962 }
2963
Calin Juravle10e244f2015-01-26 18:54:32 +00002964 bool CanBeMoved() const OVERRIDE { return true; }
2965 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002966 UNUSED(other);
2967 return true;
2968 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002969
Calin Juravle10e244f2015-01-26 18:54:32 +00002970 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002971
Calin Juravle10e244f2015-01-26 18:54:32 +00002972 bool CanThrow() const OVERRIDE { return true; }
2973
2974 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002975
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002976 uint32_t GetDexPc() const { return dex_pc_; }
2977
2978 DECLARE_INSTRUCTION(NullCheck);
2979
2980 private:
2981 const uint32_t dex_pc_;
2982
2983 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2984};
2985
2986class FieldInfo : public ValueObject {
2987 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002988 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2989 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002990
2991 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002992 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002993 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002994
2995 private:
2996 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002997 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002998 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002999};
3000
3001class HInstanceFieldGet : public HExpression<1> {
3002 public:
3003 HInstanceFieldGet(HInstruction* value,
3004 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003005 MemberOffset field_offset,
3006 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003007 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003008 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003009 SetRawInputAt(0, value);
3010 }
3011
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003012 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003013
3014 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3015 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3016 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003017 }
3018
Calin Juravle641547a2015-04-21 22:08:51 +01003019 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3020 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003021 }
3022
3023 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003024 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3025 }
3026
Calin Juravle52c48962014-12-16 17:02:57 +00003027 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003028 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003029 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003030 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003031
3032 DECLARE_INSTRUCTION(InstanceFieldGet);
3033
3034 private:
3035 const FieldInfo field_info_;
3036
3037 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3038};
3039
3040class HInstanceFieldSet : public HTemplateInstruction<2> {
3041 public:
3042 HInstanceFieldSet(HInstruction* object,
3043 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003044 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003045 MemberOffset field_offset,
3046 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003047 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003048 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003049 SetRawInputAt(0, object);
3050 SetRawInputAt(1, value);
3051 }
3052
Calin Juravle641547a2015-04-21 22:08:51 +01003053 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3054 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003055 }
3056
Calin Juravle52c48962014-12-16 17:02:57 +00003057 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003058 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003059 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003060 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003061 HInstruction* GetValue() const { return InputAt(1); }
3062
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003063 DECLARE_INSTRUCTION(InstanceFieldSet);
3064
3065 private:
3066 const FieldInfo field_info_;
3067
3068 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3069};
3070
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003071class HArrayGet : public HExpression<2> {
3072 public:
3073 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003074 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003075 SetRawInputAt(0, array);
3076 SetRawInputAt(1, index);
3077 }
3078
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003079 bool CanBeMoved() const OVERRIDE { return true; }
3080 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003081 UNUSED(other);
3082 return true;
3083 }
Calin Juravle641547a2015-04-21 22:08:51 +01003084 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3085 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003086 // TODO: We can be smarter here.
3087 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3088 // which generates the implicit null check. There are cases when these can be removed
3089 // to produce better code. If we ever add optimizations to do so we should allow an
3090 // implicit check here (as long as the address falls in the first page).
3091 return false;
3092 }
3093
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003094 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003095
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003096 HInstruction* GetArray() const { return InputAt(0); }
3097 HInstruction* GetIndex() const { return InputAt(1); }
3098
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003099 DECLARE_INSTRUCTION(ArrayGet);
3100
3101 private:
3102 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3103};
3104
3105class HArraySet : public HTemplateInstruction<3> {
3106 public:
3107 HArraySet(HInstruction* array,
3108 HInstruction* index,
3109 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003110 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003111 uint32_t dex_pc)
3112 : HTemplateInstruction(SideEffects::ChangesSomething()),
3113 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003114 expected_component_type_(expected_component_type),
3115 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003116 SetRawInputAt(0, array);
3117 SetRawInputAt(1, index);
3118 SetRawInputAt(2, value);
3119 }
3120
Calin Juravle77520bc2015-01-12 18:45:46 +00003121 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003122 // We currently always call a runtime method to catch array store
3123 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003124 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003125 }
3126
Calin Juravle641547a2015-04-21 22:08:51 +01003127 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3128 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003129 // TODO: Same as for ArrayGet.
3130 return false;
3131 }
3132
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003133 void ClearNeedsTypeCheck() {
3134 needs_type_check_ = false;
3135 }
3136
3137 bool NeedsTypeCheck() const { return needs_type_check_; }
3138
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003139 uint32_t GetDexPc() const { return dex_pc_; }
3140
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003141 HInstruction* GetArray() const { return InputAt(0); }
3142 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003143 HInstruction* GetValue() const { return InputAt(2); }
3144
3145 Primitive::Type GetComponentType() const {
3146 // The Dex format does not type floating point index operations. Since the
3147 // `expected_component_type_` is set during building and can therefore not
3148 // be correct, we also check what is the value type. If it is a floating
3149 // point type, we must use that type.
3150 Primitive::Type value_type = GetValue()->GetType();
3151 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3152 ? value_type
3153 : expected_component_type_;
3154 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003155
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003156 DECLARE_INSTRUCTION(ArraySet);
3157
3158 private:
3159 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003160 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003161 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003162
3163 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3164};
3165
3166class HArrayLength : public HExpression<1> {
3167 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003168 explicit HArrayLength(HInstruction* array)
3169 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3170 // Note that arrays do not change length, so the instruction does not
3171 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003172 SetRawInputAt(0, array);
3173 }
3174
Calin Juravle77520bc2015-01-12 18:45:46 +00003175 bool CanBeMoved() const OVERRIDE { return true; }
3176 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003177 UNUSED(other);
3178 return true;
3179 }
Calin Juravle641547a2015-04-21 22:08:51 +01003180 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3181 return obj == InputAt(0);
3182 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003183
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003184 DECLARE_INSTRUCTION(ArrayLength);
3185
3186 private:
3187 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3188};
3189
3190class HBoundsCheck : public HExpression<2> {
3191 public:
3192 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003193 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003194 DCHECK(index->GetType() == Primitive::kPrimInt);
3195 SetRawInputAt(0, index);
3196 SetRawInputAt(1, length);
3197 }
3198
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003199 bool CanBeMoved() const OVERRIDE { return true; }
3200 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003201 UNUSED(other);
3202 return true;
3203 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003204
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003205 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003206
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003207 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003208
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003209 uint32_t GetDexPc() const { return dex_pc_; }
3210
3211 DECLARE_INSTRUCTION(BoundsCheck);
3212
3213 private:
3214 const uint32_t dex_pc_;
3215
3216 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3217};
3218
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003219/**
3220 * Some DEX instructions are folded into multiple HInstructions that need
3221 * to stay live until the last HInstruction. This class
3222 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003223 * HInstruction stays live. `index` represents the stack location index of the
3224 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003225 */
3226class HTemporary : public HTemplateInstruction<0> {
3227 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003228 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003229
3230 size_t GetIndex() const { return index_; }
3231
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003232 Primitive::Type GetType() const OVERRIDE {
3233 // The previous instruction is the one that will be stored in the temporary location.
3234 DCHECK(GetPrevious() != nullptr);
3235 return GetPrevious()->GetType();
3236 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003237
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003238 DECLARE_INSTRUCTION(Temporary);
3239
3240 private:
3241 const size_t index_;
3242
3243 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3244};
3245
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003246class HSuspendCheck : public HTemplateInstruction<0> {
3247 public:
3248 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01003249 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003250
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003251 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003252 return true;
3253 }
3254
3255 uint32_t GetDexPc() const { return dex_pc_; }
3256
3257 DECLARE_INSTRUCTION(SuspendCheck);
3258
3259 private:
3260 const uint32_t dex_pc_;
3261
3262 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3263};
3264
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003265/**
3266 * Instruction to load a Class object.
3267 */
3268class HLoadClass : public HExpression<0> {
3269 public:
3270 HLoadClass(uint16_t type_index,
3271 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003272 uint32_t dex_pc)
3273 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3274 type_index_(type_index),
3275 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003276 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003277 generate_clinit_check_(false),
3278 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003279
3280 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003281
3282 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3283 return other->AsLoadClass()->type_index_ == type_index_;
3284 }
3285
3286 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3287
3288 uint32_t GetDexPc() const { return dex_pc_; }
3289 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003290 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003291
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003292 bool NeedsEnvironment() const OVERRIDE {
3293 // Will call runtime and load the class if the class is not loaded yet.
3294 // TODO: finer grain decision.
3295 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003296 }
3297
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003298 bool MustGenerateClinitCheck() const {
3299 return generate_clinit_check_;
3300 }
3301
3302 void SetMustGenerateClinitCheck() {
3303 generate_clinit_check_ = true;
3304 }
3305
3306 bool CanCallRuntime() const {
3307 return MustGenerateClinitCheck() || !is_referrers_class_;
3308 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003309
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003310 bool CanThrow() const OVERRIDE {
3311 // May call runtime and and therefore can throw.
3312 // TODO: finer grain decision.
3313 return !is_referrers_class_;
3314 }
3315
Calin Juravleacf735c2015-02-12 15:25:22 +00003316 ReferenceTypeInfo GetLoadedClassRTI() {
3317 return loaded_class_rti_;
3318 }
3319
3320 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3321 // Make sure we only set exact types (the loaded class should never be merged).
3322 DCHECK(rti.IsExact());
3323 loaded_class_rti_ = rti;
3324 }
3325
3326 bool IsResolved() {
3327 return loaded_class_rti_.IsExact();
3328 }
3329
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003330 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3331
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003332 DECLARE_INSTRUCTION(LoadClass);
3333
3334 private:
3335 const uint16_t type_index_;
3336 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003337 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003338 // Whether this instruction must generate the initialization check.
3339 // Used for code generation.
3340 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003341
Calin Juravleacf735c2015-02-12 15:25:22 +00003342 ReferenceTypeInfo loaded_class_rti_;
3343
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003344 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3345};
3346
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003347class HLoadString : public HExpression<0> {
3348 public:
3349 HLoadString(uint32_t string_index, uint32_t dex_pc)
3350 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3351 string_index_(string_index),
3352 dex_pc_(dex_pc) {}
3353
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003354 bool CanBeMoved() const OVERRIDE { return true; }
3355
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003356 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3357 return other->AsLoadString()->string_index_ == string_index_;
3358 }
3359
3360 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3361
3362 uint32_t GetDexPc() const { return dex_pc_; }
3363 uint32_t GetStringIndex() const { return string_index_; }
3364
3365 // TODO: Can we deopt or debug when we resolve a string?
3366 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003367 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003368
3369 DECLARE_INSTRUCTION(LoadString);
3370
3371 private:
3372 const uint32_t string_index_;
3373 const uint32_t dex_pc_;
3374
3375 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3376};
3377
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003378/**
3379 * Performs an initialization check on its Class object input.
3380 */
3381class HClinitCheck : public HExpression<1> {
3382 public:
3383 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003384 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003385 dex_pc_(dex_pc) {
3386 SetRawInputAt(0, constant);
3387 }
3388
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003389 bool CanBeMoved() const OVERRIDE { return true; }
3390 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3391 UNUSED(other);
3392 return true;
3393 }
3394
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003395 bool NeedsEnvironment() const OVERRIDE {
3396 // May call runtime to initialize the class.
3397 return true;
3398 }
3399
3400 uint32_t GetDexPc() const { return dex_pc_; }
3401
3402 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3403
3404 DECLARE_INSTRUCTION(ClinitCheck);
3405
3406 private:
3407 const uint32_t dex_pc_;
3408
3409 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3410};
3411
3412class HStaticFieldGet : public HExpression<1> {
3413 public:
3414 HStaticFieldGet(HInstruction* cls,
3415 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003416 MemberOffset field_offset,
3417 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003418 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003419 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003420 SetRawInputAt(0, cls);
3421 }
3422
Calin Juravle52c48962014-12-16 17:02:57 +00003423
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003424 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003425
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003426 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003427 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3428 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003429 }
3430
3431 size_t ComputeHashCode() const OVERRIDE {
3432 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3433 }
3434
Calin Juravle52c48962014-12-16 17:02:57 +00003435 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003436 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3437 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003438 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003439
3440 DECLARE_INSTRUCTION(StaticFieldGet);
3441
3442 private:
3443 const FieldInfo field_info_;
3444
3445 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3446};
3447
3448class HStaticFieldSet : public HTemplateInstruction<2> {
3449 public:
3450 HStaticFieldSet(HInstruction* cls,
3451 HInstruction* value,
3452 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003453 MemberOffset field_offset,
3454 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003455 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003456 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003457 SetRawInputAt(0, cls);
3458 SetRawInputAt(1, value);
3459 }
3460
Calin Juravle52c48962014-12-16 17:02:57 +00003461 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003462 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3463 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003464 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003465
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003466 HInstruction* GetValue() const { return InputAt(1); }
3467
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003468 DECLARE_INSTRUCTION(StaticFieldSet);
3469
3470 private:
3471 const FieldInfo field_info_;
3472
3473 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3474};
3475
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003476// Implement the move-exception DEX instruction.
3477class HLoadException : public HExpression<0> {
3478 public:
3479 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3480
3481 DECLARE_INSTRUCTION(LoadException);
3482
3483 private:
3484 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3485};
3486
3487class HThrow : public HTemplateInstruction<1> {
3488 public:
3489 HThrow(HInstruction* exception, uint32_t dex_pc)
3490 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3491 SetRawInputAt(0, exception);
3492 }
3493
3494 bool IsControlFlow() const OVERRIDE { return true; }
3495
3496 bool NeedsEnvironment() const OVERRIDE { return true; }
3497
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003498 bool CanThrow() const OVERRIDE { return true; }
3499
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003500 uint32_t GetDexPc() const { return dex_pc_; }
3501
3502 DECLARE_INSTRUCTION(Throw);
3503
3504 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003505 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003506
3507 DISALLOW_COPY_AND_ASSIGN(HThrow);
3508};
3509
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003510class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003511 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003512 HInstanceOf(HInstruction* object,
3513 HLoadClass* constant,
3514 bool class_is_final,
3515 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003516 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3517 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003518 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003519 dex_pc_(dex_pc) {
3520 SetRawInputAt(0, object);
3521 SetRawInputAt(1, constant);
3522 }
3523
3524 bool CanBeMoved() const OVERRIDE { return true; }
3525
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003526 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003527 return true;
3528 }
3529
3530 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003531 return false;
3532 }
3533
3534 uint32_t GetDexPc() const { return dex_pc_; }
3535
3536 bool IsClassFinal() const { return class_is_final_; }
3537
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003538 // Used only in code generation.
3539 bool MustDoNullCheck() const { return must_do_null_check_; }
3540 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3541
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003542 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003543
3544 private:
3545 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003546 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003547 const uint32_t dex_pc_;
3548
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003549 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3550};
3551
Calin Juravleb1498f62015-02-16 13:13:29 +00003552class HBoundType : public HExpression<1> {
3553 public:
3554 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3555 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3556 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003557 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003558 SetRawInputAt(0, input);
3559 }
3560
3561 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3562
3563 bool CanBeNull() const OVERRIDE {
3564 // `null instanceof ClassX` always return false so we can't be null.
3565 return false;
3566 }
3567
3568 DECLARE_INSTRUCTION(BoundType);
3569
3570 private:
3571 // Encodes the most upper class that this instruction can have. In other words
3572 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3573 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3574 const ReferenceTypeInfo bound_type_;
3575
3576 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3577};
3578
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003579class HCheckCast : public HTemplateInstruction<2> {
3580 public:
3581 HCheckCast(HInstruction* object,
3582 HLoadClass* constant,
3583 bool class_is_final,
3584 uint32_t dex_pc)
3585 : HTemplateInstruction(SideEffects::None()),
3586 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003587 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003588 dex_pc_(dex_pc) {
3589 SetRawInputAt(0, object);
3590 SetRawInputAt(1, constant);
3591 }
3592
3593 bool CanBeMoved() const OVERRIDE { return true; }
3594
3595 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3596 return true;
3597 }
3598
3599 bool NeedsEnvironment() const OVERRIDE {
3600 // Instruction may throw a CheckCastError.
3601 return true;
3602 }
3603
3604 bool CanThrow() const OVERRIDE { return true; }
3605
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003606 bool MustDoNullCheck() const { return must_do_null_check_; }
3607 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3608
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003609 uint32_t GetDexPc() const { return dex_pc_; }
3610
3611 bool IsClassFinal() const { return class_is_final_; }
3612
3613 DECLARE_INSTRUCTION(CheckCast);
3614
3615 private:
3616 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003617 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003618 const uint32_t dex_pc_;
3619
3620 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003621};
3622
Calin Juravle27df7582015-04-17 19:12:31 +01003623class HMemoryBarrier : public HTemplateInstruction<0> {
3624 public:
3625 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3626 : HTemplateInstruction(SideEffects::None()),
3627 barrier_kind_(barrier_kind) {}
3628
3629 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3630
3631 DECLARE_INSTRUCTION(MemoryBarrier);
3632
3633 private:
3634 const MemBarrierKind barrier_kind_;
3635
3636 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3637};
3638
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003639class HMonitorOperation : public HTemplateInstruction<1> {
3640 public:
3641 enum OperationKind {
3642 kEnter,
3643 kExit,
3644 };
3645
3646 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3647 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3648 SetRawInputAt(0, object);
3649 }
3650
3651 // Instruction may throw a Java exception, so we need an environment.
3652 bool NeedsEnvironment() const OVERRIDE { return true; }
3653 bool CanThrow() const OVERRIDE { return true; }
3654
3655 uint32_t GetDexPc() const { return dex_pc_; }
3656
3657 bool IsEnter() const { return kind_ == kEnter; }
3658
3659 DECLARE_INSTRUCTION(MonitorOperation);
3660
Calin Juravle52c48962014-12-16 17:02:57 +00003661 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003662 const OperationKind kind_;
3663 const uint32_t dex_pc_;
3664
3665 private:
3666 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3667};
3668
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003669class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003670 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003671 MoveOperands(Location source,
3672 Location destination,
3673 Primitive::Type type,
3674 HInstruction* instruction)
3675 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003676
3677 Location GetSource() const { return source_; }
3678 Location GetDestination() const { return destination_; }
3679
3680 void SetSource(Location value) { source_ = value; }
3681 void SetDestination(Location value) { destination_ = value; }
3682
3683 // The parallel move resolver marks moves as "in-progress" by clearing the
3684 // destination (but not the source).
3685 Location MarkPending() {
3686 DCHECK(!IsPending());
3687 Location dest = destination_;
3688 destination_ = Location::NoLocation();
3689 return dest;
3690 }
3691
3692 void ClearPending(Location dest) {
3693 DCHECK(IsPending());
3694 destination_ = dest;
3695 }
3696
3697 bool IsPending() const {
3698 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3699 return destination_.IsInvalid() && !source_.IsInvalid();
3700 }
3701
3702 // True if this blocks a move from the given location.
3703 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003704 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003705 }
3706
3707 // A move is redundant if it's been eliminated, if its source and
3708 // destination are the same, or if its destination is unneeded.
3709 bool IsRedundant() const {
3710 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3711 }
3712
3713 // We clear both operands to indicate move that's been eliminated.
3714 void Eliminate() {
3715 source_ = destination_ = Location::NoLocation();
3716 }
3717
3718 bool IsEliminated() const {
3719 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3720 return source_.IsInvalid();
3721 }
3722
Nicolas Geoffray90218252015-04-15 11:56:51 +01003723 bool Is64BitMove() const {
3724 return Primitive::Is64BitType(type_);
3725 }
3726
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003727 HInstruction* GetInstruction() const { return instruction_; }
3728
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003729 private:
3730 Location source_;
3731 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003732 // The type this move is for.
3733 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003734 // The instruction this move is assocatied with. Null when this move is
3735 // for moving an input in the expected locations of user (including a phi user).
3736 // This is only used in debug mode, to ensure we do not connect interval siblings
3737 // in the same parallel move.
3738 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003739};
3740
3741static constexpr size_t kDefaultNumberOfMoves = 4;
3742
3743class HParallelMove : public HTemplateInstruction<0> {
3744 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003745 explicit HParallelMove(ArenaAllocator* arena)
3746 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003747
Nicolas Geoffray90218252015-04-15 11:56:51 +01003748 void AddMove(Location source,
3749 Location destination,
3750 Primitive::Type type,
3751 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003752 DCHECK(source.IsValid());
3753 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003754 if (kIsDebugBuild) {
3755 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003756 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003757 if (moves_.Get(i).GetInstruction() == instruction) {
3758 // Special case the situation where the move is for the spill slot
3759 // of the instruction.
3760 if ((GetPrevious() == instruction)
3761 || ((GetPrevious() == nullptr)
3762 && instruction->IsPhi()
3763 && instruction->GetBlock() == GetBlock())) {
3764 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3765 << "Doing parallel moves for the same instruction.";
3766 } else {
3767 DCHECK(false) << "Doing parallel moves for the same instruction.";
3768 }
3769 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003770 }
3771 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003772 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003773 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3774 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003775 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003776 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003777 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003778 }
3779
3780 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003781 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003782 }
3783
3784 size_t NumMoves() const { return moves_.Size(); }
3785
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003786 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003787
3788 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003789 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003790
3791 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3792};
3793
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003794class HGraphVisitor : public ValueObject {
3795 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003796 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3797 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003798
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003799 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003800 virtual void VisitBasicBlock(HBasicBlock* block);
3801
Roland Levillain633021e2014-10-01 14:12:25 +01003802 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003803 void VisitInsertionOrder();
3804
Roland Levillain633021e2014-10-01 14:12:25 +01003805 // Visit the graph following dominator tree reverse post-order.
3806 void VisitReversePostOrder();
3807
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003808 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003809
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003810 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003811#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003812 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3813
3814 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3815
3816#undef DECLARE_VISIT_INSTRUCTION
3817
3818 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003819 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003820
3821 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3822};
3823
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003824class HGraphDelegateVisitor : public HGraphVisitor {
3825 public:
3826 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3827 virtual ~HGraphDelegateVisitor() {}
3828
3829 // Visit functions that delegate to to super class.
3830#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003831 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003832
3833 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3834
3835#undef DECLARE_VISIT_INSTRUCTION
3836
3837 private:
3838 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3839};
3840
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003841class HInsertionOrderIterator : public ValueObject {
3842 public:
3843 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3844
3845 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3846 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3847 void Advance() { ++index_; }
3848
3849 private:
3850 const HGraph& graph_;
3851 size_t index_;
3852
3853 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3854};
3855
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003856class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003857 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003858 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3859 // Check that reverse post order of the graph has been built.
3860 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3861 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003862
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003863 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3864 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003865 void Advance() { ++index_; }
3866
3867 private:
3868 const HGraph& graph_;
3869 size_t index_;
3870
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003871 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003872};
3873
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003874class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003875 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003876 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003877 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3878 // Check that reverse post order of the graph has been built.
3879 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3880 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003881
3882 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003883 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003884 void Advance() { --index_; }
3885
3886 private:
3887 const HGraph& graph_;
3888 size_t index_;
3889
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003890 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003891};
3892
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003893class HLinearPostOrderIterator : public ValueObject {
3894 public:
3895 explicit HLinearPostOrderIterator(const HGraph& graph)
3896 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3897
3898 bool Done() const { return index_ == 0; }
3899
3900 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3901
3902 void Advance() {
3903 --index_;
3904 DCHECK_GE(index_, 0U);
3905 }
3906
3907 private:
3908 const GrowableArray<HBasicBlock*>& order_;
3909 size_t index_;
3910
3911 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3912};
3913
3914class HLinearOrderIterator : public ValueObject {
3915 public:
3916 explicit HLinearOrderIterator(const HGraph& graph)
3917 : order_(graph.GetLinearOrder()), index_(0) {}
3918
3919 bool Done() const { return index_ == order_.Size(); }
3920 HBasicBlock* Current() const { return order_.Get(index_); }
3921 void Advance() { ++index_; }
3922
3923 private:
3924 const GrowableArray<HBasicBlock*>& order_;
3925 size_t index_;
3926
3927 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3928};
3929
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003930// Iterator over the blocks that art part of the loop. Includes blocks part
3931// of an inner loop. The order in which the blocks are iterated is on their
3932// block id.
3933class HBlocksInLoopIterator : public ValueObject {
3934 public:
3935 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3936 : blocks_in_loop_(info.GetBlocks()),
3937 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3938 index_(0) {
3939 if (!blocks_in_loop_.IsBitSet(index_)) {
3940 Advance();
3941 }
3942 }
3943
3944 bool Done() const { return index_ == blocks_.Size(); }
3945 HBasicBlock* Current() const { return blocks_.Get(index_); }
3946 void Advance() {
3947 ++index_;
3948 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3949 if (blocks_in_loop_.IsBitSet(index_)) {
3950 break;
3951 }
3952 }
3953 }
3954
3955 private:
3956 const BitVector& blocks_in_loop_;
3957 const GrowableArray<HBasicBlock*>& blocks_;
3958 size_t index_;
3959
3960 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3961};
3962
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003963inline int64_t Int64FromConstant(HConstant* constant) {
3964 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3965 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3966 : constant->AsLongConstant()->GetValue();
3967}
3968
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003969} // namespace art
3970
3971#endif // ART_COMPILER_OPTIMIZING_NODES_H_