[llvm] [IR] Add a helper `Function::isReturnNonNull` (PR #128107)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 20 18:07:51 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-ir
Author: Yingwei Zheng (dtcxzyw)
<details>
<summary>Changes</summary>
The code is copied from `CallBase::isReturnNonNull`. It is required by the follow-up of https://github.com/llvm/llvm-project/pull/127979.
---
Full diff: https://github.com/llvm/llvm-project/pull/128107.diff
2 Files Affected:
- (modified) llvm/include/llvm/IR/Function.h (+5)
- (modified) llvm/lib/IR/Function.cpp (+11)
``````````diff
diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h
index 29041688124bc..7ea8673bedad1 100644
--- a/llvm/include/llvm/IR/Function.h
+++ b/llvm/include/llvm/IR/Function.h
@@ -731,6 +731,11 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
/// create a Function) from the Function Src to this one.
void copyAttributesFrom(const Function *Src);
+ /// Return true if the return value is known to be not null.
+ /// This may be because it has the nonnull attribute, or because at least
+ /// one byte is dereferenceable and the pointer is in addrspace(0).
+ bool isReturnNonNull() const;
+
/// deleteBody - This method deletes the body of the function, and converts
/// the linkage to external.
///
diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp
index 5666f0a53866f..d22cf65769e26 100644
--- a/llvm/lib/IR/Function.cpp
+++ b/llvm/lib/IR/Function.cpp
@@ -873,6 +873,17 @@ void Function::copyAttributesFrom(const Function *Src) {
setPrologueData(Src->getPrologueData());
}
+bool Function::isReturnNonNull() const {
+ if (hasRetAttribute(Attribute::NonNull))
+ return true;
+
+ if (AttributeSets.getRetDereferenceableBytes() > 0 &&
+ !NullPointerIsDefined(this, getReturnType()->getPointerAddressSpace()))
+ return true;
+
+ return false;
+}
+
MemoryEffects Function::getMemoryEffects() const {
return getAttributes().getMemoryEffects();
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/128107
More information about the llvm-commits
mailing list