[llvm] [LoadStoreVectorizer] Propagate alignment through contiguous chain (PR #145733)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 23 01:40:36 PDT 2025


================
@@ -57,16 +57,54 @@ bool inferAlignment(Function &F, AssumptionCache &AC, DominatorTree &DT) {
     }
   }
 
-  // Compute alignment from known bits.
   for (BasicBlock &BB : F) {
+    // We need to reset the map for each block because alignment information
+    // can't be propagated across blocks. This is because control flow could
+    // be dependent on the address at runtime, making an alignment assumption
+    // within one block not true in another. Some sort of dominator tree
+    // approach could be better, but restricting within a basic block is correct
+    // too.
+    DenseMap<Value *, Align> BestBasePointerAligns;
+
     for (Instruction &I : BB) {
+      // Compute alignment from known bits.
       Changed |= tryToImproveAlign(
           DL, &I, [&](Value *PtrOp, Align OldAlign, Align PrefAlign) {
             KnownBits Known = computeKnownBits(PtrOp, DL, &AC, &I, &DT);
             unsigned TrailZ = std::min(Known.countMinTrailingZeros(),
                                        +Value::MaxAlignmentExponent);
             return Align(1ull << std::min(Known.getBitWidth() - 1, TrailZ));
           });
+
+      // Propagate alignment between loads and stores that originate from the
+      // same base pointer.
+      Changed |= tryToImproveAlign(
+          DL, &I, [&](Value *PtrOp, Align LoadStoreAlign, Align PrefAlign) {
----------------
nikic wrote:

Backwards propagation is more complicated because it has to handle implicit control flow. I doubt that doing this would be worthwhile in practice.

https://github.com/llvm/llvm-project/pull/145733


More information about the llvm-commits mailing list