Skip to content

[Mem2Reg] Generate non-terminator unreachable for !noundef undef #96639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,15 @@ static void addAssumeNonNull(AssumptionCache *AC, LoadInst *LI) {
static void convertMetadataToAssumes(LoadInst *LI, Value *Val,
const DataLayout &DL, AssumptionCache *AC,
const DominatorTree *DT) {
if (isa<UndefValue>(Val) && LI->hasMetadata(LLVMContext::MD_noundef)) {
// Insert non-terminator unreachable.
LLVMContext &Ctx = LI->getContext();
new StoreInst(ConstantInt::getTrue(Ctx),
PoisonValue::get(PointerType::getUnqual(Ctx)),
/*isVolatile=*/false, Align(1), LI);
return;
}

// If the load was marked as nonnull we don't want to lose that information
// when we erase this Load. So we preserve it with an assume. As !nonnull
// returns poison while assume violations are immediate undefined behavior,
Expand Down
12 changes: 6 additions & 6 deletions llvm/test/Transforms/Mem2Reg/preserve-nonnull-load-metadata.ll
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ fin:
define ptr @no_store_single_load_noundef() {
; CHECK-LABEL: @no_store_single_load_noundef(
; CHECK-NEXT: entry:
; CHECK-NEXT: store i1 true, ptr poison, align 1
; CHECK-NEXT: ret ptr undef
;
entry:
Expand All @@ -156,8 +157,10 @@ define ptr @no_store_multiple_loads_noundef(i1 %c) {
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
; CHECK: if:
; CHECK-NEXT: store i1 true, ptr poison, align 1
; CHECK-NEXT: ret ptr undef
; CHECK: else:
; CHECK-NEXT: store i1 true, ptr poison, align 1
; CHECK-NEXT: ret ptr undef
;
entry:
Expand All @@ -176,8 +179,7 @@ if:
define ptr @no_store_single_load_nonnull_noundef() {
; CHECK-LABEL: @no_store_single_load_nonnull_noundef(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TMP0:%.*]] = icmp ne ptr undef, null
; CHECK-NEXT: call void @llvm.assume(i1 [[TMP0]])
; CHECK-NEXT: store i1 true, ptr poison, align 1
; CHECK-NEXT: ret ptr undef
;
entry:
Expand All @@ -191,12 +193,10 @@ define ptr @no_store_multiple_loads_nonnull_noundef(i1 %c) {
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
; CHECK: if:
; CHECK-NEXT: [[TMP0:%.*]] = icmp ne ptr undef, null
; CHECK-NEXT: call void @llvm.assume(i1 [[TMP0]])
; CHECK-NEXT: store i1 true, ptr poison, align 1
; CHECK-NEXT: ret ptr undef
; CHECK: else:
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne ptr undef, null
; CHECK-NEXT: call void @llvm.assume(i1 [[TMP1]])
; CHECK-NEXT: store i1 true, ptr poison, align 1
; CHECK-NEXT: ret ptr undef
;
entry:
Expand Down
Loading