Index: lib/Transforms/InstCombine/InstCombineAddSub.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -1044,7 +1044,9 @@ const APInt *Val; if (match(RHS, m_APInt(Val))) { - // X + (signbit) --> X ^ signbit + // X + (signbit) --> X ^ signbit. We may drop nsw/nuw here and forgo + // optimizations based on that. + // TODO: We could add metadata to preserve the wrapping information? if (Val->isSignBit()) return BinaryOperator::CreateXor(LHS, RHS); Index: test/Transforms/InstCombine/add.ll =================================================================== --- test/Transforms/InstCombine/add.ll +++ test/Transforms/InstCombine/add.ll @@ -265,7 +265,7 @@ ret i32 %add } -; Lose no-wrap info by converting to xor? %x is known non-negative +; Lose no-wrap info by converting to xor. %x is known non-negative ; here, but not after converting to xor. define i8 @add_nsw_signbit(i8 %x) { @@ -277,7 +277,7 @@ ret i8 %y } -; Lose no-wrap info by converting to xor? %x is known non-negative +; Lose no-wrap info by converting to xor. %x is known non-negative ; (x < 128 unsigned), but not after converting to xor. define i8 @add_nuw_signbit(i8 %x) { Index: test/Transforms/InstCombine/icmp-add.ll =================================================================== --- test/Transforms/InstCombine/icmp-add.ll +++ test/Transforms/InstCombine/icmp-add.ll @@ -229,7 +229,8 @@ ret i1 %c } -; FIXME: InstCombine should not lose wrapping information by changing the add to xor. +; This is known 'true', but we canonicalize the add to xor and lose that information. +; Metadata that preserves the nsw-ness of the add could be used to improve this. define i1 @slt_zero_add_nsw_signbit(i8 %x) { ; CHECK-LABEL: @slt_zero_add_nsw_signbit( @@ -241,7 +242,8 @@ ret i1 %z } -; FIXME: InstCombine should not lose wrapping information by changing the add to xor. +; This is known 'true', but we canonicalize the add to xor and lose that information. +; Metadata that preserves the nuw-ness of the add could be used to improve this. define i1 @slt_zero_add_nuw_signbit(i8 %x) { ; CHECK-LABEL: @slt_zero_add_nuw_signbit(