[PATCH] D54532: [InstructionSimplify] Add support for saturating add/sub

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 18 07:25:50 PST 2018


nikic added inline comments.


================
Comment at: lib/Analysis/InstructionSimplify.cpp:4925-4928
+    if (match(Op1, m_Zero()) || match(Op1, m_Undef()))
+      return Op0;
+    // 0 + X -> X, undef + X -> X
+    if (match(Op0, m_Zero()) || match(Op0, m_Undef()))
----------------
spatel wrote:
> It's slightly better to return a constant when one of the operands is undef because that eliminates the use of a variable. That also matches the behavior of the subtracts. 
> 
> For sadd_sat, we can always return 0, and for uadd_sat, we can always return -1?
The question of undef handling is also discussed here: https://reviews.llvm.org/D54237#1294571

The problem is that we can't return 0 for sadd_sat, due to the asymmetric range for signed integers. If one operand is -128, the largest number you can reach is -128 + 127 = -1, not 0. That's why I'm returning the other operand here, so that handling is consistent for uadd/sadd.

But ... now that I think about it, shouldn't it be legal to return -1 for *both* the signed and the unsigned case?


https://reviews.llvm.org/D54532





More information about the llvm-commits mailing list