[llvm] [InstCombine] Remove redundant alignment assumptions. (PR #123348)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 1 09:39:41 PDT 2025


================
@@ -3245,6 +3246,30 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
         MaybeSimplifyHint(OBU.Inputs[0]);
         MaybeSimplifyHint(OBU.Inputs[1]);
       }
+
+      // Try to fold alignment assumption into a load's !align metadata, if the
+      // assumption is valid in the load's context and remove redundant ones.
+      if (OBU.getTagName() == "align" && OBU.Inputs.size() == 2) {
+        RetainedKnowledge RK = getKnowledgeFromBundle(
+            *cast<AssumeInst>(II), II->bundle_op_info_begin()[Idx]);
+        if (!RK || RK.AttrKind != Attribute::Alignment ||
+            !isPowerOf2_64(RK.ArgValue))
+          continue;
+
+        // Don't try to remove align assumptions for pointers derived from
+        // arguments. We might lose information if the function gets inline and
+        // the align argument attribute disappears.
+        Value *UO = getUnderlyingObject(RK.WasOn);
+        if (!UO || isa<Argument>(UO))
+          continue;
+
+        KnownBits Known = computeKnownBits(RK.WasOn, 0, nullptr);
----------------
fhahn wrote:

Added a comment (we don't want to use the assumption we are about to remove).

> Though I think we'd be better off adding an AllowEphemeralValues option to SimplifyQuery and use it as appropriate. We can safely set AllowEphemeralValues=true when inferring alignment and can drop the current special case for align operand bundles.
Can be done as separate improvement IIUC, assuming you mean the special case below?

```
         // Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might                                    <<<
        // be the producer of the pointer in the bundle. At the moment, align
        // assumptions aren't optimized away.
        if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
            isPowerOf2_64(RK.ArgValue) &&
            isValidAssumeForContext(I, Q.CxtI, Q.DT, /*AllowEphemerals*/ true))
          Known.Zero.setLowBits(Log2_64(RK.ArgValue));
```



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


More information about the llvm-commits mailing list