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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 10 07:28:42 PST 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);
+        unsigned TZ = std::min(Known.countMinTrailingZeros(), 63u);
+        if ((1ULL << TZ) < RK.ArgValue)
+          continue;
+        auto *New = CallBase::removeOperandBundle(II, OBU.getTagID());
----------------
nikic wrote:

Unnecessary variable.

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


More information about the llvm-commits mailing list