[llvm] [InstCombine] Fold align assume into load's !align metadata if possible. (PR #108958)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 17 05:17:41 PDT 2024


================
@@ -3095,6 +3096,23 @@ 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.
+      if (OBU.getTagName() == "align" && OBU.Inputs.size() == 2) {
+        auto *LI = dyn_cast<LoadInst>(OBU.Inputs[0]);
+        if (!LI ||
+            !isValidAssumeForContext(II, LI, &DT, /*AllowEphemerals=*/true))
+          continue;
+        auto *Align = dyn_cast<ConstantInt>(OBU.Inputs[1]);
+        if (!Align || !isPowerOf2_64(Align->getZExtValue()) || Align->getType()->getScalarSizeInBits() != 64)
+          continue;
+        LI->setMetadata(
+            LLVMContext::MD_align,
+            MDNode::get(II->getContext(), ValueAsMetadata::getConstant(Align)));
----------------
nikic wrote:

Should also add noundef to preserve the fact this is IUB.

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


More information about the llvm-commits mailing list