From 9d3094b38e51c85e93fc5a9c8ab23c95b73f78d4 Mon Sep 17 00:00:00 2001 From: Evan Cheng Date: Tue, 12 May 2009 23:58:14 +0000 Subject: If header of inner loop is aligned, do not align the outer loop header. We don't want to add nops in the outer loop for the sake of aligning the inner loop. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@71609 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CodePlacementOpt.cpp | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) (limited to 'lib/CodeGen') diff --git a/lib/CodeGen/CodePlacementOpt.cpp b/lib/CodeGen/CodePlacementOpt.cpp index 2e1d12d..61a8b12 100644 --- a/lib/CodeGen/CodePlacementOpt.cpp +++ b/lib/CodeGen/CodePlacementOpt.cpp @@ -62,6 +62,8 @@ namespace { private: bool OptimizeIntraLoopEdges(); + bool HeaderShouldBeAligned(MachineBasicBlock *MBB, MachineLoop *L, + SmallPtrSet &DoNotAlign); bool AlignLoops(MachineFunction &MF); }; @@ -244,14 +246,37 @@ bool CodePlacementOpt::OptimizeIntraLoopEdges() { /// should be aligned. For now, we will not align it if all the predcessors /// (i.e. loop back edges) are laid out above the header. FIXME: Do not /// align small loops. -static bool HeaderShouldBeAligned(MachineBasicBlock *MBB) { +bool +CodePlacementOpt::HeaderShouldBeAligned(MachineBasicBlock *MBB, MachineLoop *L, + SmallPtrSet &DoNotAlign) { + if (DoNotAlign.count(MBB)) + return false; + + bool BackEdgeBelow = false; for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), PE = MBB->pred_end(); PI != PE; ++PI) { MachineBasicBlock *PredMBB = *PI; - if (PredMBB == MBB || PredMBB->getNumber() > MBB->getNumber()) - return true; + if (PredMBB == MBB || PredMBB->getNumber() > MBB->getNumber()) { + BackEdgeBelow = true; + break; + } + } + + if (!BackEdgeBelow) + return false; + + // Ok, we are going to align this loop header. If it's an inner loop, + // do not align its outer loop. + MachineBasicBlock *PreHeader = L->getLoopPreheader(); + if (PreHeader) { + MachineLoop *L = MLI->getLoopFor(PreHeader); + if (L) { + MachineBasicBlock *HeaderBlock = L->getHeader(); + HeaderBlock->setAlignment(0); + DoNotAlign.insert(HeaderBlock); + } } - return false; + return true; } /// AlignLoops - Align loop headers to target preferred alignments. @@ -269,14 +294,16 @@ bool CodePlacementOpt::AlignLoops(MachineFunction &MF) { MF.RenumberBlocks(); bool Changed = false; + SmallPtrSet DoNotAlign; for (unsigned i = 0, e = LoopHeaders.size(); i != e; ++i) { MachineBasicBlock *HeaderMBB = LoopHeaders[i]; MachineBasicBlock *PredMBB = prior(MachineFunction::iterator(HeaderMBB)); - if (MLI->getLoopFor(HeaderMBB) == MLI->getLoopFor(PredMBB)) + MachineLoop *L = MLI->getLoopFor(HeaderMBB); + if (L == MLI->getLoopFor(PredMBB)) // If previously BB is in the same loop, don't align this BB. We want // to prevent adding noop's inside a loop. continue; - if (HeaderShouldBeAligned(HeaderMBB)) { + if (HeaderShouldBeAligned(HeaderMBB, L, DoNotAlign)) { HeaderMBB->setAlignment(Align); Changed = true; ++NumHeaderAligned; -- cgit v1.1