| 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 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 | 647 |
| 648 void Cfg::sortAndCombineAllocas(CfgVector<Inst *> &Allocas, | 648 void Cfg::sortAndCombineAllocas(CfgVector<Inst *> &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(), [](Inst *I1, Inst *I2) { |
| 655 auto *A1 = llvm::dyn_cast<InstAlloca>(I1); | 655 auto *A1 = llvm::dyn_cast<InstAlloca>(I1); |
| 656 auto *A2 = llvm::dyn_cast<InstAlloca>(I2); | 656 auto *A2 = llvm::dyn_cast<InstAlloca>(I2); |
| 657 return A1->getAlignInBytes() > A2->getAlignInBytes(); | 657 return A1->getAlignInBytes() >= A2->getAlignInBytes(); |
| 658 }); | 658 }); |
| 659 // Process the allocas in order of decreasing stack alignment. This allows | 659 // 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 | 660 // 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" | 661 // stack growth. It also allows there to be at most one stack alignment "and" |
| 662 // instruction for a whole list of allocas. | 662 // instruction for a whole list of allocas. |
| 663 uint32_t CurrentOffset = 0; | 663 uint32_t CurrentOffset = 0; |
| 664 CfgVector<int32_t> Offsets; | 664 CfgVector<int32_t> Offsets; |
| 665 for (Inst *Instr : Allocas) { | 665 for (Inst *Instr : Allocas) { |
| 666 auto *Alloca = llvm::cast<InstAlloca>(Instr); | 666 auto *Alloca = llvm::cast<InstAlloca>(Instr); |
| 667 // Adjust the size of the allocation up to the next multiple of the | 667 // 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(); | 739 const uint32_t StackAlignment = getTarget()->getStackAlignment(); |
| 740 CfgNode *EntryNode = getEntryNode(); | 740 CfgNode *EntryNode = getEntryNode(); |
| 741 assert(EntryNode); | 741 assert(EntryNode); |
| 742 // LLVM enforces power of 2 alignment. | 742 // LLVM enforces power of 2 alignment. |
| 743 assert(llvm::isPowerOf2_32(StackAlignment)); | 743 assert(llvm::isPowerOf2_32(StackAlignment)); |
| 744 // Determine if there are large alignment allocations in the entry block or | 744 // Determine if there are large alignment allocations in the entry block or |
| 745 // dynamic allocations (variable size in the entry block). | 745 // dynamic allocations (variable size in the entry block). |
| 746 bool HasLargeAlignment = false; | 746 bool HasLargeAlignment = false; |
| 747 bool HasDynamicAllocation = false; | 747 bool HasDynamicAllocation = false; |
| 748 for (Inst &Instr : EntryNode->getInsts()) { | 748 for (Inst &Instr : EntryNode->getInsts()) { |
| 749 if (Instr.isDeleted()) |
| 750 continue; |
| 749 if (auto *Alloca = llvm::dyn_cast<InstAlloca>(&Instr)) { | 751 if (auto *Alloca = llvm::dyn_cast<InstAlloca>(&Instr)) { |
| 750 uint32_t AlignmentParam = Alloca->getAlignInBytes(); | 752 uint32_t AlignmentParam = Alloca->getAlignInBytes(); |
| 751 if (AlignmentParam > StackAlignment) | 753 if (AlignmentParam > StackAlignment) |
| 752 HasLargeAlignment = true; | 754 HasLargeAlignment = true; |
| 753 if (llvm::isa<Constant>(Alloca->getSizeInBytes())) | 755 if (llvm::isa<Constant>(Alloca->getSizeInBytes())) |
| 754 Alloca->setKnownFrameOffset(); | 756 Alloca->setKnownFrameOffset(); |
| 755 else { | 757 else { |
| 756 HasDynamicAllocation = true; | 758 HasDynamicAllocation = true; |
| 757 // If Allocas are not sorted, the first dynamic allocation causes | 759 // If Allocas are not sorted, the first dynamic allocation causes |
| 758 // later Allocas to be at unknown offsets relative to the stack/frame. | 760 // later Allocas to be at unknown offsets relative to the stack/frame. |
| 759 if (!SortAndCombine) | 761 if (!SortAndCombine) |
| 760 break; | 762 break; |
| 761 } | 763 } |
| 762 } | 764 } |
| 763 } | 765 } |
| 764 // Don't do the heavyweight sorting and layout for low optimization levels. | 766 // Don't do the heavyweight sorting and layout for low optimization levels. |
| 765 if (!SortAndCombine) | 767 if (!SortAndCombine) |
| 766 return; | 768 return; |
| 767 // Any alloca outside the entry block is a dynamic allocation. | 769 // Any alloca outside the entry block is a dynamic allocation. |
| 768 for (CfgNode *Node : Nodes) { | 770 for (CfgNode *Node : Nodes) { |
| 769 if (Node == EntryNode) | 771 if (Node == EntryNode) |
| 770 continue; | 772 continue; |
| 771 for (Inst &Instr : Node->getInsts()) { | 773 for (Inst &Instr : Node->getInsts()) { |
| 774 if (Instr.isDeleted()) |
| 775 continue; |
| 772 if (llvm::isa<InstAlloca>(&Instr)) { | 776 if (llvm::isa<InstAlloca>(&Instr)) { |
| 773 // Allocations outside the entry block require a frame pointer. | 777 // Allocations outside the entry block require a frame pointer. |
| 774 HasDynamicAllocation = true; | 778 HasDynamicAllocation = true; |
| 775 break; | 779 break; |
| 776 } | 780 } |
| 777 } | 781 } |
| 778 if (HasDynamicAllocation) | 782 if (HasDynamicAllocation) |
| 779 break; | 783 break; |
| 780 } | 784 } |
| 781 // Mark the target as requiring a frame pointer. | 785 // Mark the target as requiring a frame pointer. |
| 782 if (HasLargeAlignment || HasDynamicAllocation) | 786 if (HasLargeAlignment || HasDynamicAllocation) |
| 783 getTarget()->setHasFramePointer(); | 787 getTarget()->setHasFramePointer(); |
| 784 // Collect the Allocas into the two vectors. | 788 // Collect the Allocas into the two vectors. |
| 785 // Allocas in the entry block that have constant size and alignment less | 789 // Allocas in the entry block that have constant size and alignment less |
| 786 // than or equal to the function's stack alignment. | 790 // than or equal to the function's stack alignment. |
| 787 CfgVector<Inst *> FixedAllocas; | 791 CfgVector<Inst *> FixedAllocas; |
| 788 // Allocas in the entry block that have constant size and alignment greater | 792 // Allocas in the entry block that have constant size and alignment greater |
| 789 // than the function's stack alignment. | 793 // than the function's stack alignment. |
| 790 CfgVector<Inst *> AlignedAllocas; | 794 CfgVector<Inst *> AlignedAllocas; |
| 791 // Maximum alignment used by any alloca. | 795 // Maximum alignment used by any alloca. |
| 792 uint32_t MaxAlignment = StackAlignment; | 796 uint32_t MaxAlignment = StackAlignment; |
| 793 for (Inst &Instr : EntryNode->getInsts()) { | 797 for (Inst &Instr : EntryNode->getInsts()) { |
| 798 if (Instr.isDeleted()) |
| 799 continue; |
| 794 if (auto *Alloca = llvm::dyn_cast<InstAlloca>(&Instr)) { | 800 if (auto *Alloca = llvm::dyn_cast<InstAlloca>(&Instr)) { |
| 795 if (!llvm::isa<Constant>(Alloca->getSizeInBytes())) | 801 if (!llvm::isa<Constant>(Alloca->getSizeInBytes())) |
| 796 continue; | 802 continue; |
| 797 uint32_t AlignmentParam = Alloca->getAlignInBytes(); | 803 uint32_t AlignmentParam = Alloca->getAlignInBytes(); |
| 798 // For default align=0, set it to the real value 1, to avoid any | 804 // For default align=0, set it to the real value 1, to avoid any |
| 799 // bit-manipulation problems below. | 805 // bit-manipulation problems below. |
| 800 AlignmentParam = std::max(AlignmentParam, 1u); | 806 AlignmentParam = std::max(AlignmentParam, 1u); |
| 801 assert(llvm::isPowerOf2_32(AlignmentParam)); | 807 assert(llvm::isPowerOf2_32(AlignmentParam)); |
| 802 if (HasDynamicAllocation && AlignmentParam > StackAlignment) { | 808 if (HasDynamicAllocation && AlignmentParam > StackAlignment) { |
| 803 // If we have both dynamic allocations and large stack alignments, | 809 // If we have both dynamic allocations and large stack alignments, |
| (...skipping 807 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1611 } | 1617 } |
| 1612 } | 1618 } |
| 1613 // Print each basic block | 1619 // Print each basic block |
| 1614 for (CfgNode *Node : Nodes) | 1620 for (CfgNode *Node : Nodes) |
| 1615 Node->dump(this); | 1621 Node->dump(this); |
| 1616 if (isVerbose(IceV_Instructions)) | 1622 if (isVerbose(IceV_Instructions)) |
| 1617 Str << "}\n"; | 1623 Str << "}\n"; |
| 1618 } | 1624 } |
| 1619 | 1625 |
| 1620 } // end of namespace Ice | 1626 } // end of namespace Ice |
| OLD | NEW |