llvm.org GIT mirror llvm / ff18310
Add a basic verifier for SCEV's backedge taken counts. Enabled with -verify-scev. This could be extended significantly but hopefully catches the common cases now. Note that it's not enabled by default in any configuration because the way it tries to distinguish SCEVs is still fragile and may produce false positives. Also the test-suite isn't clean yet, one example is that it fails if a pass drops an NSW bit but it's still present in SCEV's cached. Cleaning up all those cases will take some time. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@166786 91177308-0d34-0410-b5e6-96231b3b80d8 Benjamin Kramer 8 years ago
2 changed file(s) with 69 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
873873 virtual void releaseMemory();
874874 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
875875 virtual void print(raw_ostream &OS, const Module* = 0) const;
876 virtual void verifyAnalysis() const;
876877
877878 private:
878879 FoldingSet UniqueSCEVs;
103103 "symbolically execute a constant "
104104 "derived loop"),
105105 cl::init(100));
106
107 // FIXME: Enable this with XDEBUG when the test suite is clean.
108 static cl::opt
109 VerifySCEV("verify-scev",
110 cl::desc("Verify ScalarEvolution's backedge taken counts (slow)"));
106111
107112 INITIALIZE_PASS_BEGIN(ScalarEvolution, "scalar-evolution",
108113 "Scalar Evolution Analysis", false, true)
69336938 UnsignedRanges.erase(S);
69346939 SignedRanges.erase(S);
69356940 }
6941
6942 typedef DenseMap VerifyMap;
6943 /// getLoopBackedgeTakenCounts - Helper method for verifyAnalysis.
6944 static void
6945 getLoopBackedgeTakenCounts(Loop *L, VerifyMap &Map, ScalarEvolution &SE) {
6946 for (Loop::reverse_iterator I = L->rbegin(), E = L->rend(); I != E; ++I) {
6947 getLoopBackedgeTakenCounts(*I, Map, SE); // recurse.
6948
6949 std::string &S = Map[L];
6950 if (S.empty()) {
6951 raw_string_ostream OS(S);
6952 SE.getBackedgeTakenCount(L)->print(OS);
6953 }
6954 }
6955 }
6956
6957 void ScalarEvolution::verifyAnalysis() const {
6958 if (!VerifySCEV)
6959 return;
6960
6961 ScalarEvolution &SE = *const_cast(this);
6962
6963 // Gather stringified backedge taken counts for all loops using SCEV's caches.
6964 // FIXME: It would be much better to store actual values instead of strings,
6965 // but SCEV pointers will change if we drop the caches.
6966 VerifyMap BackedgeDumpsOld, BackedgeDumpsNew;
6967 for (LoopInfo::reverse_iterator I = LI->rbegin(), E = LI->rend(); I != E; ++I)
6968 getLoopBackedgeTakenCounts(*I, BackedgeDumpsOld, SE);
6969
6970 // Gather stringified backedge taken counts for all loops without using
6971 // SCEV's caches.
6972 SE.releaseMemory();
6973 for (LoopInfo::reverse_iterator I = LI->rbegin(), E = LI->rend(); I != E; ++I)
6974 getLoopBackedgeTakenCounts(*I, BackedgeDumpsNew, SE);
6975
6976 // Now compare whether they're the same with and without caches. This allows
6977 // verifying that no pass changed the cache.
6978 assert(BackedgeDumpsOld.size() == BackedgeDumpsNew.size() &&
6979 "New loops suddenly appeared!");
6980
6981 for (VerifyMap::iterator OldI = BackedgeDumpsOld.begin(),
6982 OldE = BackedgeDumpsOld.end(),
6983 NewI = BackedgeDumpsNew.begin();
6984 OldI != OldE; ++OldI, ++NewI) {
6985 assert(OldI->first == NewI->first && "Loop order changed!");
6986
6987 // Compare the stringified SCEVs. We don't care if undef backedgetaken count
6988 // changes.
6989 // FIXME: We currently ignore SCEV changes towards CouldNotCompute. This
6990 // means that a pass is buggy or SCEV has to learn a new pattern but is
6991 // usually not harmful.
6992 if (OldI->second != NewI->second &&
6993 OldI->second.find("undef") == std::string::npos &&
6994 NewI->second != "***COULDNOTCOMPUTE***") {
6995 dbgs() << "SCEVValidator: SCEV for Loop '"
6996 << OldI->first->getHeader()->getName()
6997 << "' from '" << OldI->second << "' to '" << NewI->second << "'!";
6998 std::abort();
6999 }
7000 }
7001
7002 // TODO: Verify more things.
7003 }