[llvm] r283462 - BranchRelaxation: Account for function alignment

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 6 09:00:59 PDT 2016


Author: arsenm
Date: Thu Oct  6 11:00:58 2016
New Revision: 283462

URL: http://llvm.org/viewvc/llvm-project?rev=283462&view=rev
Log:
BranchRelaxation: Account for function alignment

Added:
    llvm/trunk/test/CodeGen/AArch64/branch-relax-alignment.ll
Modified:
    llvm/trunk/lib/CodeGen/BranchRelaxation.cpp

Modified: llvm/trunk/lib/CodeGen/BranchRelaxation.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/BranchRelaxation.cpp?rev=283462&r1=283461&r2=283462&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/BranchRelaxation.cpp (original)
+++ llvm/trunk/lib/CodeGen/BranchRelaxation.cpp Thu Oct  6 11:00:58 2016
@@ -46,13 +46,22 @@ class BranchRelaxation : public MachineF
 
     BasicBlockInfo() : Offset(0), Size(0) {}
 
-    /// Compute the offset immediately following this block.  If LogAlign is
-    /// specified, return the offset the successor block will get if it has
-    /// this alignment.
-    unsigned postOffset(unsigned LogAlign = 0) const {
+    /// Compute the offset immediately following this block. \p MBB is the next
+    /// block.
+    unsigned postOffset(const MachineBasicBlock &MBB) const {
       unsigned PO = Offset + Size;
-      unsigned Align = 1 << LogAlign;
-      return (PO + Align - 1) / Align * Align;
+      unsigned Align = MBB.getAlignment();
+      if (Align == 0)
+        return PO;
+
+      unsigned AlignAmt = 1 << Align;
+      unsigned ParentAlign = MBB.getParent()->getAlignment();
+      if (Align <= ParentAlign)
+        return PO + OffsetToAlignment(PO, AlignAmt);
+
+      // The alignment of this MBB is larger than the function's alignment, so we
+      // can't tell whether or not it will insert nops. Assume that it will.
+      return PO + AlignAmt + OffsetToAlignment(PO, AlignAmt);
     }
   };
 
@@ -99,7 +108,7 @@ void BranchRelaxation::verify() {
     unsigned Align = MBB.getAlignment();
     unsigned Num = MBB.getNumber();
     assert(BlockInfo[Num].Offset % (1u << Align) == 0);
-    assert(!Num || BlockInfo[PrevNum].postOffset() <= BlockInfo[Num].Offset);
+    assert(!Num || BlockInfo[PrevNum].postOffset(MBB) <= BlockInfo[Num].Offset);
     PrevNum = Num;
   }
 #endif
@@ -167,8 +176,8 @@ void BranchRelaxation::adjustBlockOffset
       continue;
     // Get the offset and known bits at the end of the layout predecessor.
     // Include the alignment of the current block.
-    unsigned LogAlign = MBB.getAlignment();
-    BlockInfo[Num].Offset = BlockInfo[PrevNum].postOffset(LogAlign);
+    BlockInfo[Num].Offset = BlockInfo[PrevNum].postOffset(MBB);
+
     PrevNum = Num;
   }
 }

Added: llvm/trunk/test/CodeGen/AArch64/branch-relax-alignment.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/AArch64/branch-relax-alignment.ll?rev=283462&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/AArch64/branch-relax-alignment.ll (added)
+++ llvm/trunk/test/CodeGen/AArch64/branch-relax-alignment.ll Thu Oct  6 11:00:58 2016
@@ -0,0 +1,29 @@
+; RUN: llc -mtriple=aarch64-apple-darwin -aarch64-bcc-offset-bits=4 -align-all-nofallthru-blocks=4 < %s | FileCheck %s
+
+; Long branch is assumed because the block has a higher alignment
+; requirement than the function.
+
+; CHECK-LABEL: invert_bcc_block_align_higher_func:
+; CHECK: b.eq [[JUMP_BB1:LBB[0-9]+_[0-9]+]]
+; CHECK-NEXT: b [[JUMP_BB2:LBB[0-9]+_[0-9]+]]
+
+; CHECK: [[JUMP_BB1]]:
+; CHECK: ret
+; CHECK: .p2align 4
+
+; CHECK: [[JUMP_BB2]]:
+; CHECK: ret
+define i32 @invert_bcc_block_align_higher_func(i32 %x, i32 %y) align 4 #0 {
+  %1 = icmp eq i32 %x, %y
+  br i1 %1, label %bb1, label %bb2
+
+bb2:
+  store volatile i32 9, i32* undef
+  ret i32 1
+
+bb1:
+  store volatile i32 42, i32* undef
+  ret i32 0
+}
+
+attributes #0 = { nounwind }
\ No newline at end of file




More information about the llvm-commits mailing list