Skip to content

Commit 7e90465

Browse files
committed
[SimplifyCFG] Switch to use paramHasNonNullAttr
1 parent 5c3c0a8 commit 7e90465

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,7 +1823,11 @@ class CallBase : public Instruction {
18231823
/// Extract the number of dereferenceable bytes for a call or
18241824
/// parameter (0=unknown).
18251825
uint64_t getParamDereferenceableBytes(unsigned i) const {
1826-
return Attrs.getParamDereferenceableBytes(i);
1826+
uint64_t Bytes = Attrs.getParamDereferenceableBytes(i);
1827+
if (const Function *F = getCalledFunction())
1828+
Bytes =
1829+
std::max(Bytes, F->getAttributes().getParamDereferenceableBytes(i));
1830+
return Bytes;
18271831
}
18281832

18291833
/// Extract the number of dereferenceable_or_null bytes for a call
@@ -1841,7 +1845,11 @@ class CallBase : public Instruction {
18411845
/// Extract the number of dereferenceable_or_null bytes for a
18421846
/// parameter (0=unknown).
18431847
uint64_t getParamDereferenceableOrNullBytes(unsigned i) const {
1844-
return Attrs.getParamDereferenceableOrNullBytes(i);
1848+
uint64_t Bytes = Attrs.getParamDereferenceableOrNullBytes(i);
1849+
if (const Function *F = getCalledFunction())
1850+
Bytes = std::max(
1851+
Bytes, F->getAttributes().getParamDereferenceableOrNullBytes(i));
1852+
return Bytes;
18451853
}
18461854

18471855
/// Extract a test mask for disallowed floating-point value classes for the

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8276,15 +8276,17 @@ static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I, bool PtrValu
82768276
return true;
82778277

82788278
if (C->isNullValue()) {
8279-
for (const llvm::Use &Arg : CB->args())
8280-
if (Arg == I) {
8281-
unsigned ArgIdx = CB->getArgOperandNo(&Arg);
8282-
if (CB->isPassingUndefUB(ArgIdx) &&
8283-
CB->paramHasAttr(ArgIdx, Attribute::NonNull)) {
8284-
// Passing null to a nonnnull+noundef argument is undefined.
8285-
return !PtrValueMayBeModified;
8279+
if (C->getType()->isPointerTy()) {
8280+
for (const llvm::Use &Arg : CB->args())
8281+
if (Arg == I) {
8282+
unsigned ArgIdx = CB->getArgOperandNo(&Arg);
8283+
if (CB->paramHasNonNullAttr(ArgIdx,
8284+
/*AllowUndefOrPoison=*/false)) {
8285+
// Passing null to a nonnnull+noundef argument is undefined.
8286+
return !PtrValueMayBeModified;
8287+
}
82868288
}
8287-
}
8289+
}
82888290
} else if (isa<UndefValue>(C)) {
82898291
// Passing undef to a noundef argument is undefined.
82908292
for (const llvm::Use &Arg : CB->args())

llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ else:
238238
}
239239

240240
declare ptr @fn_nonnull_noundef_arg(ptr nonnull noundef %p)
241-
declare ptr @fn_nonnull_deref_arg(ptr nonnull dereferenceable(4) %p)
241+
declare ptr @fn_deref_arg(ptr dereferenceable(4) %p)
242242
declare ptr @fn_nonnull_deref_or_null_arg(ptr nonnull dereferenceable_or_null(4) %p)
243243
declare ptr @fn_nonnull_arg(ptr nonnull %p)
244244
declare ptr @fn_noundef_arg(ptr noundef %p)
@@ -271,7 +271,7 @@ define void @test9_deref(i1 %X, ptr %Y) {
271271
; CHECK-NEXT: entry:
272272
; CHECK-NEXT: [[TMP0:%.*]] = xor i1 [[X:%.*]], true
273273
; CHECK-NEXT: call void @llvm.assume(i1 [[TMP0]])
274-
; CHECK-NEXT: [[TMP1:%.*]] = call ptr @fn_nonnull_deref_arg(ptr [[Y:%.*]])
274+
; CHECK-NEXT: [[TMP1:%.*]] = call ptr @fn_deref_arg(ptr [[Y:%.*]])
275275
; CHECK-NEXT: ret void
276276
;
277277
entry:
@@ -282,17 +282,16 @@ if:
282282

283283
else:
284284
%phi = phi ptr [ %Y, %entry ], [ null, %if ]
285-
call ptr @fn_nonnull_deref_arg(ptr %phi)
285+
call ptr @fn_deref_arg(ptr %phi)
286286
ret void
287287
}
288288

289289
; Optimizing this code should produce assume.
290290
define void @test9_deref_or_null(i1 %X, ptr %Y) {
291291
; CHECK-LABEL: @test9_deref_or_null(
292292
; CHECK-NEXT: entry:
293-
; CHECK-NEXT: [[TMP0:%.*]] = xor i1 [[X:%.*]], true
294-
; CHECK-NEXT: call void @llvm.assume(i1 [[TMP0]])
295-
; CHECK-NEXT: [[TMP1:%.*]] = call ptr @fn_nonnull_deref_or_null_arg(ptr [[Y:%.*]])
293+
; CHECK-NEXT: [[Y:%.*]] = select i1 [[X:%.*]], ptr null, ptr [[Y1:%.*]]
294+
; CHECK-NEXT: [[TMP1:%.*]] = call ptr @fn_nonnull_deref_or_null_arg(ptr [[Y]])
296295
; CHECK-NEXT: ret void
297296
;
298297
entry:

0 commit comments

Comments
 (0)