[llvm] [LVI] Handle nonnull attributes at callsite (PR #125377)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 2 03:07:17 PST 2025


================
@@ -644,6 +644,13 @@ static void AddNonNullPointersByInstruction(
     AddNonNullPointer(MI->getRawDest(), PtrSet);
     if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
       AddNonNullPointer(MTI->getRawSource(), PtrSet);
+  } else if (auto *CB = dyn_cast<CallBase>(I)) {
+    for (auto &U : CB->args()) {
+      if (U->getType()->isPointerTy() &&
+          CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
+                                  /*AllowUndefOrPoison=*/false))
+        AddNonNullPointer(U.get(), PtrSet);
----------------
nikic wrote:

The nonnull annotation case is a bit different from the other ones. The other ones have an actual dereference, which implies that the underlying object must have non-nullary provenance and thus must not be null (assuming no allocation at null). For nonnull, we don't have a dereference, so we don't know that the underlying object is non-null.

So we should use a variant of AddNonNullPointer here that uses stripInBoundsOffsets instead of getUnderlyingObject. We *do* know that gep inbounds can not turns a null pointer into a non-null pointer or vice versa.

Can you add a test like `call(gep p, x), icmp(p, null)` with and without inbounds?

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


More information about the llvm-commits mailing list