MachineCFGPrinter.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- MachineCFGPrinter.h -------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Analysis/CFGPrinter.h"
  16. #include "llvm/CodeGen/MachineBasicBlock.h"
  17. #include "llvm/CodeGen/MachineFunction.h"
  18. #include "llvm/CodeGen/MachineInstr.h"
  19. #include "llvm/Support/DOTGraphTraits.h"
  20. namespace llvm {
  21. template <class GraphType> struct GraphTraits;
  22. class DOTMachineFuncInfo {
  23. private:
  24. const MachineFunction *F;
  25. public:
  26. DOTMachineFuncInfo(const MachineFunction *F) : F(F) {}
  27. const MachineFunction *getFunction() const { return this->F; }
  28. };
  29. template <>
  30. struct GraphTraits<DOTMachineFuncInfo *>
  31. : public GraphTraits<const MachineBasicBlock *> {
  32. static NodeRef getEntryNode(DOTMachineFuncInfo *CFGInfo) {
  33. return &(CFGInfo->getFunction()->front());
  34. }
  35. // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  36. using nodes_iterator = pointer_iterator<MachineFunction::const_iterator>;
  37. static nodes_iterator nodes_begin(DOTMachineFuncInfo *CFGInfo) {
  38. return nodes_iterator(CFGInfo->getFunction()->begin());
  39. }
  40. static nodes_iterator nodes_end(DOTMachineFuncInfo *CFGInfo) {
  41. return nodes_iterator(CFGInfo->getFunction()->end());
  42. }
  43. static size_t size(DOTMachineFuncInfo *CFGInfo) {
  44. return CFGInfo->getFunction()->size();
  45. }
  46. };
  47. template <>
  48. struct DOTGraphTraits<DOTMachineFuncInfo *> : public DefaultDOTGraphTraits {
  49. DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
  50. static void eraseComment(std::string &OutStr, unsigned &I, unsigned Idx) {
  51. OutStr.erase(OutStr.begin() + I, OutStr.begin() + Idx);
  52. --I;
  53. }
  54. static std::string getSimpleNodeLabel(const MachineBasicBlock *Node,
  55. DOTMachineFuncInfo *) {
  56. return SimpleNodeLabelString(Node);
  57. }
  58. static std::string getCompleteNodeLabel(
  59. const MachineBasicBlock *Node, DOTMachineFuncInfo *,
  60. function_ref<void(raw_string_ostream &, const MachineBasicBlock &)>
  61. HandleBasicBlock =
  62. [](raw_string_ostream &OS,
  63. const MachineBasicBlock &Node) -> void { OS << Node; },
  64. function_ref<void(std::string &, unsigned &, unsigned)>
  65. HandleComment = eraseComment) {
  66. return CompleteNodeLabelString(Node, HandleBasicBlock, HandleComment);
  67. }
  68. std::string getNodeLabel(const MachineBasicBlock *Node,
  69. DOTMachineFuncInfo *CFGInfo) {
  70. if (isSimple())
  71. return getSimpleNodeLabel(Node, CFGInfo);
  72. return getCompleteNodeLabel(Node, CFGInfo);
  73. }
  74. static std::string getGraphName(DOTMachineFuncInfo *CFGInfo) {
  75. return "Machine CFG for '" + CFGInfo->getFunction()->getName().str() +
  76. "' function";
  77. }
  78. };
  79. } // namespace llvm
  80. #ifdef __GNUC__
  81. #pragma GCC diagnostic pop
  82. #endif