[PATCH] D54534: [InstCombine] Add support for saturating add/sub

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 14 08:53:04 PST 2018


nikic created this revision.
Herald added a subscriber: llvm-commits.

This adds support for saturating add/sub intrinsics to InstCombine. In particular the following folds are supported:

- sat(sat(X + C1) + C2) -> sat(X + (C1 + C2)) where legal
- sat(sat(X - C1) - C2) -> sat(X + (C1 + C2)) where legal
- sat(X1 + X2) -> add nuw/nsw where known no overflow
- sat(X1 - X2) -> sub nuw/nsw where known no overflow
- sat(X1 uadd X2) -> MAX where known overflow
- sat(X1 usub X2) -> 0 where known overflow

To properly support this, I've also changed the computeOverflowForUnsignedSub() implementation to return AlwaysOverflows results, for parity with the computeOverflowForUnsignedAdd() implementation.

A possible future improvement for the vector case is to support sat(sat(X + C1) + C2) -> sat(X + (C1 + C2)) style foldings also in the case where C1 and C2 are not constant splats.


Repository:
  rL LLVM

https://reviews.llvm.org/D54534

Files:
  lib/Analysis/ValueTracking.cpp
  lib/Transforms/InstCombine/InstCombineCalls.cpp
  test/Transforms/InstCombine/saturating-add.ll
  test/Transforms/InstCombine/saturating-sub.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54534.174049.patch
Type: text/x-patch
Size: 22345 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181114/c7f0ba2a/attachment.bin>


More information about the llvm-commits mailing list