File indexing completed on 2026-05-10 08:43:30
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include "llvm/Analysis/CFGPrinter.h"
0012 #include "llvm/CodeGen/MachineBasicBlock.h"
0013 #include "llvm/CodeGen/MachineFunction.h"
0014 #include "llvm/CodeGen/MachineInstr.h"
0015 #include "llvm/Support/DOTGraphTraits.h"
0016
0017 namespace llvm {
0018
0019 template <class GraphType> struct GraphTraits;
0020 class DOTMachineFuncInfo {
0021 private:
0022 const MachineFunction *F;
0023
0024 public:
0025 DOTMachineFuncInfo(const MachineFunction *F) : F(F) {}
0026
0027 const MachineFunction *getFunction() const { return this->F; }
0028 };
0029
0030 template <>
0031 struct GraphTraits<DOTMachineFuncInfo *>
0032 : public GraphTraits<const MachineBasicBlock *> {
0033 static NodeRef getEntryNode(DOTMachineFuncInfo *CFGInfo) {
0034 return &(CFGInfo->getFunction()->front());
0035 }
0036
0037
0038 using nodes_iterator = pointer_iterator<MachineFunction::const_iterator>;
0039
0040 static nodes_iterator nodes_begin(DOTMachineFuncInfo *CFGInfo) {
0041 return nodes_iterator(CFGInfo->getFunction()->begin());
0042 }
0043
0044 static nodes_iterator nodes_end(DOTMachineFuncInfo *CFGInfo) {
0045 return nodes_iterator(CFGInfo->getFunction()->end());
0046 }
0047
0048 static size_t size(DOTMachineFuncInfo *CFGInfo) {
0049 return CFGInfo->getFunction()->size();
0050 }
0051 };
0052
0053 template <>
0054 struct DOTGraphTraits<DOTMachineFuncInfo *> : public DefaultDOTGraphTraits {
0055
0056 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
0057
0058 static void eraseComment(std::string &OutStr, unsigned &I, unsigned Idx) {
0059 OutStr.erase(OutStr.begin() + I, OutStr.begin() + Idx);
0060 --I;
0061 }
0062
0063 static std::string getSimpleNodeLabel(const MachineBasicBlock *Node,
0064 DOTMachineFuncInfo *) {
0065 return SimpleNodeLabelString(Node);
0066 }
0067
0068 static std::string getCompleteNodeLabel(
0069 const MachineBasicBlock *Node, DOTMachineFuncInfo *,
0070 function_ref<void(raw_string_ostream &, const MachineBasicBlock &)>
0071 HandleBasicBlock =
0072 [](raw_string_ostream &OS,
0073 const MachineBasicBlock &Node) -> void { OS << Node; },
0074 function_ref<void(std::string &, unsigned &, unsigned)>
0075 HandleComment = eraseComment) {
0076 return CompleteNodeLabelString(Node, HandleBasicBlock, HandleComment);
0077 }
0078
0079 std::string getNodeLabel(const MachineBasicBlock *Node,
0080 DOTMachineFuncInfo *CFGInfo) {
0081 if (isSimple())
0082 return getSimpleNodeLabel(Node, CFGInfo);
0083
0084 return getCompleteNodeLabel(Node, CFGInfo);
0085 }
0086
0087 static std::string getGraphName(DOTMachineFuncInfo *CFGInfo) {
0088 return "Machine CFG for '" + CFGInfo->getFunction()->getName().str() +
0089 "' function";
0090 }
0091 };
0092 }