* Combine: A-(-B) -> A + B

* Bugfix:  A + -B and -A + B


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2561 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2002-05-08 22:46:53 +00:00
parent 44f87aca32
commit 5c4afb9034

View File

@ -128,13 +128,13 @@ Instruction *InstCombiner::visitAdd(BinaryOperator *I) {
return I;
}
// -B + A --> A - B
// -A + B --> B - A
if (Value *V = dyn_castNegInst(LHS))
return BinaryOperator::create(Instruction::Sub, RHS, LHS);
return BinaryOperator::create(Instruction::Sub, RHS, V);
// A + -B --> A - B
if (Value *V = dyn_castNegInst(RHS))
return BinaryOperator::create(Instruction::Sub, LHS, RHS);
return BinaryOperator::create(Instruction::Sub, LHS, V);
// Simplify add instructions with a constant RHS...
if (Constant *Op2 = dyn_cast<Constant>(RHS)) {
@ -176,13 +176,9 @@ Instruction *InstCombiner::visitSub(BinaryOperator *I) {
if (Constant *RHS = *Constant::getNullValue(I->getType()) - *Op2) // 0 - RHS
return BinaryOperator::create(Instruction::Add, Op0, RHS, I->getName());
// If this is a 'C = -B', check to see if 'B = -A', so that C = A...
if (Op0 == Constant::getNullValue(I->getType()))
if (Value *V = dyn_castNegInst(Op1)) {
AddUsesToWorkList(I); // Add all modified instrs to worklist
I->replaceAllUsesWith(V);
return I;
}
// If this is a 'C = x-B', check to see if 'B = -A', so that C = x+A...
if (Value *V = dyn_castNegInst(Op1))
return BinaryOperator::create(Instruction::Add, Op0, V);
return 0;
}