LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MCFunction.cpp
Go to the documentation of this file.
1 //===-- lib/MC/MCFunction.cpp -----------------------------------*- 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 #include "llvm/MC/MCFunction.h"
11 #include "llvm/MC/MCAtom.h"
12 #include "llvm/MC/MCModule.h"
13 #include <algorithm>
14 
15 using namespace llvm;
16 
17 // MCFunction
18 
19 MCFunction::MCFunction(StringRef Name, MCModule *Parent)
20  : Name(Name), ParentModule(Parent)
21 {}
22 
23 MCFunction::~MCFunction() {
24  for (iterator I = begin(), E = end(); I != E; ++I)
25  delete *I;
26 }
27 
29  MCBasicBlock *MCBB = new MCBasicBlock(TA, this);
30  Blocks.push_back(MCBB);
31  return *MCBB;
32 }
33 
34 MCBasicBlock *MCFunction::find(uint64_t StartAddr) {
35  for (const_iterator I = begin(), E = end(); I != E; ++I)
36  if ((*I)->getInsts()->getBeginAddr() == StartAddr)
37  return *I;
38  return 0;
39 }
40 
41 const MCBasicBlock *MCFunction::find(uint64_t StartAddr) const {
42  return const_cast<MCFunction *>(this)->find(StartAddr);
43 }
44 
45 // MCBasicBlock
46 
47 MCBasicBlock::MCBasicBlock(const MCTextAtom &Insts, MCFunction *Parent)
48  : Insts(&Insts), Parent(Parent) {
49  getParent()->getParent()->trackBBForAtom(&Insts, this);
50 }
51 
53  if (!isSuccessor(MCBB))
54  Successors.push_back(MCBB);
55 }
56 
57 bool MCBasicBlock::isSuccessor(const MCBasicBlock *MCBB) const {
58  return std::find(Successors.begin(), Successors.end(),
59  MCBB) != Successors.end();
60 }
61 
63  if (!isPredecessor(MCBB))
64  Predecessors.push_back(MCBB);
65 }
66 
67 bool MCBasicBlock::isPredecessor(const MCBasicBlock *MCBB) const {
68  return std::find(Predecessors.begin(), Predecessors.end(),
69  MCBB) != Predecessors.end();
70 }
71 
73  assert(Insts->getEndAddr() + 1 == SplitBB->Insts->getBeginAddr() &&
74  "Splitting unrelated basic blocks!");
75  SplitBB->addPredecessor(this);
76  assert(SplitBB->Successors.empty() &&
77  "Split basic block shouldn't already have successors!");
78  SplitBB->Successors = Successors;
79  Successors.clear();
80  addSuccessor(SplitBB);
81 }
bool isPredecessor(const MCBasicBlock *MCBB) const
Definition: MCFunction.cpp:67
MCBasicBlock & createBlock(const MCTextAtom &Insts)
Create an MCBasicBlock backed by Insts and add it to this function.
Definition: MCFunction.cpp:28
An atom consisting of disassembled instructions.
Definition: MCAtom.h:123
const_iterator begin() const
Definition: MCFunction.h:124
void addPredecessor(const MCBasicBlock *MCBB)
Definition: MCFunction.cpp:62
void addSuccessor(const MCBasicBlock *MCBB)
Definition: MCFunction.cpp:52
void splitBasicBlock(MCBasicBlock *SplitBB)
Split block, mirrorring NewAtom = Insts->split(..). This moves all successors to SplitBB, and adds a fallthrough to it. SplitBB The result of splitting Insts, a basic block directly following this basic block.
Definition: MCFunction.cpp:72
Represents a function in machine code, containing MCBasicBlocks. MCFunctions are created by MCModule...
Definition: MCFunction.h:85
uint64_t getBeginAddr() const
Get the start address of the atom.
Definition: MCAtom.h:43
const MCModule * getParent() const
Definition: MCFunction.h:109
bool isSuccessor(const MCBasicBlock *MCBB) const
Definition: MCFunction.cpp:57
Basic block containing a sequence of disassembled instructions. The basic block is backed by an MCTex...
Definition: MCFunction.h:33
const_iterator end() const
Definition: MCFunction.h:126
#define I(x, y, z)
Definition: MD5.cpp:54
uint64_t getEndAddr() const
Get the end address, i.e. the last one inside the atom.
Definition: MCAtom.h:45
A completely disassembled object file or executable. It comprises a list of MCAtom's, each representing a contiguous range of either instructions or data. An MCModule is created using MCObjectDisassembler::buildModule.
Definition: MCModule.h:36
BasicBlockListTy::iterator iterator
Definition: MCFunction.h:123
const MCBasicBlock * find(uint64_t StartAddr) const
Find the basic block, if any, that starts at StartAddr.
Definition: MCFunction.cpp:41
BasicBlockListTy::const_iterator const_iterator
Definition: MCFunction.h:122