Skip to content

Commit

Permalink
[InstCombine] Intersect nowrap flags between geps while folding into phi
Browse files Browse the repository at this point in the history
A miscompilation issue has been addressed with refined checking.

Fixes: llvm#115149.
  • Loading branch information
antoniofrighetto committed Nov 13, 2024
1 parent 0f44d72 commit 929cbe7
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,8 @@ Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) {
// especially bad when the PHIs are in the header of a loop.
bool NeededPhi = false;

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

// Scan to see if all operands are the same opcode, and all have one user.
for (Value *V : drop_begin(PN.incoming_values())) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/InstCombine/opaque-ptr.ll
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ define ptr @phi_of_gep_flags_1(i1 %c, ptr %p) {
; CHECK: else:
; CHECK-NEXT: br label [[JOIN]]
; CHECK: join:
; CHECK-NEXT: [[PHI:%.*]] = getelementptr nusw nuw i8, ptr [[P:%.*]], i64 4
; CHECK-NEXT: [[PHI:%.*]] = getelementptr nusw i8, ptr [[P:%.*]], i64 4
; CHECK-NEXT: ret ptr [[PHI]]
;
br i1 %c, label %if, label %else
Expand Down
3 changes: 1 addition & 2 deletions llvm/test/Transforms/InstCombine/phi.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2795,14 +2795,13 @@ BB5: ; preds = %BB4
ret void
}

; FIXME: This is a miscompilation.
define i64 @wrong_gep_arg_into_phi(ptr noundef %ptr) {
; CHECK-LABEL: @wrong_gep_arg_into_phi(
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[FOR_COND:%.*]]
; CHECK: for.cond:
; CHECK-NEXT: [[PTR_PN:%.*]] = phi ptr [ [[PTR:%.*]], [[ENTRY:%.*]] ], [ [[DOTPN:%.*]], [[FOR_COND]] ]
; CHECK-NEXT: [[DOTPN]] = getelementptr inbounds nuw i8, ptr [[PTR_PN]], i64 1
; CHECK-NEXT: [[DOTPN]] = getelementptr i8, ptr [[PTR_PN]], i64 1
; CHECK-NEXT: [[VAL:%.*]] = load i8, ptr [[DOTPN]], align 1
; CHECK-NEXT: [[COND_NOT:%.*]] = icmp eq i8 [[VAL]], 0
; CHECK-NEXT: br i1 [[COND_NOT]], label [[EXIT:%.*]], label [[FOR_COND]]
Expand Down

0 comments on commit 929cbe7

Please sign in to comment.