Index: llvm/include/llvm/CodeGen/TargetLowering.h =================================================================== --- llvm/include/llvm/CodeGen/TargetLowering.h +++ llvm/include/llvm/CodeGen/TargetLowering.h @@ -1893,8 +1893,7 @@ /// Return the maximum amount of bytes allowed to be emitted when padding for /// alignment - virtual unsigned - getMaxPermittedBytesForAlignment(MachineBasicBlock *MBB) const; + virtual unsigned getMaxPermittedBytesForAlignment() const; /// Should loops be aligned even when the function is marked OptSize (but not /// MinSize). Index: llvm/lib/CodeGen/MachineBlockPlacement.cpp =================================================================== --- llvm/lib/CodeGen/MachineBlockPlacement.cpp +++ llvm/lib/CodeGen/MachineBlockPlacement.cpp @@ -399,6 +399,8 @@ ProfileSummaryInfo *PSI = nullptr; + unsigned MaxBytesForAlignment = 0; + /// Duplicator used to duplicate tails during placement. /// /// Placement decisions can open up new tail duplication opportunities, but @@ -2945,21 +2947,10 @@ MachineBasicBlock *LayoutPred = &*std::prev(MachineFunction::iterator(ChainBB)); - auto DetermineMaxAlignmentPadding = [&]() { - // Set the maximum bytes allowed to be emitted for alignment. - unsigned MaxBytes; - if (MaxBytesForAlignmentOverride.getNumOccurrences() > 0) - MaxBytes = MaxBytesForAlignmentOverride; - else - MaxBytes = TLI->getMaxPermittedBytesForAlignment(ChainBB); - ChainBB->setMaxBytesForAlignment(MaxBytes); - }; - // Force alignment if all the predecessors are jumps. We already checked // that the block isn't cold above. if (!LayoutPred->isSuccessor(ChainBB)) { - ChainBB->setAlignment(Align); - DetermineMaxAlignmentPadding(); + ChainBB->setAlignment(Align, MaxBytesForAlignment); continue; } @@ -2970,10 +2961,8 @@ BranchProbability LayoutProb = MBPI->getEdgeProbability(LayoutPred, ChainBB); BlockFrequency LayoutEdgeFreq = MBFI->getBlockFreq(LayoutPred) * LayoutProb; - if (LayoutEdgeFreq <= (Freq * ColdProb)) { - ChainBB->setAlignment(Align); - DetermineMaxAlignmentPadding(); - } + if (LayoutEdgeFreq <= (Freq * ColdProb)) + ChainBB->setAlignment(Align, MaxBytesForAlignment); } } @@ -3356,6 +3345,12 @@ MPDT = nullptr; PSI = &getAnalysis().getPSI(); + // Set the maximum bytes allowed to be emitted for alignment. + if (MaxBytesForAlignmentOverride.getNumOccurrences() > 0) + MaxBytesForAlignment = MaxBytesForAlignmentOverride; + else + MaxBytesForAlignment = TLI->getMaxPermittedBytesForAlignment(); + initDupThreshold(); // Initialize PreferredLoopExit to nullptr here since it may never be set if Index: llvm/lib/CodeGen/TargetLoweringBase.cpp =================================================================== --- llvm/lib/CodeGen/TargetLoweringBase.cpp +++ llvm/lib/CodeGen/TargetLoweringBase.cpp @@ -2058,8 +2058,7 @@ return PrefLoopAlignment; } -unsigned TargetLoweringBase::getMaxPermittedBytesForAlignment( - MachineBasicBlock *MBB) const { +unsigned TargetLoweringBase::getMaxPermittedBytesForAlignment() const { return MaxBytesForAlignment; }