summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Green <david.green@arm.com>2019-10-20 11:28:33 +0100
committerDavid Green <david.green@arm.com>2019-10-28 15:21:16 +0000
commitbf21f0d489fb461a8eeb4d6342d28ed2c6e4048d (patch)
tree0de5001abd0788d689e5ba0c5979e6eeab619d59
parentd157a9bc8ba1085cc4808c6941412322a7fd884e (diff)
downloadbcm5719-llvm-bf21f0d489fb461a8eeb4d6342d28ed2c6e4048d.tar.gz
bcm5719-llvm-bf21f0d489fb461a8eeb4d6342d28ed2c6e4048d.zip
[InstCombine] Extra combine for uadd_sat
This is an extra fold for a canonical form of uadd_sat, as shown in D68651. It essentially selects uadd from an add and a select. Differential Revision: https://reviews.llvm.org/D69244
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp7
-rw-r--r--llvm/test/Transforms/InstCombine/saturating-add-sub.ll12
2 files changed, 11 insertions, 8 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 9fc871e49b3..b06d31a3fa2 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -781,6 +781,13 @@ static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal,
return Builder.CreateBinaryIntrinsic(
Intrinsic::uadd_sat, BO->getOperand(0), BO->getOperand(1));
}
+ // The overflow may be detected via the add wrapping round.
+ if (match(Cmp0, m_c_Add(m_Specific(Cmp1), m_Value(Y))) &&
+ match(FVal, m_c_Add(m_Specific(Cmp1), m_Specific(Y)))) {
+ // ((X + Y) u< X) ? -1 : (X + Y) --> uadd.sat(X, Y)
+ // ((X + Y) u< Y) ? -1 : (X + Y) --> uadd.sat(X, Y)
+ return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, Cmp1, Y);
+ }
return nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
index 06232070421..57ef7515e66 100644
--- a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
+++ b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
@@ -1486,10 +1486,8 @@ define i32 @uadd_sat_constant_commute(i32 %x) {
define i32 @uadd_sat_canon(i32 %x, i32 %y) {
; CHECK-LABEL: @uadd_sat_canon(
-; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[A]], [[X]]
-; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 -1, i32 [[A]]
-; CHECK-NEXT: ret i32 [[R]]
+; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.uadd.sat.i32(i32 [[X:%.*]], i32 [[Y:%.*]])
+; CHECK-NEXT: ret i32 [[TMP1]]
;
%a = add i32 %x, %y
%c = icmp ult i32 %a, %x
@@ -1499,10 +1497,8 @@ define i32 @uadd_sat_canon(i32 %x, i32 %y) {
define i32 @uadd_sat_canon_y(i32 %x, i32 %y) {
; CHECK-LABEL: @uadd_sat_canon_y(
-; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[A]], [[Y]]
-; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 -1, i32 [[A]]
-; CHECK-NEXT: ret i32 [[R]]
+; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.uadd.sat.i32(i32 [[Y:%.*]], i32 [[X:%.*]])
+; CHECK-NEXT: ret i32 [[TMP1]]
;
%a = add i32 %x, %y
%c = icmp ult i32 %a, %y
OpenPOWER on IntegriCloud