Review Report

Agent analysis and findings from PR review
Bugs Found
4
Total Time
2m 48s
Chat Rounds
21
Total Tokens
1,029,233
Est. Cost
-

Test Strategies (Phase 1)

1 Const-one zero-mask scalar counterexample
Target
Mutate/add scalar tests of `(icmp sge/sgt A, 0/-1) ? 1 : (A | Mask)` and inverse `(icmp slt/sle A, 0/-1) ? (A | Mask) : 1` with `Mask = 0` or a zero-valued nonnegative expression.
Rationale
The fold only proves Mask is nonnegative. For the constant-1 arm, equivalence additionally needs A|Mask >= 1 on the constant-selected side. If A=0 and Mask=0, old result is 1 but smin(0,1) is 0.
Expected Issue
InstCombine may replace a select that returns constant 1 for A>=0 with smin(A|Mask,1), producing 0 when A=0 and Mask=0.
2 Known-nonnegative-but-not-known-nonzero mask
Target
Mutate select-min/max or select-icmp tests to use `%mask = and i32 %m, 127` (or vector equivalent) as the OR mask and constant arm 1.
Rationale
`isKnownNonNegative(Mask)` is insufficient for ConstVal=1. A variable mask with sign bit cleared is accepted but can still be zero, causing the same A=0 failure while avoiding a trivial constant-zero case.
Expected Issue
Miscompile persists with non-constant masks that are known nonnegative but may be zero, so a test using `and %m, 127` or a `range [0, ...]` argument can expose over-broad use of `isKnownNonNegative`.
3 Poison-generating `or disjoint` on suppressed arm
Target
Mutate the OR in the matched pattern to `or disjoint`, with `A` nonnegative and overlapping the nonnegative mask (for example A=1, Mask=1) so the old select chooses 0/1 and suppresses the OR.
Rationale
`or disjoint` is poison if both operands share a 1 bit. The original select can suppress poison from the unselected OR arm, but the new `smin` always uses the OR result. The patch does not reject `or disjoint` or drop poison-generating annotations.
Expected Issue
The fold may turn a non-selected poison-producing `or disjoint` into an unconditionally used operand of `llvm.smin`, causing poison where the original select returned a defined constant.
4 Vector lane-wise const-one and poison tests
Target
Mutate vector select tests to `<N x iM>` with `%mask = and <...> %m, splat(signbit-1)` or zeroinitializer, constant arm splat/constant 1, and lane values including A=0.
Rationale
The transformation is applied element-wise for vector icmp/select/smin. The same missing nonzero precondition can occur in only some lanes, so vector tests catch lane-specific reasoning and constant matching issues.
Expected Issue
Vector lanes with A=0 and Mask=0 may be folded to lane value 0 instead of old lane value 1; poison lanes from `or disjoint` may also leak through unselected lanes.
5 Commuted OR and swapped predicate coverage
Target
Create mutations with `%or = or %mask, %a` and with equivalent swapped comparisons like `icmp sle 0, %a`/`icmp sgt -1, %a`, using both constant 0 (safe) and constant 1 (unsafe if accepted).
Rationale
The implementation only explicitly takes `A` from icmp operand 0 and checks `X == A` after matching `m_Or`. Commuted OR should be okay if the matcher is commutative, while swapped icmp forms are currently not handled. These tests guard the exact structural assumptions and prevent accidental unsafe broadening.
Expected Issue
Folds may be missed or, if future commuted matching is added incorrectly, may use the wrong operand as A/Mask; tests document required behavior for commuted OR operands and swapped compare forms.

Bugs Found (Phase 2)

BUG #1
Original IR
define i8 @known_nonneg_maybe_zero_sgt(i8 %inl, i8 %y) {
  %mask = and i8 %y, 127
  %or = or i8 %inl, %mask
  %cmp = icmp sgt i8 %inl, -1
  %s = select i1 %cmp, i8 1, i8 %or
  ret i8 %s
}
Transformed IR
; ModuleID = '/data/tmp/tmplev036be/orig.ll'
source_filename = "/data/tmp/tmplev036be/orig.ll"

define i8 @known_nonneg_maybe_zero_sgt(i8 %inl, i8 %y) {
  %mask = and i8 %y, 127
  %or = or i8 %inl, %mask
  %s = call i8 @llvm.smin.i8(i8 %or, i8 1)
  ret i8 %s
}

; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.smin.i8(i8, i8) #0

