1 //===-- MCFunction.h --------------------------------------------*- C++ -*-===// 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 the data structures to hold a CFG reconstructed from 11 // machine code. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_MC_MCANALYSIS_MCFUNCTION_H 16 #define LLVM_MC_MCANALYSIS_MCFUNCTION_H 17 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/MC/MCInst.h" 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 namespace llvm { 25 26 class MCFunction; 27 class MCModule; 28 class MCTextAtom; 29 30 /// \brief Basic block containing a sequence of disassembled instructions. 31 /// The basic block is backed by an MCTextAtom, which holds the instructions, 32 /// and the address range it covers. 33 /// Create a basic block using MCFunction::createBlock. 34 class MCBasicBlock { 35 const MCTextAtom *Insts; 36 37 // MCFunction owns the basic block. 38 MCFunction *Parent; 39 friend class MCFunction; 40 MCBasicBlock(const MCTextAtom &Insts, MCFunction *Parent); 41 42 /// \name Predecessors/Successors, to represent the CFG. 43 /// @{ 44 typedef std::vector<const MCBasicBlock *> BasicBlockListTy; 45 BasicBlockListTy Successors; 46 BasicBlockListTy Predecessors; 47 /// @} 48 public: 49 50 /// \brief Get the backing MCTextAtom, containing the instruction sequence. getInsts()51 const MCTextAtom *getInsts() const { return Insts; } 52 53 /// \name Get the owning MCFunction. 54 /// @{ getParent()55 const MCFunction *getParent() const { return Parent; } getParent()56 MCFunction *getParent() { return Parent; } 57 /// @} 58 59 /// MC CFG access: Predecessors/Successors. 60 /// @{ 61 typedef BasicBlockListTy::const_iterator succ_const_iterator; succ_begin()62 succ_const_iterator succ_begin() const { return Successors.begin(); } succ_end()63 succ_const_iterator succ_end() const { return Successors.end(); } 64 65 typedef BasicBlockListTy::const_iterator pred_const_iterator; pred_begin()66 pred_const_iterator pred_begin() const { return Predecessors.begin(); } pred_end()67 pred_const_iterator pred_end() const { return Predecessors.end(); } 68 69 void addSuccessor(const MCBasicBlock *MCBB); 70 bool isSuccessor(const MCBasicBlock *MCBB) const; 71 72 void addPredecessor(const MCBasicBlock *MCBB); 73 bool isPredecessor(const MCBasicBlock *MCBB) const; 74 75 /// \brief Split block, mirrorring NewAtom = Insts->split(..). 76 /// This moves all successors to \p SplitBB, and 77 /// adds a fallthrough to it. 78 /// \p SplitBB The result of splitting Insts, a basic block directly following 79 /// this basic block. 80 void splitBasicBlock(MCBasicBlock *SplitBB); 81 /// @} 82 }; 83 84 /// \brief Represents a function in machine code, containing MCBasicBlocks. 85 /// MCFunctions are created by MCModule. 86 class MCFunction { 87 MCFunction (const MCFunction&) LLVM_DELETED_FUNCTION; 88 MCFunction& operator=(const MCFunction&) LLVM_DELETED_FUNCTION; 89 90 std::string Name; 91 MCModule *ParentModule; 92 typedef std::vector<std::unique_ptr<MCBasicBlock>> BasicBlockListTy; 93 BasicBlockListTy Blocks; 94 95 // MCModule owns the function. 96 friend class MCModule; 97 MCFunction(StringRef Name, MCModule *Parent); 98 99 public: 100 /// \brief Create an MCBasicBlock backed by Insts and add it to this function. 101 /// \param Insts Sequence of straight-line code backing the basic block. 102 /// \returns The newly created basic block. 103 MCBasicBlock &createBlock(const MCTextAtom &Insts); 104 getName()105 StringRef getName() const { return Name; } 106 107 /// \name Get the owning MC Module. 108 /// @{ getParent()109 const MCModule *getParent() const { return ParentModule; } getParent()110 MCModule *getParent() { return ParentModule; } 111 /// @} 112 113 /// \name Access to the function's basic blocks. No ordering is enforced, 114 /// except that the first block is the entry block. 115 /// @{ 116 /// \brief Get the entry point basic block. getEntryBlock()117 const MCBasicBlock *getEntryBlock() const { return front(); } getEntryBlock()118 MCBasicBlock *getEntryBlock() { return front(); } 119 empty()120 bool empty() const { return Blocks.empty(); } 121 122 typedef BasicBlockListTy::const_iterator const_iterator; 123 typedef BasicBlockListTy:: iterator iterator; begin()124 const_iterator begin() const { return Blocks.begin(); } begin()125 iterator begin() { return Blocks.begin(); } end()126 const_iterator end() const { return Blocks.end(); } end()127 iterator end() { return Blocks.end(); } 128 front()129 const MCBasicBlock* front() const { return Blocks.front().get(); } front()130 MCBasicBlock* front() { return Blocks.front().get(); } back()131 const MCBasicBlock* back() const { return Blocks.back().get(); } back()132 MCBasicBlock* back() { return Blocks.back().get(); } 133 134 /// \brief Find the basic block, if any, that starts at \p StartAddr. 135 const MCBasicBlock *find(uint64_t StartAddr) const; 136 MCBasicBlock *find(uint64_t StartAddr); 137 /// @} 138 }; 139 140 } 141 142 #endif 143