1 //===-- llvm/MC/MCFunction.h ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the data structures to hold a CFG reconstructed from
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_MC_MCFUNCTION_H
16 #define LLVM_MC_MCFUNCTION_H
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/MC/MCInst.h"
29 /// \brief Basic block containing a sequence of disassembled instructions.
30 /// The basic block is backed by an MCTextAtom, which holds the instructions,
31 /// and the address range it covers.
32 /// Create a basic block using MCFunction::createBlock.
34 const MCTextAtom *Insts;
36 // MCFunction owns the basic block.
38 friend class MCFunction;
39 MCBasicBlock(const MCTextAtom &Insts, MCFunction *Parent);
41 /// \name Predecessors/Successors, to represent the CFG.
43 typedef std::vector<const MCBasicBlock *> BasicBlockListTy;
44 BasicBlockListTy Successors;
45 BasicBlockListTy Predecessors;
49 /// \brief Get the backing MCTextAtom, containing the instruction sequence.
50 const MCTextAtom *getInsts() const { return Insts; }
52 /// \name Get the owning MCFunction.
54 const MCFunction *getParent() const { return Parent; }
55 MCFunction *getParent() { return Parent; }
58 /// MC CFG access: Predecessors/Successors.
60 typedef BasicBlockListTy::const_iterator succ_const_iterator;
61 succ_const_iterator succ_begin() const { return Successors.begin(); }
62 succ_const_iterator succ_end() const { return Successors.end(); }
64 typedef BasicBlockListTy::const_iterator pred_const_iterator;
65 pred_const_iterator pred_begin() const { return Predecessors.begin(); }
66 pred_const_iterator pred_end() const { return Predecessors.end(); }
68 void addSuccessor(const MCBasicBlock *MCBB);
69 bool isSuccessor(const MCBasicBlock *MCBB) const;
71 void addPredecessor(const MCBasicBlock *MCBB);
72 bool isPredecessor(const MCBasicBlock *MCBB) const;
74 /// \brief Split block, mirrorring NewAtom = Insts->split(..).
75 /// This moves all successors to \p SplitBB, and
76 /// adds a fallthrough to it.
77 /// \p SplitBB The result of splitting Insts, a basic block directly following
79 /// \returns A new basic block, backed by \p SplitBB.
80 void splitBasicBlock(MCBasicBlock *SplitBB);
84 /// \brief Represents a function in machine code, containing MCBasicBlocks.
85 /// MCFunctions are created by MCModule.
87 MCFunction (const MCFunction&) LLVM_DELETED_FUNCTION;
88 MCFunction& operator=(const MCFunction&) LLVM_DELETED_FUNCTION;
91 MCModule *ParentModule;
92 typedef std::vector<MCBasicBlock*> BasicBlockListTy;
93 BasicBlockListTy Blocks;
95 // MCModule owns the function.
96 friend class MCModule;
97 MCFunction(StringRef Name, MCModule *Parent);
101 /// \brief Create an MCBasicBlock backed by Insts and add it to this function.
102 /// \param Insts Sequence of straight-line code backing the basic block.
103 /// \returns The newly created basic block.
104 MCBasicBlock &createBlock(const MCTextAtom &Insts);
106 StringRef getName() const { return Name; }
108 /// \name Get the owning MC Module.
110 const MCModule *getParent() const { return ParentModule; }
111 MCModule *getParent() { return ParentModule; }
114 /// \name Access to the function's basic blocks. No ordering is enforced,
115 /// except that the first block is the entry block.
117 /// \brief Get the entry point basic block.
118 const MCBasicBlock *getEntryBlock() const { return front(); }
119 MCBasicBlock *getEntryBlock() { return front(); }
121 bool empty() const { return Blocks.empty(); }
123 typedef BasicBlockListTy::const_iterator const_iterator;
124 typedef BasicBlockListTy:: iterator iterator;
125 const_iterator begin() const { return Blocks.begin(); }
126 iterator begin() { return Blocks.begin(); }
127 const_iterator end() const { return Blocks.end(); }
128 iterator end() { return Blocks.end(); }
130 const MCBasicBlock* front() const { return Blocks.front(); }
131 MCBasicBlock* front() { return Blocks.front(); }
132 const MCBasicBlock* back() const { return Blocks.back(); }
133 MCBasicBlock* back() { return Blocks.back(); }
135 /// \brief Find the basic block, if any, that starts at \p StartAddr.
136 const MCBasicBlock *find(uint64_t StartAddr) const;
137 MCBasicBlock *find(uint64_t StartAddr);