Re-apply 70645, converting ScalarEvolution to use

CallbackVH, with fixes. allUsesReplacedWith need to
walk the def-use chains and invalidate all users of a
value that is replaced. SCEVs of users need to be
recalcualted even if the new value is equivalent. Also,
make forgetLoopPHIs walk def-use chains, since any
SCEV that depends on a PHI should be recalculated when
more information about that PHI becomes available.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@70927 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman
2009-05-04 22:30:44 +00:00
parent bf2176a000
commit 35738ac150
9 changed files with 157 additions and 182 deletions

View File

@@ -24,6 +24,7 @@
#include "llvm/Pass.h" #include "llvm/Pass.h"
#include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/LoopInfo.h"
#include "llvm/Support/DataTypes.h" #include "llvm/Support/DataTypes.h"
#include "llvm/Support/ValueHandle.h"
#include <iosfwd> #include <iosfwd>
namespace llvm { namespace llvm {
@@ -140,13 +141,23 @@ namespace llvm {
static bool classof(const SCEV *S); 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, /// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
/// freeing the objects when the last reference is dropped. /// freeing the objects when the last reference is dropped.
class SCEVHandle { class SCEVHandle {
SCEV *S; const SCEV *S;
SCEVHandle(); // DO NOT IMPLEMENT SCEVHandle(); // DO NOT IMPLEMENT
public: public:
SCEVHandle(const SCEV *s) : S(const_cast<SCEV*>(s)) { SCEVHandle(const SCEV *s) : S(s) {
assert(S && "Cannot create a handle to a null SCEV!"); assert(S && "Cannot create a handle to a null SCEV!");
S->addRef(); S->addRef();
} }
@@ -155,13 +166,13 @@ namespace llvm {
} }
~SCEVHandle() { S->dropRef(); } ~SCEVHandle() { S->dropRef(); }
operator SCEV*() const { return S; } operator const SCEV*() const { return S; }
SCEV &operator*() const { return *S; } const SCEV &operator*() const { return *S; }
SCEV *operator->() const { return S; } const SCEV *operator->() const { return S; }
bool operator==(SCEV *RHS) const { return S == RHS; } bool operator==(const SCEV *RHS) const { return S == RHS; }
bool operator!=(SCEV *RHS) const { return S != RHS; } bool operator!=(const SCEV *RHS) const { return S != RHS; }
const SCEVHandle &operator=(SCEV *RHS) { const SCEVHandle &operator=(SCEV *RHS) {
if (S != RHS) { if (S != RHS) {
@@ -184,7 +195,7 @@ namespace llvm {
template<typename From> struct simplify_type; template<typename From> struct simplify_type;
template<> struct simplify_type<const SCEVHandle> { template<> struct simplify_type<const SCEVHandle> {
typedef SCEV* SimpleType; typedef const SCEV* SimpleType;
static SimpleType getSimplifiedValue(const SCEVHandle &Node) { static SimpleType getSimplifiedValue(const SCEVHandle &Node) {
return Node; return Node;
} }
@@ -197,6 +208,8 @@ namespace llvm {
/// they must ask this class for services. /// they must ask this class for services.
/// ///
class ScalarEvolution : public FunctionPass { class ScalarEvolution : public FunctionPass {
friend class SCEVCallbackVH;
/// F - The function we are analyzing. /// F - The function we are analyzing.
/// ///
Function *F; Function *F;
@@ -215,7 +228,7 @@ namespace llvm {
/// Scalars - This is a cache of the scalars we have analyzed so far. /// Scalars - This is a cache of the scalars we have analyzed so far.
/// ///
std::map<Value*, SCEVHandle> Scalars; std::map<SCEVCallbackVH, SCEVHandle> Scalars;
/// BackedgeTakenInfo - Information about the backedge-taken count /// BackedgeTakenInfo - Information about the backedge-taken count
/// of a loop. This currently inclues an exact count and a maximum count. /// of a loop. This currently inclues an exact count and a maximum count.
@@ -232,7 +245,7 @@ namespace llvm {
/*implicit*/ BackedgeTakenInfo(SCEVHandle exact) : /*implicit*/ BackedgeTakenInfo(SCEVHandle exact) :
Exact(exact), Max(exact) {} Exact(exact), Max(exact) {}
/*implicit*/ BackedgeTakenInfo(SCEV *exact) : /*implicit*/ BackedgeTakenInfo(const SCEV *exact) :
Exact(exact), Max(exact) {} Exact(exact), Max(exact) {}
BackedgeTakenInfo(SCEVHandle exact, SCEVHandle max) : BackedgeTakenInfo(SCEVHandle exact, SCEVHandle max) :
@@ -302,18 +315,18 @@ namespace llvm {
/// HowFarToZero - Return the number of times a backedge comparing the /// HowFarToZero - Return the number of times a backedge comparing the
/// specified value to zero will execute. If not computable, return /// specified value to zero will execute. If not computable, return
/// UnknownValue. /// 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 /// HowFarToNonZero - Return the number of times a backedge checking the
/// specified value for nonzero will execute. If not computable, return /// specified value for nonzero will execute. If not computable, return
/// UnknownValue. /// 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 /// HowManyLessThans - Return the number of times a backedge containing the
/// specified less-than comparison will execute. If not computable, return /// specified less-than comparison will execute. If not computable, return
/// UnknownValue. isSigned specifies whether the less-than is signed. /// UnknownValue. isSigned specifies whether the less-than is signed.
BackedgeTakenInfo HowManyLessThans(SCEV *LHS, SCEV *RHS, const Loop *L, BackedgeTakenInfo HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
bool isSigned); const Loop *L, bool isSigned);
/// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
/// (which may not be an immediate predecessor) which has exactly one /// (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 /// getSCEVAtScope - Compute the value of the specified expression within
/// the indicated loop (which may be null to indicate in no loop). If the /// the indicated loop (which may be null to indicate in no loop). If the
/// expression cannot be evaluated, return UnknownValue itself. /// 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 /// forgetLoopPHIs - Delete the memoized SCEVs associated with the
/// PHI nodes in the given loop. This is used when the trip count of /// 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 /// a conditional between LHS and RHS. This is used to help avoid max
/// expressions in loop trip counts. /// expressions in loop trip counts.
bool isLoopGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, 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 /// getBackedgeTakenCount - If the specified loop has a predictable
/// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
@@ -487,11 +500,6 @@ namespace llvm {
/// is deleted. /// is deleted.
void forgetLoopBackedgeTakenCount(const Loop *L); 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 bool runOnFunction(Function &F);
virtual void releaseMemory(); virtual void releaseMemory();
virtual void getAnalysisUsage(AnalysisUsage &AU) const; virtual void getAnalysisUsage(AnalysisUsage &AU) const;

View File

@@ -25,7 +25,6 @@ namespace llvm {
class Instruction; class Instruction;
class Pass; class Pass;
class AliasAnalysis; class AliasAnalysis;
class ValueDeletionListener;
/// DeleteDeadBlock - Delete the specified block, which must have no /// DeleteDeadBlock - Delete the specified block, which must have no
/// predecessors. /// predecessors.
@@ -41,9 +40,8 @@ void FoldSingleEntryPHINodes(BasicBlock *BB);
/// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
/// is dead. Also recursively delete any operands that become dead as /// 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 /// 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 /// it is ultimately unused or if it reaches an unused cycle.
/// ValueDeletionListener is specified, it is notified of the deletions. void DeleteDeadPHIs(BasicBlock *BB);
void DeleteDeadPHIs(BasicBlock *BB, ValueDeletionListener *VDL = 0);
/// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
/// if possible. The return value indicates success or failure. /// if possible. The return value indicates success or failure.

View File

@@ -50,40 +50,17 @@ bool ConstantFoldTerminator(BasicBlock *BB);
/// ///
bool isInstructionTriviallyDead(Instruction *I); 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 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
/// trivially dead instruction, delete it. If that makes any of its operands /// trivially dead instruction, delete it. If that makes any of its operands
/// trivially dead, delete them too, recursively. /// trivially dead, delete them too, recursively.
/// void RecursivelyDeleteTriviallyDeadInstructions(Value *V);
/// 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);
/// RecursivelyDeleteDeadPHINode - If the specified value is an effectively /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
/// dead PHI node, due to being a def-use chain of single-use nodes that /// 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, /// 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 /// delete it. If that makes any of its operands trivially dead, delete them
/// too, recursively. /// too, recursively.
/// void RecursivelyDeleteDeadPHINode(PHINode *PN);
/// 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);
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// Control Flow Graph Restructuring. // Control Flow Graph Restructuring.

View File

@@ -204,7 +204,7 @@ bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
// SCEVTruncates - Only allow the creation of one SCEVTruncateExpr for any // SCEVTruncates - Only allow the creation of one SCEVTruncateExpr for any
// particular input. Don't use a SCEVHandle here, or else the object will // particular input. Don't use a SCEVHandle here, or else the object will
// never be deleted! // never be deleted!
static ManagedStatic<std::map<std::pair<SCEV*, const Type*>, static ManagedStatic<std::map<std::pair<const SCEV*, const Type*>,
SCEVTruncateExpr*> > SCEVTruncates; SCEVTruncateExpr*> > SCEVTruncates;
SCEVTruncateExpr::SCEVTruncateExpr(const SCEVHandle &op, const Type *ty) 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 // SCEVZeroExtends - Only allow the creation of one SCEVZeroExtendExpr for any
// particular input. Don't use a SCEVHandle here, or else the object will never // particular input. Don't use a SCEVHandle here, or else the object will never
// be deleted! // be deleted!
static ManagedStatic<std::map<std::pair<SCEV*, const Type*>, static ManagedStatic<std::map<std::pair<const SCEV*, const Type*>,
SCEVZeroExtendExpr*> > SCEVZeroExtends; SCEVZeroExtendExpr*> > SCEVZeroExtends;
SCEVZeroExtendExpr::SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty) 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 // SCEVSignExtends - Only allow the creation of one SCEVSignExtendExpr for any
// particular input. Don't use a SCEVHandle here, or else the object will never // particular input. Don't use a SCEVHandle here, or else the object will never
// be deleted! // be deleted!
static ManagedStatic<std::map<std::pair<SCEV*, const Type*>, static ManagedStatic<std::map<std::pair<const SCEV*, const Type*>,
SCEVSignExtendExpr*> > SCEVSignExtends; SCEVSignExtendExpr*> > SCEVSignExtends;
SCEVSignExtendExpr::SCEVSignExtendExpr(const SCEVHandle &op, const Type *ty) 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 // SCEVCommExprs - Only allow the creation of one SCEVCommutativeExpr for any
// particular input. Don't use a SCEVHandle here, or else the object will never // particular input. Don't use a SCEVHandle here, or else the object will never
// be deleted! // be deleted!
static ManagedStatic<std::map<std::pair<unsigned, std::vector<SCEV*> >, static ManagedStatic<std::map<std::pair<unsigned, std::vector<const SCEV*> >,
SCEVCommutativeExpr*> > SCEVCommExprs; SCEVCommutativeExpr*> > SCEVCommExprs;
SCEVCommutativeExpr::~SCEVCommutativeExpr() { SCEVCommutativeExpr::~SCEVCommutativeExpr() {
SCEVCommExprs->erase(std::make_pair(getSCEVType(), std::vector<const SCEV*> SCEVOps(Operands.begin(), Operands.end());
std::vector<SCEV*>(Operands.begin(), SCEVCommExprs->erase(std::make_pair(getSCEVType(), SCEVOps));
Operands.end())));
} }
void SCEVCommutativeExpr::print(raw_ostream &OS) const { 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 // 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 // input. Don't use a SCEVHandle here, or else the object will never be
// deleted! // deleted!
static ManagedStatic<std::map<std::pair<SCEV*, SCEV*>, static ManagedStatic<std::map<std::pair<const SCEV*, const SCEV*>,
SCEVUDivExpr*> > SCEVUDivs; SCEVUDivExpr*> > SCEVUDivs;
SCEVUDivExpr::~SCEVUDivExpr() { SCEVUDivExpr::~SCEVUDivExpr() {
@@ -351,13 +350,13 @@ const Type *SCEVUDivExpr::getType() const {
// SCEVAddRecExprs - Only allow the creation of one SCEVAddRecExpr for any // SCEVAddRecExprs - Only allow the creation of one SCEVAddRecExpr for any
// particular input. Don't use a SCEVHandle here, or else the object will never // particular input. Don't use a SCEVHandle here, or else the object will never
// be deleted! // be deleted!
static ManagedStatic<std::map<std::pair<const Loop *, std::vector<SCEV*> >, static ManagedStatic<std::map<std::pair<const Loop *,
std::vector<const SCEV*> >,
SCEVAddRecExpr*> > SCEVAddRecExprs; SCEVAddRecExpr*> > SCEVAddRecExprs;
SCEVAddRecExpr::~SCEVAddRecExpr() { SCEVAddRecExpr::~SCEVAddRecExpr() {
SCEVAddRecExprs->erase(std::make_pair(L, std::vector<const SCEV*> SCEVOps(Operands.begin(), Operands.end());
std::vector<SCEV*>(Operands.begin(), SCEVAddRecExprs->erase(std::make_pair(L, SCEVOps));
Operands.end())));
} }
bool SCEVAddRecExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { bool SCEVAddRecExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
@@ -480,7 +479,7 @@ static void GroupByComplexity(std::vector<SCEVHandle> &Ops) {
// be extremely short in practice. Note that we take this approach because we // 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. // 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) { for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
SCEV *S = Ops[i]; const SCEV *S = Ops[i];
unsigned Complexity = S->getSCEVType(); unsigned Complexity = S->getSCEVType();
// If there are any objects of the same complexity and same value as this // If there are any objects of the same complexity and same value as this
@@ -919,9 +918,9 @@ SCEVHandle ScalarEvolution::getAddExpr(std::vector<SCEVHandle> &Ops) {
// something is not already an operand of the multiply. If so, merge it into // something is not already an operand of the multiply. If so, merge it into
// the multiply. // the multiply.
for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) { for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]); const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { 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) for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(MulOpSCEV)) { if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(MulOpSCEV)) {
// Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1))
@@ -952,7 +951,7 @@ SCEVHandle ScalarEvolution::getAddExpr(std::vector<SCEVHandle> &Ops) {
for (unsigned OtherMulIdx = Idx+1; for (unsigned OtherMulIdx = Idx+1;
OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]); OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
++OtherMulIdx) { ++OtherMulIdx) {
SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]); const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
// If MulOp occurs in OtherMul, we can fold the two multiplies // If MulOp occurs in OtherMul, we can fold the two multiplies
// together. // together.
for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
@@ -995,7 +994,7 @@ SCEVHandle ScalarEvolution::getAddExpr(std::vector<SCEVHandle> &Ops) {
// Scan all of the other operands to this add and add them to the vector if // 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. // they are loop invariant w.r.t. the recurrence.
std::vector<SCEVHandle> LIOps; std::vector<SCEVHandle> LIOps;
SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
for (unsigned i = 0, e = Ops.size(); i != e; ++i) for (unsigned i = 0, e = Ops.size(); i != e; ++i)
if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
LIOps.push_back(Ops[i]); LIOps.push_back(Ops[i]);
@@ -1030,7 +1029,7 @@ SCEVHandle ScalarEvolution::getAddExpr(std::vector<SCEVHandle> &Ops) {
for (unsigned OtherIdx = Idx+1; for (unsigned OtherIdx = Idx+1;
OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
if (OtherIdx != Idx) { if (OtherIdx != Idx) {
SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
if (AddRec->getLoop() == OtherAddRec->getLoop()) { if (AddRec->getLoop() == OtherAddRec->getLoop()) {
// Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D} // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D}
std::vector<SCEVHandle> NewOps(AddRec->op_begin(), AddRec->op_end()); std::vector<SCEVHandle> NewOps(AddRec->op_begin(), AddRec->op_end());
@@ -1059,7 +1058,7 @@ SCEVHandle ScalarEvolution::getAddExpr(std::vector<SCEVHandle> &Ops) {
// Okay, it looks like we really DO need an add expr. Check to see if we // Okay, it looks like we really DO need an add expr. Check to see if we
// already have one, otherwise create a new one. // already have one, otherwise create a new one.
std::vector<SCEV*> SCEVOps(Ops.begin(), Ops.end()); std::vector<const SCEV*> SCEVOps(Ops.begin(), Ops.end());
SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scAddExpr, SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scAddExpr,
SCEVOps)]; SCEVOps)];
if (Result == 0) Result = new SCEVAddExpr(Ops); if (Result == 0) Result = new SCEVAddExpr(Ops);
@@ -1143,7 +1142,7 @@ SCEVHandle ScalarEvolution::getMulExpr(std::vector<SCEVHandle> &Ops) {
// Scan all of the other operands to this mul and add them to the vector if // 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. // they are loop invariant w.r.t. the recurrence.
std::vector<SCEVHandle> LIOps; std::vector<SCEVHandle> LIOps;
SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
for (unsigned i = 0, e = Ops.size(); i != e; ++i) for (unsigned i = 0, e = Ops.size(); i != e; ++i)
if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
LIOps.push_back(Ops[i]); LIOps.push_back(Ops[i]);
@@ -1157,7 +1156,7 @@ SCEVHandle ScalarEvolution::getMulExpr(std::vector<SCEVHandle> &Ops) {
std::vector<SCEVHandle> NewOps; std::vector<SCEVHandle> NewOps;
NewOps.reserve(AddRec->getNumOperands()); NewOps.reserve(AddRec->getNumOperands());
if (LIOps.size() == 1) { if (LIOps.size() == 1) {
SCEV *Scale = LIOps[0]; const SCEV *Scale = LIOps[0];
for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i))); NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i)));
} else { } else {
@@ -1188,10 +1187,10 @@ SCEVHandle ScalarEvolution::getMulExpr(std::vector<SCEVHandle> &Ops) {
for (unsigned OtherIdx = Idx+1; for (unsigned OtherIdx = Idx+1;
OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
if (OtherIdx != Idx) { if (OtherIdx != Idx) {
SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
if (AddRec->getLoop() == OtherAddRec->getLoop()) { if (AddRec->getLoop() == OtherAddRec->getLoop()) {
// F * G --> {A,+,B} * {C,+,D} --> {A*C,+,F*D + G*B + B*D} // 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(), SCEVHandle NewStart = getMulExpr(F->getStart(),
G->getStart()); G->getStart());
SCEVHandle B = F->getStepRecurrence(*this); SCEVHandle B = F->getStepRecurrence(*this);
@@ -1216,7 +1215,7 @@ SCEVHandle ScalarEvolution::getMulExpr(std::vector<SCEVHandle> &Ops) {
// Okay, it looks like we really DO need an mul expr. Check to see if we // Okay, it looks like we really DO need an mul expr. Check to see if we
// already have one, otherwise create a new one. // already have one, otherwise create a new one.
std::vector<SCEV*> SCEVOps(Ops.begin(), Ops.end()); std::vector<const SCEV*> SCEVOps(Ops.begin(), Ops.end());
SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scMulExpr, SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scMulExpr,
SCEVOps)]; SCEVOps)];
if (Result == 0) if (Result == 0)
@@ -1286,9 +1285,8 @@ SCEVHandle ScalarEvolution::getAddRecExpr(std::vector<SCEVHandle> &Operands,
} }
} }
SCEVAddRecExpr *&Result = std::vector<const SCEV*> SCEVOps(Operands.begin(), Operands.end());
(*SCEVAddRecExprs)[std::make_pair(L, std::vector<SCEV*>(Operands.begin(), SCEVAddRecExpr *&Result = (*SCEVAddRecExprs)[std::make_pair(L, SCEVOps)];
Operands.end()))];
if (Result == 0) Result = new SCEVAddRecExpr(Operands, L); if (Result == 0) Result = new SCEVAddRecExpr(Operands, L);
return Result; return Result;
} }
@@ -1366,7 +1364,7 @@ SCEVHandle ScalarEvolution::getSMaxExpr(std::vector<SCEVHandle> Ops) {
// Okay, it looks like we really DO need an smax expr. Check to see if we // Okay, it looks like we really DO need an smax expr. Check to see if we
// already have one, otherwise create a new one. // already have one, otherwise create a new one.
std::vector<SCEV*> SCEVOps(Ops.begin(), Ops.end()); std::vector<const SCEV*> SCEVOps(Ops.begin(), Ops.end());
SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scSMaxExpr, SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scSMaxExpr,
SCEVOps)]; SCEVOps)];
if (Result == 0) Result = new SCEVSMaxExpr(Ops); if (Result == 0) Result = new SCEVSMaxExpr(Ops);
@@ -1446,7 +1444,7 @@ SCEVHandle ScalarEvolution::getUMaxExpr(std::vector<SCEVHandle> Ops) {
// Okay, it looks like we really DO need a umax expr. Check to see if we // Okay, it looks like we really DO need a umax expr. Check to see if we
// already have one, otherwise create a new one. // already have one, otherwise create a new one.
std::vector<SCEV*> SCEVOps(Ops.begin(), Ops.end()); std::vector<const SCEV*> SCEVOps(Ops.begin(), Ops.end());
SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scUMaxExpr, SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scUMaxExpr,
SCEVOps)]; SCEVOps)];
if (Result == 0) Result = new SCEVUMaxExpr(Ops); if (Result == 0) Result = new SCEVUMaxExpr(Ops);
@@ -1467,34 +1465,6 @@ SCEVHandle ScalarEvolution::getUnknown(Value *V) {
// Basic SCEV Analysis and PHI Idiom Recognition Code // 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<Value *, 16> Worklist;
if (Scalars.erase(V)) {
if (PHINode *PN = dyn_cast<PHINode>(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<Instruction>(*UI);
if (Scalars.erase(Inst)) {
if (PHINode *PN = dyn_cast<PHINode>(VV))
ConstantEvolutionLoopExitValue.erase(PN);
Worklist.push_back(Inst);
}
}
}
}
/// isSCEVable - Test if values of the given type are analyzable within /// isSCEVable - Test if values of the given type are analyzable within
/// the SCEV framework. This primarily includes integer types, and it /// the SCEV framework. This primarily includes integer types, and it
/// can optionally include pointer types if the ScalarEvolution class /// can optionally include pointer types if the ScalarEvolution class
@@ -1556,10 +1526,10 @@ bool ScalarEvolution::hasSCEV(Value *V) const {
SCEVHandle ScalarEvolution::getSCEV(Value *V) { SCEVHandle ScalarEvolution::getSCEV(Value *V) {
assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
std::map<Value*, SCEVHandle>::iterator I = Scalars.find(V); std::map<SCEVCallbackVH, SCEVHandle>::iterator I = Scalars.find(V);
if (I != Scalars.end()) return I->second; if (I != Scalars.end()) return I->second;
SCEVHandle S = createSCEV(V); SCEVHandle S = createSCEV(V);
Scalars.insert(std::make_pair(V, S)); Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S));
return S; return S;
} }
@@ -1648,7 +1618,8 @@ ScalarEvolution::getTruncateOrSignExtend(const SCEVHandle &V,
void ScalarEvolution:: void ScalarEvolution::
ReplaceSymbolicValueWithConcrete(Instruction *I, const SCEVHandle &SymName, ReplaceSymbolicValueWithConcrete(Instruction *I, const SCEVHandle &SymName,
const SCEVHandle &NewVal) { const SCEVHandle &NewVal) {
std::map<Value*, SCEVHandle>::iterator SI = Scalars.find(I); std::map<SCEVCallbackVH, SCEVHandle>::iterator SI =
Scalars.find(SCEVCallbackVH(I, this));
if (SI == Scalars.end()) return; if (SI == Scalars.end()) return;
SCEVHandle NV = SCEVHandle NV =
@@ -1680,7 +1651,7 @@ SCEVHandle ScalarEvolution::createNodeForPHI(PHINode *PN) {
SCEVHandle SymbolicName = getUnknown(PN); SCEVHandle SymbolicName = getUnknown(PN);
assert(Scalars.find(PN) == Scalars.end() && assert(Scalars.find(PN) == Scalars.end() &&
"PHI node already processed?"); "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 // Using this symbolic name for the PHI, analyze the value coming around
// the back-edge. // 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 /// PHI nodes in the given loop. This is used when the trip count of
/// the loop may have changed. /// the loop may have changed.
void ScalarEvolution::forgetLoopPHIs(const Loop *L) { void ScalarEvolution::forgetLoopPHIs(const Loop *L) {
for (BasicBlock::iterator I = L->getHeader()->begin(); BasicBlock *Header = L->getHeader();
SmallVector<Instruction *, 16> Worklist;
for (BasicBlock::iterator I = Header->begin();
PHINode *PN = dyn_cast<PHINode>(I); ++I) PHINode *PN = dyn_cast<PHINode>(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<Instruction>(UI));
}
} }
/// ComputeBackedgeTakenCount - Compute the number of times the backedge /// 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 // We can only recognize very limited forms of loop index expressions, in
// particular, only affine AddRec's like {C1,+,C2}. // particular, only affine AddRec's like {C1,+,C2}.
SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx); const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx);
if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) || if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) ||
!isa<SCEVConstant>(IdxExpr->getOperand(0)) || !isa<SCEVConstant>(IdxExpr->getOperand(0)) ||
!isa<SCEVConstant>(IdxExpr->getOperand(1))) !isa<SCEVConstant>(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 /// getSCEVAtScope - Compute the value of the specified expression within the
/// indicated loop (which may be null to indicate in no loop). If the /// indicated loop (which may be null to indicate in no loop). If the
/// expression cannot be evaluated, return UnknownValue. /// 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! // FIXME: this should be turned into a virtual method on SCEV!
if (isa<SCEVConstant>(V)) return V; if (isa<SCEVConstant>(V)) return V;
@@ -2847,13 +2829,13 @@ static SCEVHandle SolveLinEquationWithOverflow(const APInt &A, const APInt &B,
static std::pair<SCEVHandle,SCEVHandle> static std::pair<SCEVHandle,SCEVHandle>
SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) {
assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!");
SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0)); const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0));
SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1)); const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1));
SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2)); const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2));
// We currently can only solve this if the coefficients are constants. // We currently can only solve this if the coefficients are constants.
if (!LC || !MC || !NC) { if (!LC || !MC || !NC) {
SCEV *CNC = SE.getCouldNotCompute(); const SCEV *CNC = SE.getCouldNotCompute();
return std::make_pair(CNC, CNC); return std::make_pair(CNC, CNC);
} }
@@ -2889,7 +2871,7 @@ SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) {
APInt NegB(-B); APInt NegB(-B);
APInt TwoA( A << 1 ); APInt TwoA( A << 1 );
if (TwoA.isMinValue()) { if (TwoA.isMinValue()) {
SCEV *CNC = SE.getCouldNotCompute(); const SCEV *CNC = SE.getCouldNotCompute();
return std::make_pair(CNC, CNC); 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 /// HowFarToZero - Return the number of times a backedge comparing the specified
/// value to zero will execute. If not computable, return UnknownValue /// 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 the value is a constant
if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
// If the value is already zero, the branch will execute zero times. // 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. return UnknownValue; // Otherwise it will loop infinitely.
} }
SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V); const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V);
if (!AddRec || AddRec->getLoop() != L) if (!AddRec || AddRec->getLoop() != L)
return UnknownValue; return UnknownValue;
@@ -2953,8 +2935,8 @@ SCEVHandle ScalarEvolution::HowFarToZero(SCEV *V, const Loop *L) {
// the quadratic equation to solve it. // the quadratic equation to solve it.
std::pair<SCEVHandle,SCEVHandle> Roots = SolveQuadraticEquation(AddRec, std::pair<SCEVHandle,SCEVHandle> Roots = SolveQuadraticEquation(AddRec,
*this); *this);
SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
if (R1) { if (R1) {
#if 0 #if 0
errs() << "HFTZ: " << *V << " - sol#1: " << *R1 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 /// HowFarToNonZero - Return the number of times a backedge checking the
/// specified value for nonzero will execute. If not computable, return /// specified value for nonzero will execute. If not computable, return
/// UnknownValue /// 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 // 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 // handle them yet except for the trivial case. This could be expanded in the
// future as needed. // future as needed.
@@ -3029,7 +3011,7 @@ ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) {
/// expressions in loop trip counts. /// expressions in loop trip counts.
bool ScalarEvolution::isLoopGuardedByCond(const Loop *L, bool ScalarEvolution::isLoopGuardedByCond(const Loop *L,
ICmpInst::Predicate Pred, ICmpInst::Predicate Pred,
SCEV *LHS, SCEV *RHS) { const SCEV *LHS, const SCEV *RHS) {
BasicBlock *Preheader = L->getLoopPreheader(); BasicBlock *Preheader = L->getLoopPreheader();
BasicBlock *PreheaderDest = L->getHeader(); BasicBlock *PreheaderDest = L->getHeader();
@@ -3133,11 +3115,12 @@ bool ScalarEvolution::isLoopGuardedByCond(const Loop *L,
/// specified less-than comparison will execute. If not computable, return /// specified less-than comparison will execute. If not computable, return
/// UnknownValue. /// UnknownValue.
ScalarEvolution::BackedgeTakenInfo ScalarEvolution:: 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". // Only handle: "ADDREC < LoopInvariant".
if (!RHS->isLoopInvariant(L)) return UnknownValue; if (!RHS->isLoopInvariant(L)) return UnknownValue;
SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS); const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS);
if (!AddRec || AddRec->getLoop() != L) if (!AddRec || AddRec->getLoop() != L)
return UnknownValue; return UnknownValue;
@@ -3304,8 +3287,8 @@ SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
// Next, solve the constructed addrec // Next, solve the constructed addrec
std::pair<SCEVHandle,SCEVHandle> Roots = std::pair<SCEVHandle,SCEVHandle> Roots =
SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE); SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE);
SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
if (R1) { if (R1) {
// Pick the smallest positive root value. // Pick the smallest positive root value.
if (ConstantInt *CB = 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<PHINode>(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<User *, 16> 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<PHINode>(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<PHINode>(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 // ScalarEvolution Class Implementation
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//

View File

@@ -124,7 +124,6 @@ DeleteTriviallyDeadInstructions(SmallPtrSet<Instruction*, 16> &Insts) {
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i))) if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i)))
Insts.insert(U); Insts.insert(U);
SE->deleteValueFromRecords(I);
DOUT << "INDVARS: Deleting: " << *I; DOUT << "INDVARS: Deleting: " << *I;
I->eraseFromParent(); I->eraseFromParent();
Changed = true; Changed = true;
@@ -308,7 +307,6 @@ void IndVarSimplify::RewriteLoopExitValues(Loop *L,
// the PHI entirely. This is safe, because the NewVal won't be variant // 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. // in the loop, so we don't need an LCSSA phi node anymore.
if (NumPreds == 1) { if (NumPreds == 1) {
SE->deleteValueFromRecords(PN);
PN->replaceAllUsesWith(ExitVal); PN->replaceAllUsesWith(ExitVal);
PN->eraseFromParent(); PN->eraseFromParent();
break; break;

View File

@@ -246,13 +246,6 @@ bool LoopDeletion::runOnLoop(Loop* L, LPPassManager& LPM) {
DT.eraseNode(*LI); DT.eraseNode(*LI);
if (DF) DF->removeBlock(*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 // Remove the block from the reference counting scheme, so that we can
// delete it freely later. // delete it freely later.
(*LI)->dropAllReferences(); (*LI)->dropAllReferences();

View File

@@ -253,8 +253,6 @@ void LoopStrengthReduce::DeleteTriviallyDeadInstructions() {
if (I == 0 || !isInstructionTriviallyDead(I)) if (I == 0 || !isInstructionTriviallyDead(I))
continue; continue;
SE->deleteValueFromRecords(I);
for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) { for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) {
if (Instruction *U = dyn_cast<Instruction>(*OI)) { if (Instruction *U = dyn_cast<Instruction>(*OI)) {
*OI = 0; *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. // Remove the old compare instruction. The old indvar is probably dead too.
DeadInsts.push_back(cast<Instruction>(CondUse->OperandValToReplace)); DeadInsts.push_back(cast<Instruction>(CondUse->OperandValToReplace));
SE->deleteValueFromRecords(OldCond);
OldCond->replaceAllUsesWith(Cond); OldCond->replaceAllUsesWith(Cond);
OldCond->eraseFromParent(); OldCond->eraseFromParent();
@@ -2214,7 +2211,7 @@ ICmpInst *LoopStrengthReduce::OptimizeSMax(Loop *L, ICmpInst *Cond,
SCEVHandle IterationCount = SE->getAddExpr(BackedgeTakenCount, One); SCEVHandle IterationCount = SE->getAddExpr(BackedgeTakenCount, One);
// Check for a max calculation that matches the pattern. // Check for a max calculation that matches the pattern.
SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(IterationCount); const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(IterationCount);
if (!SMax || SMax != SE->getSCEV(Sel)) return Cond; if (!SMax || SMax != SE->getSCEV(Sel)) return Cond;
SCEVHandle SMaxLHS = SMax->getOperand(0); SCEVHandle SMaxLHS = SMax->getOperand(0);
@@ -2251,16 +2248,12 @@ ICmpInst *LoopStrengthReduce::OptimizeSMax(Loop *L, ICmpInst *Cond,
Cond->getOperand(0), NewRHS, "scmp", Cond); Cond->getOperand(0), NewRHS, "scmp", Cond);
// Delete the max calculation instructions. // Delete the max calculation instructions.
SE->deleteValueFromRecords(Cond);
Cond->replaceAllUsesWith(NewCond); Cond->replaceAllUsesWith(NewCond);
Cond->eraseFromParent(); Cond->eraseFromParent();
Instruction *Cmp = cast<Instruction>(Sel->getOperand(0)); Instruction *Cmp = cast<Instruction>(Sel->getOperand(0));
SE->deleteValueFromRecords(Sel);
Sel->eraseFromParent(); Sel->eraseFromParent();
if (Cmp->use_empty()) { if (Cmp->use_empty())
SE->deleteValueFromRecords(Cmp);
Cmp->eraseFromParent(); Cmp->eraseFromParent();
}
CondUse->User = NewCond; CondUse->User = NewCond;
return NewCond; return NewCond;
} }
@@ -2367,7 +2360,6 @@ void LoopStrengthReduce::OptimizeShadowIV(Loop *L) {
NewPH->addIncoming(NewIncr, PH->getIncomingBlock(Latch)); NewPH->addIncoming(NewIncr, PH->getIncomingBlock(Latch));
/* Remove cast operation */ /* Remove cast operation */
SE->deleteValueFromRecords(ShadowUse);
ShadowUse->replaceAllUsesWith(NewPH); ShadowUse->replaceAllUsesWith(NewPH);
ShadowUse->eraseFromParent(); ShadowUse->eraseFromParent();
SI->second.Users.erase(CandidateUI); SI->second.Users.erase(CandidateUI);
@@ -2507,17 +2499,8 @@ bool LoopStrengthReduce::runOnLoop(Loop *L, LPPassManager &LPM) {
DeleteTriviallyDeadInstructions(); DeleteTriviallyDeadInstructions();
// At this point, it is worth checking to see if any recurrence PHIs are also // 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 // dead, so that we can remove them as well.
// current, use a ValueDeletionListener class. DeleteDeadPHIs(L->getHeader());
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);
return Changed; return Changed;
} }

View File

@@ -78,9 +78,8 @@ void llvm::FoldSingleEntryPHINodes(BasicBlock *BB) {
/// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
/// is dead. Also recursively delete any operands that become dead as /// 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 /// 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 /// it is ultimately unused or if it reaches an unused cycle.
/// ValueDeletionListener is specified, it is notified of the deletions. void llvm::DeleteDeadPHIs(BasicBlock *BB) {
void llvm::DeleteDeadPHIs(BasicBlock *BB, ValueDeletionListener *VDL) {
// Recursively deleting a PHI may cause multiple PHIs to be deleted // 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. // or RAUW'd undef, so use an array of WeakVH for the PHIs to delete.
SmallVector<WeakVH, 8> PHIs; SmallVector<WeakVH, 8> PHIs;
@@ -90,7 +89,7 @@ void llvm::DeleteDeadPHIs(BasicBlock *BB, ValueDeletionListener *VDL) {
for (unsigned i = 0, e = PHIs.size(); i != e; ++i) for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*())) if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*()))
RecursivelyDeleteDeadPHINode(PN, VDL); RecursivelyDeleteDeadPHINode(PN);
} }
/// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,

View File

@@ -178,18 +178,10 @@ bool llvm::isInstructionTriviallyDead(Instruction *I) {
return false; 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 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
/// trivially dead instruction, delete it. If that makes any of its operands /// trivially dead instruction, delete it. If that makes any of its operands
/// trivially dead, delete them too, recursively. /// trivially dead, delete them too, recursively.
/// void llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V) {
/// 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) {
Instruction *I = dyn_cast<Instruction>(V); Instruction *I = dyn_cast<Instruction>(V);
if (!I || !I->use_empty() || !isInstructionTriviallyDead(I)) if (!I || !I->use_empty() || !isInstructionTriviallyDead(I))
return; return;
@@ -201,10 +193,6 @@ void llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V,
I = DeadInsts.back(); I = DeadInsts.back();
DeadInsts.pop_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 // Null out all of the instruction's operands to see if any operand becomes
// dead as we go. // dead as we go.
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { 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, /// 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 /// delete it. If that makes any of its operands trivially dead, delete them
/// too, recursively. /// too, recursively.
///
/// If a ValueDeletionListener is specified, it is notified of instructions that
/// are actually deleted (before they are actually deleted).
void 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 // 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, // 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<PHINode>(JP))) { if (!PHIs.insert(cast<PHINode>(JP))) {
// Break the cycle and delete the PHI and its operands. // Break the cycle and delete the PHI and its operands.
JP->replaceAllUsesWith(UndefValue::get(JP->getType())); JP->replaceAllUsesWith(UndefValue::get(JP->getType()));
RecursivelyDeleteTriviallyDeadInstructions(JP, VDL); RecursivelyDeleteTriviallyDeadInstructions(JP);
break; break;
} }
} }