| OLD | NEW |
| 1 //===- subzero/src/IceCfg.cpp - Control flow graph implementation ---------===// | 1 //===- subzero/src/IceCfg.cpp - Control flow graph implementation ---------===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 /// | 9 /// |
| 10 /// \file | 10 /// \file |
| (...skipping 627 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 638 } | 638 } |
| 639 } | 639 } |
| 640 } | 640 } |
| 641 } | 641 } |
| 642 | 642 |
| 643 void Cfg::doArgLowering() { | 643 void Cfg::doArgLowering() { |
| 644 TimerMarker T(TimerStack::TT_doArgLowering, this); | 644 TimerMarker T(TimerStack::TT_doArgLowering, this); |
| 645 getTarget()->lowerArguments(); | 645 getTarget()->lowerArguments(); |
| 646 } | 646 } |
| 647 | 647 |
| 648 void Cfg::sortAndCombineAllocas(CfgVector<Inst *> &Allocas, | 648 void Cfg::sortAndCombineAllocas(CfgVector<InstAlloca *> &Allocas, |
| 649 uint32_t CombinedAlignment, InstList &Insts, | 649 uint32_t CombinedAlignment, InstList &Insts, |
| 650 AllocaBaseVariableType BaseVariableType) { | 650 AllocaBaseVariableType BaseVariableType) { |
| 651 if (Allocas.empty()) | 651 if (Allocas.empty()) |
| 652 return; | 652 return; |
| 653 // Sort by decreasing alignment. | 653 // Sort by decreasing alignment. |
| 654 std::sort(Allocas.begin(), Allocas.end(), [](Inst *I1, Inst *I2) { | 654 std::sort(Allocas.begin(), Allocas.end(), [](InstAlloca *A1, InstAlloca *A2) { |
| 655 auto *A1 = llvm::dyn_cast<InstAlloca>(I1); | 655 uint32_t Align1 = A1->getAlignInBytes(); |
| 656 auto *A2 = llvm::dyn_cast<InstAlloca>(I2); | 656 uint32_t Align2 = A2->getAlignInBytes(); |
| 657 return A1->getAlignInBytes() > A2->getAlignInBytes(); | 657 if (Align1 == Align2) |
| 658 return A1->getNumber() > A2->getNumber(); |
| 659 else |
| 660 return Align1 > Align2; |
| 658 }); | 661 }); |
| 659 // Process the allocas in order of decreasing stack alignment. This allows | 662 // Process the allocas in order of decreasing stack alignment. This allows |
| 660 // us to pack less-aligned pieces after more-aligned ones, resulting in less | 663 // us to pack less-aligned pieces after more-aligned ones, resulting in less |
| 661 // stack growth. It also allows there to be at most one stack alignment "and" | 664 // stack growth. It also allows there to be at most one stack alignment "and" |
| 662 // instruction for a whole list of allocas. | 665 // instruction for a whole list of allocas. |
| 663 uint32_t CurrentOffset = 0; | 666 uint32_t CurrentOffset = 0; |
| 664 CfgVector<int32_t> Offsets; | 667 CfgVector<int32_t> Offsets; |
| 665 for (Inst *Instr : Allocas) { | 668 for (Inst *Instr : Allocas) { |
| 666 auto *Alloca = llvm::cast<InstAlloca>(Instr); | 669 auto *Alloca = llvm::cast<InstAlloca>(Instr); |
| 667 // Adjust the size of the allocation up to the next multiple of the | 670 // Adjust the size of the allocation up to the next multiple of the |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 739 const uint32_t StackAlignment = getTarget()->getStackAlignment(); | 742 const uint32_t StackAlignment = getTarget()->getStackAlignment(); |
| 740 CfgNode *EntryNode = getEntryNode(); | 743 CfgNode *EntryNode = getEntryNode(); |
| 741 assert(EntryNode); | 744 assert(EntryNode); |
| 742 // LLVM enforces power of 2 alignment. | 745 // LLVM enforces power of 2 alignment. |
| 743 assert(llvm::isPowerOf2_32(StackAlignment)); | 746 assert(llvm::isPowerOf2_32(StackAlignment)); |
| 744 // Determine if there are large alignment allocations in the entry block or | 747 // Determine if there are large alignment allocations in the entry block or |
| 745 // dynamic allocations (variable size in the entry block). | 748 // dynamic allocations (variable size in the entry block). |
| 746 bool HasLargeAlignment = false; | 749 bool HasLargeAlignment = false; |
| 747 bool HasDynamicAllocation = false; | 750 bool HasDynamicAllocation = false; |
| 748 for (Inst &Instr : EntryNode->getInsts()) { | 751 for (Inst &Instr : EntryNode->getInsts()) { |
| 752 if (Instr.isDeleted()) |
| 753 continue; |
| 749 if (auto *Alloca = llvm::dyn_cast<InstAlloca>(&Instr)) { | 754 if (auto *Alloca = llvm::dyn_cast<InstAlloca>(&Instr)) { |
| 750 uint32_t AlignmentParam = Alloca->getAlignInBytes(); | 755 uint32_t AlignmentParam = Alloca->getAlignInBytes(); |
| 751 if (AlignmentParam > StackAlignment) | 756 if (AlignmentParam > StackAlignment) |
| 752 HasLargeAlignment = true; | 757 HasLargeAlignment = true; |
| 753 if (llvm::isa<Constant>(Alloca->getSizeInBytes())) | 758 if (llvm::isa<Constant>(Alloca->getSizeInBytes())) |
| 754 Alloca->setKnownFrameOffset(); | 759 Alloca->setKnownFrameOffset(); |
| 755 else { | 760 else { |
| 756 HasDynamicAllocation = true; | 761 HasDynamicAllocation = true; |
| 757 // If Allocas are not sorted, the first dynamic allocation causes | 762 // If Allocas are not sorted, the first dynamic allocation causes |
| 758 // later Allocas to be at unknown offsets relative to the stack/frame. | 763 // later Allocas to be at unknown offsets relative to the stack/frame. |
| 759 if (!SortAndCombine) | 764 if (!SortAndCombine) |
| 760 break; | 765 break; |
| 761 } | 766 } |
| 762 } | 767 } |
| 763 } | 768 } |
| 764 // Don't do the heavyweight sorting and layout for low optimization levels. | 769 // Don't do the heavyweight sorting and layout for low optimization levels. |
| 765 if (!SortAndCombine) | 770 if (!SortAndCombine) |
| 766 return; | 771 return; |
| 767 // Any alloca outside the entry block is a dynamic allocation. | 772 // Any alloca outside the entry block is a dynamic allocation. |
| 768 for (CfgNode *Node : Nodes) { | 773 for (CfgNode *Node : Nodes) { |
| 769 if (Node == EntryNode) | 774 if (Node == EntryNode) |
| 770 continue; | 775 continue; |
| 771 for (Inst &Instr : Node->getInsts()) { | 776 for (Inst &Instr : Node->getInsts()) { |
| 777 if (Instr.isDeleted()) |
| 778 continue; |
| 772 if (llvm::isa<InstAlloca>(&Instr)) { | 779 if (llvm::isa<InstAlloca>(&Instr)) { |
| 773 // Allocations outside the entry block require a frame pointer. | 780 // Allocations outside the entry block require a frame pointer. |
| 774 HasDynamicAllocation = true; | 781 HasDynamicAllocation = true; |
| 775 break; | 782 break; |
| 776 } | 783 } |
| 777 } | 784 } |
| 778 if (HasDynamicAllocation) | 785 if (HasDynamicAllocation) |
| 779 break; | 786 break; |
| 780 } | 787 } |
| 781 // Mark the target as requiring a frame pointer. | 788 // Mark the target as requiring a frame pointer. |
| 782 if (HasLargeAlignment || HasDynamicAllocation) | 789 if (HasLargeAlignment || HasDynamicAllocation) |
| 783 getTarget()->setHasFramePointer(); | 790 getTarget()->setHasFramePointer(); |
| 784 // Collect the Allocas into the two vectors. | 791 // Collect the Allocas into the two vectors. |
| 785 // Allocas in the entry block that have constant size and alignment less | 792 // Allocas in the entry block that have constant size and alignment less |
| 786 // than or equal to the function's stack alignment. | 793 // than or equal to the function's stack alignment. |
| 787 CfgVector<Inst *> FixedAllocas; | 794 CfgVector<InstAlloca *> FixedAllocas; |
| 788 // Allocas in the entry block that have constant size and alignment greater | 795 // Allocas in the entry block that have constant size and alignment greater |
| 789 // than the function's stack alignment. | 796 // than the function's stack alignment. |
| 790 CfgVector<Inst *> AlignedAllocas; | 797 CfgVector<InstAlloca *> AlignedAllocas; |
| 791 // Maximum alignment used by any alloca. | 798 // Maximum alignment used by any alloca. |
| 792 uint32_t MaxAlignment = StackAlignment; | 799 uint32_t MaxAlignment = StackAlignment; |
| 793 for (Inst &Instr : EntryNode->getInsts()) { | 800 for (Inst &Instr : EntryNode->getInsts()) { |
| 801 if (Instr.isDeleted()) |
| 802 continue; |
| 794 if (auto *Alloca = llvm::dyn_cast<InstAlloca>(&Instr)) { | 803 if (auto *Alloca = llvm::dyn_cast<InstAlloca>(&Instr)) { |
| 795 if (!llvm::isa<Constant>(Alloca->getSizeInBytes())) | 804 if (!llvm::isa<Constant>(Alloca->getSizeInBytes())) |
| 796 continue; | 805 continue; |
| 797 uint32_t AlignmentParam = Alloca->getAlignInBytes(); | 806 uint32_t AlignmentParam = Alloca->getAlignInBytes(); |
| 798 // For default align=0, set it to the real value 1, to avoid any | 807 // For default align=0, set it to the real value 1, to avoid any |
| 799 // bit-manipulation problems below. | 808 // bit-manipulation problems below. |
| 800 AlignmentParam = std::max(AlignmentParam, 1u); | 809 AlignmentParam = std::max(AlignmentParam, 1u); |
| 801 assert(llvm::isPowerOf2_32(AlignmentParam)); | 810 assert(llvm::isPowerOf2_32(AlignmentParam)); |
| 802 if (HasDynamicAllocation && AlignmentParam > StackAlignment) { | 811 if (HasDynamicAllocation && AlignmentParam > StackAlignment) { |
| 803 // If we have both dynamic allocations and large stack alignments, | 812 // If we have both dynamic allocations and large stack alignments, |
| (...skipping 807 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1611 } | 1620 } |
| 1612 } | 1621 } |
| 1613 // Print each basic block | 1622 // Print each basic block |
| 1614 for (CfgNode *Node : Nodes) | 1623 for (CfgNode *Node : Nodes) |
| 1615 Node->dump(this); | 1624 Node->dump(this); |
| 1616 if (isVerbose(IceV_Instructions)) | 1625 if (isVerbose(IceV_Instructions)) |
| 1617 Str << "}\n"; | 1626 Str << "}\n"; |
| 1618 } | 1627 } |
| 1619 | 1628 |
| 1620 } // end of namespace Ice | 1629 } // end of namespace Ice |
| OLD | NEW |