[llvm-branch-commits] [llvm-branch] r223644 - Merging r214385:

David Majnemer david.majnemer at gmail.com
Mon Dec 8 01:11:37 PST 2014


Author: majnemer
Date: Mon Dec  8 03:11:36 2014
New Revision: 223644

URL: http://llvm.org/viewvc/llvm-project?rev=223644&view=rev
Log:
Merging r214385:
------------------------------------------------------------------------
r214385 | majnemer | 2014-07-30 21:49:29 -0700 (Wed, 30 Jul 2014) | 9 lines

InstCombine: Correctly propagate NSW/NUW for x-(-A) -> x+A

We can only propagate the nsw bits if both subtraction instructions are
marked with the appropriate bit.

N.B.  We only propagate the nsw bit in InstCombine because the nuw case
is already handled in InstSimplify.

This fixes PR20189.
------------------------------------------------------------------------

Modified:
    llvm/branches/release_35/   (props changed)
    llvm/branches/release_35/lib/Transforms/InstCombine/InstCombineAddSub.cpp
    llvm/branches/release_35/test/Transforms/InstCombine/sub.ll

Propchange: llvm/branches/release_35/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Dec  8 03:11:36 2014
@@ -1,3 +1,3 @@
 /llvm/branches/Apple/Pertwee:110850,110961
 /llvm/branches/type-system-rewrite:133420-134817
-/llvm/trunk:155241,213653,213665,213726,213749,213773,213793,213798-213799,213815,213847,213880,213883-213884,213894-213896,213899,213915,213966,213999,214060,214129,214180,214287,214331,214423,214429,214519,214670,214674,214679,215685,215711,215806,216064,216262,216531,216891,216920,217102,217115,217257,218745,221009,221408,221453,221501,222338,222376,222500,223163,223170-223171
+/llvm/trunk:155241,213653,213665,213726,213749,213773,213793,213798-213799,213815,213847,213880,213883-213884,213894-213896,213899,213915,213966,213999,214060,214129,214180,214287,214331,214385,214423,214429,214519,214670,214674,214679,215685,215711,215806,216064,216262,216531,216891,216920,217102,217115,217257,218745,221009,221408,221453,221501,222338,222376,222500,223163,223170-223171

Modified: llvm/branches/release_35/lib/Transforms/InstCombine/InstCombineAddSub.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_35/lib/Transforms/InstCombine/InstCombineAddSub.cpp?rev=223644&r1=223643&r2=223644&view=diff
==============================================================================
--- llvm/branches/release_35/lib/Transforms/InstCombine/InstCombineAddSub.cpp (original)
+++ llvm/branches/release_35/lib/Transforms/InstCombine/InstCombineAddSub.cpp Mon Dec  8 03:11:36 2014
@@ -1462,11 +1462,17 @@ Instruction *InstCombiner::visitSub(Bina
   if (Value *V = SimplifyUsingDistributiveLaws(I))
     return ReplaceInstUsesWith(I, V);
 
-  // If this is a 'B = x-(-A)', change to B = x+A.  This preserves NSW/NUW.
+  // If this is a 'B = x-(-A)', change to B = x+A.
   if (Value *V = dyn_castNegVal(Op1)) {
     BinaryOperator *Res = BinaryOperator::CreateAdd(Op0, V);
-    Res->setHasNoSignedWrap(I.hasNoSignedWrap());
-    Res->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
+
+    if (const auto *BO = dyn_cast<BinaryOperator>(Op1)) {
+      assert(BO->getOpcode() == Instruction::Sub &&
+             "Expected a subtraction operator!");
+      if (BO->hasNoSignedWrap() && I.hasNoSignedWrap())
+        Res->setHasNoSignedWrap(true);
+    }
+
     return Res;
   }
 

Modified: llvm/branches/release_35/test/Transforms/InstCombine/sub.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_35/test/Transforms/InstCombine/sub.ll?rev=223644&r1=223643&r2=223644&view=diff
==============================================================================
--- llvm/branches/release_35/test/Transforms/InstCombine/sub.ll (original)
+++ llvm/branches/release_35/test/Transforms/InstCombine/sub.ll Mon Dec  8 03:11:36 2014
@@ -464,3 +464,12 @@ define i32 @test38(i32 %A) {
 ; CHECK-NEXT: [[SEXT:%.*]] = sext i1 [[ICMP]] to i32
 ; CHECK-NEXT: ret i32 [[SEXT]]
 }
+
+define i32 @test39(i32 %A, i32 %x) {
+  %B = sub i32 0, %A
+  %C = sub nsw i32 %x, %B
+  ret i32 %C
+; CHECK-LABEL: @test39(
+; CHECK: %C = add i32 %x, %A
+; CHECK: ret i32 %C
+}





More information about the llvm-branch-commits mailing list