[llvm] [SimplifyCFG] Enable sinking stores and loads with differing alignments (PR #215858)

Sayan Sivakumaran via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 12 11:13:58 PDT 2026


https://github.com/sivakusayan created https://github.com/llvm/llvm-project/pull/215858

Rough draft, still need to add pre-commit tests and think about this change for a bit.

>From 283c4d66f9cbe783e7f6a2359990c21a334dff30 Mon Sep 17 00:00:00 2001
From: Sayan Sivakumaran <sivakusayan at gmail.com>
Date: Wed, 12 Aug 2026 13:12:12 -0500
Subject: [PATCH] [SimplifyCFG] Enable sinking stores and loads with differing
 alignments

---
 llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 089892dc573f3..5d37e9003f46c 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2248,7 +2248,8 @@ static bool canSinkInstructions(
   const Instruction *I0 = Insts.front();
   const auto I0MMRA = MMRAMetadata(*I0);
   for (auto *I : Insts) {
-    if (!I->isSameOperationAs(I0, Instruction::CompareUsingIntersectedAttrs))
+    if (!I->isSameOperationAs(I0, Instruction::CompareUsingIntersectedAttrs |
+                                      Instruction::CompareIgnoringAlignment))
       return false;
 
     // Treat MMRAs conservatively. This pass can be quite aggressive and
@@ -2380,13 +2381,25 @@ static void sinkLastInstruction(ArrayRef<BasicBlock*> Blocks) {
       I0->applyMergedLocation(I0->getDebugLoc(), I->getDebugLoc());
       combineMetadataForCSE(I0, I, true);
       I0->andIRFlags(I);
-      if (auto *CB = dyn_cast<CallBase>(I0)) {
+
+      switch (I0->getOpcode()) {
+      case Instruction::Call:
+      case Instruction::Invoke:
+      case Instruction::CallBr: {
+        auto *CB = cast<CallBase>(I0);
         bool Success = CB->tryIntersectAttributes(cast<CallBase>(I));
         assert(Success && "We should not be trying to sink callbases "
                           "with non-intersectable attributes");
         // For NDEBUG Compile.
         (void)Success;
+        break;
       }
+      case Instruction::Store:
+      case Instruction::Load:
+        setLoadStoreAlignment(
+            I0, std::min(getLoadStoreAlignment(I0), getLoadStoreAlignment(I)));
+        break;
+      };
     }
 
   for (User *U : make_early_inc_range(I0->users())) {



More information about the llvm-commits mailing list