[llvm] [InstCombine] Canonicalize `switch(X^C)` expressions to `switch(X)` (PR #143677)

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 26 08:07:02 PDT 2025


antoniofrighetto wrote:

> > > From llvm-opt-benchmark, a potential problem is that this can something turn small switch values into very large ones -- the common case is a xor by INT_MIN. I haven't checked how this affects codegen.
> > 
> > 
> > When not invoking the middle-end (except for SimplifyCFG, was not expecting llc to invoke it), the codegen looks slightly worse both in the optimized and unoptimized case: https://llvm.godbolt.org/z/jceqnev4n. Not sure if we wish to proceed further with the canonicalization :(
> 
> I think it is ok to perform this canonicalization as it doesn't break the `density` of switch cases (i.e., reverting SimplifyCFG transforms). As for the regression, it can be fixed by canonicalizing `sub X, INT_MIN -> xor X, INT_MIN`: https://alive2.llvm.org/ce/z/UGblFI

It turns out it may be particularly hard to proceed with `xor X, INT_MIN -> sub X, INT_MIN` fold, as there are a lot of transforms that lean on the inverse (current) canonicalization. Conveniently (though maybe not that elegantly), we may prevent this by checking if the constant value is at the extremes (tests updated). May this look better?


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


More information about the llvm-commits mailing list