CFGPrinter.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
  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 file defines a '-dot-cfg' analysis pass, which emits the
  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. #include "llvm/Analysis/CFGPrinter.h"
  20. #include "llvm/Pass.h"
  21. #include "llvm/Support/FileSystem.h"
  22. using namespace llvm;
  23. namespace {
  24. struct CFGViewer : public FunctionPass {
  25. static char ID; // Pass identifcation, replacement for typeid
  26. CFGViewer() : FunctionPass(ID) {
  27. // initializeCFGOnlyViewerPass(*PassRegistry::getPassRegistry()); // HLSL Change - initialize up front
  28. }
  29. bool runOnFunction(Function &F) override {
  30. // HLSL Change Starts
  31. if (OSOverride != nullptr) {
  32. *OSOverride << "\ngraph: " << "cfg" << F.getName() << ".dot\n";
  33. llvm::WriteGraph(*OSOverride, (const Function*)&F, false, F.getName());
  34. return false;
  35. }
  36. // HLSL Change Ends
  37. F.viewCFG();
  38. return false;
  39. }
  40. void print(raw_ostream &OS, const Module* = nullptr) const override {}
  41. void getAnalysisUsage(AnalysisUsage &AU) const override {
  42. AU.setPreservesAll();
  43. }
  44. };
  45. }
  46. char CFGViewer::ID = 0;
  47. INITIALIZE_PASS(CFGViewer, "view-cfg", "View CFG of function", false, true)
  48. namespace {
  49. struct CFGOnlyViewer : public FunctionPass {
  50. static char ID; // Pass identifcation, replacement for typeid
  51. CFGOnlyViewer() : FunctionPass(ID) {
  52. // initializeCFGOnlyViewerPass(*PassRegistry::getPassRegistry()); // HLSL Change - initialize up front
  53. }
  54. bool runOnFunction(Function &F) override {
  55. // HLSL Change Starts
  56. if (OSOverride != nullptr) {
  57. *OSOverride << "\ngraph: " << "cfg" << F.getName() << ".dot\n";
  58. llvm::WriteGraph(*OSOverride, (const Function*)&F, true, F.getName());
  59. return false;
  60. }
  61. // HLSL Change Ends
  62. F.viewCFGOnly();
  63. return false;
  64. }
  65. void print(raw_ostream &OS, const Module* = nullptr) const override {}
  66. void getAnalysisUsage(AnalysisUsage &AU) const override {
  67. AU.setPreservesAll();
  68. }
  69. };
  70. }
  71. char CFGOnlyViewer::ID = 0;
  72. INITIALIZE_PASS(CFGOnlyViewer, "view-cfg-only",
  73. "View CFG of function (with no function bodies)", false, true)
  74. namespace {
  75. struct CFGPrinter : public FunctionPass {
  76. static char ID; // Pass identification, replacement for typeid
  77. CFGPrinter() : FunctionPass(ID) {
  78. // initializeCFGPrinterPass(*PassRegistry::getPassRegistry()); // HLSL Change - initialize up front
  79. }
  80. bool runOnFunction(Function &F) override {
  81. // HLSL Change Starts
  82. if (OSOverride != nullptr) {
  83. *OSOverride << "\ngraph: " << "cfg." << F.getName() << ".dot\n";
  84. llvm::WriteGraph(*OSOverride, (const Function*)&F, false, F.getName());
  85. return false;
  86. }
  87. // HLSL Change Ends
  88. std::string Filename = ("cfg." + F.getName() + ".dot").str();
  89. errs() << "Writing '" << Filename << "'...";
  90. std::error_code EC;
  91. raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
  92. if (!EC)
  93. WriteGraph(File, (const Function*)&F);
  94. else
  95. errs() << " error opening file for writing!";
  96. errs() << "\n";
  97. return false;
  98. }
  99. void print(raw_ostream &OS, const Module* = nullptr) const override {}
  100. void getAnalysisUsage(AnalysisUsage &AU) const override {
  101. AU.setPreservesAll();
  102. }
  103. };
  104. }
  105. char CFGPrinter::ID = 0;
  106. INITIALIZE_PASS(CFGPrinter, "dot-cfg", "Print CFG of function to 'dot' file",
  107. false, true)
  108. namespace {
  109. struct CFGOnlyPrinter : public FunctionPass {
  110. static char ID; // Pass identification, replacement for typeid
  111. CFGOnlyPrinter() : FunctionPass(ID) {
  112. // initializeCFGOnlyPrinterPass(*PassRegistry::getPassRegistry()); // HLSL Change - initialize up front
  113. }
  114. bool runOnFunction(Function &F) override {
  115. // HLSL Change Starts
  116. if (OSOverride != nullptr) {
  117. *OSOverride << "\ngraph: " << "cfg." << F.getName() << ".dot\n";
  118. llvm::WriteGraph(*OSOverride, (const Function*)&F, true, F.getName());
  119. return false;
  120. }
  121. // HLSL Change Ends
  122. std::string Filename = ("cfg." + F.getName() + ".dot").str();
  123. errs() << "Writing '" << Filename << "'...";
  124. std::error_code EC;
  125. raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
  126. if (!EC)
  127. WriteGraph(File, (const Function*)&F, true);
  128. else
  129. errs() << " error opening file for writing!";
  130. errs() << "\n";
  131. return false;
  132. }
  133. void print(raw_ostream &OS, const Module* = nullptr) const override {}
  134. void getAnalysisUsage(AnalysisUsage &AU) const override {
  135. AU.setPreservesAll();
  136. }
  137. };
  138. }
  139. char CFGOnlyPrinter::ID = 0;
  140. INITIALIZE_PASS(CFGOnlyPrinter, "dot-cfg-only",
  141. "Print CFG of function to 'dot' file (with no function bodies)",
  142. false, true)
  143. /// viewCFG - This function is meant for use from the debugger. You can just
  144. /// say 'call F->viewCFG()' and a ghostview window should pop up from the
  145. /// program, displaying the CFG of the current function. This depends on there
  146. /// being a 'dot' and 'gv' program in your path.
  147. ///
  148. void Function::viewCFG() const {
  149. ViewGraph(this, "cfg" + getName());
  150. }
  151. /// viewCFGOnly - This function is meant for use from the debugger. It works
  152. /// just like viewCFG, but it does not include the contents of basic blocks
  153. /// into the nodes, just the label. If you are only interested in the CFG
  154. /// this can make the graph smaller.
  155. ///
  156. void Function::viewCFGOnly() const {
  157. ViewGraph(this, "cfg" + getName(), true);
  158. }
  159. FunctionPass *llvm::createCFGPrinterPass () {
  160. return new CFGPrinter();
  161. }
  162. FunctionPass *llvm::createCFGOnlyPrinterPass () {
  163. return new CFGOnlyPrinter();
  164. }
  165. // HLSL Change Starts
  166. void llvm::initializeCFGPrinterPasses(PassRegistry &Registry) {
  167. initializeCFGPrinterPass(Registry);
  168. initializeCFGOnlyPrinterPass(Registry);
  169. initializeCFGViewerPass(Registry);
  170. initializeCFGOnlyViewerPass(Registry);
  171. }
  172. // HLSL Change Ends