MC CFG: Add more MCFunction container methods (find, empty).
[oota-llvm.git] / include / llvm / MC / MCFunction.h
1 //===-- llvm/MC/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_MCFUNCTION_H
16 #define LLVM_MC_MCFUNCTION_H
17
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/MC/MCInst.h"
20 #include <string>
21 #include <vector>
22
23 namespace llvm {
24
25 class MCFunction;
26 class MCModule;
27 class MCTextAtom;
28
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.
33 class MCBasicBlock {
34   const MCTextAtom *Insts;
35
36   // MCFunction owns the basic block.
37   MCFunction *Parent;
38   friend class MCFunction;
39   MCBasicBlock(const MCTextAtom &Insts, MCFunction *Parent);
40
41   /// \name Predecessors/Successors, to represent the CFG.
42   /// @{
43   typedef std::vector<const MCBasicBlock *> BasicBlockListTy;
44   BasicBlockListTy Successors;
45   BasicBlockListTy Predecessors;
46   /// @}
47 public:
48
49   /// \brief Get the backing MCTextAtom, containing the instruction sequence.
50   const MCTextAtom *getInsts() const { return Insts; }
51
52   /// \name Get the owning MCFunction.
53   /// @{
54   const MCFunction *getParent() const { return Parent; }
55         MCFunction *getParent()       { return Parent; }
56   /// @}
57
58   /// MC CFG access: Predecessors/Successors.
59   /// @{
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(); }
63
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(); }
67
68   void addSuccessor(const MCBasicBlock *MCBB);
69   bool isSuccessor(const MCBasicBlock *MCBB) const;
70
71   void addPredecessor(const MCBasicBlock *MCBB);
72   bool isPredecessor(const MCBasicBlock *MCBB) const;
73   /// @}
74 };
75
76 /// \brief Represents a function in machine code, containing MCBasicBlocks.
77 /// MCFunctions are created by MCModule.
78 class MCFunction {
79   MCFunction           (const MCFunction&) LLVM_DELETED_FUNCTION;
80   MCFunction& operator=(const MCFunction&) LLVM_DELETED_FUNCTION;
81
82   std::string Name;
83   MCModule *ParentModule;
84   typedef std::vector<MCBasicBlock*> BasicBlockListTy;
85   BasicBlockListTy Blocks;
86
87   // MCModule owns the function.
88   friend class MCModule;
89   MCFunction(StringRef Name, MCModule *Parent);
90   ~MCFunction();
91
92 public:
93   /// \brief Create an MCBasicBlock backed by Insts and add it to this function.
94   /// \param Insts Sequence of straight-line code backing the basic block.
95   /// \returns The newly created basic block.
96   MCBasicBlock &createBlock(const MCTextAtom &Insts);
97
98   StringRef getName() const { return Name; }
99
100   /// \name Access to the function's basic blocks. No ordering is enforced,
101   /// except that the first block is the entry block.
102   /// @{
103   /// \brief Get the entry point basic block.
104   const MCBasicBlock *getEntryBlock() const { return front(); }
105         MCBasicBlock *getEntryBlock()       { return front(); }
106
107   bool empty() const { return Blocks.empty(); }
108
109   typedef BasicBlockListTy::const_iterator const_iterator;
110   typedef BasicBlockListTy::      iterator       iterator;
111   const_iterator begin() const { return Blocks.begin(); }
112         iterator begin()       { return Blocks.begin(); }
113   const_iterator   end() const { return Blocks.end(); }
114         iterator   end()       { return Blocks.end(); }
115
116   const MCBasicBlock* front() const { return Blocks.front(); }
117         MCBasicBlock* front()       { return Blocks.front(); }
118   const MCBasicBlock*  back() const { return Blocks.back(); }
119         MCBasicBlock*  back()       { return Blocks.back(); }
120
121   /// \brief Find the basic block, if any, that starts at \p StartAddr.
122   const MCBasicBlock *find(uint64_t StartAddr) const;
123         MCBasicBlock *find(uint64_t StartAddr);
124   /// @}
125 };
126
127 }
128
129 #endif