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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 10 00:10:50 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();
----------------
nikic wrote:

I looked through the history, and there was some discussion on https://reviews.llvm.org/D99135. Basically it got caught up in the "allocas can be freed by lifetime.end" discussion. But then getPointerDereferenceableBytes() overwrites the decision anyway...

I think it would make sense to change canBeFreed() to handle allocas. The lifetime.end problem is (as far as I know) only relevant for a single transform (LICM scalar promotion with store speculation), and we'll either mitigate it there, or fix this as part of a full redesign of how alloca lifetimes are represented.

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


More information about the llvm-commits mailing list