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