- 0-x == y -> x + y ==0
0-x == y ir is
define i64 @eq(i64, i64) local_unnamed_addr #0 { %3 = sub nsw i64 0, %0 %4 = icmp eq i64 %3, %1 %5 = zext i1 %4 to i64 ret i64 %5 }
Now the code generated by llvm is as follows
0000000000000000 <eq>: 0: d0 00 63 7c neg r3,r3 4: 78 22 63 7c xor r3,r3,r4 8: 74 00 63 7c cntlzd r3,r3 c: e2 d7 63 78 rldicl r3,r3,58,63 10: 20 00 80 4e blr
We can optimize the code as follows
0000000000000000 <eq>: 0: 14 1a 64 7c add r3,r4,r3 4: 74 00 63 7c cntlzd r3,r3 8: e2 d7 63 78 rldicl r3,r3,58,63 c: 20 00 80 4e blr
- 0 - x != y -> x + y != 0 0 - x != y ir is
define i64 @neq(i64, i64) local_unnamed_addr #0 { %3 = sub nsw i64 0, %0 %4 = icmp ne i64 %3, %1 %5 = zext i1 %4 to i64 ret i64 %5 }
Now the code generated by llvm is as follows
0000000000000000 <neq>: 0: d0 00 63 7c neg r3,r3 4: 78 22 63 7c xor r3,r3,r4 8: ff ff 83 30 addic r4,r3,-1 c: 10 19 64 7c subfe r3,r4,r3 10: 20 00 80 4e blr
We can optimize the code as follows
0000000000000000 <neq>: 0: 14 1a 64 7c add r3,r4,r3 4: ff ff 83 30 addic r4,r3,-1 8: 10 19 64 7c subfe r3,r4,r3 c: 20 00 80 4e blr