[llvm] [IPSCCP] Infer attributes on arguments (PR #107114)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 9 01:41:32 PDT 2024


================
@@ -354,31 +354,45 @@ bool SCCPSolver::removeNonFeasibleEdges(BasicBlock *BB, DomTreeUpdater &DTU,
   return true;
 }
 
+static void inferAttribute(Function *F, unsigned AttrIndex,
+                           const ValueLatticeElement &Val) {
+  // If there is a known constant range for the value, add range attribute.
+  if (Val.isConstantRange() && !Val.getConstantRange().isSingleElement()) {
+    // Do not add range attribute if the value may include undef.
+    if (Val.isConstantRangeIncludingUndef())
+      return;
+
+    // Take the intersection of the existing attribute and the inferred range.
+    Attribute OldAttr = F->getAttributeAtIndex(AttrIndex, Attribute::Range);
+    ConstantRange CR = Val.getConstantRange();
+    if (OldAttr.isValid())
+      CR = CR.intersectWith(OldAttr.getRange());
+    F->addAttributeAtIndex(
+        AttrIndex, Attribute::get(F->getContext(), Attribute::Range, CR));
+    return;
+  }
+  // Infer nonnull attribute.
+  if (Val.isNotConstant() && Val.getNotConstant()->getType()->isPointerTy() &&
+      Val.getNotConstant()->isNullValue() &&
+      !F->getAttributeAtIndex(AttrIndex, Attribute::NonNull).isValid()) {
+    F->addAttributeAtIndex(AttrIndex,
----------------
nikic wrote:

I've added a test where nonnull is added to an argument.

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


More information about the llvm-commits mailing list