attributes #0 = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
Output Log
----------------------------------------
define i8 @known_nonneg_maybe_zero_sgt(i8 %inl, i8 %y) {
#0:
  %mask = and i8 %y, 127
  %or = or i8 %inl, %mask
  %cmp = icmp sgt i8 %inl, 255
  %s = select i1 %cmp, i8 1, i8 %or
  ret i8 %s
}
=>
define i8 @known_nonneg_maybe_zero_sgt(i8 %inl, i8 %y) {
#0:
  %mask = and i8 %y, 127
  %or = or i8 %inl, %mask
  %s = smin i8 %or, 1
  ret i8 %s
}
Transformation doesn't verify!

ERROR: Target is more poisonous than source

Example:
i8 %inl = #x00 (0)
i8 %y = poison

Source:
i8 %mask = poison
i8 %or = poison
i1 %cmp = #x1 (1)
i8 %s = #x01 (1)

Target:
i8 %mask = poison
i8 %or = poison
i8 %s = poison
Source value: #x01 (1)
Target value: poison

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
BUG #2
Analysis

The difference is real: program exit code is the returned i8. Original returns 1 for arguments (0,0); optimized returns 0 after folding to smin(or,1). This is a defined input, not relying on poison.

Original IR
define i8 @known_nonneg_maybe_zero_sgt(i8 %inl, i8 %y) {
  %mask = and i8 %y, 127
  %or = or i8 %inl, %mask
  %cmp = icmp sgt i8 %inl, -1
  %s = select i1 %cmp, i8 1, i8 %or
  ret i8 %s
}

define i8 @main(i32 %argc, ptr %argv) {
entry:
  %r = call i8 @known_nonneg_maybe_zero_sgt(i8 0, i8 0)
  ret i8 %r
}
Transformed IR
; ModuleID = '/data/tmp/tmpvswnjkuv/orig.ll'
source_filename = "/data/tmp/tmpvswnjkuv/orig.ll"

define i8 @known_nonneg_maybe_zero_sgt(i8 %inl, i8 %y) {
  %mask = and i8 %y, 127
  %or = or i8 %inl, %mask
  %s = call i8 @llvm.smin.i8(i8 %or, i8 1)
  ret i8 %s
}

; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.smin.i8(i8, i8) #0

attributes #0 = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }

define i8 @main(i32 %argc, ptr %argv) {
entry:
  %r = call i8 @known_nonneg_maybe_zero_sgt(i8 0, i8 0)
  ret i8 %r
}
Output Log
Confirmed as bug by agent.
Original Output: {'timed_out': False, 'return_code': 1, 'stdout': '', 'stderr': '', 'executor': 'llubi'}
Transformed Output: {'timed_out': False, 'return_code': 0, 'stdout': '', 'stderr': '', 'executor': 'llubi'}
BUG #3
Original IR
define <2 x i8> @vector_const_one(<2 x i8> %a, <2 x i8> %y) {
  %mask = and <2 x i8> %y, <i8 127, i8 127>
  %or = or <2 x i8> %a, %mask
  %cmp = icmp sge <2 x i8> %a, zeroinitializer
  %s = select <2 x i1> %cmp, <2 x i8> <i8 1, i8 1>, <2 x i8> %or
  ret <2 x i8> %s
}
Transformed IR
; ModuleID = '/data/tmp/tmpybm38bcw/orig.ll'
source_filename = "/data/tmp/tmpybm38bcw/orig.ll"

define <2 x i8> @vector_const_one(<2 x i8> %a, <2 x i8> %y) {
  %mask = and <2 x i8> %y, splat (i8 127)
  %or = or <2 x i8> %a, %mask
  %s = call <2 x i8> @llvm.smin.v2i8(<2 x i8> %or, <2 x i8> splat (i8 1))
  ret <2 x i8> %s
}

; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
declare <2 x i8> @llvm.smin.v2i8(<2 x i8>, <2 x i8>) #0

attributes #0 = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
Output Log
----------------------------------------
define <2 x i8> @vector_const_one(<2 x i8> %a, <2 x i8> %y) {
#0:
  %mask = and <2 x i8> %y, { 127, 127 }
  %or = or <2 x i8> %a, %mask
  %cmp = icmp sge <2 x i8> %a, { 0, 0 }
  %s = select <2 x i1> %cmp, <2 x i8> { 1, 1 }, <2 x i8> %or
  ret <2 x i8> %s
}
=>
define <2 x i8> @vector_const_one(<2 x i8> %a, <2 x i8> %y) {
#0:
  %mask = and <2 x i8> %y, { 127, 127 }
  %or = or <2 x i8> %a, %mask
  %s = smin <2 x i8> %or, { 1, 1 }
  ret <2 x i8> %s
}
Transformation doesn't verify!

