[InstCombine] Drop nonnull assumes if the pointer is already known to be nonnull (#180434)

This commit is contained in:
Nikolas Klauser 2026-02-09 13:13:32 +01:00 committed by GitHub
parent 4bb16b130d
commit 6dbdfd824a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 53 additions and 6 deletions

View File

@ -3671,6 +3671,16 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
continue;
return CallBase::removeOperandBundle(II, OBU.getTagID());
}
if (OBU.getTagName() == "nonnull" && OBU.Inputs.size() == 1) {
RetainedKnowledge RK = getKnowledgeFromOperandInAssume(
*cast<AssumeInst>(II), II->arg_size() + Idx);
if (!RK || RK.AttrKind != Attribute::NonNull ||
!isKnownNonZero(RK.WasOn,
getSimplifyQuery().getWithInstruction(II)))
continue;
return CallBase::removeOperandBundle(II, OBU.getTagID());
}
}
// Convert nonnull assume like:

View File

@ -1,8 +1,8 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=instcombine --debug-counter=assume-queries-counter=0 -S | FileCheck %s --check-prefixes=COUNTER1
; RUN: opt < %s -passes=instcombine --debug-counter=assume-queries-counter=1-2 -S | FileCheck %s --check-prefixes=COUNTER2
; RUN: opt < %s -passes=instcombine --debug-counter=assume-queries-counter=2-6 -S | FileCheck %s --check-prefixes=COUNTER3
; RUN: opt < %s -passes=instcombine --debug-counter=assume-queries-counter=1 -S | FileCheck %s --check-prefixes=COUNTER1
; RUN: opt < %s -passes=instcombine --debug-counter=assume-queries-counter=2-3 -S | FileCheck %s --check-prefixes=COUNTER2
; RUN: opt < %s -passes=instcombine --debug-counter=assume-queries-counter=4-7 -S | FileCheck %s --check-prefixes=COUNTER3
declare i1 @get_val()
declare void @llvm.assume(i1)
@ -38,8 +38,9 @@ define dso_local i1 @test2(ptr readonly %0) {
; COUNTER2-NEXT: ret i1 false
;
; COUNTER3-LABEL: @test2(
; COUNTER3-NEXT: call void @llvm.assume(i1 true) [ "nonnull"(ptr [[TMP0:%.*]]) ]
; COUNTER3-NEXT: ret i1 false
; COUNTER3-NEXT: [[TMP2:%.*]] = icmp eq ptr [[TMP0:%.*]], null
; COUNTER3-NEXT: call void @llvm.assume(i1 true) [ "nonnull"(ptr [[TMP0]]) ]
; COUNTER3-NEXT: ret i1 [[TMP2]]
;
%2 = icmp eq ptr %0, null
call void @llvm.assume(i1 true) ["nonnull"(ptr %0)]

View File

@ -419,6 +419,43 @@ define i1 @nonnull5(ptr %a) {
ret i1 %rval
}
define void @redundant_nonnull1(ptr nonnull %ptr) {
; CHECK-LABEL: @redundant_nonnull1(
; CHECK-NEXT: ret void
;
call void @llvm.assume(i1 true) [ "nonnull"(ptr %ptr) ]
ret void
}
define void @redundant_nonnull2(ptr %ptr) {
; CHECK-LABEL: @redundant_nonnull2(
; CHECK-NEXT: [[NULL_CMP_NOT:%.*]] = icmp eq ptr [[PTR:%.*]], null
; CHECK-NEXT: br i1 [[NULL_CMP_NOT]], label [[FALSE:%.*]], label [[TRUE:%.*]]
; CHECK: true:
; CHECK-NEXT: ret void
; CHECK: false:
; CHECK-NEXT: ret void
;
%cmp = icmp ne ptr %ptr, null
br i1 %cmp, label %true, label %false
true:
call void @llvm.assume(i1 true) [ "nonnull"(ptr %ptr) ]
ret void
false:
ret void
}
define void @redundant_nonnull3(ptr %ptr) {
; CHECK-LABEL: @redundant_nonnull3(
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "nonnull"(ptr [[PTR:%.*]]) ]
; CHECK-NEXT: ret void
;
call void @llvm.assume(i1 true) [ "nonnull"(ptr %ptr) ]
call void @llvm.assume(i1 true) [ "nonnull"(ptr %ptr) ]
ret void
}
; PR35846 - https://bugs.llvm.org/show_bug.cgi?id=35846
define i32 @assumption_conflicts_with_known_bits(i32 %a, i32 %b) {

View File

@ -13,7 +13,6 @@ define ptr @f1(ptr %i0) {
; CHECK-NEXT: br label %[[LOOP_LATCH:.*]]
; CHECK: [[LOOP_LATCH]]:
; CHECK-NEXT: [[I3:%.*]] = phi ptr [ [[I0]], %[[LOOP_LATCH_LR_PH]] ], [ [[I5:%.*]], %[[LOOP_LATCH]] ]
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "nonnull"(ptr [[I3]]) ]
; CHECK-NEXT: [[I5]] = load ptr, ptr [[I3]], align 8
; CHECK-NEXT: [[I2:%.*]] = icmp eq ptr [[I5]], null
; CHECK-NEXT: br i1 [[I2]], label %[[LOOP_EXIT_CRIT_EDGE:.*]], label %[[LOOP_LATCH]]