nodes.h revision 360231a056e796c36ffe62348507e904dc9efb9b
1/* 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 20#include "locations.h" 21#include "offsets.h" 22#include "primitive.h" 23#include "utils/allocation.h" 24#include "utils/arena_bit_vector.h" 25#include "utils/growable_array.h" 26 27namespace art { 28 29class HBasicBlock; 30class HEnvironment; 31class HInstruction; 32class HIntConstant; 33class HGraphVisitor; 34class HPhi; 35class HSuspendCheck; 36class LiveInterval; 37class LocationSummary; 38 39static const int kDefaultNumberOfBlocks = 8; 40static const int kDefaultNumberOfSuccessors = 2; 41static const int kDefaultNumberOfPredecessors = 2; 42static const int kDefaultNumberOfDominatedBlocks = 1; 43static const int kDefaultNumberOfBackEdges = 1; 44 45enum IfCondition { 46 kCondEQ, 47 kCondNE, 48 kCondLT, 49 kCondLE, 50 kCondGT, 51 kCondGE, 52}; 53 54class HInstructionList { 55 public: 56 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {} 57 58 void AddInstruction(HInstruction* instruction); 59 void RemoveInstruction(HInstruction* instruction); 60 61 // Return true if this list contains `instruction`. 62 bool Contains(HInstruction* instruction) const; 63 64 // Return true if `instruction1` is found before `instruction2` in 65 // this instruction list and false otherwise. Abort if none 66 // of these instructions is found. 67 bool FoundBefore(const HInstruction* instruction1, 68 const HInstruction* instruction2) const; 69 70 private: 71 HInstruction* first_instruction_; 72 HInstruction* last_instruction_; 73 74 friend class HBasicBlock; 75 friend class HInstructionIterator; 76 friend class HBackwardInstructionIterator; 77 78 DISALLOW_COPY_AND_ASSIGN(HInstructionList); 79}; 80 81// Control-flow graph of a method. Contains a list of basic blocks. 82class HGraph : public ArenaObject { 83 public: 84 explicit HGraph(ArenaAllocator* arena) 85 : arena_(arena), 86 blocks_(arena, kDefaultNumberOfBlocks), 87 reverse_post_order_(arena, kDefaultNumberOfBlocks), 88 maximum_number_of_out_vregs_(0), 89 number_of_vregs_(0), 90 number_of_in_vregs_(0), 91 number_of_temporaries_(0), 92 current_instruction_id_(0) {} 93 94 ArenaAllocator* GetArena() const { return arena_; } 95 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; } 96 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); } 97 98 HBasicBlock* GetEntryBlock() const { return entry_block_; } 99 HBasicBlock* GetExitBlock() const { return exit_block_; } 100 101 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; } 102 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; } 103 104 void AddBlock(HBasicBlock* block); 105 106 void BuildDominatorTree(); 107 void TransformToSSA(); 108 void SimplifyCFG(); 109 110 // Find all natural loops in this graph. Aborts computation and returns false 111 // if one loop is not natural, that is the header does not dominate the back 112 // edge. 113 bool FindNaturalLoops() const; 114 115 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor); 116 void SimplifyLoop(HBasicBlock* header); 117 118 int GetNextInstructionId() { 119 return current_instruction_id_++; 120 } 121 122 uint16_t GetMaximumNumberOfOutVRegs() const { 123 return maximum_number_of_out_vregs_; 124 } 125 126 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) { 127 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_); 128 } 129 130 void UpdateNumberOfTemporaries(size_t count) { 131 number_of_temporaries_ = std::max(count, number_of_temporaries_); 132 } 133 134 size_t GetNumberOfTemporaries() const { 135 return number_of_temporaries_; 136 } 137 138 void SetNumberOfVRegs(uint16_t number_of_vregs) { 139 number_of_vregs_ = number_of_vregs; 140 } 141 142 uint16_t GetNumberOfVRegs() const { 143 return number_of_vregs_; 144 } 145 146 void SetNumberOfInVRegs(uint16_t value) { 147 number_of_in_vregs_ = value; 148 } 149 150 uint16_t GetNumberOfInVRegs() const { 151 return number_of_in_vregs_; 152 } 153 154 uint16_t GetNumberOfLocalVRegs() const { 155 return number_of_vregs_ - number_of_in_vregs_; 156 } 157 158 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const { 159 return reverse_post_order_; 160 } 161 162 private: 163 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const; 164 void VisitBlockForDominatorTree(HBasicBlock* block, 165 HBasicBlock* predecessor, 166 GrowableArray<size_t>* visits); 167 void FindBackEdges(ArenaBitVector* visited); 168 void VisitBlockForBackEdges(HBasicBlock* block, 169 ArenaBitVector* visited, 170 ArenaBitVector* visiting); 171 void RemoveDeadBlocks(const ArenaBitVector& visited) const; 172 173 ArenaAllocator* const arena_; 174 175 // List of blocks in insertion order. 176 GrowableArray<HBasicBlock*> blocks_; 177 178 // List of blocks to perform a reverse post order tree traversal. 179 GrowableArray<HBasicBlock*> reverse_post_order_; 180 181 HBasicBlock* entry_block_; 182 HBasicBlock* exit_block_; 183 184 // The maximum number of virtual registers arguments passed to a HInvoke in this graph. 185 uint16_t maximum_number_of_out_vregs_; 186 187 // The number of virtual registers in this method. Contains the parameters. 188 uint16_t number_of_vregs_; 189 190 // The number of virtual registers used by parameters of this method. 191 uint16_t number_of_in_vregs_; 192 193 // The number of temporaries that will be needed for the baseline compiler. 194 size_t number_of_temporaries_; 195 196 // The current id to assign to a newly added instruction. See HInstruction.id_. 197 int current_instruction_id_; 198 199 DISALLOW_COPY_AND_ASSIGN(HGraph); 200}; 201 202class HLoopInformation : public ArenaObject { 203 public: 204 HLoopInformation(HBasicBlock* header, HGraph* graph) 205 : header_(header), 206 suspend_check_(nullptr), 207 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges), 208 // Make bit vector growable, as the number of blocks may change. 209 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {} 210 211 HBasicBlock* GetHeader() const { 212 return header_; 213 } 214 215 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; } 216 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; } 217 bool HasSuspendCheck() const { return suspend_check_ != nullptr; } 218 219 void AddBackEdge(HBasicBlock* back_edge) { 220 back_edges_.Add(back_edge); 221 } 222 223 void RemoveBackEdge(HBasicBlock* back_edge) { 224 back_edges_.Delete(back_edge); 225 } 226 227 bool IsBackEdge(HBasicBlock* block) { 228 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) { 229 if (back_edges_.Get(i) == block) return true; 230 } 231 return false; 232 } 233 234 int NumberOfBackEdges() const { 235 return back_edges_.Size(); 236 } 237 238 HBasicBlock* GetPreHeader() const; 239 240 const GrowableArray<HBasicBlock*>& GetBackEdges() const { 241 return back_edges_; 242 } 243 244 void ClearBackEdges() { 245 back_edges_.Reset(); 246 } 247 248 // Find blocks that are part of this loop. Returns whether the loop is a natural loop, 249 // that is the header dominates the back edge. 250 bool Populate(); 251 252 // Returns whether this loop information contains `block`. 253 // Note that this loop information *must* be populated before entering this function. 254 bool Contains(const HBasicBlock& block) const; 255 256 // Returns whether this loop information is an inner loop of `other`. 257 // Note that `other` *must* be populated before entering this function. 258 bool IsIn(const HLoopInformation& other) const; 259 260 const ArenaBitVector& GetBlocks() const { return blocks_; } 261 262 private: 263 // Internal recursive implementation of `Populate`. 264 void PopulateRecursive(HBasicBlock* block); 265 266 HBasicBlock* header_; 267 HSuspendCheck* suspend_check_; 268 GrowableArray<HBasicBlock*> back_edges_; 269 ArenaBitVector blocks_; 270 271 DISALLOW_COPY_AND_ASSIGN(HLoopInformation); 272}; 273 274static constexpr size_t kNoLifetime = -1; 275static constexpr uint32_t kNoDexPc = -1; 276 277// A block in a method. Contains the list of instructions represented 278// as a double linked list. Each block knows its predecessors and 279// successors. 280 281class HBasicBlock : public ArenaObject { 282 public: 283 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc) 284 : graph_(graph), 285 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors), 286 successors_(graph->GetArena(), kDefaultNumberOfSuccessors), 287 loop_information_(nullptr), 288 dominator_(nullptr), 289 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks), 290 block_id_(-1), 291 dex_pc_(dex_pc), 292 lifetime_start_(kNoLifetime), 293 lifetime_end_(kNoLifetime) {} 294 295 const GrowableArray<HBasicBlock*>& GetPredecessors() const { 296 return predecessors_; 297 } 298 299 const GrowableArray<HBasicBlock*>& GetSuccessors() const { 300 return successors_; 301 } 302 303 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const { 304 return dominated_blocks_; 305 } 306 307 bool IsEntryBlock() const { 308 return graph_->GetEntryBlock() == this; 309 } 310 311 bool IsExitBlock() const { 312 return graph_->GetExitBlock() == this; 313 } 314 315 void AddBackEdge(HBasicBlock* back_edge) { 316 if (loop_information_ == nullptr) { 317 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_); 318 } 319 DCHECK_EQ(loop_information_->GetHeader(), this); 320 loop_information_->AddBackEdge(back_edge); 321 } 322 323 HGraph* GetGraph() const { return graph_; } 324 325 int GetBlockId() const { return block_id_; } 326 void SetBlockId(int id) { block_id_ = id; } 327 328 HBasicBlock* GetDominator() const { return dominator_; } 329 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; } 330 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); } 331 332 int NumberOfBackEdges() const { 333 return loop_information_ == nullptr 334 ? 0 335 : loop_information_->NumberOfBackEdges(); 336 } 337 338 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; } 339 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; } 340 const HInstructionList& GetInstructions() const { return instructions_; } 341 const HInstructionList& GetPhis() const { return phis_; } 342 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; } 343 344 void AddSuccessor(HBasicBlock* block) { 345 successors_.Add(block); 346 block->predecessors_.Add(this); 347 } 348 349 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) { 350 size_t successor_index = GetSuccessorIndexOf(existing); 351 DCHECK_NE(successor_index, static_cast<size_t>(-1)); 352 existing->RemovePredecessor(this); 353 new_block->predecessors_.Add(this); 354 successors_.Put(successor_index, new_block); 355 } 356 357 void RemovePredecessor(HBasicBlock* block) { 358 predecessors_.Delete(block); 359 } 360 361 void ClearAllPredecessors() { 362 predecessors_.Reset(); 363 } 364 365 void AddPredecessor(HBasicBlock* block) { 366 predecessors_.Add(block); 367 block->successors_.Add(this); 368 } 369 370 void SwapPredecessors() { 371 DCHECK_EQ(predecessors_.Size(), 2u); 372 HBasicBlock* temp = predecessors_.Get(0); 373 predecessors_.Put(0, predecessors_.Get(1)); 374 predecessors_.Put(1, temp); 375 } 376 377 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) { 378 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) { 379 if (predecessors_.Get(i) == predecessor) { 380 return i; 381 } 382 } 383 return -1; 384 } 385 386 size_t GetSuccessorIndexOf(HBasicBlock* successor) { 387 for (size_t i = 0, e = successors_.Size(); i < e; ++i) { 388 if (successors_.Get(i) == successor) { 389 return i; 390 } 391 } 392 return -1; 393 } 394 395 void AddInstruction(HInstruction* instruction); 396 void RemoveInstruction(HInstruction* instruction); 397 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor); 398 // Replace instruction `initial` with `replacement` within this block. 399 void ReplaceAndRemoveInstructionWith(HInstruction* initial, 400 HInstruction* replacement); 401 void AddPhi(HPhi* phi); 402 void RemovePhi(HPhi* phi); 403 404 bool IsLoopHeader() const { 405 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this); 406 } 407 408 bool IsLoopPreHeaderFirstPredecessor() const { 409 DCHECK(IsLoopHeader()); 410 DCHECK(!GetPredecessors().IsEmpty()); 411 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader(); 412 } 413 414 HLoopInformation* GetLoopInformation() const { 415 return loop_information_; 416 } 417 418 // Set the loop_information_ on this block. This method overrides the current 419 // loop_information if it is an outer loop of the passed loop information. 420 void SetInLoop(HLoopInformation* info) { 421 if (IsLoopHeader()) { 422 // Nothing to do. This just means `info` is an outer loop. 423 } else if (loop_information_ == nullptr) { 424 loop_information_ = info; 425 } else if (loop_information_->Contains(*info->GetHeader())) { 426 // Block is currently part of an outer loop. Make it part of this inner loop. 427 // Note that a non loop header having a loop information means this loop information 428 // has already been populated 429 loop_information_ = info; 430 } else { 431 // Block is part of an inner loop. Do not update the loop information. 432 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()` 433 // at this point, because this method is being called while populating `info`. 434 } 435 } 436 437 bool IsInLoop() const { return loop_information_ != nullptr; } 438 439 // Returns wheter this block dominates the blocked passed as parameter. 440 bool Dominates(HBasicBlock* block) const; 441 442 size_t GetLifetimeStart() const { return lifetime_start_; } 443 size_t GetLifetimeEnd() const { return lifetime_end_; } 444 445 void SetLifetimeStart(size_t start) { lifetime_start_ = start; } 446 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; } 447 448 uint32_t GetDexPc() const { return dex_pc_; } 449 450 private: 451 HGraph* const graph_; 452 GrowableArray<HBasicBlock*> predecessors_; 453 GrowableArray<HBasicBlock*> successors_; 454 HInstructionList instructions_; 455 HInstructionList phis_; 456 HLoopInformation* loop_information_; 457 HBasicBlock* dominator_; 458 GrowableArray<HBasicBlock*> dominated_blocks_; 459 int block_id_; 460 // The dex program counter of the first instruction of this block. 461 const uint32_t dex_pc_; 462 size_t lifetime_start_; 463 size_t lifetime_end_; 464 465 DISALLOW_COPY_AND_ASSIGN(HBasicBlock); 466}; 467 468#define FOR_EACH_CONCRETE_INSTRUCTION(M) \ 469 M(Add, BinaryOperation) \ 470 M(Condition, BinaryOperation) \ 471 M(Equal, Condition) \ 472 M(NotEqual, Condition) \ 473 M(LessThan, Condition) \ 474 M(LessThanOrEqual, Condition) \ 475 M(GreaterThan, Condition) \ 476 M(GreaterThanOrEqual, Condition) \ 477 M(Exit, Instruction) \ 478 M(Goto, Instruction) \ 479 M(If, Instruction) \ 480 M(IntConstant, Constant) \ 481 M(InvokeStatic, Invoke) \ 482 M(InvokeVirtual, Invoke) \ 483 M(LoadLocal, Instruction) \ 484 M(Local, Instruction) \ 485 M(LongConstant, Constant) \ 486 M(NewInstance, Instruction) \ 487 M(Not, Instruction) \ 488 M(ParameterValue, Instruction) \ 489 M(ParallelMove, Instruction) \ 490 M(Phi, Instruction) \ 491 M(Return, Instruction) \ 492 M(ReturnVoid, Instruction) \ 493 M(StoreLocal, Instruction) \ 494 M(Sub, BinaryOperation) \ 495 M(Compare, BinaryOperation) \ 496 M(InstanceFieldGet, Instruction) \ 497 M(InstanceFieldSet, Instruction) \ 498 M(ArrayGet, Instruction) \ 499 M(ArraySet, Instruction) \ 500 M(ArrayLength, Instruction) \ 501 M(BoundsCheck, Instruction) \ 502 M(NullCheck, Instruction) \ 503 M(Temporary, Instruction) \ 504 M(SuspendCheck, Instruction) \ 505 506#define FOR_EACH_INSTRUCTION(M) \ 507 FOR_EACH_CONCRETE_INSTRUCTION(M) \ 508 M(Constant, Instruction) \ 509 M(BinaryOperation, Instruction) \ 510 M(Invoke, Instruction) 511 512#define FORWARD_DECLARATION(type, super) class H##type; 513FOR_EACH_INSTRUCTION(FORWARD_DECLARATION) 514#undef FORWARD_DECLARATION 515 516#define DECLARE_INSTRUCTION(type) \ 517 virtual InstructionKind GetKind() const { return k##type; } \ 518 virtual const char* DebugName() const { return #type; } \ 519 virtual const H##type* As##type() const OVERRIDE { return this; } \ 520 virtual H##type* As##type() OVERRIDE { return this; } \ 521 virtual bool InstructionTypeEquals(HInstruction* other) const { \ 522 return other->Is##type(); \ 523 } \ 524 virtual void Accept(HGraphVisitor* visitor) 525 526template <typename T> 527class HUseListNode : public ArenaObject { 528 public: 529 HUseListNode(T* user, size_t index, HUseListNode* tail) 530 : user_(user), index_(index), tail_(tail) {} 531 532 HUseListNode* GetTail() const { return tail_; } 533 T* GetUser() const { return user_; } 534 size_t GetIndex() const { return index_; } 535 536 void SetTail(HUseListNode<T>* node) { tail_ = node; } 537 538 private: 539 T* const user_; 540 const size_t index_; 541 HUseListNode<T>* tail_; 542 543 DISALLOW_COPY_AND_ASSIGN(HUseListNode); 544}; 545 546// Represents the side effects an instruction may have. 547class SideEffects : public ValueObject { 548 public: 549 SideEffects() : flags_(0) {} 550 551 static SideEffects None() { 552 return SideEffects(0); 553 } 554 555 static SideEffects All() { 556 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_); 557 } 558 559 static SideEffects ChangesSomething() { 560 return SideEffects((1 << kFlagChangesCount) - 1); 561 } 562 563 static SideEffects DependsOnSomething() { 564 int count = kFlagDependsOnCount - kFlagChangesCount; 565 return SideEffects(((1 << count) - 1) << kFlagChangesCount); 566 } 567 568 SideEffects Union(SideEffects other) const { 569 return SideEffects(flags_ | other.flags_); 570 } 571 572 bool HasSideEffects() const { 573 size_t all_bits_set = (1 << kFlagChangesCount) - 1; 574 return (flags_ & all_bits_set) != 0; 575 } 576 577 bool HasAllSideEffects() const { 578 size_t all_bits_set = (1 << kFlagChangesCount) - 1; 579 return all_bits_set == (flags_ & all_bits_set); 580 } 581 582 bool DependsOn(SideEffects other) const { 583 size_t depends_flags = other.ComputeDependsFlags(); 584 return (flags_ & depends_flags) != 0; 585 } 586 587 bool HasDependencies() const { 588 int count = kFlagDependsOnCount - kFlagChangesCount; 589 size_t all_bits_set = (1 << count) - 1; 590 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0; 591 } 592 593 private: 594 static constexpr int kFlagChangesSomething = 0; 595 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1; 596 597 static constexpr int kFlagDependsOnSomething = kFlagChangesCount; 598 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1; 599 600 explicit SideEffects(size_t flags) : flags_(flags) {} 601 602 size_t ComputeDependsFlags() const { 603 return flags_ << kFlagChangesCount; 604 } 605 606 size_t flags_; 607}; 608 609class HInstruction : public ArenaObject { 610 public: 611 explicit HInstruction(SideEffects side_effects) 612 : previous_(nullptr), 613 next_(nullptr), 614 block_(nullptr), 615 id_(-1), 616 ssa_index_(-1), 617 uses_(nullptr), 618 env_uses_(nullptr), 619 environment_(nullptr), 620 locations_(nullptr), 621 live_interval_(nullptr), 622 lifetime_position_(kNoLifetime), 623 side_effects_(side_effects) {} 624 625 virtual ~HInstruction() {} 626 627#define DECLARE_KIND(type, super) k##type, 628 enum InstructionKind { 629 FOR_EACH_INSTRUCTION(DECLARE_KIND) 630 }; 631#undef DECLARE_KIND 632 633 HInstruction* GetNext() const { return next_; } 634 HInstruction* GetPrevious() const { return previous_; } 635 636 HBasicBlock* GetBlock() const { return block_; } 637 void SetBlock(HBasicBlock* block) { block_ = block; } 638 bool IsInBlock() const { return block_ != nullptr; } 639 bool IsInLoop() const { return block_->IsInLoop(); } 640 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); } 641 642 virtual size_t InputCount() const = 0; 643 virtual HInstruction* InputAt(size_t i) const = 0; 644 645 virtual void Accept(HGraphVisitor* visitor) = 0; 646 virtual const char* DebugName() const = 0; 647 648 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; } 649 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0; 650 651 virtual bool NeedsEnvironment() const { return false; } 652 virtual bool IsControlFlow() const { return false; } 653 bool HasSideEffects() const { return side_effects_.HasSideEffects(); } 654 655 void AddUseAt(HInstruction* user, size_t index) { 656 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_); 657 } 658 659 void AddEnvUseAt(HEnvironment* user, size_t index) { 660 DCHECK(user != nullptr); 661 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>( 662 user, index, env_uses_); 663 } 664 665 void RemoveUser(HInstruction* user, size_t index); 666 void RemoveEnvironmentUser(HEnvironment* user, size_t index); 667 668 HUseListNode<HInstruction>* GetUses() const { return uses_; } 669 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; } 670 671 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; } 672 bool HasEnvironmentUses() const { return env_uses_ != nullptr; } 673 674 size_t NumberOfUses() const { 675 // TODO: Optimize this method if it is used outside of the HGraphVisualizer. 676 size_t result = 0; 677 HUseListNode<HInstruction>* current = uses_; 678 while (current != nullptr) { 679 current = current->GetTail(); 680 ++result; 681 } 682 return result; 683 } 684 685 // Does this instruction dominate `other_instruction`? Aborts if 686 // this instruction and `other_instruction` are both phis. 687 bool Dominates(HInstruction* other_instruction) const; 688 689 int GetId() const { return id_; } 690 void SetId(int id) { id_ = id; } 691 692 int GetSsaIndex() const { return ssa_index_; } 693 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; } 694 bool HasSsaIndex() const { return ssa_index_ != -1; } 695 696 bool HasEnvironment() const { return environment_ != nullptr; } 697 HEnvironment* GetEnvironment() const { return environment_; } 698 void SetEnvironment(HEnvironment* environment) { environment_ = environment; } 699 700 // Returns the number of entries in the environment. Typically, that is the 701 // number of dex registers in a method. It could be more in case of inlining. 702 size_t EnvironmentSize() const; 703 704 LocationSummary* GetLocations() const { return locations_; } 705 void SetLocations(LocationSummary* locations) { locations_ = locations; } 706 707 void ReplaceWith(HInstruction* instruction); 708 709 bool HasOnlyOneUse() const { 710 return uses_ != nullptr && uses_->GetTail() == nullptr; 711 } 712 713#define INSTRUCTION_TYPE_CHECK(type, super) \ 714 bool Is##type() const { return (As##type() != nullptr); } \ 715 virtual const H##type* As##type() const { return nullptr; } \ 716 virtual H##type* As##type() { return nullptr; } 717 718 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK) 719#undef INSTRUCTION_TYPE_CHECK 720 721 // Returns whether the instruction can be moved within the graph. 722 virtual bool CanBeMoved() const { return false; } 723 724 // Returns whether the two instructions are of the same kind. 725 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; } 726 727 // Returns whether any data encoded in the two instructions is equal. 728 // This method does not look at the inputs. Both instructions must be 729 // of the same type, otherwise the method has undefined behavior. 730 virtual bool InstructionDataEquals(HInstruction* other) const { return false; } 731 732 // Returns whether two instructions are equal, that is: 733 // 1) They have the same type and contain the same data, 734 // 2) Their inputs are identical. 735 bool Equals(HInstruction* other) const; 736 737 virtual InstructionKind GetKind() const = 0; 738 739 virtual size_t ComputeHashCode() const { 740 size_t result = GetKind(); 741 for (size_t i = 0, e = InputCount(); i < e; ++i) { 742 result = (result * 31) + InputAt(i)->GetId(); 743 } 744 return result; 745 } 746 747 SideEffects GetSideEffects() const { return side_effects_; } 748 749 size_t GetLifetimePosition() const { return lifetime_position_; } 750 void SetLifetimePosition(size_t position) { lifetime_position_ = position; } 751 LiveInterval* GetLiveInterval() const { return live_interval_; } 752 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; } 753 bool HasLiveInterval() const { return live_interval_ != nullptr; } 754 755 private: 756 HInstruction* previous_; 757 HInstruction* next_; 758 HBasicBlock* block_; 759 760 // An instruction gets an id when it is added to the graph. 761 // It reflects creation order. A negative id means the instruction 762 // has not been added to the graph. 763 int id_; 764 765 // When doing liveness analysis, instructions that have uses get an SSA index. 766 int ssa_index_; 767 768 // List of instructions that have this instruction as input. 769 HUseListNode<HInstruction>* uses_; 770 771 // List of environments that contain this instruction. 772 HUseListNode<HEnvironment>* env_uses_; 773 774 // The environment associated with this instruction. Not null if the instruction 775 // might jump out of the method. 776 HEnvironment* environment_; 777 778 // Set by the code generator. 779 LocationSummary* locations_; 780 781 // Set by the liveness analysis. 782 LiveInterval* live_interval_; 783 784 // Set by the liveness analysis, this is the position in a linear 785 // order of blocks where this instruction's live interval start. 786 size_t lifetime_position_; 787 788 const SideEffects side_effects_; 789 790 friend class HBasicBlock; 791 friend class HInstructionList; 792 793 DISALLOW_COPY_AND_ASSIGN(HInstruction); 794}; 795 796template<typename T> 797class HUseIterator : public ValueObject { 798 public: 799 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {} 800 801 bool Done() const { return current_ == nullptr; } 802 803 void Advance() { 804 DCHECK(!Done()); 805 current_ = current_->GetTail(); 806 } 807 808 HUseListNode<T>* Current() const { 809 DCHECK(!Done()); 810 return current_; 811 } 812 813 private: 814 HUseListNode<T>* current_; 815 816 friend class HValue; 817}; 818 819// A HEnvironment object contains the values of virtual registers at a given location. 820class HEnvironment : public ArenaObject { 821 public: 822 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) { 823 vregs_.SetSize(number_of_vregs); 824 for (size_t i = 0; i < number_of_vregs; i++) { 825 vregs_.Put(i, nullptr); 826 } 827 } 828 829 void Populate(const GrowableArray<HInstruction*>& env) { 830 for (size_t i = 0; i < env.Size(); i++) { 831 HInstruction* instruction = env.Get(i); 832 vregs_.Put(i, instruction); 833 if (instruction != nullptr) { 834 instruction->AddEnvUseAt(this, i); 835 } 836 } 837 } 838 839 void SetRawEnvAt(size_t index, HInstruction* instruction) { 840 vregs_.Put(index, instruction); 841 } 842 843 HInstruction* GetInstructionAt(size_t index) const { 844 return vregs_.Get(index); 845 } 846 847 GrowableArray<HInstruction*>* GetVRegs() { 848 return &vregs_; 849 } 850 851 size_t Size() const { return vregs_.Size(); } 852 853 private: 854 GrowableArray<HInstruction*> vregs_; 855 856 DISALLOW_COPY_AND_ASSIGN(HEnvironment); 857}; 858 859class HInputIterator : public ValueObject { 860 public: 861 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {} 862 863 bool Done() const { return index_ == instruction_->InputCount(); } 864 HInstruction* Current() const { return instruction_->InputAt(index_); } 865 void Advance() { index_++; } 866 867 private: 868 HInstruction* instruction_; 869 size_t index_; 870 871 DISALLOW_COPY_AND_ASSIGN(HInputIterator); 872}; 873 874class HInstructionIterator : public ValueObject { 875 public: 876 explicit HInstructionIterator(const HInstructionList& instructions) 877 : instruction_(instructions.first_instruction_) { 878 next_ = Done() ? nullptr : instruction_->GetNext(); 879 } 880 881 bool Done() const { return instruction_ == nullptr; } 882 HInstruction* Current() const { return instruction_; } 883 void Advance() { 884 instruction_ = next_; 885 next_ = Done() ? nullptr : instruction_->GetNext(); 886 } 887 888 private: 889 HInstruction* instruction_; 890 HInstruction* next_; 891 892 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator); 893}; 894 895class HBackwardInstructionIterator : public ValueObject { 896 public: 897 explicit HBackwardInstructionIterator(const HInstructionList& instructions) 898 : instruction_(instructions.last_instruction_) { 899 next_ = Done() ? nullptr : instruction_->GetPrevious(); 900 } 901 902 bool Done() const { return instruction_ == nullptr; } 903 HInstruction* Current() const { return instruction_; } 904 void Advance() { 905 instruction_ = next_; 906 next_ = Done() ? nullptr : instruction_->GetPrevious(); 907 } 908 909 private: 910 HInstruction* instruction_; 911 HInstruction* next_; 912 913 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator); 914}; 915 916// An embedded container with N elements of type T. Used (with partial 917// specialization for N=0) because embedded arrays cannot have size 0. 918template<typename T, intptr_t N> 919class EmbeddedArray { 920 public: 921 EmbeddedArray() : elements_() {} 922 923 intptr_t GetLength() const { return N; } 924 925 const T& operator[](intptr_t i) const { 926 DCHECK_LT(i, GetLength()); 927 return elements_[i]; 928 } 929 930 T& operator[](intptr_t i) { 931 DCHECK_LT(i, GetLength()); 932 return elements_[i]; 933 } 934 935 const T& At(intptr_t i) const { 936 return (*this)[i]; 937 } 938 939 void SetAt(intptr_t i, const T& val) { 940 (*this)[i] = val; 941 } 942 943 private: 944 T elements_[N]; 945}; 946 947template<typename T> 948class EmbeddedArray<T, 0> { 949 public: 950 intptr_t length() const { return 0; } 951 const T& operator[](intptr_t i) const { 952 LOG(FATAL) << "Unreachable"; 953 static T sentinel = 0; 954 return sentinel; 955 } 956 T& operator[](intptr_t i) { 957 LOG(FATAL) << "Unreachable"; 958 static T sentinel = 0; 959 return sentinel; 960 } 961}; 962 963template<intptr_t N> 964class HTemplateInstruction: public HInstruction { 965 public: 966 HTemplateInstruction<N>(SideEffects side_effects) 967 : HInstruction(side_effects), inputs_() {} 968 virtual ~HTemplateInstruction() {} 969 970 virtual size_t InputCount() const { return N; } 971 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; } 972 973 protected: 974 virtual void SetRawInputAt(size_t i, HInstruction* instruction) { 975 inputs_[i] = instruction; 976 } 977 978 private: 979 EmbeddedArray<HInstruction*, N> inputs_; 980 981 friend class SsaBuilder; 982}; 983 984template<intptr_t N> 985class HExpression : public HTemplateInstruction<N> { 986 public: 987 HExpression<N>(Primitive::Type type, SideEffects side_effects) 988 : HTemplateInstruction<N>(side_effects), type_(type) {} 989 virtual ~HExpression() {} 990 991 virtual Primitive::Type GetType() const { return type_; } 992 993 private: 994 const Primitive::Type type_; 995}; 996 997// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow 998// instruction that branches to the exit block. 999class HReturnVoid : public HTemplateInstruction<0> { 1000 public: 1001 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {} 1002 1003 virtual bool IsControlFlow() const { return true; } 1004 1005 DECLARE_INSTRUCTION(ReturnVoid); 1006 1007 private: 1008 DISALLOW_COPY_AND_ASSIGN(HReturnVoid); 1009}; 1010 1011// Represents dex's RETURN opcodes. A HReturn is a control flow 1012// instruction that branches to the exit block. 1013class HReturn : public HTemplateInstruction<1> { 1014 public: 1015 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) { 1016 SetRawInputAt(0, value); 1017 } 1018 1019 virtual bool IsControlFlow() const { return true; } 1020 1021 DECLARE_INSTRUCTION(Return); 1022 1023 private: 1024 DISALLOW_COPY_AND_ASSIGN(HReturn); 1025}; 1026 1027// The exit instruction is the only instruction of the exit block. 1028// Instructions aborting the method (HTrow and HReturn) must branch to the 1029// exit block. 1030class HExit : public HTemplateInstruction<0> { 1031 public: 1032 HExit() : HTemplateInstruction(SideEffects::None()) {} 1033 1034 virtual bool IsControlFlow() const { return true; } 1035 1036 DECLARE_INSTRUCTION(Exit); 1037 1038 private: 1039 DISALLOW_COPY_AND_ASSIGN(HExit); 1040}; 1041 1042// Jumps from one block to another. 1043class HGoto : public HTemplateInstruction<0> { 1044 public: 1045 HGoto() : HTemplateInstruction(SideEffects::None()) {} 1046 1047 virtual bool IsControlFlow() const { return true; } 1048 1049 HBasicBlock* GetSuccessor() const { 1050 return GetBlock()->GetSuccessors().Get(0); 1051 } 1052 1053 DECLARE_INSTRUCTION(Goto); 1054 1055 private: 1056 DISALLOW_COPY_AND_ASSIGN(HGoto); 1057}; 1058 1059 1060// Conditional branch. A block ending with an HIf instruction must have 1061// two successors. 1062class HIf : public HTemplateInstruction<1> { 1063 public: 1064 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) { 1065 SetRawInputAt(0, input); 1066 } 1067 1068 virtual bool IsControlFlow() const { return true; } 1069 1070 HBasicBlock* IfTrueSuccessor() const { 1071 return GetBlock()->GetSuccessors().Get(0); 1072 } 1073 1074 HBasicBlock* IfFalseSuccessor() const { 1075 return GetBlock()->GetSuccessors().Get(1); 1076 } 1077 1078 DECLARE_INSTRUCTION(If); 1079 1080 virtual bool IsIfInstruction() const { return true; } 1081 1082 private: 1083 DISALLOW_COPY_AND_ASSIGN(HIf); 1084}; 1085 1086class HBinaryOperation : public HExpression<2> { 1087 public: 1088 HBinaryOperation(Primitive::Type result_type, 1089 HInstruction* left, 1090 HInstruction* right) : HExpression(result_type, SideEffects::None()) { 1091 SetRawInputAt(0, left); 1092 SetRawInputAt(1, right); 1093 } 1094 1095 HInstruction* GetLeft() const { return InputAt(0); } 1096 HInstruction* GetRight() const { return InputAt(1); } 1097 Primitive::Type GetResultType() const { return GetType(); } 1098 1099 virtual bool IsCommutative() { return false; } 1100 1101 virtual bool CanBeMoved() const { return true; } 1102 virtual bool InstructionDataEquals(HInstruction* other) const { return true; } 1103 1104 // Try to statically evaluate `operation` and return an HConstant 1105 // containing the result of this evaluation. If `operation` cannot 1106 // be evaluated as a constant, return nullptr. 1107 HConstant* TryStaticEvaluation(ArenaAllocator* allocator) const; 1108 1109 // Apply this operation to `x` and `y`. 1110 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0; 1111 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0; 1112 1113 DECLARE_INSTRUCTION(BinaryOperation); 1114 1115 private: 1116 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation); 1117}; 1118 1119class HCondition : public HBinaryOperation { 1120 public: 1121 HCondition(HInstruction* first, HInstruction* second) 1122 : HBinaryOperation(Primitive::kPrimBoolean, first, second), 1123 needs_materialization_(true) {} 1124 1125 virtual bool IsCommutative() { return true; } 1126 1127 bool NeedsMaterialization() const { return needs_materialization_; } 1128 void ClearNeedsMaterialization() { needs_materialization_ = false; } 1129 1130 // For code generation purposes, returns whether this instruction is just before 1131 // `if_`, and disregard moves in between. 1132 bool IsBeforeWhenDisregardMoves(HIf* if_) const; 1133 1134 DECLARE_INSTRUCTION(Condition); 1135 1136 virtual IfCondition GetCondition() const = 0; 1137 1138 private: 1139 // For register allocation purposes, returns whether this instruction needs to be 1140 // materialized (that is, not just be in the processor flags). 1141 bool needs_materialization_; 1142 1143 DISALLOW_COPY_AND_ASSIGN(HCondition); 1144}; 1145 1146// Instruction to check if two inputs are equal to each other. 1147class HEqual : public HCondition { 1148 public: 1149 HEqual(HInstruction* first, HInstruction* second) 1150 : HCondition(first, second) {} 1151 1152 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { 1153 return x == y ? 1 : 0; 1154 } 1155 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { 1156 return x == y ? 1 : 0; 1157 } 1158 1159 DECLARE_INSTRUCTION(Equal); 1160 1161 virtual IfCondition GetCondition() const { 1162 return kCondEQ; 1163 } 1164 1165 private: 1166 DISALLOW_COPY_AND_ASSIGN(HEqual); 1167}; 1168 1169class HNotEqual : public HCondition { 1170 public: 1171 HNotEqual(HInstruction* first, HInstruction* second) 1172 : HCondition(first, second) {} 1173 1174 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { 1175 return x != y ? 1 : 0; 1176 } 1177 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { 1178 return x != y ? 1 : 0; 1179 } 1180 1181 DECLARE_INSTRUCTION(NotEqual); 1182 1183 virtual IfCondition GetCondition() const { 1184 return kCondNE; 1185 } 1186 1187 private: 1188 DISALLOW_COPY_AND_ASSIGN(HNotEqual); 1189}; 1190 1191class HLessThan : public HCondition { 1192 public: 1193 HLessThan(HInstruction* first, HInstruction* second) 1194 : HCondition(first, second) {} 1195 1196 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { 1197 return x < y ? 1 : 0; 1198 } 1199 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { 1200 return x < y ? 1 : 0; 1201 } 1202 1203 DECLARE_INSTRUCTION(LessThan); 1204 1205 virtual IfCondition GetCondition() const { 1206 return kCondLT; 1207 } 1208 1209 private: 1210 DISALLOW_COPY_AND_ASSIGN(HLessThan); 1211}; 1212 1213class HLessThanOrEqual : public HCondition { 1214 public: 1215 HLessThanOrEqual(HInstruction* first, HInstruction* second) 1216 : HCondition(first, second) {} 1217 1218 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { 1219 return x <= y ? 1 : 0; 1220 } 1221 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { 1222 return x <= y ? 1 : 0; 1223 } 1224 1225 DECLARE_INSTRUCTION(LessThanOrEqual); 1226 1227 virtual IfCondition GetCondition() const { 1228 return kCondLE; 1229 } 1230 1231 private: 1232 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual); 1233}; 1234 1235class HGreaterThan : public HCondition { 1236 public: 1237 HGreaterThan(HInstruction* first, HInstruction* second) 1238 : HCondition(first, second) {} 1239 1240 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { 1241 return x > y ? 1 : 0; 1242 } 1243 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { 1244 return x > y ? 1 : 0; 1245 } 1246 1247 DECLARE_INSTRUCTION(GreaterThan); 1248 1249 virtual IfCondition GetCondition() const { 1250 return kCondGT; 1251 } 1252 1253 private: 1254 DISALLOW_COPY_AND_ASSIGN(HGreaterThan); 1255}; 1256 1257class HGreaterThanOrEqual : public HCondition { 1258 public: 1259 HGreaterThanOrEqual(HInstruction* first, HInstruction* second) 1260 : HCondition(first, second) {} 1261 1262 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { 1263 return x >= y ? 1 : 0; 1264 } 1265 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { 1266 return x >= y ? 1 : 0; 1267 } 1268 1269 DECLARE_INSTRUCTION(GreaterThanOrEqual); 1270 1271 virtual IfCondition GetCondition() const { 1272 return kCondGE; 1273 } 1274 1275 private: 1276 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual); 1277}; 1278 1279 1280// Instruction to check how two inputs compare to each other. 1281// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1. 1282class HCompare : public HBinaryOperation { 1283 public: 1284 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second) 1285 : HBinaryOperation(Primitive::kPrimInt, first, second) { 1286 DCHECK_EQ(type, first->GetType()); 1287 DCHECK_EQ(type, second->GetType()); 1288 } 1289 1290 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { 1291 return 1292 x == y ? 0 : 1293 x > y ? 1 : 1294 -1; 1295 } 1296 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { 1297 return 1298 x == y ? 0 : 1299 x > y ? 1 : 1300 -1; 1301 } 1302 1303 DECLARE_INSTRUCTION(Compare); 1304 1305 private: 1306 DISALLOW_COPY_AND_ASSIGN(HCompare); 1307}; 1308 1309// A local in the graph. Corresponds to a Dex register. 1310class HLocal : public HTemplateInstruction<0> { 1311 public: 1312 explicit HLocal(uint16_t reg_number) 1313 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {} 1314 1315 DECLARE_INSTRUCTION(Local); 1316 1317 uint16_t GetRegNumber() const { return reg_number_; } 1318 1319 private: 1320 // The Dex register number. 1321 const uint16_t reg_number_; 1322 1323 DISALLOW_COPY_AND_ASSIGN(HLocal); 1324}; 1325 1326// Load a given local. The local is an input of this instruction. 1327class HLoadLocal : public HExpression<1> { 1328 public: 1329 HLoadLocal(HLocal* local, Primitive::Type type) 1330 : HExpression(type, SideEffects::None()) { 1331 SetRawInputAt(0, local); 1332 } 1333 1334 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } 1335 1336 DECLARE_INSTRUCTION(LoadLocal); 1337 1338 private: 1339 DISALLOW_COPY_AND_ASSIGN(HLoadLocal); 1340}; 1341 1342// Store a value in a given local. This instruction has two inputs: the value 1343// and the local. 1344class HStoreLocal : public HTemplateInstruction<2> { 1345 public: 1346 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) { 1347 SetRawInputAt(0, local); 1348 SetRawInputAt(1, value); 1349 } 1350 1351 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } 1352 1353 DECLARE_INSTRUCTION(StoreLocal); 1354 1355 private: 1356 DISALLOW_COPY_AND_ASSIGN(HStoreLocal); 1357}; 1358 1359class HConstant : public HExpression<0> { 1360 public: 1361 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {} 1362 1363 virtual bool CanBeMoved() const { return true; } 1364 1365 DECLARE_INSTRUCTION(Constant); 1366 1367 private: 1368 DISALLOW_COPY_AND_ASSIGN(HConstant); 1369}; 1370 1371// Constants of the type int. Those can be from Dex instructions, or 1372// synthesized (for example with the if-eqz instruction). 1373class HIntConstant : public HConstant { 1374 public: 1375 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {} 1376 1377 int32_t GetValue() const { return value_; } 1378 1379 virtual bool InstructionDataEquals(HInstruction* other) const { 1380 return other->AsIntConstant()->value_ == value_; 1381 } 1382 1383 virtual size_t ComputeHashCode() const { return GetValue(); } 1384 1385 DECLARE_INSTRUCTION(IntConstant); 1386 1387 private: 1388 const int32_t value_; 1389 1390 DISALLOW_COPY_AND_ASSIGN(HIntConstant); 1391}; 1392 1393class HLongConstant : public HConstant { 1394 public: 1395 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {} 1396 1397 int64_t GetValue() const { return value_; } 1398 1399 virtual bool InstructionDataEquals(HInstruction* other) const { 1400 return other->AsLongConstant()->value_ == value_; 1401 } 1402 1403 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); } 1404 1405 DECLARE_INSTRUCTION(LongConstant); 1406 1407 private: 1408 const int64_t value_; 1409 1410 DISALLOW_COPY_AND_ASSIGN(HLongConstant); 1411}; 1412 1413class HInvoke : public HInstruction { 1414 public: 1415 HInvoke(ArenaAllocator* arena, 1416 uint32_t number_of_arguments, 1417 Primitive::Type return_type, 1418 uint32_t dex_pc) 1419 : HInstruction(SideEffects::All()), 1420 inputs_(arena, number_of_arguments), 1421 return_type_(return_type), 1422 dex_pc_(dex_pc) { 1423 inputs_.SetSize(number_of_arguments); 1424 } 1425 1426 virtual size_t InputCount() const { return inputs_.Size(); } 1427 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); } 1428 1429 // Runtime needs to walk the stack, so Dex -> Dex calls need to 1430 // know their environment. 1431 virtual bool NeedsEnvironment() const { return true; } 1432 1433 void SetArgumentAt(size_t index, HInstruction* argument) { 1434 SetRawInputAt(index, argument); 1435 } 1436 1437 virtual void SetRawInputAt(size_t index, HInstruction* input) { 1438 inputs_.Put(index, input); 1439 } 1440 1441 virtual Primitive::Type GetType() const { return return_type_; } 1442 1443 uint32_t GetDexPc() const { return dex_pc_; } 1444 1445 DECLARE_INSTRUCTION(Invoke); 1446 1447 protected: 1448 GrowableArray<HInstruction*> inputs_; 1449 const Primitive::Type return_type_; 1450 const uint32_t dex_pc_; 1451 1452 private: 1453 DISALLOW_COPY_AND_ASSIGN(HInvoke); 1454}; 1455 1456class HInvokeStatic : public HInvoke { 1457 public: 1458 HInvokeStatic(ArenaAllocator* arena, 1459 uint32_t number_of_arguments, 1460 Primitive::Type return_type, 1461 uint32_t dex_pc, 1462 uint32_t index_in_dex_cache) 1463 : HInvoke(arena, number_of_arguments, return_type, dex_pc), 1464 index_in_dex_cache_(index_in_dex_cache) {} 1465 1466 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; } 1467 1468 DECLARE_INSTRUCTION(InvokeStatic); 1469 1470 private: 1471 const uint32_t index_in_dex_cache_; 1472 1473 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic); 1474}; 1475 1476class HInvokeVirtual : public HInvoke { 1477 public: 1478 HInvokeVirtual(ArenaAllocator* arena, 1479 uint32_t number_of_arguments, 1480 Primitive::Type return_type, 1481 uint32_t dex_pc, 1482 uint32_t vtable_index) 1483 : HInvoke(arena, number_of_arguments, return_type, dex_pc), 1484 vtable_index_(vtable_index) {} 1485 1486 uint32_t GetVTableIndex() const { return vtable_index_; } 1487 1488 DECLARE_INSTRUCTION(InvokeVirtual); 1489 1490 private: 1491 const uint32_t vtable_index_; 1492 1493 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual); 1494}; 1495 1496class HNewInstance : public HExpression<0> { 1497 public: 1498 HNewInstance(uint32_t dex_pc, uint16_t type_index) 1499 : HExpression(Primitive::kPrimNot, SideEffects::None()), 1500 dex_pc_(dex_pc), 1501 type_index_(type_index) {} 1502 1503 uint32_t GetDexPc() const { return dex_pc_; } 1504 uint16_t GetTypeIndex() const { return type_index_; } 1505 1506 // Calls runtime so needs an environment. 1507 virtual bool NeedsEnvironment() const { return true; } 1508 1509 DECLARE_INSTRUCTION(NewInstance); 1510 1511 private: 1512 const uint32_t dex_pc_; 1513 const uint16_t type_index_; 1514 1515 DISALLOW_COPY_AND_ASSIGN(HNewInstance); 1516}; 1517 1518class HAdd : public HBinaryOperation { 1519 public: 1520 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right) 1521 : HBinaryOperation(result_type, left, right) {} 1522 1523 virtual bool IsCommutative() { return true; } 1524 1525 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { 1526 return x + y; 1527 } 1528 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { 1529 return x + y; 1530 } 1531 1532 DECLARE_INSTRUCTION(Add); 1533 1534 private: 1535 DISALLOW_COPY_AND_ASSIGN(HAdd); 1536}; 1537 1538class HSub : public HBinaryOperation { 1539 public: 1540 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right) 1541 : HBinaryOperation(result_type, left, right) {} 1542 1543 virtual bool IsCommutative() { return false; } 1544 1545 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { 1546 return x - y; 1547 } 1548 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { 1549 return x - y; 1550 } 1551 1552 DECLARE_INSTRUCTION(Sub); 1553 1554 private: 1555 DISALLOW_COPY_AND_ASSIGN(HSub); 1556}; 1557 1558// The value of a parameter in this method. Its location depends on 1559// the calling convention. 1560class HParameterValue : public HExpression<0> { 1561 public: 1562 HParameterValue(uint8_t index, Primitive::Type parameter_type) 1563 : HExpression(parameter_type, SideEffects::None()), index_(index) {} 1564 1565 uint8_t GetIndex() const { return index_; } 1566 1567 DECLARE_INSTRUCTION(ParameterValue); 1568 1569 private: 1570 // The index of this parameter in the parameters list. Must be less 1571 // than HGraph::number_of_in_vregs_; 1572 const uint8_t index_; 1573 1574 DISALLOW_COPY_AND_ASSIGN(HParameterValue); 1575}; 1576 1577class HNot : public HExpression<1> { 1578 public: 1579 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) { 1580 SetRawInputAt(0, input); 1581 } 1582 1583 virtual bool CanBeMoved() const { return true; } 1584 virtual bool InstructionDataEquals(HInstruction* other) const { return true; } 1585 1586 DECLARE_INSTRUCTION(Not); 1587 1588 private: 1589 DISALLOW_COPY_AND_ASSIGN(HNot); 1590}; 1591 1592class HPhi : public HInstruction { 1593 public: 1594 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type) 1595 : HInstruction(SideEffects::None()), 1596 inputs_(arena, number_of_inputs), 1597 reg_number_(reg_number), 1598 type_(type), 1599 is_live_(false) { 1600 inputs_.SetSize(number_of_inputs); 1601 } 1602 1603 virtual size_t InputCount() const { return inputs_.Size(); } 1604 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); } 1605 1606 virtual void SetRawInputAt(size_t index, HInstruction* input) { 1607 inputs_.Put(index, input); 1608 } 1609 1610 void AddInput(HInstruction* input); 1611 1612 virtual Primitive::Type GetType() const { return type_; } 1613 void SetType(Primitive::Type type) { type_ = type; } 1614 1615 uint32_t GetRegNumber() const { return reg_number_; } 1616 1617 void SetDead() { is_live_ = false; } 1618 void SetLive() { is_live_ = true; } 1619 bool IsDead() const { return !is_live_; } 1620 bool IsLive() const { return is_live_; } 1621 1622 DECLARE_INSTRUCTION(Phi); 1623 1624 private: 1625 GrowableArray<HInstruction*> inputs_; 1626 const uint32_t reg_number_; 1627 Primitive::Type type_; 1628 bool is_live_; 1629 1630 DISALLOW_COPY_AND_ASSIGN(HPhi); 1631}; 1632 1633class HNullCheck : public HExpression<1> { 1634 public: 1635 HNullCheck(HInstruction* value, uint32_t dex_pc) 1636 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) { 1637 SetRawInputAt(0, value); 1638 } 1639 1640 virtual bool CanBeMoved() const { return true; } 1641 virtual bool InstructionDataEquals(HInstruction* other) const { return true; } 1642 1643 virtual bool NeedsEnvironment() const { return true; } 1644 1645 uint32_t GetDexPc() const { return dex_pc_; } 1646 1647 DECLARE_INSTRUCTION(NullCheck); 1648 1649 private: 1650 const uint32_t dex_pc_; 1651 1652 DISALLOW_COPY_AND_ASSIGN(HNullCheck); 1653}; 1654 1655class FieldInfo : public ValueObject { 1656 public: 1657 FieldInfo(MemberOffset field_offset, Primitive::Type field_type) 1658 : field_offset_(field_offset), field_type_(field_type) {} 1659 1660 MemberOffset GetFieldOffset() const { return field_offset_; } 1661 Primitive::Type GetFieldType() const { return field_type_; } 1662 1663 private: 1664 const MemberOffset field_offset_; 1665 const Primitive::Type field_type_; 1666}; 1667 1668class HInstanceFieldGet : public HExpression<1> { 1669 public: 1670 HInstanceFieldGet(HInstruction* value, 1671 Primitive::Type field_type, 1672 MemberOffset field_offset) 1673 : HExpression(field_type, SideEffects::DependsOnSomething()), 1674 field_info_(field_offset, field_type) { 1675 SetRawInputAt(0, value); 1676 } 1677 1678 virtual bool CanBeMoved() const { return true; } 1679 virtual bool InstructionDataEquals(HInstruction* other) const { 1680 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue(); 1681 return other_offset == GetFieldOffset().SizeValue(); 1682 } 1683 1684 virtual size_t ComputeHashCode() const { 1685 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue(); 1686 } 1687 1688 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } 1689 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } 1690 1691 DECLARE_INSTRUCTION(InstanceFieldGet); 1692 1693 private: 1694 const FieldInfo field_info_; 1695 1696 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet); 1697}; 1698 1699class HInstanceFieldSet : public HTemplateInstruction<2> { 1700 public: 1701 HInstanceFieldSet(HInstruction* object, 1702 HInstruction* value, 1703 Primitive::Type field_type, 1704 MemberOffset field_offset) 1705 : HTemplateInstruction(SideEffects::ChangesSomething()), 1706 field_info_(field_offset, field_type) { 1707 SetRawInputAt(0, object); 1708 SetRawInputAt(1, value); 1709 } 1710 1711 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } 1712 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } 1713 1714 DECLARE_INSTRUCTION(InstanceFieldSet); 1715 1716 private: 1717 const FieldInfo field_info_; 1718 1719 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet); 1720}; 1721 1722class HArrayGet : public HExpression<2> { 1723 public: 1724 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type) 1725 : HExpression(type, SideEffects::DependsOnSomething()) { 1726 SetRawInputAt(0, array); 1727 SetRawInputAt(1, index); 1728 } 1729 1730 virtual bool CanBeMoved() const { return true; } 1731 virtual bool InstructionDataEquals(HInstruction* other) const { return true; } 1732 1733 DECLARE_INSTRUCTION(ArrayGet); 1734 1735 private: 1736 DISALLOW_COPY_AND_ASSIGN(HArrayGet); 1737}; 1738 1739class HArraySet : public HTemplateInstruction<3> { 1740 public: 1741 HArraySet(HInstruction* array, 1742 HInstruction* index, 1743 HInstruction* value, 1744 Primitive::Type component_type, 1745 uint32_t dex_pc) 1746 : HTemplateInstruction(SideEffects::ChangesSomething()), 1747 dex_pc_(dex_pc), 1748 component_type_(component_type) { 1749 SetRawInputAt(0, array); 1750 SetRawInputAt(1, index); 1751 SetRawInputAt(2, value); 1752 } 1753 1754 virtual bool NeedsEnvironment() const { 1755 // We currently always call a runtime method to catch array store 1756 // exceptions. 1757 return InputAt(2)->GetType() == Primitive::kPrimNot; 1758 } 1759 1760 uint32_t GetDexPc() const { return dex_pc_; } 1761 1762 Primitive::Type GetComponentType() const { return component_type_; } 1763 1764 DECLARE_INSTRUCTION(ArraySet); 1765 1766 private: 1767 const uint32_t dex_pc_; 1768 const Primitive::Type component_type_; 1769 1770 DISALLOW_COPY_AND_ASSIGN(HArraySet); 1771}; 1772 1773class HArrayLength : public HExpression<1> { 1774 public: 1775 explicit HArrayLength(HInstruction* array) 1776 : HExpression(Primitive::kPrimInt, SideEffects::None()) { 1777 // Note that arrays do not change length, so the instruction does not 1778 // depend on any write. 1779 SetRawInputAt(0, array); 1780 } 1781 1782 virtual bool CanBeMoved() const { return true; } 1783 virtual bool InstructionDataEquals(HInstruction* other) const { return true; } 1784 1785 DECLARE_INSTRUCTION(ArrayLength); 1786 1787 private: 1788 DISALLOW_COPY_AND_ASSIGN(HArrayLength); 1789}; 1790 1791class HBoundsCheck : public HExpression<2> { 1792 public: 1793 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc) 1794 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) { 1795 DCHECK(index->GetType() == Primitive::kPrimInt); 1796 SetRawInputAt(0, index); 1797 SetRawInputAt(1, length); 1798 } 1799 1800 virtual bool CanBeMoved() const { return true; } 1801 virtual bool InstructionDataEquals(HInstruction* other) const { return true; } 1802 1803 virtual bool NeedsEnvironment() const { return true; } 1804 1805 uint32_t GetDexPc() const { return dex_pc_; } 1806 1807 DECLARE_INSTRUCTION(BoundsCheck); 1808 1809 private: 1810 const uint32_t dex_pc_; 1811 1812 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck); 1813}; 1814 1815/** 1816 * Some DEX instructions are folded into multiple HInstructions that need 1817 * to stay live until the last HInstruction. This class 1818 * is used as a marker for the baseline compiler to ensure its preceding 1819 * HInstruction stays live. `index` is the temporary number that is used 1820 * for knowing the stack offset where to store the instruction. 1821 */ 1822class HTemporary : public HTemplateInstruction<0> { 1823 public: 1824 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {} 1825 1826 size_t GetIndex() const { return index_; } 1827 1828 DECLARE_INSTRUCTION(Temporary); 1829 1830 private: 1831 const size_t index_; 1832 1833 DISALLOW_COPY_AND_ASSIGN(HTemporary); 1834}; 1835 1836class HSuspendCheck : public HTemplateInstruction<0> { 1837 public: 1838 explicit HSuspendCheck(uint32_t dex_pc) 1839 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {} 1840 1841 virtual bool NeedsEnvironment() const { 1842 return true; 1843 } 1844 1845 uint32_t GetDexPc() const { return dex_pc_; } 1846 1847 DECLARE_INSTRUCTION(SuspendCheck); 1848 1849 private: 1850 const uint32_t dex_pc_; 1851 1852 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck); 1853}; 1854 1855class MoveOperands : public ArenaObject { 1856 public: 1857 MoveOperands(Location source, Location destination, HInstruction* instruction) 1858 : source_(source), destination_(destination), instruction_(instruction) {} 1859 1860 Location GetSource() const { return source_; } 1861 Location GetDestination() const { return destination_; } 1862 1863 void SetSource(Location value) { source_ = value; } 1864 void SetDestination(Location value) { destination_ = value; } 1865 1866 // The parallel move resolver marks moves as "in-progress" by clearing the 1867 // destination (but not the source). 1868 Location MarkPending() { 1869 DCHECK(!IsPending()); 1870 Location dest = destination_; 1871 destination_ = Location::NoLocation(); 1872 return dest; 1873 } 1874 1875 void ClearPending(Location dest) { 1876 DCHECK(IsPending()); 1877 destination_ = dest; 1878 } 1879 1880 bool IsPending() const { 1881 DCHECK(!source_.IsInvalid() || destination_.IsInvalid()); 1882 return destination_.IsInvalid() && !source_.IsInvalid(); 1883 } 1884 1885 // True if this blocks a move from the given location. 1886 bool Blocks(Location loc) const { 1887 return !IsEliminated() && source_.Equals(loc); 1888 } 1889 1890 // A move is redundant if it's been eliminated, if its source and 1891 // destination are the same, or if its destination is unneeded. 1892 bool IsRedundant() const { 1893 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_); 1894 } 1895 1896 // We clear both operands to indicate move that's been eliminated. 1897 void Eliminate() { 1898 source_ = destination_ = Location::NoLocation(); 1899 } 1900 1901 bool IsEliminated() const { 1902 DCHECK(!source_.IsInvalid() || destination_.IsInvalid()); 1903 return source_.IsInvalid(); 1904 } 1905 1906 HInstruction* GetInstruction() const { return instruction_; } 1907 1908 private: 1909 Location source_; 1910 Location destination_; 1911 // The instruction this move is assocatied with. Null when this move is 1912 // for moving an input in the expected locations of user (including a phi user). 1913 // This is only used in debug mode, to ensure we do not connect interval siblings 1914 // in the same parallel move. 1915 HInstruction* instruction_; 1916 1917 DISALLOW_COPY_AND_ASSIGN(MoveOperands); 1918}; 1919 1920static constexpr size_t kDefaultNumberOfMoves = 4; 1921 1922class HParallelMove : public HTemplateInstruction<0> { 1923 public: 1924 explicit HParallelMove(ArenaAllocator* arena) 1925 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {} 1926 1927 void AddMove(MoveOperands* move) { 1928 if (kIsDebugBuild && move->GetInstruction() != nullptr) { 1929 for (size_t i = 0, e = moves_.Size(); i < e; ++i) { 1930 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction()) 1931 << "Doing parallel moves for the same instruction."; 1932 } 1933 } 1934 moves_.Add(move); 1935 } 1936 1937 MoveOperands* MoveOperandsAt(size_t index) const { 1938 return moves_.Get(index); 1939 } 1940 1941 size_t NumMoves() const { return moves_.Size(); } 1942 1943 DECLARE_INSTRUCTION(ParallelMove); 1944 1945 private: 1946 GrowableArray<MoveOperands*> moves_; 1947 1948 DISALLOW_COPY_AND_ASSIGN(HParallelMove); 1949}; 1950 1951class HGraphVisitor : public ValueObject { 1952 public: 1953 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {} 1954 virtual ~HGraphVisitor() {} 1955 1956 virtual void VisitInstruction(HInstruction* instruction) {} 1957 virtual void VisitBasicBlock(HBasicBlock* block); 1958 1959 void VisitInsertionOrder(); 1960 1961 HGraph* GetGraph() const { return graph_; } 1962 1963 // Visit functions for instruction classes. 1964#define DECLARE_VISIT_INSTRUCTION(name, super) \ 1965 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); } 1966 1967 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) 1968 1969#undef DECLARE_VISIT_INSTRUCTION 1970 1971 private: 1972 HGraph* graph_; 1973 1974 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor); 1975}; 1976 1977class HGraphDelegateVisitor : public HGraphVisitor { 1978 public: 1979 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {} 1980 virtual ~HGraphDelegateVisitor() {} 1981 1982 // Visit functions that delegate to to super class. 1983#define DECLARE_VISIT_INSTRUCTION(name, super) \ 1984 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); } 1985 1986 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) 1987 1988#undef DECLARE_VISIT_INSTRUCTION 1989 1990 private: 1991 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor); 1992}; 1993 1994class HInsertionOrderIterator : public ValueObject { 1995 public: 1996 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {} 1997 1998 bool Done() const { return index_ == graph_.GetBlocks().Size(); } 1999 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); } 2000 void Advance() { ++index_; } 2001 2002 private: 2003 const HGraph& graph_; 2004 size_t index_; 2005 2006 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator); 2007}; 2008 2009class HReversePostOrderIterator : public ValueObject { 2010 public: 2011 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {} 2012 2013 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); } 2014 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); } 2015 void Advance() { ++index_; } 2016 2017 private: 2018 const HGraph& graph_; 2019 size_t index_; 2020 2021 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator); 2022}; 2023 2024class HPostOrderIterator : public ValueObject { 2025 public: 2026 explicit HPostOrderIterator(const HGraph& graph) 2027 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {} 2028 2029 bool Done() const { return index_ == 0; } 2030 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); } 2031 void Advance() { --index_; } 2032 2033 private: 2034 const HGraph& graph_; 2035 size_t index_; 2036 2037 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator); 2038}; 2039 2040} // namespace art 2041 2042#endif // ART_COMPILER_OPTIMIZING_NODES_H_ 2043