[CodeGen] -fno-delete-null-pointer-checks: change dereferenceable to dereferenceable_or_null

After D17993, with -fno-delete-null-pointer-checks we add the dereferenceable attribute to the `this` pointer.

We have observed that one internal target which worked before fails even with -fno-delete-null-pointer-checks.
Switching to dereferenceable_or_null fixes the problem.

dereferenceable currently does not always respect NullPointerIsValid and may
imply nonnull and lead to aggressive optimization. The optimization may be
related to `CallBase::isReturnNonNull`, `Argument::hasNonNullAttr`, or
`Value::getPointerDereferenceableBytes`. See D66664 and D66618 for some discussions.

Reviewed By: bkramer, rsmith

Differential Revision: https://reviews.llvm.org/D92297
This commit is contained in:
Fangrui Song 2020-11-30 12:44:35 -08:00
parent 66124098a8
commit 164410324d
2 changed files with 16 additions and 7 deletions

View File

@ -2168,13 +2168,21 @@ void CodeGenModule::ConstructAttributeList(
if (!CodeGenOpts.NullPointerIsValid &&
getContext().getTargetAddressSpace(FI.arg_begin()->type) == 0) {
Attrs.addAttribute(llvm::Attribute::NonNull);
Attrs.addDereferenceableAttr(
getMinimumObjectSize(
FI.arg_begin()->type.castAs<PointerType>()->getPointeeType())
.getQuantity());
} else {
// FIXME dereferenceable should be correct here, regardless of
// NullPointerIsValid. However, dereferenceable currently does not always
// respect NullPointerIsValid and may imply nonnull and break the program.
// See https://reviews.llvm.org/D66618 for discussions.
Attrs.addDereferenceableOrNullAttr(
getMinimumObjectSize(
FI.arg_begin()->type.castAs<PointerType>()->getPointeeType())
.getQuantity());
}
Attrs.addDereferenceableAttr(
getMinimumObjectSize(
FI.arg_begin()->type.castAs<PointerType>()->getPointeeType())
.getQuantity());
ArgAttrs[IRArgs.first] = llvm::AttributeSet::get(getLLVMContext(), Attrs);
}

View File

@ -12,8 +12,9 @@ void TestReturnsVoid(Struct &s) {
s.ReturnsVoid();
// CHECK-YES: call void @_ZN6Struct11ReturnsVoidEv(%struct.Struct* nonnull dereferenceable(12) %0)
// CHECK-NO: call void @_ZN6Struct11ReturnsVoidEv(%struct.Struct* dereferenceable(12) %0)
/// FIXME Use dereferenceable after dereferenceable respects NullPointerIsValid.
// CHECK-NO: call void @_ZN6Struct11ReturnsVoidEv(%struct.Struct* dereferenceable_or_null(12) %0)
}
// CHECK-YES: declare void @_ZN6Struct11ReturnsVoidEv(%struct.Struct* nonnull dereferenceable(12))
// CHECK-NO: declare void @_ZN6Struct11ReturnsVoidEv(%struct.Struct* dereferenceable(12))
// CHECK-NO: declare void @_ZN6Struct11ReturnsVoidEv(%struct.Struct* dereferenceable_or_null(12))