diff options
Diffstat (limited to 'contrib/llvm/lib/Transforms/Scalar/Reassociate.cpp')
| -rw-r--r-- | contrib/llvm/lib/Transforms/Scalar/Reassociate.cpp | 109 | 
1 files changed, 53 insertions, 56 deletions
diff --git a/contrib/llvm/lib/Transforms/Scalar/Reassociate.cpp b/contrib/llvm/lib/Transforms/Scalar/Reassociate.cpp index d1acf785d07e..fb970c747ce1 100644 --- a/contrib/llvm/lib/Transforms/Scalar/Reassociate.cpp +++ b/contrib/llvm/lib/Transforms/Scalar/Reassociate.cpp @@ -26,6 +26,8 @@  #include "llvm/ADT/STLExtras.h"  #include "llvm/ADT/SetVector.h"  #include "llvm/ADT/Statistic.h" +#include "llvm/Analysis/GlobalsModRef.h" +#include "llvm/Analysis/ValueTracking.h"  #include "llvm/IR/CFG.h"  #include "llvm/IR/Constants.h"  #include "llvm/IR/DerivedTypes.h" @@ -62,7 +64,7 @@ namespace {  /// Print out the expression identified in the Ops list.  ///  static void PrintOps(Instruction *I, const SmallVectorImpl<ValueEntry> &Ops) { -  Module *M = I->getParent()->getParent()->getParent(); +  Module *M = I->getModule();    dbgs() << Instruction::getOpcodeName(I->getOpcode()) << " "         << *Ops[0].Op->getType() << '\t';    for (unsigned i = 0, e = Ops.size(); i != e; ++i) { @@ -82,20 +84,6 @@ namespace {      Factor(Value *Base, unsigned Power) : Base(Base), Power(Power) {} -    /// \brief Sort factors by their Base. -    struct BaseSorter { -      bool operator()(const Factor &LHS, const Factor &RHS) { -        return LHS.Base < RHS.Base; -      } -    }; - -    /// \brief Compare factors for equal bases. -    struct BaseEqual { -      bool operator()(const Factor &LHS, const Factor &RHS) { -        return LHS.Base == RHS.Base; -      } -    }; -      /// \brief Sort factors in descending order by their power.      struct PowerDescendingSorter {        bool operator()(const Factor &LHS, const Factor &RHS) { @@ -172,6 +160,7 @@ namespace {      void getAnalysisUsage(AnalysisUsage &AU) const override {        AU.setPreservesCFG(); +      AU.addPreserved<GlobalsAAWrapperPass>();      }    private:      void BuildRankMap(Function &F); @@ -255,27 +244,6 @@ static BinaryOperator *isReassociableOp(Value *V, unsigned Opcode1,    return nullptr;  } -static bool isUnmovableInstruction(Instruction *I) { -  switch (I->getOpcode()) { -  case Instruction::PHI: -  case Instruction::LandingPad: -  case Instruction::Alloca: -  case Instruction::Load: -  case Instruction::Invoke: -  case Instruction::UDiv: -  case Instruction::SDiv: -  case Instruction::FDiv: -  case Instruction::URem: -  case Instruction::SRem: -  case Instruction::FRem: -    return true; -  case Instruction::Call: -    return !isa<DbgInfoIntrinsic>(I); -  default: -    return false; -  } -} -  void Reassociate::BuildRankMap(Function &F) {    unsigned i = 2; @@ -295,7 +263,7 @@ void Reassociate::BuildRankMap(Function &F) {      // we cannot move.  This ensures that the ranks for these instructions are      // all different in the block.      for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) -      if (isUnmovableInstruction(I)) +      if (mayBeMemoryDependent(*I))          ValueRankMap[&*I] = ++BBRank;    }  } @@ -913,7 +881,11 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,  /// that computes the negative version of the value specified.  The negative  /// version of the value is returned, and BI is left pointing at the instruction  /// that should be processed next by the reassociation pass. -static Value *NegateValue(Value *V, Instruction *BI) { +/// Also add intermediate instructions to the redo list that are modified while +/// pushing the negates through adds.  These will be revisited to see if +/// additional opportunities have been exposed. +static Value *NegateValue(Value *V, Instruction *BI, +                          SetVector<AssertingVH<Instruction>> &ToRedo) {    if (Constant *C = dyn_cast<Constant>(V)) {      if (C->getType()->isFPOrFPVectorTy()) {        return ConstantExpr::getFNeg(C); @@ -934,8 +906,8 @@ static Value *NegateValue(Value *V, Instruction *BI) {    if (BinaryOperator *I =            isReassociableOp(V, Instruction::Add, Instruction::FAdd)) {      // Push the negates through the add. -    I->setOperand(0, NegateValue(I->getOperand(0), BI)); -    I->setOperand(1, NegateValue(I->getOperand(1), BI)); +    I->setOperand(0, NegateValue(I->getOperand(0), BI, ToRedo)); +    I->setOperand(1, NegateValue(I->getOperand(1), BI, ToRedo));      if (I->getOpcode() == Instruction::Add) {        I->setHasNoUnsignedWrap(false);        I->setHasNoSignedWrap(false); @@ -948,6 +920,10 @@ static Value *NegateValue(Value *V, Instruction *BI) {      //      I->moveBefore(BI);      I->setName(I->getName()+".neg"); + +    // Add the intermediate negates to the redo list as processing them later +    // could expose more reassociating opportunities. +    ToRedo.insert(I);      return I;    } @@ -972,26 +948,28 @@ static Value *NegateValue(Value *V, Instruction *BI) {        if (InvokeInst *II = dyn_cast<InvokeInst>(InstInput)) {          InsertPt = II->getNormalDest()->begin();        } else { -        InsertPt = InstInput; -        ++InsertPt; +        InsertPt = ++InstInput->getIterator();        }        while (isa<PHINode>(InsertPt)) ++InsertPt;      } else {        InsertPt = TheNeg->getParent()->getParent()->getEntryBlock().begin();      } -    TheNeg->moveBefore(InsertPt); +    TheNeg->moveBefore(&*InsertPt);      if (TheNeg->getOpcode() == Instruction::Sub) {        TheNeg->setHasNoUnsignedWrap(false);        TheNeg->setHasNoSignedWrap(false);      } else {        TheNeg->andIRFlags(BI);      } +    ToRedo.insert(TheNeg);      return TheNeg;    }    // Insert a 'neg' instruction that subtracts the value from zero to get the    // negation. -  return CreateNeg(V, V->getName() + ".neg", BI, BI); +  BinaryOperator *NewNeg = CreateNeg(V, V->getName() + ".neg", BI, BI); +  ToRedo.insert(NewNeg); +  return NewNeg;  }  /// Return true if we should break up this subtract of X-Y into (X + -Y). @@ -1025,14 +1003,15 @@ static bool ShouldBreakUpSubtract(Instruction *Sub) {  /// If we have (X-Y), and if either X is an add, or if this is only used by an  /// add, transform this into (X+(0-Y)) to promote better reassociation. -static BinaryOperator *BreakUpSubtract(Instruction *Sub) { +static BinaryOperator * +BreakUpSubtract(Instruction *Sub, SetVector<AssertingVH<Instruction>> &ToRedo) {    // Convert a subtract into an add and a neg instruction. This allows sub    // instructions to be commuted with other add instructions.    //    // Calculate the negative value of Operand 1 of the sub instruction,    // and set it as the RHS of the add instruction we just made.    // -  Value *NegVal = NegateValue(Sub->getOperand(1), Sub); +  Value *NegVal = NegateValue(Sub->getOperand(1), Sub, ToRedo);    BinaryOperator *New = CreateAdd(Sub->getOperand(0), NegVal, "", Sub, Sub);    Sub->setOperand(0, Constant::getNullValue(Sub->getType())); // Drop use of op.    Sub->setOperand(1, Constant::getNullValue(Sub->getType())); // Drop use of op. @@ -1166,7 +1145,7 @@ Value *Reassociate::RemoveFactorFromExpression(Value *V, Value *Factor) {      return nullptr;    } -  BasicBlock::iterator InsertPt = BO; ++InsertPt; +  BasicBlock::iterator InsertPt = ++BO->getIterator();    // If this was just a single multiply, remove the multiply and return the only    // remaining operand. @@ -1179,7 +1158,7 @@ Value *Reassociate::RemoveFactorFromExpression(Value *V, Value *Factor) {    }    if (NeedsNegate) -    V = CreateNeg(V, "neg", InsertPt, BO); +    V = CreateNeg(V, "neg", &*InsertPt, BO);    return V;  } @@ -1250,7 +1229,7 @@ static Value *OptimizeAndOrXor(unsigned Opcode,    return nullptr;  } -/// Helper funciton of CombineXorOpnd(). It creates a bitwise-and +/// Helper function of CombineXorOpnd(). It creates a bitwise-and  /// instruction with the given two operands, and return the resulting  /// instruction. There are two special cases: 1) if the constant operand is 0,  /// it will return NULL. 2) if the constant is ~0, the symbolic operand will @@ -2083,7 +2062,7 @@ void Reassociate::OptimizeInst(Instruction *I) {      return;    // Don't optimize floating point instructions that don't have unsafe algebra. -  if (I->getType()->isFloatingPointTy() && !I->hasUnsafeAlgebra()) +  if (I->getType()->isFPOrFPVectorTy() && !I->hasUnsafeAlgebra())      return;    // Do not reassociate boolean (i1) expressions.  We want to preserve the @@ -2099,7 +2078,7 @@ void Reassociate::OptimizeInst(Instruction *I) {    // see if we can convert it to X+-Y.    if (I->getOpcode() == Instruction::Sub) {      if (ShouldBreakUpSubtract(I)) { -      Instruction *NI = BreakUpSubtract(I); +      Instruction *NI = BreakUpSubtract(I, RedoInsts);        RedoInsts.insert(I);        MadeChange = true;        I = NI; @@ -2110,6 +2089,12 @@ void Reassociate::OptimizeInst(Instruction *I) {            (!I->hasOneUse() ||             !isReassociableOp(I->user_back(), Instruction::Mul))) {          Instruction *NI = LowerNegateToMultiply(I); +        // If the negate was simplified, revisit the users to see if we can +        // reassociate further. +        for (User *U : NI->users()) { +          if (BinaryOperator *Tmp = dyn_cast<BinaryOperator>(U)) +            RedoInsts.insert(Tmp); +        }          RedoInsts.insert(I);          MadeChange = true;          I = NI; @@ -2117,7 +2102,7 @@ void Reassociate::OptimizeInst(Instruction *I) {      }    } else if (I->getOpcode() == Instruction::FSub) {      if (ShouldBreakUpSubtract(I)) { -      Instruction *NI = BreakUpSubtract(I); +      Instruction *NI = BreakUpSubtract(I, RedoInsts);        RedoInsts.insert(I);        MadeChange = true;        I = NI; @@ -2127,7 +2112,13 @@ void Reassociate::OptimizeInst(Instruction *I) {        if (isReassociableOp(I->getOperand(1), Instruction::FMul) &&            (!I->hasOneUse() ||             !isReassociableOp(I->user_back(), Instruction::FMul))) { +        // If the negate was simplified, revisit the users to see if we can +        // reassociate further.          Instruction *NI = LowerNegateToMultiply(I); +        for (User *U : NI->users()) { +          if (BinaryOperator *Tmp = dyn_cast<BinaryOperator>(U)) +            RedoInsts.insert(Tmp); +        }          RedoInsts.insert(I);          MadeChange = true;          I = NI; @@ -2142,8 +2133,14 @@ void Reassociate::OptimizeInst(Instruction *I) {    // If this is an interior node of a reassociable tree, ignore it until we    // get to the root of the tree, to avoid N^2 analysis.    unsigned Opcode = BO->getOpcode(); -  if (BO->hasOneUse() && BO->user_back()->getOpcode() == Opcode) +  if (BO->hasOneUse() && BO->user_back()->getOpcode() == Opcode) { +    // During the initial run we will get to the root of the tree. +    // But if we get here while we are redoing instructions, there is no +    // guarantee that the root will be visited. So Redo later +    if (BO->user_back() != BO) +      RedoInsts.insert(BO->user_back());      return; +  }    // If this is an add tree that is used by a sub instruction, ignore it    // until we process the subtract. @@ -2250,10 +2247,10 @@ bool Reassociate::runOnFunction(Function &F) {    for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {      // Optimize every instruction in the basic block.      for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE; ) -      if (isInstructionTriviallyDead(II)) { -        EraseInst(II++); +      if (isInstructionTriviallyDead(&*II)) { +        EraseInst(&*II++);        } else { -        OptimizeInst(II); +        OptimizeInst(&*II);          assert(II->getParent() == BI && "Moved to a different block!");          ++II;        }  | 
