[fuzzer-found] [InstSimplify] Miscompile at -O2: `(X < Y) ? X : min(X, Y)` --> `min(X, Y)` keeps the `range` attributes of the min/max call
InstSimplify folds select (icmp ugt Y, X), X, umin(X, Y) to the existing umin(X, Y) call. If that call has a range attribute, on its return value or on an argument, the result becomes poison for inputs where the select picked X and the call was out of range. Later passes use the return range, so the C function below returns -95 instead of -1 at -O2, -O3 and -Os. clang 22.1.8, 23.1.6 and main are affected.
Reproducer
godbolt (opt trunk, opt 23.1.0 and opt 22.1.0): https://godbolt.org/z/4bK41n774
define i64 @test(i64 %x, i64 %y) {
%c = icmp ugt i64 %y, %x
%m = call range(i64 100, 201) i64 @llvm.umin.i64(i64 %x, i64 %y)
%r = select i1 %c, i64 %x, i64 %m
ret i64 %r
}
declare i64 @llvm.umin.i64(i64, i64)
$ opt -S -passes=instsimplify repro.ll
...
define i64 @test(i64 %x, i64 %y) {
%m = call range(i64 100, 201) i64 @llvm.umin.i64(i64 %x, i64 %y)
ret i64 %m
}
For x = 5, y = 150 the source returns 5: %c is true, and the poison %m is not selected. The target returns %m, which is poison because umin(5, 150) = 5 is outside [100, 201).
Alive2: https://alive2.llvm.org/ce/z/8mg7zK reports Transformation doesn't verify! ERROR: Target is more poisonous than source (its example is x = 0, y = 1).
Wrong output at -O2
godbolt (x86-64 clang trunk at -O2 and -O0, clang 23.1.0 and 22.1.0 at -O2, with execution): https://godbolt.org/z/jnjcE1TnG
int printf(const char *, ...);
typedef unsigned long long u64;
static inline __attribute__((always_inline)) u64 umin64(u64 a, u64 b) { return a < b ? a : b; }
static inline __attribute__((always_inline)) void note(int z, int *seen) { if (z) *seen = 1; }
__attribute__((noinline)) int f(u64 x, unsigned char b, int z) {
int seen = 0;
u64 y = 100 + (u64)b; /* 100 .. 355 */
u64 r;
if (y > x) {
r = x;
} else {
note(z, &seen);
r = umin64(x, y);
} /* r = min(x, y) on both paths */
if (r >= 100)
return (int)(r - 100);
return -1;
}
int main(void) {
printf("%d\n", f(5, 50, 0)); /* min(5, 150) = 5, so -1 */
return 0;
}
$ clang -O0 repro.c && ./a.out
-1
$ clang -O2 repro.c && ./a.out
-95
f at -O2: the r >= 100 check is gone.
define dso_local range(i32 0, 256) i32 @f(i64 noundef %x, i8 noundef %b, i32 %z) local_unnamed_addr #0 {
entry:
%conv = zext i8 %b to i64
%add = add nuw nsw i64 %conv, 100
%cond.i = tail call range(i64 100, 0) i64 @llvm.umin.i64(i64 range(i64 100, 0) %x, i64 range(i64 100, 356) %add)
%0 = trunc nuw nsw i64 %cond.i to i32
%conv5 = add nsw i32 %0, -100
ret i32 %conv5
}
What goes wrong
Each step before the fold is correct:
- IPSCCP sees that
umin64is only called wherey <= x, andy >= 100, so both arguments are at least 100. It putsrange(i64 100, 0)on the first parameter and on the return value ofumin64. - Inlining moves those attributes onto the
llvm.umincall, which is still inside theelseblock. - SimplifyCFG turns the
if/elseinto a select. The call is now executed for every input, but its result is only used wheny <= x:
%cmp = icmp ugt i64 %add, %x
%cond.i = call range(i64 100, 0) i64 @llvm.umin.i64(i64 range(i64 100, 0) %x, i64 range(i64 100, 356) %add)
%r.0 = select i1 %cmp, i64 %x, i64 %cond.i
%cmp2 = icmp samesign ugt i64 %r.0, 99
- InstCombine, through InstSimplify, replaces
%r.0by%cond.i. Withx = 5the call's result is 5, outside its return range, so%r.0is now poison.%cmp2folds to true because of the return range.
-opt-bisect-limit on the -O2 pipeline gives the right result up to this InstCombine run and the wrong one from it on.
Where it comes from
simplifyCmpSelOfMaxMin in llvm/lib/Analysis/InstructionSimplify.cpp returns the existing call without looking at its attributes:
// (X > Y) ? X : max(X, Y) --> max(X, Y)
// (X >= Y) ? X : max(X, Y) --> max(X, Y)
// (X < Y) ? X : min(X, Y) --> min(X, Y)
// (X <= Y) ? X : min(X, Y) --> min(X, Y)
...
if (MMPred == CmpInst::getStrictPredicate(Pred))
return MMI;
...
// (X == Y) ? X : max/min(X, Y) --> max/min(X, Y)
if (Pred == CmpInst::ICMP_EQ)
return MMI;
Notes
optfrom 22.1.8, 23.1.6 and main at 9833b0c710ef (2026-11-26) all foldrepro.llas shown.repro.cprints -95 at-Os,-O2and-O3with clang 22.1.8, 23.1.6 and main at 9833b0c710ef (x86-64). It prints -1 at-O0and-O1, and with gcc.- The same happens with
umax,sminandsmax, and with theicmp eqform of the fold. - With the attribute on an argument instead (
call i64 @llvm.umin.i64(i64 %x, i64 range(i64 0, 100) %y)) the select is removed as well, andllubireports poison forx = 5,y = 200, where the source returns 5. - #245656 (fixed by #245874) was the same kind of problem in InstCombine's
foldSelectZeroOrFixedOp. That fix is in the main build tested here. note()in the C code only keeps the call in a block of its own until SimplifyCFG forms the select. Without it, EarlyCSE replaces the call byyunder the dominating condition and the result is right.
This problem was found with a fuzzer; the reproducers above were written by hand from its test case. The report was written with LLM assistance.
I have a patch for this and would like to take it.