Skip to content

Commit 929cbe7

Browse files
[InstCombine] Intersect nowrap flags between geps while folding into phi
A miscompilation issue has been addressed with refined checking. Fixes: llvm#115149.
1 parent 0f44d72 commit 929cbe7

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,8 @@ Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) {
545545
// especially bad when the PHIs are in the header of a loop.
546546
bool NeededPhi = false;
547547

548-
GEPNoWrapFlags NW = GEPNoWrapFlags::all();
548+
// Remember flags of the first phi-operand getelementptr.
549+
GEPNoWrapFlags NW = FirstInst->getNoWrapFlags();
549550

550551
// Scan to see if all operands are the same opcode, and all have one user.
551552
for (Value *V : drop_begin(PN.incoming_values())) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ define ptr @phi_of_gep_flags_1(i1 %c, ptr %p) {
549549
; CHECK: else:
550550
; CHECK-NEXT: br label [[JOIN]]
551551
; CHECK: join:
552-
; CHECK-NEXT: [[PHI:%.*]] = getelementptr nusw nuw i8, ptr [[P:%.*]], i64 4
552+
; CHECK-NEXT: [[PHI:%.*]] = getelementptr nusw i8, ptr [[P:%.*]], i64 4
553553
; CHECK-NEXT: ret ptr [[PHI]]
554554
;
555555
br i1 %c, label %if, label %else

llvm/test/Transforms/InstCombine/phi.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,14 +2795,13 @@ BB5: ; preds = %BB4
27952795
ret void
27962796
}
27972797

2798-
; FIXME: This is a miscompilation.
27992798
define i64 @wrong_gep_arg_into_phi(ptr noundef %ptr) {
28002799
; CHECK-LABEL: @wrong_gep_arg_into_phi(
28012800
; CHECK-NEXT: entry:
28022801
; CHECK-NEXT: br label [[FOR_COND:%.*]]
28032802
; CHECK: for.cond:
28042803
; CHECK-NEXT: [[PTR_PN:%.*]] = phi ptr [ [[PTR:%.*]], [[ENTRY:%.*]] ], [ [[DOTPN:%.*]], [[FOR_COND]] ]
2805-
; CHECK-NEXT: [[DOTPN]] = getelementptr inbounds nuw i8, ptr [[PTR_PN]], i64 1
2804+
; CHECK-NEXT: [[DOTPN]] = getelementptr i8, ptr [[PTR_PN]], i64 1
28062805
; CHECK-NEXT: [[VAL:%.*]] = load i8, ptr [[DOTPN]], align 1
28072806
; CHECK-NEXT: [[COND_NOT:%.*]] = icmp eq i8 [[VAL]], 0
28082807
; CHECK-NEXT: br i1 [[COND_NOT]], label [[EXIT:%.*]], label [[FOR_COND]]

0 commit comments

Comments
 (0)