ERROR: Target is more poisonous than source

Example:
<2 x i8> %a = < #x00 (0), #x00 (0) >
<2 x i8> %y = < poison, #x03 (3) >

Source:
<2 x i8> %mask = < poison, #x03 (3) >
<2 x i8> %or = < poison, #x03 (3) >
<2 x i1> %cmp = < #x1 (1), #x1 (1) >
<2 x i8> %s = < #x01 (1), #x01 (1) >

Target:
<2 x i8> %mask = < poison, #x03 (3) >
<2 x i8> %or = < poison, #x03 (3) >
<2 x i8> %s = < poison, #x01 (1) >
Source value: < #x01 (1), #x01 (1) >
Target value: < poison, #x01 (1) >

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
BUG #4
Original IR
define i8 @or_disjoint_suppressed(i8 %a) {
  %or = or disjoint i8 %a, 1
  %cmp = icmp sgt i8 %a, -1
  %s = select i1 %cmp, i8 0, i8 %or
  ret i8 %s
}
Transformed IR
; ModuleID = '/data/tmp/tmpnyyc0_jr/orig.ll'
source_filename = "/data/tmp/tmpnyyc0_jr/orig.ll"

define i8 @or_disjoint_suppressed(i8 %a) {
  %or = or disjoint i8 %a, 1
  %s = call i8 @llvm.smin.i8(i8 %or, i8 0)
  ret i8 %s
}

; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.smin.i8(i8, i8) #0

attributes #0 = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
Output Log
----------------------------------------
define i8 @or_disjoint_suppressed(i8 %a) {
#0:
  %or = or disjoint i8 %a, 1
  %cmp = icmp sgt i8 %a, 255
  %s = select i1 %cmp, i8 0, i8 %or
  ret i8 %s
}
=>
define i8 @or_disjoint_suppressed(i8 %a) {
#0:
  %or = or disjoint i8 %a, 1
  %s = smin i8 %or, 0
  ret i8 %s
}
Transformation doesn't verify!

ERROR: Target is more poisonous than source

Example:
i8 %a = #x01 (1)

Source:
i8 %or = poison
i1 %cmp = #x1 (1)
i8 %s = #x00 (0)

Target:
i8 %or = poison
i8 %s = poison
Source value: #x00 (0)
Target value: poison

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors

Analysis & Findings

Bug Triggering Analysis:
The new InstCombine fold matches this pattern because the predicate is icmp sgt %inl, -1, the selected non-constant arm is %or = or %inl, %mask, the compared value %inl is operand 0 of the OR, the constant arm is 1, and %mask = and %y, 127 is known nonnegative by known-bits analysis.

InstCombine rewrites the function to:

%mask = and i8 %y, 127
%or = or i8 %inl, %mask
%s = call i8 @llvm.smin.i8(i8 %or, i8 1)
ret i8 %s

This is not equivalent. For the fully defined input %inl = 0, %y = 0, the source computes %mask = 0, %or = 0, %cmp = true, and the select returns 1. The optimized form computes smin(0, 1) = 0. The difftest confirmed this concrete execution difference: original returned exit code 1, transformed returned exit code 0.

Alive2 also reports the transformation as incorrect. Its counterexample uses poison %y, showing an additional poison-suppression problem: when %cmp is true, the source select suppresses poison from the %or arm and returns the defined constant 1, while the transformed smin unconditionally consumes %or and becomes poison.

Fix Weakness Analysis:
The patch only checks isKnownNonNegative(Mask, SQ). That precondition is sufficient for the constant arm 0: when %inl is nonnegative, %inl | Mask is nonnegative, so smin(%or, 0) equals 0. However, it is insufficient for the constant arm 1: equivalence requires %inl | Mask >= 1 whenever the constant arm is selected. The current code does not prove that. A known-nonnegative mask may still be zero, and %inl may also be zero on the nonnegative side of the comparison, so %or may be 0.

The weakness is not limited to this scalar case. The same issue appears for vector lanes, and a separate Alive2 test showed that or disjoint can introduce another unsoundness by making a poison-generating OR unconditionally used by smin even when the original select chose the constant arm. The minimal concrete defined-value bug above is sufficient to demonstrate that the fix is over-broad for ConstVal == 1.