llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[ValueTracking] Missed Optimizations with SIToFP and UIToFP

Open 134ARG opened this issue 8 months ago • 1 comments

For the IR pattern like:

define i1 @test_sitofp_nonneg(i64 %a) {
entry:
  ; 32767 means a max of 15 active bits. 
  ; half precision's max exponent is 15. This perfectly fits without Inf!
  %and = and i64 %a, 32767
  %f = uitofp i64 %and to half
  %is.inf = fcmp oeq half %f, 0xH7C00
  ret i1 %is.inf
}

Since the masked value will always fit into half, the function could be wrapped into a constant false return:

define i1 @test_sitofp_nonneg(i64 %a) {
	ret i1 false
}

Current opt just leaves it as is: https://godbolt.org/z/zTTdWGner.

Alive2: https://alive2.llvm.org/ce/z/K9pe8z.

This is primarily due to the current computeKnownFPClass() does not propagate some of the KnowBits facts from the integer side to floating point side when handling integer to floating point conversion.

134ARG avatar Mar 17 '26 04:03 134ARG

I could propose a patch for that. Please assign it to me.

134ARG avatar Mar 17 '26 04:03 134ARG