Skip to content

Commit b4cac6a

Browse files
committed
[IR] Add a helper Function::isReturnNonNull
1 parent 1b610e6 commit b4cac6a

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

llvm/include/llvm/IR/Function.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,11 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
731731
/// create a Function) from the Function Src to this one.
732732
void copyAttributesFrom(const Function *Src);
733733

734+
/// Return true if the return value is known to be not null.
735+
/// This may be because it has the nonnull attribute, or because at least
736+
/// one byte is dereferenceable and the pointer is in addrspace(0).
737+
bool isReturnNonNull() const;
738+
734739
/// deleteBody - This method deletes the body of the function, and converts
735740
/// the linkage to external.
736741
///

llvm/lib/IR/Function.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,17 @@ void Function::copyAttributesFrom(const Function *Src) {
873873
setPrologueData(Src->getPrologueData());
874874
}
875875

876+
bool Function::isReturnNonNull() const {
877+
if (hasRetAttribute(Attribute::NonNull))
878+
return true;
879+
880+
if (AttributeSets.getRetDereferenceableBytes() > 0 &&
881+
!NullPointerIsDefined(this, getReturnType()->getPointerAddressSpace()))
882+
return true;
883+
884+
return false;
885+
}
886+
876887
MemoryEffects Function::getMemoryEffects() const {
877888
return getAttributes().getMemoryEffects();
878889
}

0 commit comments

Comments
 (0)