Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 1 | //===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===// |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Duncan Sands | 9c40c28 | 2008-09-23 12:47:39 +0000 | [diff] [blame] | 10 | // This file defines a '-dot-cfg' analysis pass, which emits the |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 11 | // cfg.<fnname>.dot file for each function in the program, with a graph of the |
| 12 | // CFG for that function. |
| 13 | // |
| 14 | // The other main feature of this file is that it implements the |
| 15 | // Function::viewCFG method, which is useful for debugging passes which operate |
| 16 | // on the CFG. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Brian Gaeke | 104341f | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/CFGPrinter.h" |
Chris Lattner | da42426 | 2009-10-18 04:09:11 +0000 | [diff] [blame] | 21 | #include "llvm/Pass.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 22 | #include "llvm/Support/FileSystem.h" |
Chris Lattner | 62aff84 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 25 | static cl::opt<bool> CFGHeatPerFunction("cfg-heat-per-function", |
| 26 | cl::init(false), cl::Hidden, |
| 27 | cl::desc("Heat CFG per function")); |
| 28 | |
| 29 | static cl::opt<bool> ShowHeatColors("cfg-heat-colors", cl::init(true), |
| 30 | cl::Hidden, |
| 31 | cl::desc("Show heat colors in CFG")); |
| 32 | |
| 33 | static cl::opt<bool> UseRawEdgeWeight("cfg-raw-weights", cl::init(false), |
| 34 | cl::Hidden, |
| 35 | cl::desc("Use raw weights for labels. " |
| 36 | "Use percentages as default.")); |
| 37 | |
| 38 | static cl::opt<bool> ShowEdgeWeight("cfg-weights", cl::init(true), cl::Hidden, |
| 39 | cl::desc("Show edges labeled with weights")); |
| 40 | |
| 41 | static void writeHeatCFGToDotFile(Function &F, BlockFrequencyInfo *BFI, |
| 42 | BranchProbabilityInfo *BPI, uint64_t MaxFreq, |
| 43 | bool UseHeuristic, bool isSimple) { |
| 44 | std::string Filename = ("cfg." + F.getName() + ".dot").str(); |
| 45 | errs() << "Writing '" << Filename << "'..."; |
| 46 | |
| 47 | std::error_code EC; |
| 48 | raw_fd_ostream File(Filename, EC, sys::fs::F_Text); |
| 49 | |
| 50 | CFGDOTInfo CFGInfo(&F, BFI, BPI, MaxFreq); |
| 51 | CFGInfo.setHeuristic(UseHeuristic); |
| 52 | CFGInfo.setHeatColors(ShowHeatColors); |
| 53 | CFGInfo.setEdgeWeights(ShowEdgeWeight); |
| 54 | CFGInfo.setRawEdgeWeights(UseRawEdgeWeight); |
| 55 | |
| 56 | if (!EC) |
| 57 | WriteGraph(File, &CFGInfo, isSimple); |
| 58 | else |
| 59 | errs() << " error opening file for writing!"; |
| 60 | errs() << "\n"; |
| 61 | } |
| 62 | |
| 63 | static void writeAllCFGsToDotFile(Module &M, |
| 64 | function_ref<BlockFrequencyInfo *(Function &)> LookupBFI, |
| 65 | function_ref<BranchProbabilityInfo *(Function &)> LookupBPI, |
| 66 | bool isSimple) { |
| 67 | bool UseHeuristic = true; |
| 68 | uint64_t MaxFreq = 0; |
| 69 | if (!CFGHeatPerFunction) |
| 70 | MaxFreq = getMaxFreq(M, LookupBFI, UseHeuristic); |
| 71 | |
| 72 | for (auto &F : M) { |
| 73 | if (F.isDeclaration()) continue; |
| 74 | auto *BFI = LookupBFI(F); |
| 75 | auto *BPI = LookupBPI(F); |
| 76 | if (CFGHeatPerFunction) |
| 77 | MaxFreq = getMaxFreq(F, BFI, UseHeuristic); |
| 78 | writeHeatCFGToDotFile(F, BFI, BPI, MaxFreq, UseHeuristic, isSimple); |
| 79 | } |
| 80 | |
| 81 | } |
| 82 | |
| 83 | static void viewHeatCFG(Function &F, BlockFrequencyInfo *BFI, |
| 84 | BranchProbabilityInfo *BPI, uint64_t MaxFreq, |
| 85 | bool UseHeuristic, bool isSimple) { |
| 86 | CFGDOTInfo CFGInfo(&F, BFI, BPI, MaxFreq); |
| 87 | CFGInfo.setHeuristic(UseHeuristic); |
| 88 | CFGInfo.setHeatColors(ShowHeatColors); |
| 89 | CFGInfo.setEdgeWeights(ShowEdgeWeight); |
| 90 | CFGInfo.setRawEdgeWeights(UseRawEdgeWeight); |
| 91 | |
| 92 | ViewGraph(&CFGInfo, "cfg." + F.getName(), isSimple); |
| 93 | } |
| 94 | |
| 95 | static void viewAllCFGs(Module &M, |
| 96 | function_ref<BlockFrequencyInfo *(Function &)> LookupBFI, |
| 97 | function_ref<BranchProbabilityInfo *(Function &)> LookupBPI, |
| 98 | bool isSimple) { |
| 99 | bool UseHeuristic = true; |
| 100 | uint64_t MaxFreq = 0; |
| 101 | if (!CFGHeatPerFunction) |
| 102 | MaxFreq = getMaxFreq(M, LookupBFI, UseHeuristic); |
| 103 | |
| 104 | for (auto &F : M) { |
| 105 | if (F.isDeclaration()) continue; |
| 106 | auto *BFI = LookupBFI(F); |
| 107 | auto *BPI = LookupBPI(F); |
| 108 | if (CFGHeatPerFunction) |
| 109 | MaxFreq = getMaxFreq(F, BFI, UseHeuristic); |
| 110 | viewHeatCFG(F, BFI, BPI, MaxFreq, UseHeuristic, isSimple); |
| 111 | } |
| 112 | |
| 113 | } |
| 114 | |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 115 | namespace { |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 116 | struct CFGViewerLegacyPass : public ModulePass { |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 117 | static char ID; // Pass identifcation, replacement for typeid |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 118 | CFGViewerLegacyPass() : ModulePass(ID) { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 119 | initializeCFGViewerLegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 120 | } |
Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 121 | |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 122 | bool runOnModule(Module &M) override { |
| 123 | auto LookupBFI = [this](Function &F) { |
| 124 | return &this->getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI(); |
| 125 | }; |
| 126 | auto LookupBPI = [this](Function &F) { |
| 127 | return &this->getAnalysis<BranchProbabilityInfoWrapperPass>(F).getBPI(); |
| 128 | }; |
| 129 | viewAllCFGs(M, LookupBFI, LookupBPI, /*isSimple=*/false); |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 130 | return false; |
| 131 | } |
| 132 | |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 133 | void print(raw_ostream &OS, const Module * = nullptr) const override {} |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 134 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 135 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 136 | ModulePass::getAnalysisUsage(AU); |
| 137 | AU.addRequired<BlockFrequencyInfoWrapperPass>(); |
| 138 | AU.addRequired<BranchProbabilityInfoWrapperPass>(); |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 139 | AU.setPreservesAll(); |
| 140 | } |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 141 | |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 142 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 143 | } |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 144 | |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 145 | char CFGViewerLegacyPass::ID = 0; |
| 146 | INITIALIZE_PASS(CFGViewerLegacyPass, "view-cfg", "View CFG of function", false, true) |
| 147 | |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 148 | PreservedAnalyses CFGViewerPass::run(Module &M, |
| 149 | ModuleAnalysisManager &AM) { |
| 150 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
| 151 | auto LookupBFI = [&FAM](Function &F) { |
| 152 | return &FAM.getResult<BlockFrequencyAnalysis>(F); |
| 153 | }; |
| 154 | auto LookupBPI = [&FAM](Function &F) { |
| 155 | return &FAM.getResult<BranchProbabilityAnalysis>(F); |
| 156 | }; |
| 157 | viewAllCFGs(M, LookupBFI, LookupBPI, /*isSimple=*/false); |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 158 | return PreservedAnalyses::all(); |
| 159 | } |
| 160 | |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 161 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 162 | namespace { |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 163 | struct CFGOnlyViewerLegacyPass : public ModulePass { |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 164 | static char ID; // Pass identifcation, replacement for typeid |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 165 | CFGOnlyViewerLegacyPass() : ModulePass(ID) { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 166 | initializeCFGOnlyViewerLegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 167 | } |
Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 168 | |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 169 | bool runOnModule(Module &M) override { |
| 170 | auto LookupBFI = [this](Function &F) { |
| 171 | return &this->getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI(); |
| 172 | }; |
| 173 | auto LookupBPI = [this](Function &F) { |
| 174 | return &this->getAnalysis<BranchProbabilityInfoWrapperPass>(F).getBPI(); |
| 175 | }; |
| 176 | viewAllCFGs(M, LookupBFI, LookupBPI, /*isSimple=*/true); |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 180 | void print(raw_ostream &OS, const Module * = nullptr) const override {} |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 181 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 182 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 183 | ModulePass::getAnalysisUsage(AU); |
| 184 | AU.addRequired<BlockFrequencyInfoWrapperPass>(); |
| 185 | AU.addRequired<BranchProbabilityInfoWrapperPass>(); |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 186 | AU.setPreservesAll(); |
| 187 | } |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 188 | |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 189 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 190 | } |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 191 | |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 192 | char CFGOnlyViewerLegacyPass::ID = 0; |
| 193 | INITIALIZE_PASS(CFGOnlyViewerLegacyPass, "view-cfg-only", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 194 | "View CFG of function (with no function bodies)", false, true) |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 195 | |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 196 | PreservedAnalyses CFGOnlyViewerPass::run(Module &M, |
| 197 | ModuleAnalysisManager &AM) { |
| 198 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
| 199 | auto LookupBFI = [&FAM](Function &F) { |
| 200 | return &FAM.getResult<BlockFrequencyAnalysis>(F); |
| 201 | }; |
| 202 | auto LookupBPI = [&FAM](Function &F) { |
| 203 | return &FAM.getResult<BranchProbabilityAnalysis>(F); |
| 204 | }; |
| 205 | viewAllCFGs(M, LookupBFI, LookupBPI, /*isSimple=*/true); |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 206 | return PreservedAnalyses::all(); |
| 207 | } |
| 208 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 209 | namespace { |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 210 | struct CFGPrinterLegacyPass : public ModulePass { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 211 | static char ID; // Pass identification, replacement for typeid |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 212 | CFGPrinterLegacyPass() : ModulePass(ID) { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 213 | initializeCFGPrinterLegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 214 | } |
Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 215 | |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 216 | bool runOnModule(Module &M) override { |
| 217 | auto LookupBFI = [this](Function &F) { |
| 218 | return &this->getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI(); |
| 219 | }; |
| 220 | auto LookupBPI = [this](Function &F) { |
| 221 | return &this->getAnalysis<BranchProbabilityInfoWrapperPass>(F).getBPI(); |
| 222 | }; |
| 223 | writeAllCFGsToDotFile(M, LookupBFI, LookupBPI, /*isSimple=*/false); |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 224 | return false; |
| 225 | } |
| 226 | |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 227 | void print(raw_ostream &OS, const Module * = nullptr) const override {} |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 228 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 229 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 230 | ModulePass::getAnalysisUsage(AU); |
| 231 | AU.addRequired<BlockFrequencyInfoWrapperPass>(); |
| 232 | AU.addRequired<BranchProbabilityInfoWrapperPass>(); |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 233 | AU.setPreservesAll(); |
| 234 | } |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 235 | |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 236 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 237 | } |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 238 | |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 239 | char CFGPrinterLegacyPass::ID = 0; |
| 240 | INITIALIZE_PASS(CFGPrinterLegacyPass, "dot-cfg", "Print CFG of function to 'dot' file", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 241 | false, true) |
Chris Lattner | 62aff84 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 242 | |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 243 | PreservedAnalyses CFGPrinterPass::run(Module &M, |
| 244 | ModuleAnalysisManager &AM) { |
| 245 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
| 246 | auto LookupBFI = [&FAM](Function &F) { |
| 247 | return &FAM.getResult<BlockFrequencyAnalysis>(F); |
| 248 | }; |
| 249 | auto LookupBPI = [&FAM](Function &F) { |
| 250 | return &FAM.getResult<BranchProbabilityAnalysis>(F); |
| 251 | }; |
| 252 | writeAllCFGsToDotFile(M, LookupBFI, LookupBPI, /*isSimple=*/false); |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 253 | return PreservedAnalyses::all(); |
| 254 | } |
| 255 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 256 | namespace { |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 257 | struct CFGOnlyPrinterLegacyPass : public ModulePass { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 258 | static char ID; // Pass identification, replacement for typeid |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 259 | CFGOnlyPrinterLegacyPass() : ModulePass(ID) { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 260 | initializeCFGOnlyPrinterLegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 261 | } |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 262 | |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 263 | bool runOnModule(Module &M) override { |
| 264 | auto LookupBFI = [this](Function &F) { |
| 265 | return &this->getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI(); |
| 266 | }; |
| 267 | auto LookupBPI = [this](Function &F) { |
| 268 | return &this->getAnalysis<BranchProbabilityInfoWrapperPass>(F).getBPI(); |
| 269 | }; |
| 270 | writeAllCFGsToDotFile(M, LookupBFI, LookupBPI, /*isSimple=*/true); |
Chris Lattner | 62aff84 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 271 | return false; |
| 272 | } |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 273 | |
| 274 | void print(raw_ostream &OS, const Module * = nullptr) const override {} |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 275 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 276 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 277 | ModulePass::getAnalysisUsage(AU); |
| 278 | AU.addRequired<BlockFrequencyInfoWrapperPass>(); |
| 279 | AU.addRequired<BranchProbabilityInfoWrapperPass>(); |
Chris Lattner | 62aff84 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 280 | AU.setPreservesAll(); |
| 281 | } |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 62aff84 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 283 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 284 | } |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 285 | |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 286 | char CFGOnlyPrinterLegacyPass::ID = 0; |
| 287 | INITIALIZE_PASS(CFGOnlyPrinterLegacyPass, "dot-cfg-only", |
Owen Anderson | d31d82d | 2010-08-23 17:52:01 +0000 | [diff] [blame] | 288 | "Print CFG of function to 'dot' file (with no function bodies)", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 289 | false, true) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 290 | |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 291 | PreservedAnalyses CFGOnlyPrinterPass::run(Module &M, |
| 292 | ModuleAnalysisManager &AM) { |
| 293 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
| 294 | auto LookupBFI = [&FAM](Function &F) { |
| 295 | return &FAM.getResult<BlockFrequencyAnalysis>(F); |
| 296 | }; |
| 297 | auto LookupBPI = [&FAM](Function &F) { |
| 298 | return &FAM.getResult<BranchProbabilityAnalysis>(F); |
| 299 | }; |
| 300 | writeAllCFGsToDotFile(M, LookupBFI, LookupBPI, /*isSimple=*/true); |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 301 | return PreservedAnalyses::all(); |
| 302 | } |
| 303 | |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 304 | /// viewCFG - This function is meant for use from the debugger. You can just |
| 305 | /// say 'call F->viewCFG()' and a ghostview window should pop up from the |
| 306 | /// program, displaying the CFG of the current function. This depends on there |
| 307 | /// being a 'dot' and 'gv' program in your path. |
| 308 | /// |
| 309 | void Function::viewCFG() const { |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 310 | |
| 311 | CFGDOTInfo CFGInfo(this); |
| 312 | ViewGraph(&CFGInfo, "cfg" + getName()); |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /// viewCFGOnly - This function is meant for use from the debugger. It works |
| 316 | /// just like viewCFG, but it does not include the contents of basic blocks |
Eric Christopher | 650c8f2 | 2014-05-20 17:11:11 +0000 | [diff] [blame] | 317 | /// into the nodes, just the label. If you are only interested in the CFG |
| 318 | /// this can make the graph smaller. |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 319 | /// |
| 320 | void Function::viewCFGOnly() const { |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 321 | |
| 322 | CFGDOTInfo CFGInfo(this); |
| 323 | ViewGraph(&CFGInfo, "cfg" + getName(), true); |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 324 | } |
Brian Gaeke | 104341f | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 325 | |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 326 | ModulePass *llvm::createCFGPrinterLegacyPassPass() { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 327 | return new CFGPrinterLegacyPass(); |
Brian Gaeke | 104341f | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 330 | ModulePass *llvm::createCFGOnlyPrinterLegacyPassPass() { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 331 | return new CFGOnlyPrinterLegacyPass(); |
Brian Gaeke | 104341f | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 332 | } |