We add folding support for add instruction in D49216. Test cases are added at rL337179.
we should also fold for sdiv instruction if sdiv's two operands are negatived leverage new added interface isKnownNegation() in D49216. for example:
define i32 @negated_operand(i32 %x) { %negx = sub nsw i32 0, %x %div = sdiv i32 %negx, %x ret i32 %div }
can be folded to
define i32 @negated_operand(i32 %x) { ret i32 -1 }
Note: we must ensure sdiv two operands are non-overflow. Otherwise, the optimization is not right.
Result from Alive:
testing transform:
%sub = sub i8 0, %x %div = sdiv i8 %x, %sub
=>
%div = i8 -1
testing result:
ERROR: Mismatch in values of i8 %div
Example:
%x i8 = 0x80 (128, -128)
%sub i8 = 0x80 (128, -128)
Source value: 0x01 (1)
Target value: 0xFF (255, -1)