Skip to content

[SimplifyCFG] Bail out on vector GEPs in passingValueIsAlwaysUndefined #142526

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 2 commits into from
Jun 4, 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
5 changes: 5 additions & 0 deletions llvm/lib/Transforms/Utils/SimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8109,6 +8109,7 @@ bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {

/// Check if passing a value to an instruction will cause undefined behavior.
static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I, bool PtrValueMayBeModified) {
assert(V->getType() == I->getType() && "Mismatched types");
Constant *C = dyn_cast<Constant>(V);
if (!C)
return false;
Expand Down Expand Up @@ -8166,6 +8167,10 @@ static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I, bool PtrValu
// Look through GEPs. A load from a GEP derived from NULL is still undefined
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(User))
if (GEP->getPointerOperand() == I) {
// The type of GEP may differ from the type of base pointer.
// Bail out on vector GEPs, as they are not handled by other checks.
if (GEP->getType()->isVectorTy())
return false;
// The current base address is null, there are four cases to consider:
// getelementptr (TY, null, 0) -> null
// getelementptr (TY, null, not zero) -> may be modified
Expand Down
20 changes: 20 additions & 0 deletions llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ declare ptr @fn_nonnull_arg(ptr nonnull %p)
declare ptr @fn_noundef_arg(ptr noundef %p)
declare ptr @fn_ptr_arg(ptr)
declare ptr @fn_ptr_arg_nounwind_willreturn(ptr) nounwind willreturn
declare void @fn_arg_vec(<2 x ptr>)

define void @test9(i1 %X, ptr %Y) {
; CHECK-LABEL: @test9(
Expand Down Expand Up @@ -917,6 +918,25 @@ bb5: ; preds = %bb3, %bb
ret i32 %i7
}

define void @test9_gep_splat(i1 %X, ptr %Y) {
; CHECK-LABEL: @test9_gep_splat(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[X:%.*]], ptr null, ptr [[Y:%.*]]
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[SPEC_SELECT]], <2 x i64> zeroinitializer
; CHECK-NEXT: call void @fn_arg_vec(<2 x ptr> [[GEP]])
; CHECK-NEXT: ret void
;
entry:
br i1 %X, label %if, label %else
if:
br label %else
else:
%phi = phi ptr [ %Y, %entry ], [ null, %if ]
%gep = getelementptr i8, ptr %phi, <2 x i64> zeroinitializer
call void @fn_arg_vec(<2 x ptr> %gep)
ret void
}

declare void @side.effect()
declare i8 @get.i8()

Expand Down