Skip to content

[IndVars] Incorrect comparison folding #56242

Closed
@nikic

Description

@nikic

In the following test (https://llvm.godbolt.org/z/rT64Yb1K6), -indvars currently folds %cmp2 to false:

declare void @use(i1)

define void @test(ptr %arr) {
entry:
  br label %loop.header

loop.header:
  %iv = phi i64 [ %iv.inc, %loop.latch ], [ 0, %entry ]
  %prev = phi i32 [ %v, %loop.latch ], [ 0, %entry ]
  %ptr = getelementptr inbounds i32, ptr %arr, i64 %iv
  %v = load i32, ptr %ptr
  %cmp1 = icmp sgt i32 %v, 0
  br i1 %cmp1, label %if, label %loop.latch

if:
  %cmp2 = icmp slt i32 %prev, 0
  call void @use(i1 %cmp2)
  br label %loop.latch

loop.latch:
  %iv.inc = add nuw nsw i64 %iv, 1
  %cmp = icmp ult i64 %iv.inc, 16
  br i1 %cmp, label %loop.header, label %exit

exit:
  ret void
}

This is incorrect: While we have a guard that %v > 0, the value of %v in the %prev phi comes from the previous iteration, so the used context is incorrect.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions