[llvm] [IR] Avoid unnecessary canBeFreed() calls (NFC) (PR #202685)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 9 09:08:28 PDT 2026


================
@@ -955,17 +955,25 @@ uint64_t Value::getPointerDereferenceableBytes(const DataLayout &DL,
     if (std::optional<TypeSize> Size = AI->getAllocationSize(DL)) {
       DerefBytes = Size->getKnownMinValue();
       CanBeNull = false;
-      CanBeFreed = false;
+      CanNotBeFreed = true;
     }
   } else if (auto *GV = dyn_cast<GlobalVariable>(this)) {
     if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) {
       // TODO: Don't outright reject hasExternalWeakLinkage but set the
       // CanBeNull flag.
       DerefBytes = DL.getTypeStoreSize(GV->getValueType()).getFixedValue();
       CanBeNull = false;
-      CanBeFreed = false;
+      CanNotBeFreed = true;
     }
   }
+
+  // Call canBeFreed() only if there are dereferenceable bytes and it's not
+  // one of the cases that can never be freed.
+  if (!CanNotBeFreed && DerefBytes != 0)
+    CanBeFreed = UseDerefAtPointSemantics && canBeFreed();
----------------
dtcxzyw wrote:

What is the reason that `canBeFreed` still returns true for `AllocaInst`?
BTW, I saw a hardcoded comparison with `statepoint-example` in `canBeFreed`. Do we have a better abstraction for this?


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


More information about the llvm-commits mailing list