Skip to content

[InstCombine][foldPHIArgGEPIntoPHI] Early return for const vector index for gep inst. #138661

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 4 commits into from
May 8, 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
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,8 @@ Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) {
// substantially cheaper to compute for the constants, so making it a
// variable index could pessimize the path. This also handles the case
// for struct indices, which must always be constant.
if (isa<ConstantInt>(FirstInst->getOperand(Op)) ||
isa<ConstantInt>(GEP->getOperand(Op)))
if (isa<Constant>(FirstInst->getOperand(Op)) ||
isa<Constant>(GEP->getOperand(Op)))
return nullptr;

if (FirstInst->getOperand(Op)->getType() !=
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ended up a bit too reduced now and no longer crashes without the change. This variant works:

%foo = type { i16, i16, i16 }

define <16 x ptr> @test(i1 %tobool) {
entry:
  %lane.0 = alloca %foo, align 16
  %lane.15 = insertelement <16 x ptr> poison, ptr %lane.0, i64 0
  %mm_vectorGEP = getelementptr inbounds %foo, <16 x ptr> %lane.15, <16 x i64> zeroinitializer, <16 x i32> splat (i32 1)
  %mm_vectorGEP2 = getelementptr inbounds %foo, <16 x ptr> %lane.15, <16 x i64> zeroinitializer, <16 x i32> splat (i32 2)
  br i1 %tobool, label %f1, label %f0

f0:
  br label %merge

f1:
  br label %merge

merge:
  %vec.phi = phi <16 x ptr> [ %mm_vectorGEP, %f0], [ %mm_vectorGEP2, %f1 ]
  ret <16 x ptr> %vec.phi
}

(This makes sure the second GEP is not optimized away.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patience, updated.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt < %s -passes=instcombine -S | FileCheck %s

%foo = type { i16, i16, i16 }

define <16 x ptr> @test(i1 %tobool) {
; CHECK-LABEL: define <16 x ptr> @test(
; CHECK-SAME: i1 [[TOBOOL:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[LANE_0:%.*]] = alloca [[FOO:%.*]], align 16
; CHECK-NEXT: [[LANE_15:%.*]] = insertelement <16 x ptr> poison, ptr [[LANE_0]], i64 0
; CHECK-NEXT: br i1 [[TOBOOL]], label %[[F1:.*]], label %[[F0:.*]]
; CHECK: [[F0]]:
; CHECK-NEXT: [[MM_VECTORGEP:%.*]] = getelementptr inbounds [[FOO]], <16 x ptr> [[LANE_15]], <16 x i64> zeroinitializer, <16 x i32> splat (i32 1)
; CHECK-NEXT: br label %[[MERGE:.*]]
; CHECK: [[F1]]:
; CHECK-NEXT: [[MM_VECTORGEP2:%.*]] = getelementptr inbounds [[FOO]], <16 x ptr> [[LANE_15]], <16 x i64> zeroinitializer, <16 x i32> splat (i32 2)
; CHECK-NEXT: br label %[[MERGE]]
; CHECK: [[MERGE]]:
; CHECK-NEXT: [[VEC_PHI:%.*]] = phi <16 x ptr> [ [[MM_VECTORGEP]], %[[F0]] ], [ [[MM_VECTORGEP2]], %[[F1]] ]
; CHECK-NEXT: ret <16 x ptr> [[VEC_PHI]]
;
entry:
%lane.0 = alloca %foo, align 16
%lane.15 = insertelement <16 x ptr> poison, ptr %lane.0, i64 0
%mm_vectorGEP = getelementptr inbounds %foo, <16 x ptr> %lane.15, <16 x i64> zeroinitializer, <16 x i32> splat (i32 1)
%mm_vectorGEP2 = getelementptr inbounds %foo, <16 x ptr> %lane.15, <16 x i64> zeroinitializer, <16 x i32> splat (i32 2)
br i1 %tobool, label %f1, label %f0

f0:
br label %merge

f1:
br label %merge

merge:
%vec.phi = phi <16 x ptr> [ %mm_vectorGEP, %f0], [ %mm_vectorGEP2, %f1 ]
ret <16 x ptr> %vec.phi
}