]> granicus.if.org Git - llvm/commitdiff
[InstCombine] sub X, sext(bool Y) -> add X, zext(bool Y)
authorSanjay Patel <spatel@rotateright.com>
Fri, 14 Oct 2016 15:24:31 +0000 (15:24 +0000)
committerSanjay Patel <spatel@rotateright.com>
Fri, 14 Oct 2016 15:24:31 +0000 (15:24 +0000)
Prefer add/zext because they are better supported in terms of value-tracking.

Note that the backend should be prepared for this IR canonicalization
(including vector types) after:
https://reviews.llvm.org/rL284015

Differential Revision: https://reviews.llvm.org/D25135

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@284241 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineAddSub.cpp
test/Transforms/InstCombine/sub.ll
test/Transforms/InstCombine/urem.ll

index a490a91535dafa5b4a8cc948124abd083420c0a7..85fa7c0d4fc458f4ed4a3423852f8d9954df9053 100644 (file)
@@ -1642,6 +1642,17 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) {
       if (Value *XNeg = dyn_castNegVal(X))
         return BinaryOperator::CreateShl(XNeg, Y);
 
+    // Subtracting -1/0 is the same as adding 1/0:
+    // sub [nsw] Op0, sext(bool Y) -> add [nsw] Op0, zext(bool Y)
+    // 'nuw' is dropped in favor of the canonical form.
+    if (match(Op1, m_SExt(m_Value(Y))) &&
+        Y->getType()->getScalarSizeInBits() == 1) {
+      Value *Zext = Builder->CreateZExt(Y, I.getType());
+      BinaryOperator *Add = BinaryOperator::CreateAdd(Op0, Zext);
+      Add->setHasNoSignedWrap(I.hasNoSignedWrap());
+      return Add;
+    }
+
     // X - A*-B -> X + A*B
     // X - -A*B -> X + A*B
     Value *A, *B;
index 2fe92d0a03fd31ad39f56c4b82f56173c0351502..68f766c54d961ed18178392265835aa7ed9b0c9a 100644 (file)
@@ -639,3 +639,56 @@ define i32 @test48(i1 %A, i32 %B, i32 %C, i32 %D) {
   %sub = sub i32 %sel0, %sel1
   ret i32 %sub
 }
+
+; Zext+add is more canonical than sext+sub.
+
+define i8 @bool_sext_sub(i8 %x, i1 %y) {
+; CHECK-LABEL: @bool_sext_sub(
+; CHECK-NEXT:    [[TMP1:%.*]] = zext i1 %y to i8
+; CHECK-NEXT:    [[SUB:%.*]] = add i8 [[TMP1]], %x
+; CHECK-NEXT:    ret i8 [[SUB]]
+;
+  %sext = sext i1 %y to i8
+  %sub = sub i8 %x, %sext
+  ret i8 %sub
+}
+
+; Vectors get the same transform.
+
+define <2 x i8> @bool_sext_sub_vec(<2 x i8> %x, <2 x i1> %y) {
+; CHECK-LABEL: @bool_sext_sub_vec(
+; CHECK-NEXT:    [[TMP1:%.*]] = zext <2 x i1> %y to <2 x i8>
+; CHECK-NEXT:    [[SUB:%.*]] = add <2 x i8> [[TMP1]], %x
+; CHECK-NEXT:    ret <2 x i8> [[SUB]]
+;
+  %sext = sext <2 x i1> %y to <2 x i8>
+  %sub = sub <2 x i8> %x, %sext
+  ret <2 x i8> %sub
+}
+
+; NSW is preserved.
+
+define <2 x i8> @bool_sext_sub_vec_nsw(<2 x i8> %x, <2 x i1> %y) {
+; CHECK-LABEL: @bool_sext_sub_vec_nsw(
+; CHECK-NEXT:    [[TMP1:%.*]] = zext <2 x i1> %y to <2 x i8>
+; CHECK-NEXT:    [[SUB:%.*]] = add nsw <2 x i8> [[TMP1]], %x
+; CHECK-NEXT:    ret <2 x i8> [[SUB]]
+;
+  %sext = sext <2 x i1> %y to <2 x i8>
+  %sub = sub nsw <2 x i8> %x, %sext
+  ret <2 x i8> %sub
+}
+
+; We favor the canonical zext+add over keeping the NUW.
+
+define i8 @bool_sext_sub_nuw(i8 %x, i1 %y) {
+; CHECK-LABEL: @bool_sext_sub_nuw(
+; CHECK-NEXT:    [[TMP1:%.*]] = zext i1 %y to i8
+; CHECK-NEXT:    [[SUB:%.*]] = add i8 [[TMP1]], %x
+; CHECK-NEXT:    ret i8 [[SUB]]
+;
+  %sext = sext i1 %y to i8
+  %sub = sub nuw i8 %x, %sext
+  ret i8 %sub
+}
+
index 5568041811d363fa17d888d5646e78698173042a..0549d759eac48f6cf690fa8d3e1da86c52d53523 100644 (file)
@@ -25,12 +25,11 @@ define i8 @big_divisor(i8 %x) {
   ret i8 %rem
 }
 
-; TODO: Should this be zext+add instead of sext+sub?
 define i5 @biggest_divisor(i5 %x) {
 ; CHECK-LABEL: @biggest_divisor(
 ; CHECK-NEXT:    [[NOT_:%.*]] = icmp eq i5 %x, -1
-; CHECK-NEXT:    [[TMP1:%.*]] = sext i1 [[NOT_]] to i5
-; CHECK-NEXT:    [[REM:%.*]] = sub i5 %x, [[TMP1]]
+; CHECK-NEXT:    [[TMP1:%.*]] = zext i1 [[NOT_]] to i5
+; CHECK-NEXT:    [[REM:%.*]] = add i5 [[TMP1]], %x
 ; CHECK-NEXT:    ret i5 [[REM]]
 ;
   %rem = urem i5 %x, -1