Avoid creating garbage instructions, just move the old add instruction

to where we need it when converting -(A+B+C) -> -A + -B + -C.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23213 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2005-09-02 06:38:04 +00:00
parent d6c69e9bba
commit 2cd85da3ed

View File

@ -318,16 +318,18 @@ static Value *NegateValue(Value *V, Instruction *BI) {
// //
if (Instruction *I = dyn_cast<Instruction>(V)) if (Instruction *I = dyn_cast<Instruction>(V))
if (I->getOpcode() == Instruction::Add && I->hasOneUse()) { if (I->getOpcode() == Instruction::Add && I->hasOneUse()) {
Value *RHS = NegateValue(I->getOperand(1), BI); // Push the negates through the add.
Value *LHS = NegateValue(I->getOperand(0), BI); I->setOperand(0, NegateValue(I->getOperand(0), BI));
I->setOperand(1, NegateValue(I->getOperand(1), BI));
// We must actually insert a new add instruction here, because the neg // We must move the add instruction here, because the neg instructions do
// instructions do not dominate the old add instruction in general. By // not dominate the old add instruction in general. By moving it, we are
// adding it now, we are assured that the neg instructions we just // assured that the neg instructions we just inserted dominate the
// inserted dominate the instruction we are about to insert after them. // instruction we are about to insert after them.
// //
return BinaryOperator::create(Instruction::Add, LHS, RHS, I->moveBefore(BI);
I->getName()+".neg", BI); I->setName(I->getName()+".neg");
return I;
} }
// Insert a 'neg' instruction that subtracts the value from zero to get the // Insert a 'neg' instruction that subtracts the value from zero to get the