2003-06-29 20:17:07 +02:00
|
|
|
//===- GraphChecker.cpp - Assert that various graph properties hold -------===//
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-06-29 20:17:07 +02:00
|
|
|
//
|
|
|
|
// This pass is used to test DSA with regression tests. It can be used to check
|
|
|
|
// that certain graph properties hold, such as two nodes being disjoint, whether
|
|
|
|
// or not a node is collapsed, etc. These are the command line arguments that
|
|
|
|
// it supports:
|
|
|
|
//
|
2004-02-21 00:27:09 +01:00
|
|
|
// --dsgc-dspass={local,bu,td} - Specify what flavor of graph to check
|
2003-06-29 20:17:07 +02:00
|
|
|
// --dsgc-abort-if-any-collapsed - Abort if any collapsed nodes are found
|
|
|
|
// --dsgc-abort-if-collapsed=<list> - Abort if a node pointed to by an SSA
|
|
|
|
// value with name in <list> is collapsed
|
2003-07-25 22:45:40 +02:00
|
|
|
// --dsgc-check-flags=<list> - Abort if the specified nodes have flags
|
|
|
|
// that are not specified.
|
2003-06-29 20:17:07 +02:00
|
|
|
// --dsgc-abort-if-merged=<list> - Abort if any of the named SSA values
|
|
|
|
// point to the same node.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-07-07 08:32:21 +02:00
|
|
|
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
|
|
|
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2003-06-29 20:17:07 +02:00
|
|
|
#include "llvm/Value.h"
|
2004-07-04 14:19:56 +02:00
|
|
|
#include <iostream>
|
2003-06-29 20:17:07 +02:00
|
|
|
#include <set>
|
|
|
|
|
2003-11-13 00:11:14 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2003-06-29 20:17:07 +02:00
|
|
|
namespace {
|
|
|
|
enum DSPass { local, bu, td };
|
|
|
|
cl::opt<DSPass>
|
|
|
|
DSPass("dsgc-dspass", cl::Hidden,
|
|
|
|
cl::desc("Specify which DSA pass the -datastructure-gc pass should use"),
|
|
|
|
cl::values(clEnumVal(local, "Local pass"),
|
|
|
|
clEnumVal(bu, "Bottom-up pass"),
|
2005-04-21 23:13:18 +02:00
|
|
|
clEnumVal(td, "Top-down pass"),
|
2004-07-16 02:04:13 +02:00
|
|
|
clEnumValEnd), cl::init(local));
|
2003-06-29 20:17:07 +02:00
|
|
|
|
|
|
|
cl::opt<bool>
|
|
|
|
AbortIfAnyCollapsed("dsgc-abort-if-any-collapsed", cl::Hidden,
|
|
|
|
cl::desc("Abort if any collapsed nodes are found"));
|
|
|
|
cl::list<std::string>
|
|
|
|
AbortIfCollapsed("dsgc-abort-if-collapsed", cl::Hidden, cl::CommaSeparated,
|
|
|
|
cl::desc("Abort if any of the named symbols is collapsed"));
|
|
|
|
cl::list<std::string>
|
2003-07-25 22:45:40 +02:00
|
|
|
CheckFlags("dsgc-check-flags", cl::Hidden, cl::CommaSeparated,
|
|
|
|
cl::desc("Check that flags are specified for nodes"));
|
2003-06-30 00:36:15 +02:00
|
|
|
cl::list<std::string>
|
2003-06-29 20:17:07 +02:00
|
|
|
AbortIfMerged("dsgc-abort-if-merged", cl::Hidden, cl::CommaSeparated,
|
|
|
|
cl::desc("Abort if any of the named symbols are merged together"));
|
|
|
|
|
|
|
|
struct DSGC : public FunctionPass {
|
|
|
|
DSGC();
|
|
|
|
bool doFinalization(Module &M);
|
|
|
|
bool runOnFunction(Function &F);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
switch (DSPass) {
|
|
|
|
case local: AU.addRequired<LocalDataStructures>(); break;
|
|
|
|
case bu: AU.addRequired<BUDataStructures>(); break;
|
|
|
|
case td: AU.addRequired<TDDataStructures>(); break;
|
|
|
|
}
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2003-06-29 22:27:16 +02:00
|
|
|
void print(std::ostream &O, const Module *M) const {}
|
2003-06-29 20:17:07 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
void verify(const DSGraph &G);
|
|
|
|
};
|
|
|
|
|
|
|
|
RegisterAnalysis<DSGC> X("datastructure-gc", "DSA Graph Checking Pass");
|
|
|
|
}
|
|
|
|
|
2005-10-24 02:38:38 +02:00
|
|
|
FunctionPass *llvm::createDataStructureGraphCheckerPass() {
|
|
|
|
return new DSGC();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-29 20:17:07 +02:00
|
|
|
DSGC::DSGC() {
|
|
|
|
if (!AbortIfAnyCollapsed && AbortIfCollapsed.empty() &&
|
2003-07-25 22:45:40 +02:00
|
|
|
CheckFlags.empty() && AbortIfMerged.empty()) {
|
2003-06-29 20:17:07 +02:00
|
|
|
std::cerr << "The -datastructure-gc is useless if you don't specify any"
|
|
|
|
" -dsgc-* options. See the -help-hidden output for a list.\n";
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// doFinalization - Verify that the globals graph is in good shape...
|
|
|
|
///
|
|
|
|
bool DSGC::doFinalization(Module &M) {
|
|
|
|
switch (DSPass) {
|
|
|
|
case local:verify(getAnalysis<LocalDataStructures>().getGlobalsGraph());break;
|
|
|
|
case bu: verify(getAnalysis<BUDataStructures>().getGlobalsGraph()); break;
|
|
|
|
case td: verify(getAnalysis<TDDataStructures>().getGlobalsGraph()); break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// runOnFunction - Get the DSGraph for this function and verify that it is ok.
|
|
|
|
///
|
|
|
|
bool DSGC::runOnFunction(Function &F) {
|
|
|
|
switch (DSPass) {
|
|
|
|
case local: verify(getAnalysis<LocalDataStructures>().getDSGraph(F)); break;
|
|
|
|
case bu: verify(getAnalysis<BUDataStructures>().getDSGraph(F)); break;
|
|
|
|
case td: verify(getAnalysis<TDDataStructures>().getDSGraph(F)); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// verify - This is the function which checks to make sure that all of the
|
|
|
|
/// invariants established on the command line are true.
|
|
|
|
///
|
|
|
|
void DSGC::verify(const DSGraph &G) {
|
|
|
|
// Loop over all of the nodes, checking to see if any are collapsed...
|
|
|
|
if (AbortIfAnyCollapsed) {
|
2005-03-16 23:42:19 +01:00
|
|
|
for (DSGraph::node_const_iterator I = G.node_begin(), E = G.node_end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (I->isNodeCompletelyFolded()) {
|
2003-06-29 20:17:07 +02:00
|
|
|
std::cerr << "Node is collapsed: ";
|
2005-03-16 23:42:19 +01:00
|
|
|
I->print(std::cerr, &G);
|
2003-06-29 20:17:07 +02:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-25 22:45:40 +02:00
|
|
|
if (!AbortIfCollapsed.empty() || !CheckFlags.empty() ||
|
2003-06-30 00:36:15 +02:00
|
|
|
!AbortIfMerged.empty()) {
|
2003-06-29 20:17:07 +02:00
|
|
|
// Convert from a list to a set, because we don't have cl::set's yet. FIXME
|
|
|
|
std::set<std::string> AbortIfCollapsedS(AbortIfCollapsed.begin(),
|
|
|
|
AbortIfCollapsed.end());
|
2003-06-30 00:36:15 +02:00
|
|
|
std::set<std::string> AbortIfMergedS(AbortIfMerged.begin(),
|
|
|
|
AbortIfMerged.end());
|
2003-07-25 22:45:40 +02:00
|
|
|
std::map<std::string, unsigned> CheckFlagsM;
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2003-07-25 22:45:40 +02:00
|
|
|
for (cl::list<std::string>::iterator I = CheckFlags.begin(),
|
|
|
|
E = CheckFlags.end(); I != E; ++I) {
|
2003-10-02 00:49:22 +02:00
|
|
|
std::string::size_type ColonPos = I->rfind(':');
|
2003-07-25 22:45:40 +02:00
|
|
|
if (ColonPos == std::string::npos) {
|
|
|
|
std::cerr << "Error: '" << *I
|
|
|
|
<< "' is an invalid value for the --dsgc-check-flags option!\n";
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Flags = 0;
|
2003-07-27 01:00:05 +02:00
|
|
|
for (unsigned C = ColonPos+1; C != I->size(); ++C)
|
|
|
|
switch ((*I)[C]) {
|
2003-07-25 22:45:40 +02:00
|
|
|
case 'S': Flags |= DSNode::AllocaNode; break;
|
|
|
|
case 'H': Flags |= DSNode::HeapNode; break;
|
|
|
|
case 'G': Flags |= DSNode::GlobalNode; break;
|
|
|
|
case 'U': Flags |= DSNode::UnknownNode; break;
|
|
|
|
case 'I': Flags |= DSNode::Incomplete; break;
|
|
|
|
case 'M': Flags |= DSNode::Modified; break;
|
|
|
|
case 'R': Flags |= DSNode::Read; break;
|
|
|
|
case 'A': Flags |= DSNode::Array; break;
|
2003-07-25 22:49:29 +02:00
|
|
|
default: std::cerr << "Invalid DSNode flag!\n"; abort();
|
2003-07-25 22:45:40 +02:00
|
|
|
}
|
|
|
|
CheckFlagsM[std::string(I->begin(), I->begin()+ColonPos)] = Flags;
|
|
|
|
}
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2003-06-29 20:17:07 +02:00
|
|
|
// Now we loop over all of the scalars, checking to see if any are collapsed
|
|
|
|
// that are not supposed to be, or if any are merged together.
|
|
|
|
const DSGraph::ScalarMapTy &SM = G.getScalarMap();
|
|
|
|
std::map<DSNode*, std::string> AbortIfMergedNodes;
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2003-06-29 20:17:07 +02:00
|
|
|
for (DSGraph::ScalarMapTy::const_iterator I = SM.begin(), E = SM.end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (I->first->hasName() && I->second.getNode()) {
|
2003-07-25 22:45:40 +02:00
|
|
|
const std::string &Name = I->first->getName();
|
2003-06-29 20:17:07 +02:00
|
|
|
DSNode *N = I->second.getNode();
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2003-06-29 20:17:07 +02:00
|
|
|
// Verify it is not collapsed if it is not supposed to be...
|
2003-06-30 00:36:15 +02:00
|
|
|
if (N->isNodeCompletelyFolded() && AbortIfCollapsedS.count(Name)) {
|
|
|
|
std::cerr << "Node for value '%" << Name << "' is collapsed: ";
|
|
|
|
N->print(std::cerr, &G);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2003-07-25 22:45:40 +02:00
|
|
|
if (CheckFlagsM.count(Name) && CheckFlagsM[Name] != N->getNodeFlags()) {
|
2006-04-25 21:33:41 +02:00
|
|
|
std::cerr << "Node flags are not as expected for node: " << Name
|
|
|
|
<< " (" << CheckFlagsM[Name] << ":" <<N->getNodeFlags()
|
|
|
|
<< ")\n";
|
2003-06-29 20:17:07 +02:00
|
|
|
N->print(std::cerr, &G);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that it is not merged if it is not supposed to be...
|
2003-06-30 00:36:15 +02:00
|
|
|
if (AbortIfMergedS.count(Name)) {
|
2003-06-29 20:17:07 +02:00
|
|
|
if (AbortIfMergedNodes.count(N)) {
|
2003-06-30 00:36:15 +02:00
|
|
|
std::cerr << "Nodes for values '%" << Name << "' and '%"
|
|
|
|
<< AbortIfMergedNodes[N] << "' is merged: ";
|
2003-06-29 20:17:07 +02:00
|
|
|
N->print(std::cerr, &G);
|
|
|
|
abort();
|
|
|
|
}
|
2003-06-30 00:36:15 +02:00
|
|
|
AbortIfMergedNodes[N] = Name;
|
2003-06-29 20:17:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|