blob: 5011d5fd6b8ef022edbb01e7dc9451fe8b8567fa [file] [log] [blame]
Ahmed Bougachaef993562013-05-24 01:07:04 +00001//===-- 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"
Ahmed Bougacha7dac32d2013-08-21 07:27:55 +000012#include "llvm/MC/MCModule.h"
Ahmed Bougachaef993562013-05-24 01:07:04 +000013#include <algorithm>
14
15using namespace llvm;
16
17// MCFunction
18
Ahmed Bougacha7dac32d2013-08-21 07:27:55 +000019MCFunction::MCFunction(StringRef Name, MCModule *Parent)
20 : Name(Name), ParentModule(Parent)
Ahmed Bougachaef993562013-05-24 01:07:04 +000021{}
22
23MCFunction::~MCFunction() {
24 for (iterator I = begin(), E = end(); I != E; ++I)
25 delete *I;
26}
27
28MCBasicBlock &MCFunction::createBlock(const MCTextAtom &TA) {
29 Blocks.push_back(new MCBasicBlock(TA, this));
30 return *Blocks.back();
31}
32
Ahmed Bougachaf9e23482013-08-21 07:27:59 +000033const MCBasicBlock *MCFunction::find(uint64_t StartAddr) const {
34 for (const_iterator I = begin(), E = end(); I != E; ++I)
35 if ((*I)->getInsts()->getBeginAddr() == StartAddr)
36 return (*I);
37 return 0;
38}
39
40MCBasicBlock *MCFunction::find(uint64_t StartAddr) {
41 return const_cast<MCBasicBlock *>(
42 const_cast<const MCFunction *>(this)->find(StartAddr));
43}
44
Ahmed Bougachaef993562013-05-24 01:07:04 +000045// MCBasicBlock
46
47MCBasicBlock::MCBasicBlock(const MCTextAtom &Insts, MCFunction *Parent)
48 : Insts(&Insts), Parent(Parent)
49{}
50
51void MCBasicBlock::addSuccessor(const MCBasicBlock *MCBB) {
Ahmed Bougachadca54ea2013-08-21 07:27:50 +000052 if (!isSuccessor(MCBB))
53 Successors.push_back(MCBB);
Ahmed Bougachaef993562013-05-24 01:07:04 +000054}
55
56bool MCBasicBlock::isSuccessor(const MCBasicBlock *MCBB) const {
57 return std::find(Successors.begin(), Successors.end(),
58 MCBB) != Successors.end();
59}
60
61void MCBasicBlock::addPredecessor(const MCBasicBlock *MCBB) {
Ahmed Bougachadca54ea2013-08-21 07:27:50 +000062 if (!isPredecessor(MCBB))
63 Predecessors.push_back(MCBB);
Ahmed Bougachaef993562013-05-24 01:07:04 +000064}
65
66bool MCBasicBlock::isPredecessor(const MCBasicBlock *MCBB) const {
67 return std::find(Predecessors.begin(), Predecessors.end(),
68 MCBB) != Predecessors.end();
69}