Skip to content

[NewGVN] Fix lifetime coercion #141477

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
May 26, 2025
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
19 changes: 11 additions & 8 deletions llvm/lib/Transforms/Scalar/NewGVN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1529,23 +1529,26 @@ NewGVN::performSymbolicLoadCoercion(Type *LoadType, Value *LoadPtr,
}
}

if (auto *II = dyn_cast<IntrinsicInst>(DepInst)) {
auto *LifetimePtr = II->getOperand(1);
if (II->getIntrinsicID() == Intrinsic::lifetime_start &&
(LoadPtr == lookupOperandLeader(LifetimePtr) ||
AA->isMustAlias(LoadPtr, LifetimePtr)))
return createConstantExpression(UndefValue::get(LoadType));
}

// All of the below are only true if the loaded pointer is produced
// by the dependent instruction.
if (LoadPtr != lookupOperandLeader(DepInst) &&
DepInst->getType()->isPointerTy() && !AA->isMustAlias(LoadPtr, DepInst))
if (!DepInst->getType()->isPointerTy() ||
(LoadPtr != lookupOperandLeader(DepInst) &&
!AA->isMustAlias(LoadPtr, DepInst)))
return nullptr;
// If this load really doesn't depend on anything, then we must be loading an
// undef value. This can happen when loading for a fresh allocation with no
// intervening stores, for example. Note that this is only true in the case
// that the result of the allocation is pointer equal to the load ptr.
if (isa<AllocaInst>(DepInst)) {
return createConstantExpression(UndefValue::get(LoadType));
}
// If this load occurs either right after a lifetime begin,
// then the loaded value is undefined.
else if (auto *II = dyn_cast<IntrinsicInst>(DepInst)) {
if (II->getIntrinsicID() == Intrinsic::lifetime_start)
return createConstantExpression(UndefValue::get(LoadType));
} else if (auto *InitVal =
getInitialValueOfAllocation(DepInst, TLI, LoadType))
return createConstantExpression(InitVal);
Expand Down
5 changes: 3 additions & 2 deletions llvm/test/Transforms/NewGVN/coercion-different-ptr.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
; RUN: opt -S -passes=newgvn < %s | FileCheck %s


; FIXME: MemorySSA says that load1 depends on the lifetime start.
; MemorySSA says that load1 depends on the lifetime start.
; That's OK since MemorySSA is may-alias; however, NewGVN should
; check whether the lifetime start *actually* defines the loaded pointer
; before simplifying to uninitialized memory.
Expand All @@ -13,7 +13,8 @@ define void @foo(ptr %arg) {
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca i8, align 16
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 1, ptr [[ALLOCA]])
; CHECK-NEXT: [[LOAD:%.*]] = load ptr, ptr [[ARG]], align 8
; CHECK-NEXT: [[CALL:%.*]] = call ptr undef(ptr [[ALLOCA]])
; CHECK-NEXT: [[LOAD1:%.*]] = load ptr, ptr [[LOAD]], align 8
; CHECK-NEXT: [[CALL:%.*]] = call ptr [[LOAD1]](ptr [[ALLOCA]])
; CHECK-NEXT: ret void
;
bb:
Expand Down