[llvm] [IR] Add a helper `Function::isReturnNonNull` (PR #128107)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 20 18:07:17 PST 2025
https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/128107
The code is copied from `CallBase::isReturnNonNull`. It is required by the follow-up of https://github.com/llvm/llvm-project/pull/127979.
>From b4cac6ad69dbe66bc86b7e0a9cd6eea7c62c161f Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Fri, 21 Feb 2025 09:40:09 +0800
Subject: [PATCH] [IR] Add a helper `Function::isReturnNonNull`
---
llvm/include/llvm/IR/Function.h | 5 +++++
llvm/lib/IR/Function.cpp | 11 +++++++++++
2 files changed, 16 insertions(+)
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();
}
More information about the llvm-commits
mailing list