blob: 48d7d05d788f63f9ba61186c926d5a1cc87e0387 [file] [log] [blame]
Andrew Trick04317cc2011-01-29 01:09:53 +00001//===- PathProfileVerifier.cpp --------------------------------*- C++ -*---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This verifier derives an edge profile file from current path profile
11// information
12//
13//===----------------------------------------------------------------------===//
14#define DEBUG_TYPE "path-profile-verifier"
15
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/Analysis/Passes.h"
17#include "llvm/Analysis/PathProfileInfo.h"
18#include "llvm/Analysis/ProfileInfoTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/Module.h"
Andrew Trick04317cc2011-01-29 01:09:53 +000020#include "llvm/Pass.h"
Andrew Trick04317cc2011-01-29 01:09:53 +000021#include "llvm/Support/CommandLine.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/Support/Debug.h"
Andrew Trick04317cc2011-01-29 01:09:53 +000023#include "llvm/Support/raw_ostream.h"
Andrew Trick04317cc2011-01-29 01:09:53 +000024#include <stdio.h>
25
26using namespace llvm;
27
28namespace {
29 class PathProfileVerifier : public ModulePass {
30 private:
31 bool runOnModule(Module &M);
32
33 public:
34 static char ID; // Pass identification, replacement for typeid
35 PathProfileVerifier() : ModulePass(ID) {
36 initializePathProfileVerifierPass(*PassRegistry::getPassRegistry());
37 }
38
39
40 virtual const char *getPassName() const {
41 return "Path Profiler Verifier";
42 }
43
44 // The verifier requires the path profile and edge profile.
45 virtual void getAnalysisUsage(AnalysisUsage& AU) const;
46 };
47}
48
49static cl::opt<std::string>
50EdgeProfileFilename("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"),
54 cl::Hidden);
55
56char PathProfileVerifier::ID = 0;
57INITIALIZE_PASS(PathProfileVerifier, "path-profile-verifier",
58 "Compare the path profile derived edge profile against the "
59 "edge profile.", true, true)
60
61ModulePass *llvm::createPathProfileVerifierPass() {
62 return new PathProfileVerifier();
63}
64
65// The verifier requires the path profile and edge profile.
66void PathProfileVerifier::getAnalysisUsage(AnalysisUsage& AU) const {
67 AU.addRequired<PathProfileInfo>();
68 AU.addPreserved<PathProfileInfo>();
69}
70
71typedef std::map<unsigned, unsigned> DuplicateToIndexMap;
72typedef std::map<BasicBlock*,DuplicateToIndexMap> BlockToDuplicateMap;
73typedef std::map<BasicBlock*,BlockToDuplicateMap> NestedBlockToIndexMap;
74
75// the verifier iterates through each path to gather the total
76// number of edge frequencies
77bool PathProfileVerifier::runOnModule (Module &M) {
78 PathProfileInfo& pathProfileInfo = getAnalysis<PathProfileInfo>();
79
80 // setup a data structure to map path edges which index an
81 // array of edge counters
82 NestedBlockToIndexMap arrayMap;
83 unsigned i = 0;
84 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
85 if (F->isDeclaration()) continue;
86
Matt Arsenault71246fb2013-03-29 18:48:45 +000087 arrayMap[(BasicBlock*)0][F->begin()][0] = i++;
Andrew Trick04317cc2011-01-29 01:09:53 +000088
89 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
90 TerminatorInst *TI = BB->getTerminator();
91
92 unsigned duplicate = 0;
93 BasicBlock* prev = 0;
94 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e;
95 prev = TI->getSuccessor(s), ++s) {
96 if (prev == TI->getSuccessor(s))
97 duplicate++;
98 else duplicate = 0;
99
100 arrayMap[BB][TI->getSuccessor(s)][duplicate] = i++;
101 }
102 }
103 }
104
105 std::vector<unsigned> edgeArray(i);
106
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;
110
111 pathProfileInfo.setCurrentFunction(F);
112
113 DEBUG(dbgs() << "function '" << F->getName() << "' ran "
114 << pathProfileInfo.pathsRun()
115 << "/" << pathProfileInfo.getPotentialPathCount()
116 << " potential paths\n");
117
118 for( ProfilePathIterator nextPath = pathProfileInfo.pathBegin(),
119 endPath = pathProfileInfo.pathEnd();
120 nextPath != endPath; nextPath++ ) {
121 ProfilePath* currentPath = nextPath->second;
122
123 ProfilePathEdgeVector* pev = currentPath->getPathEdges();
124 DEBUG(dbgs () << "path #" << currentPath->getNumber() << ": "
125 << currentPath->getCount() << "\n");
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000126 // setup the entry edge (normally path profiling doesn't care about this)
Andrew Trick04317cc2011-01-29 01:09:53 +0000127 if (currentPath->getFirstBlockInPath() == &F->getEntryBlock())
Matt Arsenault71246fb2013-03-29 18:48:45 +0000128 edgeArray[arrayMap[(BasicBlock*)0][currentPath->getFirstBlockInPath()][0]]
Andrew Trick04317cc2011-01-29 01:09:53 +0000129 += currentPath->getCount();
130
131 for( ProfilePathEdgeIterator nextEdge = pev->begin(),
132 endEdge = pev->end(); nextEdge != endEdge; nextEdge++ ) {
133 if (nextEdge != pev->begin())
134 DEBUG(dbgs() << " :: ");
135
136 BasicBlock* source = nextEdge->getSource();
137 BasicBlock* target = nextEdge->getTarget();
138 unsigned duplicateNumber = nextEdge->getDuplicateNumber();
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000139 DEBUG(dbgs() << source->getName() << " --{" << duplicateNumber
140 << "}--> " << target->getName());
Andrew Trick04317cc2011-01-29 01:09:53 +0000141
142 // Ensure all the referenced edges exist
143 // TODO: make this a separate function
144 if( !arrayMap.count(source) ) {
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000145 errs() << " error [" << F->getName() << "()]: source '"
146 << source->getName()
Andrew Trick04317cc2011-01-29 01:09:53 +0000147 << "' does not exist in the array map.\n";
148 } else if( !arrayMap[source].count(target) ) {
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000149 errs() << " error [" << F->getName() << "()]: target '"
150 << target->getName()
Andrew Trick04317cc2011-01-29 01:09:53 +0000151 << "' does not exist in the array map.\n";
152 } else if( !arrayMap[source][target].count(duplicateNumber) ) {
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000153 errs() << " error [" << F->getName() << "()]: edge "
154 << source->getName() << " -> " << target->getName()
Andrew Trick04317cc2011-01-29 01:09:53 +0000155 << " duplicate number " << duplicateNumber
156 << " does not exist in the array map.\n";
157 } else {
158 edgeArray[arrayMap[source][target][duplicateNumber]]
159 += currentPath->getCount();
160 }
161 }
162
163 DEBUG(errs() << "\n");
164
165 delete pev;
166 }
167 }
168
169 std::string errorInfo;
170 std::string filename = EdgeProfileFilename;
171
172 // Open a handle to the file
173 FILE* edgeFile = fopen(filename.c_str(),"wb");
174
175 if (!edgeFile) {
176 errs() << "error: unable to open file '" << filename << "' for output.\n";
177 return false;
178 }
179
180 errs() << "Generating edge profile '" << filename << "' ...\n";
181
182 // write argument info
183 unsigned type = ArgumentInfo;
184 unsigned num = pathProfileInfo.argList.size();
185 int zeros = 0;
186
187 fwrite(&type,sizeof(unsigned),1,edgeFile);
188 fwrite(&num,sizeof(unsigned),1,edgeFile);
189 fwrite(pathProfileInfo.argList.c_str(),1,num,edgeFile);
190 if (num&3)
191 fwrite(&zeros, 1, 4-(num&3), edgeFile);
192
193 type = EdgeInfo;
194 num = edgeArray.size();
195 fwrite(&type,sizeof(unsigned),1,edgeFile);
196 fwrite(&num,sizeof(unsigned),1,edgeFile);
197
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);
202
203 fclose (edgeFile);
204
205 return true;
206}