[llvm] [IndVarSimplify] Ensure fp values can be represented as consecutive integers (PR #166649)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 7 06:31:26 PST 2025


https://github.com/dtcxzyw commented:

This constraint is still not enough.
Consider the following case:
```
#include <cstdio>
#include <cstdlib>
int main() {
  float end = 1U << 24;
  float beg = 0.0;
  unsigned count = 0;
  for (float i = beg; i <= end; i++) {
    if (count > (1U << 26))  exit(1);
    ++count;
  }
  printf("%u\n", count);
  return 0;
}
```
Note that `(float)(1U << 24) + 1.0f == (float)(1U << 24)`. So the program should call exit(1) instead of exiting the loop.
LLVM IR:
```
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

@.str = private unnamed_addr constant [4 x i8] c"%u\0A\00", align 1

define dso_local noundef i32 @main() {
  br label %1

1:                                                ; preds = %10, %0
  %2 = phi i32 [ 0, %0 ], [ %11, %10 ]
  %3 = phi float [ 0.000000e+00, %0 ], [ %12, %10 ]
  %4 = fcmp ugt float %3, 0x4170000000000000
  br i1 %4, label %5, label %7

5:                                                ; preds = %1
  %6 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %2)
  ret i32 0

7:                                                ; preds = %1
  %8 = icmp samesign ugt i32 %2, 67108864
  br i1 %8, label %9, label %10

9:                                                ; preds = %7
  call void @exit(i32 noundef 13) #3
  unreachable

10:                                               ; preds = %7
  %11 = add nuw nsw i32 %2, 1
  %12 = fadd float %3, 1.000000e+00
  br label %1, !llvm.loop !9
}

declare void @exit(i32 noundef) local_unnamed_addr
declare noundef i32 @printf(ptr noundef readonly captures(none), ...)

!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
```
After `opt -O3` with your patch:
```
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef 16777217)
  ret i32 0
}
```

https://github.com/llvm/llvm-project/pull/166649


More information about the llvm-commits mailing list