diff --git a/include/llvm/Analysis/ScalarEvolution.h b/include/llvm/Analysis/ScalarEvolution.h index a0a7c1e81d2..fb22fd47b3a 100644 --- a/include/llvm/Analysis/ScalarEvolution.h +++ b/include/llvm/Analysis/ScalarEvolution.h @@ -24,6 +24,7 @@ #include "llvm/Pass.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/Support/DataTypes.h" +#include "llvm/Support/ValueHandle.h" #include namespace llvm { @@ -140,13 +141,23 @@ namespace llvm { static bool classof(const SCEV *S); }; + /// SCEVCallbackVH - A CallbackVH to arrange for ScalarEvolution to be + /// notified whenever a Value is deleted. + class SCEVCallbackVH : public CallbackVH { + ScalarEvolution *SE; + virtual void deleted(); + virtual void allUsesReplacedWith(Value *New); + public: + SCEVCallbackVH(Value *V, ScalarEvolution *SE = 0); + }; + /// SCEVHandle - This class is used to maintain the SCEV object's refcounts, /// freeing the objects when the last reference is dropped. class SCEVHandle { - SCEV *S; + const SCEV *S; SCEVHandle(); // DO NOT IMPLEMENT public: - SCEVHandle(const SCEV *s) : S(const_cast(s)) { + SCEVHandle(const SCEV *s) : S(s) { assert(S && "Cannot create a handle to a null SCEV!"); S->addRef(); } @@ -155,13 +166,13 @@ namespace llvm { } ~SCEVHandle() { S->dropRef(); } - operator SCEV*() const { return S; } + operator const SCEV*() const { return S; } - SCEV &operator*() const { return *S; } - SCEV *operator->() const { return S; } + const SCEV &operator*() const { return *S; } + const SCEV *operator->() const { return S; } - bool operator==(SCEV *RHS) const { return S == RHS; } - bool operator!=(SCEV *RHS) const { return S != RHS; } + bool operator==(const SCEV *RHS) const { return S == RHS; } + bool operator!=(const SCEV *RHS) const { return S != RHS; } const SCEVHandle &operator=(SCEV *RHS) { if (S != RHS) { @@ -184,7 +195,7 @@ namespace llvm { template struct simplify_type; template<> struct simplify_type { - typedef SCEV* SimpleType; + typedef const SCEV* SimpleType; static SimpleType getSimplifiedValue(const SCEVHandle &Node) { return Node; } @@ -197,6 +208,8 @@ namespace llvm { /// they must ask this class for services. /// class ScalarEvolution : public FunctionPass { + friend class SCEVCallbackVH; + /// F - The function we are analyzing. /// Function *F; @@ -215,7 +228,7 @@ namespace llvm { /// Scalars - This is a cache of the scalars we have analyzed so far. /// - std::map Scalars; + std::map Scalars; /// BackedgeTakenInfo - Information about the backedge-taken count /// of a loop. This currently inclues an exact count and a maximum count. @@ -232,7 +245,7 @@ namespace llvm { /*implicit*/ BackedgeTakenInfo(SCEVHandle exact) : Exact(exact), Max(exact) {} - /*implicit*/ BackedgeTakenInfo(SCEV *exact) : + /*implicit*/ BackedgeTakenInfo(const SCEV *exact) : Exact(exact), Max(exact) {} BackedgeTakenInfo(SCEVHandle exact, SCEVHandle max) : @@ -302,18 +315,18 @@ namespace llvm { /// HowFarToZero - Return the number of times a backedge comparing the /// specified value to zero will execute. If not computable, return /// UnknownValue. - SCEVHandle HowFarToZero(SCEV *V, const Loop *L); + SCEVHandle HowFarToZero(const SCEV *V, const Loop *L); /// HowFarToNonZero - Return the number of times a backedge checking the /// specified value for nonzero will execute. If not computable, return /// UnknownValue. - SCEVHandle HowFarToNonZero(SCEV *V, const Loop *L); + SCEVHandle HowFarToNonZero(const SCEV *V, const Loop *L); /// HowManyLessThans - Return the number of times a backedge containing the /// specified less-than comparison will execute. If not computable, return /// UnknownValue. isSigned specifies whether the less-than is signed. - BackedgeTakenInfo HowManyLessThans(SCEV *LHS, SCEV *RHS, const Loop *L, - bool isSigned); + BackedgeTakenInfo HowManyLessThans(const SCEV *LHS, const SCEV *RHS, + const Loop *L, bool isSigned); /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB /// (which may not be an immediate predecessor) which has exactly one @@ -331,7 +344,7 @@ namespace llvm { /// getSCEVAtScope - Compute the value of the specified expression within /// the indicated loop (which may be null to indicate in no loop). If the /// expression cannot be evaluated, return UnknownValue itself. - SCEVHandle getSCEVAtScope(SCEV *S, const Loop *L); + SCEVHandle getSCEVAtScope(const SCEV *S, const Loop *L); /// forgetLoopPHIs - Delete the memoized SCEVs associated with the /// PHI nodes in the given loop. This is used when the trip count of @@ -457,7 +470,7 @@ namespace llvm { /// a conditional between LHS and RHS. This is used to help avoid max /// expressions in loop trip counts. bool isLoopGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, - SCEV *LHS, SCEV *RHS); + const SCEV *LHS, const SCEV *RHS); /// getBackedgeTakenCount - If the specified loop has a predictable /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute @@ -487,11 +500,6 @@ namespace llvm { /// is deleted. void forgetLoopBackedgeTakenCount(const Loop *L); - /// deleteValueFromRecords - This method should be called by the - /// client before it removes a Value from the program, to make sure - /// that no dangling references are left around. - void deleteValueFromRecords(Value *V); - virtual bool runOnFunction(Function &F); virtual void releaseMemory(); virtual void getAnalysisUsage(AnalysisUsage &AU) const; diff --git a/include/llvm/Transforms/Utils/BasicBlockUtils.h b/include/llvm/Transforms/Utils/BasicBlockUtils.h index 367e4b4b065..95ffa460696 100644 --- a/include/llvm/Transforms/Utils/BasicBlockUtils.h +++ b/include/llvm/Transforms/Utils/BasicBlockUtils.h @@ -25,7 +25,6 @@ namespace llvm { class Instruction; class Pass; class AliasAnalysis; -class ValueDeletionListener; /// DeleteDeadBlock - Delete the specified block, which must have no /// predecessors. @@ -41,9 +40,8 @@ void FoldSingleEntryPHINodes(BasicBlock *BB); /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it /// is dead. Also recursively delete any operands that become dead as /// a result. This includes tracing the def-use list from the PHI to see if -/// it is ultimately unused or if it reaches an unused cycle. If a -/// ValueDeletionListener is specified, it is notified of the deletions. -void DeleteDeadPHIs(BasicBlock *BB, ValueDeletionListener *VDL = 0); +/// it is ultimately unused or if it reaches an unused cycle. +void DeleteDeadPHIs(BasicBlock *BB); /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, /// if possible. The return value indicates success or failure. diff --git a/include/llvm/Transforms/Utils/Local.h b/include/llvm/Transforms/Utils/Local.h index 4cb46b0c4b6..5ea1a500c59 100644 --- a/include/llvm/Transforms/Utils/Local.h +++ b/include/llvm/Transforms/Utils/Local.h @@ -50,40 +50,17 @@ bool ConstantFoldTerminator(BasicBlock *BB); /// bool isInstructionTriviallyDead(Instruction *I); -/// ValueDeletionListener - A simple abstract interface for delivering -/// notifications when Values are deleted. -/// -/// @todo Consider whether ValueDeletionListener can be made obsolete by -/// requiring clients to use CallbackVH instead. -class ValueDeletionListener { -public: - /// ValueWillBeDeleted - This method is called shortly before the specified - /// value will be deleted. - virtual void ValueWillBeDeleted(Value *V) = 0; - -protected: - virtual ~ValueDeletionListener(); -}; - /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a /// trivially dead instruction, delete it. If that makes any of its operands /// trivially dead, delete them too, recursively. -/// -/// If a ValueDeletionListener is specified, it is notified of instructions that -/// are actually deleted (before they are actually deleted). -void RecursivelyDeleteTriviallyDeadInstructions(Value *V, - ValueDeletionListener *VDL = 0); +void RecursivelyDeleteTriviallyDeadInstructions(Value *V); /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively /// dead PHI node, due to being a def-use chain of single-use nodes that /// either forms a cycle or is terminated by a trivially dead instruction, /// delete it. If that makes any of its operands trivially dead, delete them /// too, recursively. -/// -/// If a ValueDeletionListener is specified, it is notified of instructions that -/// are actually deleted (before they are actually deleted). -void RecursivelyDeleteDeadPHINode(PHINode *PN, - ValueDeletionListener *VDL = 0); +void RecursivelyDeleteDeadPHINode(PHINode *PN); //===----------------------------------------------------------------------===// // Control Flow Graph Restructuring. diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp index 83671d6eb92..1b3aae87895 100644 --- a/lib/Analysis/ScalarEvolution.cpp +++ b/lib/Analysis/ScalarEvolution.cpp @@ -204,7 +204,7 @@ bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { // SCEVTruncates - Only allow the creation of one SCEVTruncateExpr for any // particular input. Don't use a SCEVHandle here, or else the object will // never be deleted! -static ManagedStatic, +static ManagedStatic, SCEVTruncateExpr*> > SCEVTruncates; SCEVTruncateExpr::SCEVTruncateExpr(const SCEVHandle &op, const Type *ty) @@ -225,7 +225,7 @@ void SCEVTruncateExpr::print(raw_ostream &OS) const { // SCEVZeroExtends - Only allow the creation of one SCEVZeroExtendExpr for any // particular input. Don't use a SCEVHandle here, or else the object will never // be deleted! -static ManagedStatic, +static ManagedStatic, SCEVZeroExtendExpr*> > SCEVZeroExtends; SCEVZeroExtendExpr::SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty) @@ -246,7 +246,7 @@ void SCEVZeroExtendExpr::print(raw_ostream &OS) const { // SCEVSignExtends - Only allow the creation of one SCEVSignExtendExpr for any // particular input. Don't use a SCEVHandle here, or else the object will never // be deleted! -static ManagedStatic, +static ManagedStatic, SCEVSignExtendExpr*> > SCEVSignExtends; SCEVSignExtendExpr::SCEVSignExtendExpr(const SCEVHandle &op, const Type *ty) @@ -267,13 +267,12 @@ void SCEVSignExtendExpr::print(raw_ostream &OS) const { // SCEVCommExprs - Only allow the creation of one SCEVCommutativeExpr for any // particular input. Don't use a SCEVHandle here, or else the object will never // be deleted! -static ManagedStatic >, +static ManagedStatic >, SCEVCommutativeExpr*> > SCEVCommExprs; SCEVCommutativeExpr::~SCEVCommutativeExpr() { - SCEVCommExprs->erase(std::make_pair(getSCEVType(), - std::vector(Operands.begin(), - Operands.end()))); + std::vector SCEVOps(Operands.begin(), Operands.end()); + SCEVCommExprs->erase(std::make_pair(getSCEVType(), SCEVOps)); } void SCEVCommutativeExpr::print(raw_ostream &OS) const { @@ -329,7 +328,7 @@ bool SCEVCommutativeExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { // SCEVUDivs - Only allow the creation of one SCEVUDivExpr for any particular // input. Don't use a SCEVHandle here, or else the object will never be // deleted! -static ManagedStatic, +static ManagedStatic, SCEVUDivExpr*> > SCEVUDivs; SCEVUDivExpr::~SCEVUDivExpr() { @@ -351,13 +350,13 @@ const Type *SCEVUDivExpr::getType() const { // SCEVAddRecExprs - Only allow the creation of one SCEVAddRecExpr for any // particular input. Don't use a SCEVHandle here, or else the object will never // be deleted! -static ManagedStatic >, +static ManagedStatic >, SCEVAddRecExpr*> > SCEVAddRecExprs; SCEVAddRecExpr::~SCEVAddRecExpr() { - SCEVAddRecExprs->erase(std::make_pair(L, - std::vector(Operands.begin(), - Operands.end()))); + std::vector SCEVOps(Operands.begin(), Operands.end()); + SCEVAddRecExprs->erase(std::make_pair(L, SCEVOps)); } bool SCEVAddRecExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { @@ -480,7 +479,7 @@ static void GroupByComplexity(std::vector &Ops) { // be extremely short in practice. Note that we take this approach because we // do not want to depend on the addresses of the objects we are grouping. for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { - SCEV *S = Ops[i]; + const SCEV *S = Ops[i]; unsigned Complexity = S->getSCEVType(); // If there are any objects of the same complexity and same value as this @@ -919,9 +918,9 @@ SCEVHandle ScalarEvolution::getAddExpr(std::vector &Ops) { // something is not already an operand of the multiply. If so, merge it into // the multiply. for (; Idx < Ops.size() && isa(Ops[Idx]); ++Idx) { - SCEVMulExpr *Mul = cast(Ops[Idx]); + const SCEVMulExpr *Mul = cast(Ops[Idx]); for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { - SCEV *MulOpSCEV = Mul->getOperand(MulOp); + const SCEV *MulOpSCEV = Mul->getOperand(MulOp); for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) if (MulOpSCEV == Ops[AddOp] && !isa(MulOpSCEV)) { // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) @@ -952,7 +951,7 @@ SCEVHandle ScalarEvolution::getAddExpr(std::vector &Ops) { for (unsigned OtherMulIdx = Idx+1; OtherMulIdx < Ops.size() && isa(Ops[OtherMulIdx]); ++OtherMulIdx) { - SCEVMulExpr *OtherMul = cast(Ops[OtherMulIdx]); + const SCEVMulExpr *OtherMul = cast(Ops[OtherMulIdx]); // If MulOp occurs in OtherMul, we can fold the two multiplies // together. for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); @@ -995,7 +994,7 @@ SCEVHandle ScalarEvolution::getAddExpr(std::vector &Ops) { // Scan all of the other operands to this add and add them to the vector if // they are loop invariant w.r.t. the recurrence. std::vector LIOps; - SCEVAddRecExpr *AddRec = cast(Ops[Idx]); + const SCEVAddRecExpr *AddRec = cast(Ops[Idx]); for (unsigned i = 0, e = Ops.size(); i != e; ++i) if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { LIOps.push_back(Ops[i]); @@ -1030,7 +1029,7 @@ SCEVHandle ScalarEvolution::getAddExpr(std::vector &Ops) { for (unsigned OtherIdx = Idx+1; OtherIdx < Ops.size() && isa(Ops[OtherIdx]);++OtherIdx) if (OtherIdx != Idx) { - SCEVAddRecExpr *OtherAddRec = cast(Ops[OtherIdx]); + const SCEVAddRecExpr *OtherAddRec = cast(Ops[OtherIdx]); if (AddRec->getLoop() == OtherAddRec->getLoop()) { // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D} std::vector NewOps(AddRec->op_begin(), AddRec->op_end()); @@ -1059,7 +1058,7 @@ SCEVHandle ScalarEvolution::getAddExpr(std::vector &Ops) { // Okay, it looks like we really DO need an add expr. Check to see if we // already have one, otherwise create a new one. - std::vector SCEVOps(Ops.begin(), Ops.end()); + std::vector SCEVOps(Ops.begin(), Ops.end()); SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scAddExpr, SCEVOps)]; if (Result == 0) Result = new SCEVAddExpr(Ops); @@ -1143,7 +1142,7 @@ SCEVHandle ScalarEvolution::getMulExpr(std::vector &Ops) { // Scan all of the other operands to this mul and add them to the vector if // they are loop invariant w.r.t. the recurrence. std::vector LIOps; - SCEVAddRecExpr *AddRec = cast(Ops[Idx]); + const SCEVAddRecExpr *AddRec = cast(Ops[Idx]); for (unsigned i = 0, e = Ops.size(); i != e; ++i) if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { LIOps.push_back(Ops[i]); @@ -1157,7 +1156,7 @@ SCEVHandle ScalarEvolution::getMulExpr(std::vector &Ops) { std::vector NewOps; NewOps.reserve(AddRec->getNumOperands()); if (LIOps.size() == 1) { - SCEV *Scale = LIOps[0]; + const SCEV *Scale = LIOps[0]; for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i))); } else { @@ -1188,10 +1187,10 @@ SCEVHandle ScalarEvolution::getMulExpr(std::vector &Ops) { for (unsigned OtherIdx = Idx+1; OtherIdx < Ops.size() && isa(Ops[OtherIdx]);++OtherIdx) if (OtherIdx != Idx) { - SCEVAddRecExpr *OtherAddRec = cast(Ops[OtherIdx]); + const SCEVAddRecExpr *OtherAddRec = cast(Ops[OtherIdx]); if (AddRec->getLoop() == OtherAddRec->getLoop()) { // F * G --> {A,+,B} * {C,+,D} --> {A*C,+,F*D + G*B + B*D} - SCEVAddRecExpr *F = AddRec, *G = OtherAddRec; + const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec; SCEVHandle NewStart = getMulExpr(F->getStart(), G->getStart()); SCEVHandle B = F->getStepRecurrence(*this); @@ -1216,7 +1215,7 @@ SCEVHandle ScalarEvolution::getMulExpr(std::vector &Ops) { // Okay, it looks like we really DO need an mul expr. Check to see if we // already have one, otherwise create a new one. - std::vector SCEVOps(Ops.begin(), Ops.end()); + std::vector SCEVOps(Ops.begin(), Ops.end()); SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scMulExpr, SCEVOps)]; if (Result == 0) @@ -1286,9 +1285,8 @@ SCEVHandle ScalarEvolution::getAddRecExpr(std::vector &Operands, } } - SCEVAddRecExpr *&Result = - (*SCEVAddRecExprs)[std::make_pair(L, std::vector(Operands.begin(), - Operands.end()))]; + std::vector SCEVOps(Operands.begin(), Operands.end()); + SCEVAddRecExpr *&Result = (*SCEVAddRecExprs)[std::make_pair(L, SCEVOps)]; if (Result == 0) Result = new SCEVAddRecExpr(Operands, L); return Result; } @@ -1366,7 +1364,7 @@ SCEVHandle ScalarEvolution::getSMaxExpr(std::vector Ops) { // Okay, it looks like we really DO need an smax expr. Check to see if we // already have one, otherwise create a new one. - std::vector SCEVOps(Ops.begin(), Ops.end()); + std::vector SCEVOps(Ops.begin(), Ops.end()); SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scSMaxExpr, SCEVOps)]; if (Result == 0) Result = new SCEVSMaxExpr(Ops); @@ -1446,7 +1444,7 @@ SCEVHandle ScalarEvolution::getUMaxExpr(std::vector Ops) { // Okay, it looks like we really DO need a umax expr. Check to see if we // already have one, otherwise create a new one. - std::vector SCEVOps(Ops.begin(), Ops.end()); + std::vector SCEVOps(Ops.begin(), Ops.end()); SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scUMaxExpr, SCEVOps)]; if (Result == 0) Result = new SCEVUMaxExpr(Ops); @@ -1467,34 +1465,6 @@ SCEVHandle ScalarEvolution::getUnknown(Value *V) { // Basic SCEV Analysis and PHI Idiom Recognition Code // -/// deleteValueFromRecords - This method should be called by the -/// client before it removes an instruction from the program, to make sure -/// that no dangling references are left around. -void ScalarEvolution::deleteValueFromRecords(Value *V) { - SmallVector Worklist; - - if (Scalars.erase(V)) { - if (PHINode *PN = dyn_cast(V)) - ConstantEvolutionLoopExitValue.erase(PN); - Worklist.push_back(V); - } - - while (!Worklist.empty()) { - Value *VV = Worklist.back(); - Worklist.pop_back(); - - for (Instruction::use_iterator UI = VV->use_begin(), UE = VV->use_end(); - UI != UE; ++UI) { - Instruction *Inst = cast(*UI); - if (Scalars.erase(Inst)) { - if (PHINode *PN = dyn_cast(VV)) - ConstantEvolutionLoopExitValue.erase(PN); - Worklist.push_back(Inst); - } - } - } -} - /// isSCEVable - Test if values of the given type are analyzable within /// the SCEV framework. This primarily includes integer types, and it /// can optionally include pointer types if the ScalarEvolution class @@ -1556,10 +1526,10 @@ bool ScalarEvolution::hasSCEV(Value *V) const { SCEVHandle ScalarEvolution::getSCEV(Value *V) { assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); - std::map::iterator I = Scalars.find(V); + std::map::iterator I = Scalars.find(V); if (I != Scalars.end()) return I->second; SCEVHandle S = createSCEV(V); - Scalars.insert(std::make_pair(V, S)); + Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S)); return S; } @@ -1648,7 +1618,8 @@ ScalarEvolution::getTruncateOrSignExtend(const SCEVHandle &V, void ScalarEvolution:: ReplaceSymbolicValueWithConcrete(Instruction *I, const SCEVHandle &SymName, const SCEVHandle &NewVal) { - std::map::iterator SI = Scalars.find(I); + std::map::iterator SI = + Scalars.find(SCEVCallbackVH(I, this)); if (SI == Scalars.end()) return; SCEVHandle NV = @@ -1680,7 +1651,7 @@ SCEVHandle ScalarEvolution::createNodeForPHI(PHINode *PN) { SCEVHandle SymbolicName = getUnknown(PN); assert(Scalars.find(PN) == Scalars.end() && "PHI node already processed?"); - Scalars.insert(std::make_pair(PN, SymbolicName)); + Scalars.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName)); // Using this symbolic name for the PHI, analyze the value coming around // the back-edge. @@ -2131,9 +2102,20 @@ void ScalarEvolution::forgetLoopBackedgeTakenCount(const Loop *L) { /// PHI nodes in the given loop. This is used when the trip count of /// the loop may have changed. void ScalarEvolution::forgetLoopPHIs(const Loop *L) { - for (BasicBlock::iterator I = L->getHeader()->begin(); + BasicBlock *Header = L->getHeader(); + + SmallVector Worklist; + for (BasicBlock::iterator I = Header->begin(); PHINode *PN = dyn_cast(I); ++I) - deleteValueFromRecords(PN); + Worklist.push_back(PN); + + while (!Worklist.empty()) { + Instruction *I = Worklist.pop_back_val(); + if (Scalars.erase(I)) + for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); + UI != UE; ++UI) + Worklist.push_back(cast(UI)); + } } /// ComputeBackedgeTakenCount - Compute the number of times the backedge @@ -2384,7 +2366,7 @@ ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI, Constant *RHS, // We can only recognize very limited forms of loop index expressions, in // particular, only affine AddRec's like {C1,+,C2}. - SCEVAddRecExpr *IdxExpr = dyn_cast(Idx); + const SCEVAddRecExpr *IdxExpr = dyn_cast(Idx); if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) || !isa(IdxExpr->getOperand(0)) || !isa(IdxExpr->getOperand(1))) @@ -2605,7 +2587,7 @@ ComputeBackedgeTakenCountExhaustively(const Loop *L, Value *Cond, bool ExitWhen) /// getSCEVAtScope - Compute the value of the specified expression within the /// indicated loop (which may be null to indicate in no loop). If the /// expression cannot be evaluated, return UnknownValue. -SCEVHandle ScalarEvolution::getSCEVAtScope(SCEV *V, const Loop *L) { +SCEVHandle ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) { // FIXME: this should be turned into a virtual method on SCEV! if (isa(V)) return V; @@ -2847,13 +2829,13 @@ static SCEVHandle SolveLinEquationWithOverflow(const APInt &A, const APInt &B, static std::pair SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); - SCEVConstant *LC = dyn_cast(AddRec->getOperand(0)); - SCEVConstant *MC = dyn_cast(AddRec->getOperand(1)); - SCEVConstant *NC = dyn_cast(AddRec->getOperand(2)); + const SCEVConstant *LC = dyn_cast(AddRec->getOperand(0)); + const SCEVConstant *MC = dyn_cast(AddRec->getOperand(1)); + const SCEVConstant *NC = dyn_cast(AddRec->getOperand(2)); // We currently can only solve this if the coefficients are constants. if (!LC || !MC || !NC) { - SCEV *CNC = SE.getCouldNotCompute(); + const SCEV *CNC = SE.getCouldNotCompute(); return std::make_pair(CNC, CNC); } @@ -2889,7 +2871,7 @@ SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { APInt NegB(-B); APInt TwoA( A << 1 ); if (TwoA.isMinValue()) { - SCEV *CNC = SE.getCouldNotCompute(); + const SCEV *CNC = SE.getCouldNotCompute(); return std::make_pair(CNC, CNC); } @@ -2903,7 +2885,7 @@ SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { /// HowFarToZero - Return the number of times a backedge comparing the specified /// value to zero will execute. If not computable, return UnknownValue -SCEVHandle ScalarEvolution::HowFarToZero(SCEV *V, const Loop *L) { +SCEVHandle ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) { // If the value is a constant if (const SCEVConstant *C = dyn_cast(V)) { // If the value is already zero, the branch will execute zero times. @@ -2911,7 +2893,7 @@ SCEVHandle ScalarEvolution::HowFarToZero(SCEV *V, const Loop *L) { return UnknownValue; // Otherwise it will loop infinitely. } - SCEVAddRecExpr *AddRec = dyn_cast(V); + const SCEVAddRecExpr *AddRec = dyn_cast(V); if (!AddRec || AddRec->getLoop() != L) return UnknownValue; @@ -2953,8 +2935,8 @@ SCEVHandle ScalarEvolution::HowFarToZero(SCEV *V, const Loop *L) { // the quadratic equation to solve it. std::pair Roots = SolveQuadraticEquation(AddRec, *this); - SCEVConstant *R1 = dyn_cast(Roots.first); - SCEVConstant *R2 = dyn_cast(Roots.second); + const SCEVConstant *R1 = dyn_cast(Roots.first); + const SCEVConstant *R2 = dyn_cast(Roots.second); if (R1) { #if 0 errs() << "HFTZ: " << *V << " - sol#1: " << *R1 @@ -2983,7 +2965,7 @@ SCEVHandle ScalarEvolution::HowFarToZero(SCEV *V, const Loop *L) { /// HowFarToNonZero - Return the number of times a backedge checking the /// specified value for nonzero will execute. If not computable, return /// UnknownValue -SCEVHandle ScalarEvolution::HowFarToNonZero(SCEV *V, const Loop *L) { +SCEVHandle ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) { // Loops that look like: while (X == 0) are very strange indeed. We don't // handle them yet except for the trivial case. This could be expanded in the // future as needed. @@ -3029,7 +3011,7 @@ ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) { /// expressions in loop trip counts. bool ScalarEvolution::isLoopGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, - SCEV *LHS, SCEV *RHS) { + const SCEV *LHS, const SCEV *RHS) { BasicBlock *Preheader = L->getLoopPreheader(); BasicBlock *PreheaderDest = L->getHeader(); @@ -3133,11 +3115,12 @@ bool ScalarEvolution::isLoopGuardedByCond(const Loop *L, /// specified less-than comparison will execute. If not computable, return /// UnknownValue. ScalarEvolution::BackedgeTakenInfo ScalarEvolution:: -HowManyLessThans(SCEV *LHS, SCEV *RHS, const Loop *L, bool isSigned) { +HowManyLessThans(const SCEV *LHS, const SCEV *RHS, + const Loop *L, bool isSigned) { // Only handle: "ADDREC < LoopInvariant". if (!RHS->isLoopInvariant(L)) return UnknownValue; - SCEVAddRecExpr *AddRec = dyn_cast(LHS); + const SCEVAddRecExpr *AddRec = dyn_cast(LHS); if (!AddRec || AddRec->getLoop() != L) return UnknownValue; @@ -3304,8 +3287,8 @@ SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, // Next, solve the constructed addrec std::pair Roots = SolveQuadraticEquation(cast(NewAddRec), SE); - SCEVConstant *R1 = dyn_cast(Roots.first); - SCEVConstant *R2 = dyn_cast(Roots.second); + const SCEVConstant *R1 = dyn_cast(Roots.first); + const SCEVConstant *R2 = dyn_cast(Roots.second); if (R1) { // Pick the smallest positive root value. if (ConstantInt *CB = @@ -3346,6 +3329,57 @@ SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, +//===----------------------------------------------------------------------===// +// SCEVCallbackVH Class Implementation +//===----------------------------------------------------------------------===// + +void SCEVCallbackVH::deleted() { + assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!"); + if (PHINode *PN = dyn_cast(getValPtr())) + SE->ConstantEvolutionLoopExitValue.erase(PN); + SE->Scalars.erase(getValPtr()); + // this now dangles! +} + +void SCEVCallbackVH::allUsesReplacedWith(Value *) { + assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!"); + + // Forget all the expressions associated with users of the old value, + // so that future queries will recompute the expressions using the new + // value. + SmallVector Worklist; + Value *Old = getValPtr(); + bool DeleteOld = false; + for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end(); + UI != UE; ++UI) + Worklist.push_back(*UI); + while (!Worklist.empty()) { + User *U = Worklist.pop_back_val(); + // Deleting the Old value will cause this to dangle. Postpone + // that until everything else is done. + if (U == Old) { + DeleteOld = true; + continue; + } + if (PHINode *PN = dyn_cast(U)) + SE->ConstantEvolutionLoopExitValue.erase(PN); + if (SE->Scalars.erase(U)) + for (Value::use_iterator UI = U->use_begin(), UE = U->use_end(); + UI != UE; ++UI) + Worklist.push_back(*UI); + } + if (DeleteOld) { + if (PHINode *PN = dyn_cast(Old)) + SE->ConstantEvolutionLoopExitValue.erase(PN); + SE->Scalars.erase(Old); + // this now dangles! + } + // this may dangle! +} + +SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se) + : CallbackVH(V), SE(se) {} + //===----------------------------------------------------------------------===// // ScalarEvolution Class Implementation //===----------------------------------------------------------------------===// diff --git a/lib/Transforms/Scalar/IndVarSimplify.cpp b/lib/Transforms/Scalar/IndVarSimplify.cpp index be94c457934..3d9017d17e3 100644 --- a/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -124,7 +124,6 @@ DeleteTriviallyDeadInstructions(SmallPtrSet &Insts) { for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) if (Instruction *U = dyn_cast(I->getOperand(i))) Insts.insert(U); - SE->deleteValueFromRecords(I); DOUT << "INDVARS: Deleting: " << *I; I->eraseFromParent(); Changed = true; @@ -308,7 +307,6 @@ void IndVarSimplify::RewriteLoopExitValues(Loop *L, // the PHI entirely. This is safe, because the NewVal won't be variant // in the loop, so we don't need an LCSSA phi node anymore. if (NumPreds == 1) { - SE->deleteValueFromRecords(PN); PN->replaceAllUsesWith(ExitVal); PN->eraseFromParent(); break; diff --git a/lib/Transforms/Scalar/LoopDeletion.cpp b/lib/Transforms/Scalar/LoopDeletion.cpp index 83c25619bfc..96b7a5288a7 100644 --- a/lib/Transforms/Scalar/LoopDeletion.cpp +++ b/lib/Transforms/Scalar/LoopDeletion.cpp @@ -246,13 +246,6 @@ bool LoopDeletion::runOnLoop(Loop* L, LPPassManager& LPM) { DT.eraseNode(*LI); if (DF) DF->removeBlock(*LI); - // Remove instructions that we're deleting from ScalarEvolution. - for (BasicBlock::iterator BI = (*LI)->begin(), BE = (*LI)->end(); - BI != BE; ++BI) - SE.deleteValueFromRecords(BI); - - SE.deleteValueFromRecords(*LI); - // Remove the block from the reference counting scheme, so that we can // delete it freely later. (*LI)->dropAllReferences(); diff --git a/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/lib/Transforms/Scalar/LoopStrengthReduce.cpp index 50603d97578..4365adc629f 100644 --- a/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ b/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -253,8 +253,6 @@ void LoopStrengthReduce::DeleteTriviallyDeadInstructions() { if (I == 0 || !isInstructionTriviallyDead(I)) continue; - SE->deleteValueFromRecords(I); - for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) { if (Instruction *U = dyn_cast(*OI)) { *OI = 0; @@ -2130,7 +2128,6 @@ ICmpInst *LoopStrengthReduce::ChangeCompareStride(Loop *L, ICmpInst *Cond, // Remove the old compare instruction. The old indvar is probably dead too. DeadInsts.push_back(cast(CondUse->OperandValToReplace)); - SE->deleteValueFromRecords(OldCond); OldCond->replaceAllUsesWith(Cond); OldCond->eraseFromParent(); @@ -2214,7 +2211,7 @@ ICmpInst *LoopStrengthReduce::OptimizeSMax(Loop *L, ICmpInst *Cond, SCEVHandle IterationCount = SE->getAddExpr(BackedgeTakenCount, One); // Check for a max calculation that matches the pattern. - SCEVSMaxExpr *SMax = dyn_cast(IterationCount); + const SCEVSMaxExpr *SMax = dyn_cast(IterationCount); if (!SMax || SMax != SE->getSCEV(Sel)) return Cond; SCEVHandle SMaxLHS = SMax->getOperand(0); @@ -2251,16 +2248,12 @@ ICmpInst *LoopStrengthReduce::OptimizeSMax(Loop *L, ICmpInst *Cond, Cond->getOperand(0), NewRHS, "scmp", Cond); // Delete the max calculation instructions. - SE->deleteValueFromRecords(Cond); Cond->replaceAllUsesWith(NewCond); Cond->eraseFromParent(); Instruction *Cmp = cast(Sel->getOperand(0)); - SE->deleteValueFromRecords(Sel); Sel->eraseFromParent(); - if (Cmp->use_empty()) { - SE->deleteValueFromRecords(Cmp); + if (Cmp->use_empty()) Cmp->eraseFromParent(); - } CondUse->User = NewCond; return NewCond; } @@ -2367,7 +2360,6 @@ void LoopStrengthReduce::OptimizeShadowIV(Loop *L) { NewPH->addIncoming(NewIncr, PH->getIncomingBlock(Latch)); /* Remove cast operation */ - SE->deleteValueFromRecords(ShadowUse); ShadowUse->replaceAllUsesWith(NewPH); ShadowUse->eraseFromParent(); SI->second.Users.erase(CandidateUI); @@ -2507,17 +2499,8 @@ bool LoopStrengthReduce::runOnLoop(Loop *L, LPPassManager &LPM) { DeleteTriviallyDeadInstructions(); // At this point, it is worth checking to see if any recurrence PHIs are also - // dead, so that we can remove them as well. To keep ScalarEvolution - // current, use a ValueDeletionListener class. - struct LSRListener : public ValueDeletionListener { - ScalarEvolution &SE; - explicit LSRListener(ScalarEvolution &se) : SE(se) {} - - virtual void ValueWillBeDeleted(Value *V) { - SE.deleteValueFromRecords(V); - } - } VDL(*SE); - DeleteDeadPHIs(L->getHeader(), &VDL); + // dead, so that we can remove them as well. + DeleteDeadPHIs(L->getHeader()); return Changed; } diff --git a/lib/Transforms/Utils/BasicBlockUtils.cpp b/lib/Transforms/Utils/BasicBlockUtils.cpp index 0a6d7ef5dbf..6d1180d0dd9 100644 --- a/lib/Transforms/Utils/BasicBlockUtils.cpp +++ b/lib/Transforms/Utils/BasicBlockUtils.cpp @@ -78,9 +78,8 @@ void llvm::FoldSingleEntryPHINodes(BasicBlock *BB) { /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it /// is dead. Also recursively delete any operands that become dead as /// a result. This includes tracing the def-use list from the PHI to see if -/// it is ultimately unused or if it reaches an unused cycle. If a -/// ValueDeletionListener is specified, it is notified of the deletions. -void llvm::DeleteDeadPHIs(BasicBlock *BB, ValueDeletionListener *VDL) { +/// it is ultimately unused or if it reaches an unused cycle. +void llvm::DeleteDeadPHIs(BasicBlock *BB) { // Recursively deleting a PHI may cause multiple PHIs to be deleted // or RAUW'd undef, so use an array of WeakVH for the PHIs to delete. SmallVector PHIs; @@ -90,7 +89,7 @@ void llvm::DeleteDeadPHIs(BasicBlock *BB, ValueDeletionListener *VDL) { for (unsigned i = 0, e = PHIs.size(); i != e; ++i) if (PHINode *PN = dyn_cast_or_null(PHIs[i].operator Value*())) - RecursivelyDeleteDeadPHINode(PN, VDL); + RecursivelyDeleteDeadPHINode(PN); } /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, diff --git a/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp index 3b36362bb39..fea739cb46b 100644 --- a/lib/Transforms/Utils/Local.cpp +++ b/lib/Transforms/Utils/Local.cpp @@ -178,18 +178,10 @@ bool llvm::isInstructionTriviallyDead(Instruction *I) { return false; } -/// ~ValueDeletionListener - A trivial dtor, defined out of line to give the -/// class a home. -llvm::ValueDeletionListener::~ValueDeletionListener() {} - /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a /// trivially dead instruction, delete it. If that makes any of its operands /// trivially dead, delete them too, recursively. -/// -/// If a ValueDeletionListener is specified, it is notified of instructions that -/// are actually deleted (before they are actually deleted). -void llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V, - ValueDeletionListener *VDL) { +void llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V) { Instruction *I = dyn_cast(V); if (!I || !I->use_empty() || !isInstructionTriviallyDead(I)) return; @@ -201,10 +193,6 @@ void llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V, I = DeadInsts.back(); DeadInsts.pop_back(); - // If the client wanted to know, tell it about deleted instructions. - if (VDL) - VDL->ValueWillBeDeleted(I); - // Null out all of the instruction's operands to see if any operand becomes // dead as we go. for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { @@ -230,11 +218,8 @@ void llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V, /// either forms a cycle or is terminated by a trivially dead instruction, /// delete it. If that makes any of its operands trivially dead, delete them /// too, recursively. -/// -/// If a ValueDeletionListener is specified, it is notified of instructions that -/// are actually deleted (before they are actually deleted). void -llvm::RecursivelyDeleteDeadPHINode(PHINode *PN, ValueDeletionListener *VDL) { +llvm::RecursivelyDeleteDeadPHINode(PHINode *PN) { // We can remove a PHI if it is on a cycle in the def-use graph // where each node in the cycle has degree one, i.e. only one use, @@ -253,7 +238,7 @@ llvm::RecursivelyDeleteDeadPHINode(PHINode *PN, ValueDeletionListener *VDL) { if (!PHIs.insert(cast(JP))) { // Break the cycle and delete the PHI and its operands. JP->replaceAllUsesWith(UndefValue::get(JP->getType())); - RecursivelyDeleteTriviallyDeadInstructions(JP, VDL); + RecursivelyDeleteTriviallyDeadInstructions(JP); break; } }