[llvm] c9f6a5e - [MC] Move computeBundlePadding closer to its only caller. NFC

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 22 13:28:12 PDT 2024


Author: Fangrui Song
Date: 2024-06-22T13:28:07-07:00
New Revision: c9f6a5e49519f860f20f808b1af84dc3fc50ff91

URL: https://github.com/llvm/llvm-project/commit/c9f6a5e49519f860f20f808b1af84dc3fc50ff91
DIFF: https://github.com/llvm/llvm-project/commit/c9f6a5e49519f860f20f808b1af84dc3fc50ff91.diff

LOG: [MC] Move computeBundlePadding closer to its only caller. NFC

There is only one caller after #95188.

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCAssembler.h
    llvm/lib/MC/MCAssembler.cpp
    llvm/lib/MC/MCFragment.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCAssembler.h b/llvm/include/llvm/MC/MCAssembler.h
index 914c7506e754b..5cf6459ca4052 100644
--- a/llvm/include/llvm/MC/MCAssembler.h
+++ b/llvm/include/llvm/MC/MCAssembler.h
@@ -500,13 +500,6 @@ class MCAssembler {
   void dump() const;
 };
 
-/// Compute the amount of padding required before the fragment \p F to
-/// obey bundling restrictions, where \p FOffset is the fragment's offset in
-/// its section and \p FSize is the fragment's size.
-uint64_t computeBundlePadding(const MCAssembler &Assembler,
-                              const MCEncodedFragment *F, uint64_t FOffset,
-                              uint64_t FSize);
-
 } // end namespace llvm
 
 #endif // LLVM_MC_MCASSEMBLER_H

diff  --git a/llvm/lib/MC/MCAssembler.cpp b/llvm/lib/MC/MCAssembler.cpp
index 671f6f71b3a4f..fe0419b812089 100644
--- a/llvm/lib/MC/MCAssembler.cpp
+++ b/llvm/lib/MC/MCAssembler.cpp
@@ -403,6 +403,46 @@ uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout,
   llvm_unreachable("invalid fragment kind");
 }
 
+// Compute the amount of padding required before the fragment \p F to
+// obey bundling restrictions, where \p FOffset is the fragment's offset in
+// its section and \p FSize is the fragment's size.
+static uint64_t computeBundlePadding(unsigned BundleSize,
+                                     const MCEncodedFragment *F,
+                                     uint64_t FOffset, uint64_t FSize) {
+  uint64_t OffsetInBundle = FOffset & (BundleSize - 1);
+  uint64_t EndOfFragment = OffsetInBundle + FSize;
+
+  // There are two kinds of bundling restrictions:
+  //
+  // 1) For alignToBundleEnd(), add padding to ensure that the fragment will
+  //    *end* on a bundle boundary.
+  // 2) Otherwise, check if the fragment would cross a bundle boundary. If it
+  //    would, add padding until the end of the bundle so that the fragment
+  //    will start in a new one.
+  if (F->alignToBundleEnd()) {
+    // Three possibilities here:
+    //
+    // A) The fragment just happens to end at a bundle boundary, so we're good.
+    // B) The fragment ends before the current bundle boundary: pad it just
+    //    enough to reach the boundary.
+    // C) The fragment ends after the current bundle boundary: pad it until it
+    //    reaches the end of the next bundle boundary.
+    //
+    // Note: this code could be made shorter with some modulo trickery, but it's
+    // intentionally kept in its more explicit form for simplicity.
+    if (EndOfFragment == BundleSize)
+      return 0;
+    else if (EndOfFragment < BundleSize)
+      return BundleSize - EndOfFragment;
+    else { // EndOfFragment > BundleSize
+      return 2 * BundleSize - EndOfFragment;
+    }
+  } else if (OffsetInBundle > 0 && EndOfFragment > BundleSize)
+    return BundleSize - OffsetInBundle;
+  else
+    return 0;
+}
+
 void MCAsmLayout::layoutBundle(MCFragment *Prev, MCFragment *F) {
   // If bundling is enabled and this fragment has instructions in it, it has to
   // obey the bundling restrictions. With padding, we'll have:
@@ -433,8 +473,8 @@ void MCAsmLayout::layoutBundle(MCFragment *Prev, MCFragment *F) {
   if (FSize > Assembler.getBundleAlignSize())
     report_fatal_error("Fragment can't be larger than a bundle size");
 
-  uint64_t RequiredBundlePadding =
-      computeBundlePadding(Assembler, EF, EF->Offset, FSize);
+  uint64_t RequiredBundlePadding = computeBundlePadding(
+      Assembler.getBundleAlignSize(), EF, EF->Offset, FSize);
   if (RequiredBundlePadding > UINT8_MAX)
     report_fatal_error("Padding cannot exceed 255 bytes");
   EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));

diff  --git a/llvm/lib/MC/MCFragment.cpp b/llvm/lib/MC/MCFragment.cpp
index b1ed594459da1..9e9a03af945dc 100644
--- a/llvm/lib/MC/MCFragment.cpp
+++ b/llvm/lib/MC/MCFragment.cpp
@@ -155,47 +155,6 @@ uint64_t MCAsmLayout::getSectionFileSize(const MCSection *Sec) const {
   return getSectionAddressSize(Sec);
 }
 
-uint64_t llvm::computeBundlePadding(const MCAssembler &Assembler,
-                                    const MCEncodedFragment *F,
-                                    uint64_t FOffset, uint64_t FSize) {
-  uint64_t BundleSize = Assembler.getBundleAlignSize();
-  assert(BundleSize > 0 &&
-         "computeBundlePadding should only be called if bundling is enabled");
-  uint64_t BundleMask = BundleSize - 1;
-  uint64_t OffsetInBundle = FOffset & BundleMask;
-  uint64_t EndOfFragment = OffsetInBundle + FSize;
-
-  // There are two kinds of bundling restrictions:
-  //
-  // 1) For alignToBundleEnd(), add padding to ensure that the fragment will
-  //    *end* on a bundle boundary.
-  // 2) Otherwise, check if the fragment would cross a bundle boundary. If it
-  //    would, add padding until the end of the bundle so that the fragment
-  //    will start in a new one.
-  if (F->alignToBundleEnd()) {
-    // Three possibilities here:
-    //
-    // A) The fragment just happens to end at a bundle boundary, so we're good.
-    // B) The fragment ends before the current bundle boundary: pad it just
-    //    enough to reach the boundary.
-    // C) The fragment ends after the current bundle boundary: pad it until it
-    //    reaches the end of the next bundle boundary.
-    //
-    // Note: this code could be made shorter with some modulo trickery, but it's
-    // intentionally kept in its more explicit form for simplicity.
-    if (EndOfFragment == BundleSize)
-      return 0;
-    else if (EndOfFragment < BundleSize)
-      return BundleSize - EndOfFragment;
-    else { // EndOfFragment > BundleSize
-      return 2 * BundleSize - EndOfFragment;
-    }
-  } else if (OffsetInBundle > 0 && EndOfFragment > BundleSize)
-    return BundleSize - OffsetInBundle;
-  else
-    return 0;
-}
-
 /* *** */
 
 MCFragment::MCFragment(FragmentType Kind, bool HasInstructions)


        


More information about the llvm-commits mailing list