[PATCH] D33391: [DAGCombine] fold (add/uaddo (xor a, -1), 1) -> (sub 0, a)

Eli Friedman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 23 11:06:47 PDT 2017


efriedma added inline comments.


================
Comment at: lib/CodeGen/SelectionDAG/DAGCombiner.cpp:2095
+    return DAG.getNode(ISD::USUBO, DL, N->getVTList(),
+                       DAG.getConstant(0, DL, VT), N0.getOperand(0));
+
----------------
deadalnix wrote:
> efriedma wrote:
> > Are you sure this sets the overflow bit correctly?
> You made me doubt, so I put some test together, and it does indeed overflow correctly.
Are you really, 100% sure it sets the overflow bit correctly?  "uaddo (a ^ -1), 1" sets the overflow bit if "a ^ 1 == -1", or "a == 0". "usubo 0, a" sets the overflow bit if "a !=0".  Simple test program to show this:

```
#include <stdio.h>
__attribute((noinline)) int f(unsigned a, unsigned *ovf) {
   return __builtin_sub_overflow(0, a, ovf);
}
__attribute((noinline)) int f2(unsigned a, unsigned *ovf) {
  return __builtin_add_overflow(a ^ -1, 1, ovf);
}
int main() {
  unsigned sum, ovf;
  ovf = f(0, &sum); printf("%u %u\n", sum, ovf);
  ovf = f2(0, &sum); printf("%u %u\n", sum, ovf);
  ovf = f(1, &sum); printf("%u %u\n", sum, ovf);
  ovf = f2(1, &sum); printf("%u %u\n", sum, ovf);
}
```


https://reviews.llvm.org/D33391





More information about the llvm-commits mailing list