From 2cd85da3ed8e703729fb5adfe8cc2f9b1dd2f6a8 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 2 Sep 2005 06:38:04 +0000 Subject: [PATCH] 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 --- lib/Transforms/Scalar/Reassociate.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/Transforms/Scalar/Reassociate.cpp b/lib/Transforms/Scalar/Reassociate.cpp index a9b0ea6cbba..fb143c98279 100644 --- a/lib/Transforms/Scalar/Reassociate.cpp +++ b/lib/Transforms/Scalar/Reassociate.cpp @@ -318,16 +318,18 @@ static Value *NegateValue(Value *V, Instruction *BI) { // if (Instruction *I = dyn_cast(V)) if (I->getOpcode() == Instruction::Add && I->hasOneUse()) { - Value *RHS = NegateValue(I->getOperand(1), BI); - Value *LHS = NegateValue(I->getOperand(0), BI); + // Push the negates through the add. + 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 - // instructions do not dominate the old add instruction in general. By - // adding it now, we are assured that the neg instructions we just - // inserted dominate the instruction we are about to insert after them. + // We must move the add instruction here, because the neg instructions do + // not dominate the old add instruction in general. By moving it, we are + // assured that the neg instructions we just inserted dominate the + // instruction we are about to insert after them. // - return BinaryOperator::create(Instruction::Add, LHS, RHS, - I->getName()+".neg", BI); + I->moveBefore(BI); + I->setName(I->getName()+".neg"); + return I; } // Insert a 'neg' instruction that subtracts the value from zero to get the