diff options
author | David Majnemer <david.majnemer@gmail.com> | 2014-07-31 04:49:29 +0000 |
---|---|---|
committer | David Majnemer <david.majnemer@gmail.com> | 2014-07-31 04:49:29 +0000 |
commit | a92687d6365f3bef67f23929c568262045f5b45b (patch) | |
tree | 59c30fa4c30e4a0b440aeb5e02dd9a1623c4bda3 /llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp | |
parent | cd4fbcd1bb463a1d2b99808f52c1b6b99e82ef4d (diff) | |
download | llvm-a92687d6365f3bef67f23929c568262045f5b45b.zip llvm-a92687d6365f3bef67f23929c568262045f5b45b.tar.gz llvm-a92687d6365f3bef67f23929c568262045f5b45b.tar.bz2 |
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.
llvm-svn: 214385
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp index e80d6a9..ed0e1c9 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -1462,11 +1462,17 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { 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; } |