Summary
Since llvm will generate the following ir:
define i32 @and_to_xor1(i32 %0, i32 %1) local_unnamed_addr #0 { %3 = shl nsw i32 %0, 1 %4 = mul nsw i32 %1, %0 %5 = xor i32 %4, -1 %6 = and i32 %3, %5 %7 = add nsw i32 %6, %4 ret i32 %7 }
but InstCombine can not simplify this code shape.
So I use this patch to achieve this.
Test Plan:
check-llvm
There are 4 commuted variations of this pattern, so we should have a test for each. Other transforms will try to normalize the code structure, so the tests will have to overcome that to provide full coverage.
Search for "thwart complexity-based canonicalization" in the test dir for examples.
You may vary the value types across those 4 tests for more coverage as well (try unusual widths or vectors).