Don't attempt to add 'nsw' when intermediate instructions had no such guarantee.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@137572 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp
index 85091af..4166636 100644
--- a/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -146,7 +146,6 @@
   return !Overflow;
 }
 
-
 /// SimplifyAssociativeOrCommutative - This performs a few simplifications for
 /// operators which are associative or commutative:
 //
@@ -197,7 +196,10 @@
           I.setOperand(1, V);
           // Conservatively clear the optional flags, since they may not be
           // preserved by the reassociation.
-          if (MaintainNoSignedWrap(I, B, C)) {
+          if (MaintainNoSignedWrap(I, B, C) &&
+	      (!Op0 || (isa<BinaryOperator>(Op0) && Op0->hasNoSignedWrap()))) {
+            // Note: this is only valid because SimplifyBinOp doesn't look at
+            // the operands to Op0.
             I.clearSubclassOptionalData();
             I.setHasNoSignedWrap(true);
           } else {
@@ -292,10 +294,11 @@
         I.setOperand(1, Folded);
         // Conservatively clear the optional flags, since they may not be
         // preserved by the reassociation.
-        if (MaintainNoSignedWrap(I, C1, C2)) {
+        if (MaintainNoSignedWrap(I, C1, C2) && Op0->hasNoSignedWrap() &&
+	    Op1->hasNoSignedWrap()) {
+          New->setHasNoSignedWrap(true);
           I.clearSubclassOptionalData();
           I.setHasNoSignedWrap(true);
-          New->setHasNoSignedWrap(true);
         } else {
           I.clearSubclassOptionalData();
         }