Skip to content

Commit 6467b49

Browse files
committed
[InstCombine] Preserve all flags in phi of gep fold
Preserve the intersection of all flags. Add GEPNoWrapFlags::all() to serve as the initialization value for the intersection.
1 parent e7b4b43 commit 6467b49

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

llvm/include/llvm/IR/GEPNoWrapFlags.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class GEPNoWrapFlags {
4444
: Flags(IsInBounds ? (InBoundsFlag | NUSWFlag) : 0) {}
4545

4646
static GEPNoWrapFlags none() { return GEPNoWrapFlags(); }
47+
static GEPNoWrapFlags all() {
48+
return GEPNoWrapFlags(InBoundsFlag | NUSWFlag | NUWFlag);
49+
}
4750
static GEPNoWrapFlags inBounds() {
4851
return GEPNoWrapFlags(InBoundsFlag | NUSWFlag);
4952
}

llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) {
513513
// especially bad when the PHIs are in the header of a loop.
514514
bool NeededPhi = false;
515515

516-
bool AllInBounds = true;
516+
GEPNoWrapFlags NW = GEPNoWrapFlags::all();
517517

518518
// Scan to see if all operands are the same opcode, and all have one user.
519519
for (Value *V : drop_begin(PN.incoming_values())) {
@@ -523,7 +523,7 @@ Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) {
523523
GEP->getNumOperands() != FirstInst->getNumOperands())
524524
return nullptr;
525525

526-
AllInBounds &= GEP->isInBounds();
526+
NW &= GEP->getNoWrapFlags();
527527

528528
// Keep track of whether or not all GEPs are of alloca pointers.
529529
if (AllBasePointersAreAllocas &&
@@ -605,8 +605,7 @@ Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) {
605605
Value *Base = FixedOperands[0];
606606
GetElementPtrInst *NewGEP =
607607
GetElementPtrInst::Create(FirstInst->getSourceElementType(), Base,
608-
ArrayRef(FixedOperands).slice(1));
609-
if (AllInBounds) NewGEP->setIsInBounds();
608+
ArrayRef(FixedOperands).slice(1), NW);
610609
PHIArgMergedDebugLoc(NewGEP, PN);
611610
return NewGEP;
612611
}

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,58 @@ join:
541541
ret ptr %phi
542542
}
543543

544+
define ptr @phi_of_gep_flags_1(i1 %c, ptr %p) {
545+
; CHECK-LABEL: @phi_of_gep_flags_1(
546+
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
547+
; CHECK: if:
548+
; CHECK-NEXT: br label [[JOIN:%.*]]
549+
; CHECK: else:
550+
; CHECK-NEXT: br label [[JOIN]]
551+
; CHECK: join:
552+
; CHECK-NEXT: [[PHI:%.*]] = getelementptr nusw nuw i8, ptr [[P:%.*]], i64 4
553+
; CHECK-NEXT: ret ptr [[PHI]]
554+
;
555+
br i1 %c, label %if, label %else
556+
557+
if:
558+
%gep1 = getelementptr inbounds i32, ptr %p, i64 1
559+
br label %join
560+
561+
else:
562+
%gep2 = getelementptr nusw nuw i32, ptr %p, i64 1
563+
br label %join
564+
565+
join:
566+
%phi = phi ptr [ %gep1, %if ], [ %gep2, %else ]
567+
ret ptr %phi
568+
}
569+
570+
define ptr @phi_of_gep_flags_2(i1 %c, ptr %p) {
571+
; CHECK-LABEL: @phi_of_gep_flags_2(
572+
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
573+
; CHECK: if:
574+
; CHECK-NEXT: br label [[JOIN:%.*]]
575+
; CHECK: else:
576+
; CHECK-NEXT: br label [[JOIN]]
577+
; CHECK: join:
578+
; CHECK-NEXT: [[PHI:%.*]] = getelementptr nuw i8, ptr [[P:%.*]], i64 4
579+
; CHECK-NEXT: ret ptr [[PHI]]
580+
;
581+
br i1 %c, label %if, label %else
582+
583+
if:
584+
%gep1 = getelementptr nusw nuw i32, ptr %p, i64 1
585+
br label %join
586+
587+
else:
588+
%gep2 = getelementptr nuw i32, ptr %p, i64 1
589+
br label %join
590+
591+
join:
592+
%phi = phi ptr [ %gep1, %if ], [ %gep2, %else ]
593+
ret ptr %phi
594+
}
595+
544596
define ptr @phi_of_gep_different_type(i1 %c, ptr %p) {
545597
; CHECK-LABEL: @phi_of_gep_different_type(
546598
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]

0 commit comments

Comments
 (0)