Skip to content

Commit a4d9240

Browse files
authored
[InstCombine] Fix GEPNoWrapFlags propagation in foldGEPOfPhi (#121572)
Closes #121459.
1 parent 68d2656 commit a4d9240

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2782,6 +2782,7 @@ static Instruction *foldGEPOfPhi(GetElementPtrInst &GEP, PHINode *PN,
27822782
// loop iteration).
27832783
if (Op1 == &GEP)
27842784
return nullptr;
2785+
GEPNoWrapFlags NW = Op1->getNoWrapFlags();
27852786

27862787
int DI = -1;
27872788

@@ -2838,6 +2839,8 @@ static Instruction *foldGEPOfPhi(GetElementPtrInst &GEP, PHINode *PN,
28382839
}
28392840
}
28402841
}
2842+
2843+
NW &= Op2->getNoWrapFlags();
28412844
}
28422845

28432846
// If not all GEPs are identical we'll have to create a new PHI node.
@@ -2847,6 +2850,8 @@ static Instruction *foldGEPOfPhi(GetElementPtrInst &GEP, PHINode *PN,
28472850
return nullptr;
28482851

28492852
auto *NewGEP = cast<GetElementPtrInst>(Op1->clone());
2853+
NewGEP->setNoWrapFlags(NW);
2854+
28502855
if (DI == -1) {
28512856
// All the GEPs feeding the PHI are identical. Clone one down into our
28522857
// BB so that it can be merged with the current GEP.

llvm/test/Transforms/InstCombine/opaque-ptr.ll

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,64 @@ join:
654654
ret ptr %gep
655655
}
656656

657+
define ptr @gep_of_phi_of_gep_flags1(i1 %c, ptr %p) {
658+
; CHECK-LABEL: @gep_of_phi_of_gep_flags1(
659+
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
660+
; CHECK: if:
661+
; CHECK-NEXT: br label [[JOIN:%.*]]
662+
; CHECK: else:
663+
; CHECK-NEXT: br label [[JOIN]]
664+
; CHECK: join:
665+
; CHECK-NEXT: [[TMP1:%.*]] = phi i64 [ 4, [[IF]] ], [ 8, [[ELSE]] ]
666+
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr i8, ptr [[P:%.*]], i64 [[TMP1]]
667+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[TMP2]], i64 4
668+
; CHECK-NEXT: ret ptr [[GEP]]
669+
;
670+
br i1 %c, label %if, label %else
671+
672+
if:
673+
%gep1 = getelementptr inbounds i32, ptr %p, i64 1
674+
br label %join
675+
676+
else:
677+
%gep2 = getelementptr i32, ptr %p, i64 2
678+
br label %join
679+
680+
join:
681+
%phi = phi ptr [ %gep1, %if ], [ %gep2, %else ]
682+
%gep = getelementptr i32, ptr %phi, i64 1
683+
ret ptr %gep
684+
}
685+
686+
define ptr @gep_of_phi_of_gep_flags2(i1 %c, ptr %p) {
687+
; CHECK-LABEL: @gep_of_phi_of_gep_flags2(
688+
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
689+
; CHECK: if:
690+
; CHECK-NEXT: br label [[JOIN:%.*]]
691+
; CHECK: else:
692+
; CHECK-NEXT: br label [[JOIN]]
693+
; CHECK: join:
694+
; CHECK-NEXT: [[TMP1:%.*]] = phi i64 [ 4, [[IF]] ], [ 8, [[ELSE]] ]
695+
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr nuw i8, ptr [[P:%.*]], i64 [[TMP1]]
696+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[TMP2]], i64 4
697+
; CHECK-NEXT: ret ptr [[GEP]]
698+
;
699+
br i1 %c, label %if, label %else
700+
701+
if:
702+
%gep1 = getelementptr nuw i32, ptr %p, i64 1
703+
br label %join
704+
705+
else:
706+
%gep2 = getelementptr nuw i32, ptr %p, i64 2
707+
br label %join
708+
709+
join:
710+
%phi = phi ptr [ %gep1, %if ], [ %gep2, %else ]
711+
%gep = getelementptr i32, ptr %phi, i64 1
712+
ret ptr %gep
713+
}
714+
657715
define ptr @gep_of_phi_of_gep_different_type(i1 %c, ptr %p) {
658716
; CHECK-LABEL: @gep_of_phi_of_gep_different_type(
659717
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]

0 commit comments

Comments
 (0)