1 //===- PathProfileVerifier.cpp --------------------------------*- C++ -*---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This verifier derives an edge profile file from current path profile
13 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "path-profile-verifier"
16 #include "llvm/Analysis/Passes.h"
17 #include "llvm/Analysis/PathProfileInfo.h"
18 #include "llvm/Analysis/ProfileInfoTypes.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
29 class PathProfileVerifier : public ModulePass {
31 bool runOnModule(Module &M);
34 static char ID; // Pass identification, replacement for typeid
35 PathProfileVerifier() : ModulePass(ID) {
36 initializePathProfileVerifierPass(*PassRegistry::getPassRegistry());
40 virtual const char *getPassName() const {
41 return "Path Profiler Verifier";
44 // The verifier requires the path profile and edge profile.
45 virtual void getAnalysisUsage(AnalysisUsage& AU) const;
49 static cl::opt<std::string>
50 EdgeProfileFilename("path-profile-verifier-file",
51 cl::init("edgefrompath.llvmprof.out"),
52 cl::value_desc("filename"),
53 cl::desc("Edge profile file generated by -path-profile-verifier"),
56 char PathProfileVerifier::ID = 0;
57 INITIALIZE_PASS(PathProfileVerifier, "path-profile-verifier",
58 "Compare the path profile derived edge profile against the "
59 "edge profile.", true, true)
61 ModulePass *llvm::createPathProfileVerifierPass() {
62 return new PathProfileVerifier();
65 // The verifier requires the path profile and edge profile.
66 void PathProfileVerifier::getAnalysisUsage(AnalysisUsage& AU) const {
67 AU.addRequired<PathProfileInfo>();
68 AU.addPreserved<PathProfileInfo>();
71 typedef std::map<unsigned, unsigned> DuplicateToIndexMap;
72 typedef std::map<BasicBlock*,DuplicateToIndexMap> BlockToDuplicateMap;
73 typedef std::map<BasicBlock*,BlockToDuplicateMap> NestedBlockToIndexMap;
75 // the verifier iterates through each path to gather the total
76 // number of edge frequencies
77 bool PathProfileVerifier::runOnModule (Module &M) {
78 PathProfileInfo& pathProfileInfo = getAnalysis<PathProfileInfo>();
80 // setup a data structure to map path edges which index an
81 // array of edge counters
82 NestedBlockToIndexMap arrayMap;
84 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
85 if (F->isDeclaration()) continue;
87 arrayMap[(BasicBlock*)0][F->begin()][0] = i++;
89 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
90 TerminatorInst *TI = BB->getTerminator();
92 unsigned duplicate = 0;
94 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e;
95 prev = TI->getSuccessor(s), ++s) {
96 if (prev == TI->getSuccessor(s))
100 arrayMap[BB][TI->getSuccessor(s)][duplicate] = i++;
105 std::vector<unsigned> edgeArray(i);
107 // iterate through each path and increment the edge counters as needed
108 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
109 if (F->isDeclaration()) continue;
111 pathProfileInfo.setCurrentFunction(F);
113 DEBUG(dbgs() << "function '" << F->getName() << "' ran "
114 << pathProfileInfo.pathsRun()
115 << "/" << pathProfileInfo.getPotentialPathCount()
116 << " potential paths\n");
118 for( ProfilePathIterator nextPath = pathProfileInfo.pathBegin(),
119 endPath = pathProfileInfo.pathEnd();
120 nextPath != endPath; nextPath++ ) {
121 ProfilePath* currentPath = nextPath->second;
123 ProfilePathEdgeVector* pev = currentPath->getPathEdges();
124 DEBUG(dbgs () << "path #" << currentPath->getNumber() << ": "
125 << currentPath->getCount() << "\n");
126 // setup the entry edge (normally path profiling doesn't care about this)
127 if (currentPath->getFirstBlockInPath() == &F->getEntryBlock())
128 edgeArray[arrayMap[(BasicBlock*)0][currentPath->getFirstBlockInPath()][0]]
129 += currentPath->getCount();
131 for( ProfilePathEdgeIterator nextEdge = pev->begin(),
132 endEdge = pev->end(); nextEdge != endEdge; nextEdge++ ) {
133 if (nextEdge != pev->begin())
134 DEBUG(dbgs() << " :: ");
136 BasicBlock* source = nextEdge->getSource();
137 BasicBlock* target = nextEdge->getTarget();
138 unsigned duplicateNumber = nextEdge->getDuplicateNumber();
139 DEBUG(dbgs() << source->getName() << " --{" << duplicateNumber
140 << "}--> " << target->getName());
142 // Ensure all the referenced edges exist
143 // TODO: make this a separate function
144 if( !arrayMap.count(source) ) {
145 errs() << " error [" << F->getName() << "()]: source '"
147 << "' does not exist in the array map.\n";
148 } else if( !arrayMap[source].count(target) ) {
149 errs() << " error [" << F->getName() << "()]: target '"
151 << "' does not exist in the array map.\n";
152 } else if( !arrayMap[source][target].count(duplicateNumber) ) {
153 errs() << " error [" << F->getName() << "()]: edge "
154 << source->getName() << " -> " << target->getName()
155 << " duplicate number " << duplicateNumber
156 << " does not exist in the array map.\n";
158 edgeArray[arrayMap[source][target][duplicateNumber]]
159 += currentPath->getCount();
163 DEBUG(errs() << "\n");
169 std::string errorInfo;
170 std::string filename = EdgeProfileFilename;
172 // Open a handle to the file
173 FILE* edgeFile = fopen(filename.c_str(),"wb");
176 errs() << "error: unable to open file '" << filename << "' for output.\n";
180 errs() << "Generating edge profile '" << filename << "' ...\n";
182 // write argument info
183 unsigned type = ArgumentInfo;
184 unsigned num = pathProfileInfo.argList.size();
187 fwrite(&type,sizeof(unsigned),1,edgeFile);
188 fwrite(&num,sizeof(unsigned),1,edgeFile);
189 fwrite(pathProfileInfo.argList.c_str(),1,num,edgeFile);
191 fwrite(&zeros, 1, 4-(num&3), edgeFile);
194 num = edgeArray.size();
195 fwrite(&type,sizeof(unsigned),1,edgeFile);
196 fwrite(&num,sizeof(unsigned),1,edgeFile);
198 // write each edge to the file
199 for( std::vector<unsigned>::iterator s = edgeArray.begin(),
200 e = edgeArray.end(); s != e; s++)
201 fwrite(&*s, sizeof (unsigned), 1, edgeFile);