1 //===- ProfileVerifierPass.cpp - LLVM Pass to estimate profile info -------===//
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 file implements a pass that checks profiling information for
13 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "profile-verifier"
15 #include "llvm/Instructions.h"
16 #include "llvm/Module.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Analysis/ProfileInfo.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/CallSite.h"
21 #include "llvm/Support/CFG.h"
22 #include "llvm/Support/InstIterator.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Support/Format.h"
25 #include "llvm/Support/Debug.h"
29 static cl::opt
<bool,false>
30 ProfileVerifierDisableAssertions("profile-verifier-noassert",
31 cl::desc("Disable assertions"));
34 template<class FType
, class BType
>
35 class ProfileVerifierPassT
: public FunctionPass
{
37 struct DetailedBlockInfo
{
46 ProfileInfoT
<FType
, BType
> *PI
;
47 std::set
<const BType
*> BBisVisited
;
48 std::set
<const FType
*> FisVisited
;
49 bool DisableAssertions
;
51 // When debugging is enabled, the verifier prints a whole slew of debug
52 // information, otherwise its just the assert. These are all the helper
54 bool PrintedDebugTree
;
55 std::set
<const BType
*> BBisPrinted
;
56 void debugEntry(DetailedBlockInfo
*);
57 void printDebugInfo(const BType
*BB
);
60 static char ID
; // Class identification, replacement for typeinfo
62 explicit ProfileVerifierPassT () : FunctionPass(ID
) {
63 initializeProfileVerifierPassPass(*PassRegistry::getPassRegistry());
64 DisableAssertions
= ProfileVerifierDisableAssertions
;
66 explicit ProfileVerifierPassT (bool da
) : FunctionPass(ID
),
67 DisableAssertions(da
) {
68 initializeProfileVerifierPassPass(*PassRegistry::getPassRegistry());
71 void getAnalysisUsage(AnalysisUsage
&AU
) const {
73 AU
.addRequired
<ProfileInfoT
<FType
, BType
> >();
76 const char *getPassName() const {
77 return "Profiling information verifier";
80 /// run - Verify the profile information.
81 bool runOnFunction(FType
&F
);
82 void recurseBasicBlock(const BType
*);
84 bool exitReachable(const FType
*);
85 double ReadOrAssert(typename ProfileInfoT
<FType
, BType
>::Edge
);
86 void CheckValue(bool, const char*, DetailedBlockInfo
*);
89 typedef ProfileVerifierPassT
<Function
, BasicBlock
> ProfileVerifierPass
;
91 template<class FType
, class BType
>
92 void ProfileVerifierPassT
<FType
, BType
>::printDebugInfo(const BType
*BB
) {
94 if (BBisPrinted
.find(BB
) != BBisPrinted
.end()) return;
96 double BBWeight
= PI
->getExecutionCount(BB
);
97 if (BBWeight
== ProfileInfoT
<FType
, BType
>::MissingValue
) { BBWeight
= 0; }
100 std::set
<const BType
*> ProcessedPreds
;
101 for (const_pred_iterator bbi
= pred_begin(BB
), bbe
= pred_end(BB
);
102 bbi
!= bbe
; ++bbi
) {
103 if (ProcessedPreds
.insert(*bbi
).second
) {
104 typename ProfileInfoT
<FType
, BType
>::Edge E
= PI
->getEdge(*bbi
,BB
);
105 double EdgeWeight
= PI
->getEdgeWeight(E
);
106 if (EdgeWeight
== ProfileInfoT
<FType
, BType
>::MissingValue
) { EdgeWeight
= 0; }
107 dbgs() << "calculated in-edge " << E
<< ": "
108 << format("%20.20g",EdgeWeight
) << "\n";
109 inWeight
+= EdgeWeight
;
113 double outWeight
= 0;
115 std::set
<const BType
*> ProcessedSuccs
;
116 for ( succ_const_iterator bbi
= succ_begin(BB
), bbe
= succ_end(BB
);
117 bbi
!= bbe
; ++bbi
) {
118 if (ProcessedSuccs
.insert(*bbi
).second
) {
119 typename ProfileInfoT
<FType
, BType
>::Edge E
= PI
->getEdge(BB
,*bbi
);
120 double EdgeWeight
= PI
->getEdgeWeight(E
);
121 if (EdgeWeight
== ProfileInfoT
<FType
, BType
>::MissingValue
) { EdgeWeight
= 0; }
122 dbgs() << "calculated out-edge " << E
<< ": "
123 << format("%20.20g",EdgeWeight
) << "\n";
124 outWeight
+= EdgeWeight
;
128 dbgs() << "Block " << BB
->getNameStr() << " in "
129 << BB
->getParent()->getNameStr() << ":"
130 << "BBWeight=" << format("%20.20g",BBWeight
) << ","
131 << "inWeight=" << format("%20.20g",inWeight
) << ","
132 << "inCount=" << inCount
<< ","
133 << "outWeight=" << format("%20.20g",outWeight
) << ","
134 << "outCount" << outCount
<< "\n";
136 // mark as visited and recurse into subnodes
137 BBisPrinted
.insert(BB
);
138 for ( succ_const_iterator bbi
= succ_begin(BB
), bbe
= succ_end(BB
);
139 bbi
!= bbe
; ++bbi
) {
140 printDebugInfo(*bbi
);
144 template<class FType
, class BType
>
145 void ProfileVerifierPassT
<FType
, BType
>::debugEntry (DetailedBlockInfo
*DI
) {
146 dbgs() << "TROUBLE: Block " << DI
->BB
->getNameStr() << " in "
147 << DI
->BB
->getParent()->getNameStr() << ":"
148 << "BBWeight=" << format("%20.20g",DI
->BBWeight
) << ","
149 << "inWeight=" << format("%20.20g",DI
->inWeight
) << ","
150 << "inCount=" << DI
->inCount
<< ","
151 << "outWeight=" << format("%20.20g",DI
->outWeight
) << ","
152 << "outCount=" << DI
->outCount
<< "\n";
153 if (!PrintedDebugTree
) {
154 PrintedDebugTree
= true;
155 printDebugInfo(&(DI
->BB
->getParent()->getEntryBlock()));
159 // This compares A and B for equality.
160 static bool Equals(double A
, double B
) {
164 // This checks if the function "exit" is reachable from an given function
165 // via calls, this is necessary to check if a profile is valid despite the
166 // counts not fitting exactly.
167 template<class FType
, class BType
>
168 bool ProfileVerifierPassT
<FType
, BType
>::exitReachable(const FType
*F
) {
169 if (!F
) return false;
171 if (FisVisited
.count(F
)) return false;
173 FType
*Exit
= F
->getParent()->getFunction("exit");
178 FisVisited
.insert(F
);
180 for (const_inst_iterator I
= inst_begin(F
), E
= inst_end(F
); I
!= E
; ++I
) {
181 if (const CallInst
*CI
= dyn_cast
<CallInst
>(&*I
)) {
182 FType
*F
= CI
->getCalledFunction();
184 exits
|= exitReachable(F
);
186 // This is a call to a pointer, all bets are off...
195 #define ASSERTMESSAGE(M) \
196 { dbgs() << "ASSERT:" << (M) << "\n"; \
197 if (!DisableAssertions) assert(0 && (M)); }
199 template<class FType
, class BType
>
200 double ProfileVerifierPassT
<FType
, BType
>::ReadOrAssert(typename ProfileInfoT
<FType
, BType
>::Edge E
) {
201 double EdgeWeight
= PI
->getEdgeWeight(E
);
202 if (EdgeWeight
== ProfileInfoT
<FType
, BType
>::MissingValue
) {
203 dbgs() << "Edge " << E
<< " in Function "
204 << ProfileInfoT
<FType
, BType
>::getFunction(E
)->getNameStr() << ": ";
205 ASSERTMESSAGE("Edge has missing value");
208 if (EdgeWeight
< 0) {
209 dbgs() << "Edge " << E
<< " in Function "
210 << ProfileInfoT
<FType
, BType
>::getFunction(E
)->getNameStr() << ": ";
211 ASSERTMESSAGE("Edge has negative value");
217 template<class FType
, class BType
>
218 void ProfileVerifierPassT
<FType
, BType
>::CheckValue(bool Error
,
220 DetailedBlockInfo
*DI
) {
222 DEBUG(debugEntry(DI
));
223 dbgs() << "Block " << DI
->BB
->getNameStr() << " in Function "
224 << DI
->BB
->getParent()->getNameStr() << ": ";
225 ASSERTMESSAGE(Message
);
230 // This calculates the Information for a block and then recurses into the
232 template<class FType
, class BType
>
233 void ProfileVerifierPassT
<FType
, BType
>::recurseBasicBlock(const BType
*BB
) {
235 // Break the recursion by remembering all visited blocks.
236 if (BBisVisited
.find(BB
) != BBisVisited
.end()) return;
238 // Use a data structure to store all the information, this can then be handed
239 // to debug printers.
240 DetailedBlockInfo DI
;
242 DI
.outCount
= DI
.inCount
= 0;
243 DI
.inWeight
= DI
.outWeight
= 0;
245 // Read predecessors.
246 std::set
<const BType
*> ProcessedPreds
;
247 const_pred_iterator bpi
= pred_begin(BB
), bpe
= pred_end(BB
);
248 // If there are none, check for (0,BB) edge.
250 DI
.inWeight
+= ReadOrAssert(PI
->getEdge(0,BB
));
253 for (;bpi
!= bpe
; ++bpi
) {
254 if (ProcessedPreds
.insert(*bpi
).second
) {
255 DI
.inWeight
+= ReadOrAssert(PI
->getEdge(*bpi
,BB
));
261 std::set
<const BType
*> ProcessedSuccs
;
262 succ_const_iterator bbi
= succ_begin(BB
), bbe
= succ_end(BB
);
263 // If there is an (0,BB) edge, consider it too. (This is done not only when
264 // there are no successors, but every time; not every function contains
265 // return blocks with no successors (think loop latch as return block)).
266 double w
= PI
->getEdgeWeight(PI
->getEdge(BB
,0));
267 if (w
!= ProfileInfoT
<FType
, BType
>::MissingValue
) {
271 for (;bbi
!= bbe
; ++bbi
) {
272 if (ProcessedSuccs
.insert(*bbi
).second
) {
273 DI
.outWeight
+= ReadOrAssert(PI
->getEdge(BB
,*bbi
));
278 // Read block weight.
279 DI
.BBWeight
= PI
->getExecutionCount(BB
);
280 CheckValue(DI
.BBWeight
== ProfileInfoT
<FType
, BType
>::MissingValue
,
281 "BasicBlock has missing value", &DI
);
282 CheckValue(DI
.BBWeight
< 0,
283 "BasicBlock has negative value", &DI
);
285 // Check if this block is a setjmp target.
286 bool isSetJmpTarget
= false;
287 if (DI
.outWeight
> DI
.inWeight
) {
288 for (typename
BType::const_iterator i
= BB
->begin(), ie
= BB
->end();
290 if (const CallInst
*CI
= dyn_cast
<CallInst
>(&*i
)) {
291 FType
*F
= CI
->getCalledFunction();
292 if (F
&& (F
->getName() == "_setjmp")) {
293 isSetJmpTarget
= true; break;
298 // Check if this block is eventually reaching exit.
299 bool isExitReachable
= false;
300 if (DI
.inWeight
> DI
.outWeight
) {
301 for (typename
BType::const_iterator i
= BB
->begin(), ie
= BB
->end();
303 if (const CallInst
*CI
= dyn_cast
<CallInst
>(&*i
)) {
304 FType
*F
= CI
->getCalledFunction();
307 isExitReachable
|= exitReachable(F
);
309 // This is a call to a pointer, all bets are off...
310 isExitReachable
= true;
312 if (isExitReachable
) break;
317 if (DI
.inCount
> 0 && DI
.outCount
== 0) {
318 // If this is a block with no successors.
319 if (!isSetJmpTarget
) {
320 CheckValue(!Equals(DI
.inWeight
,DI
.BBWeight
),
321 "inWeight and BBWeight do not match", &DI
);
323 } else if (DI
.inCount
== 0 && DI
.outCount
> 0) {
324 // If this is a block with no predecessors.
325 if (!isExitReachable
)
326 CheckValue(!Equals(DI
.BBWeight
,DI
.outWeight
),
327 "BBWeight and outWeight do not match", &DI
);
329 // If this block has successors and predecessors.
330 if (DI
.inWeight
> DI
.outWeight
&& !isExitReachable
)
331 CheckValue(!Equals(DI
.inWeight
,DI
.outWeight
),
332 "inWeight and outWeight do not match", &DI
);
333 if (DI
.inWeight
< DI
.outWeight
&& !isSetJmpTarget
)
334 CheckValue(!Equals(DI
.inWeight
,DI
.outWeight
),
335 "inWeight and outWeight do not match", &DI
);
339 // Mark this block as visited, rescurse into successors.
340 BBisVisited
.insert(BB
);
341 for ( succ_const_iterator bbi
= succ_begin(BB
), bbe
= succ_end(BB
);
342 bbi
!= bbe
; ++bbi
) {
343 recurseBasicBlock(*bbi
);
347 template<class FType
, class BType
>
348 bool ProfileVerifierPassT
<FType
, BType
>::runOnFunction(FType
&F
) {
349 PI
= getAnalysisIfAvailable
<ProfileInfoT
<FType
, BType
> >();
351 ASSERTMESSAGE("No ProfileInfo available");
353 // Prepare global variables.
354 PrintedDebugTree
= false;
357 // Fetch entry block and recurse into it.
358 const BType
*entry
= &F
.getEntryBlock();
359 recurseBasicBlock(entry
);
361 if (PI
->getExecutionCount(&F
) != PI
->getExecutionCount(entry
))
362 ASSERTMESSAGE("Function count and entry block count do not match");
367 template<class FType
, class BType
>
368 char ProfileVerifierPassT
<FType
, BType
>::ID
= 0;
371 INITIALIZE_PASS_BEGIN(ProfileVerifierPass
, "profile-verifier",
372 "Verify profiling information", false, true)
373 INITIALIZE_AG_DEPENDENCY(ProfileInfo
)
374 INITIALIZE_PASS_END(ProfileVerifierPass
, "profile-verifier",
375 "Verify profiling information", false, true)
378 FunctionPass
*createProfileVerifierPass() {
379 return new ProfileVerifierPass(ProfileVerifierDisableAssertions
);