Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 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" |
Ahmed Bougacha | 7dac32d | 2013-08-21 07:27:55 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCModule.h" |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
| 15 | using namespace llvm; |
| 16 | |
| 17 | // MCFunction |
| 18 | |
Ahmed Bougacha | 7dac32d | 2013-08-21 07:27:55 +0000 | [diff] [blame] | 19 | MCFunction::MCFunction(StringRef Name, MCModule *Parent) |
| 20 | : Name(Name), ParentModule(Parent) |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 21 | {} |
| 22 | |
| 23 | MCFunction::~MCFunction() { |
| 24 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 25 | delete *I; |
| 26 | } |
| 27 | |
| 28 | MCBasicBlock &MCFunction::createBlock(const MCTextAtom &TA) { |
Ahmed Bougacha | 46d353f | 2013-08-21 07:28:17 +0000 | [diff] [blame] | 29 | MCBasicBlock *MCBB = new MCBasicBlock(TA, this); |
| 30 | Blocks.push_back(MCBB); |
| 31 | return *MCBB; |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Ahmed Bougacha | f9e2348 | 2013-08-21 07:27:59 +0000 | [diff] [blame] | 34 | const MCBasicBlock *MCFunction::find(uint64_t StartAddr) const { |
| 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 | MCBasicBlock *MCFunction::find(uint64_t StartAddr) { |
| 42 | return const_cast<MCBasicBlock *>( |
| 43 | const_cast<const MCFunction *>(this)->find(StartAddr)); |
| 44 | } |
| 45 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 46 | // MCBasicBlock |
| 47 | |
| 48 | MCBasicBlock::MCBasicBlock(const MCTextAtom &Insts, MCFunction *Parent) |
Ahmed Bougacha | aeb2bbc | 2013-08-21 07:28:24 +0000 | [diff] [blame^] | 49 | : Insts(&Insts), Parent(Parent) { |
| 50 | getParent()->getParent()->trackBBForAtom(&Insts, this); |
| 51 | } |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 52 | |
| 53 | void MCBasicBlock::addSuccessor(const MCBasicBlock *MCBB) { |
Ahmed Bougacha | dca54ea | 2013-08-21 07:27:50 +0000 | [diff] [blame] | 54 | if (!isSuccessor(MCBB)) |
| 55 | Successors.push_back(MCBB); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | bool MCBasicBlock::isSuccessor(const MCBasicBlock *MCBB) const { |
| 59 | return std::find(Successors.begin(), Successors.end(), |
| 60 | MCBB) != Successors.end(); |
| 61 | } |
| 62 | |
| 63 | void MCBasicBlock::addPredecessor(const MCBasicBlock *MCBB) { |
Ahmed Bougacha | dca54ea | 2013-08-21 07:27:50 +0000 | [diff] [blame] | 64 | if (!isPredecessor(MCBB)) |
| 65 | Predecessors.push_back(MCBB); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | bool MCBasicBlock::isPredecessor(const MCBasicBlock *MCBB) const { |
| 69 | return std::find(Predecessors.begin(), Predecessors.end(), |
| 70 | MCBB) != Predecessors.end(); |
| 71 | } |
Ahmed Bougacha | aeb2bbc | 2013-08-21 07:28:24 +0000 | [diff] [blame^] | 72 | |
| 73 | void MCBasicBlock::splitBasicBlock(MCBasicBlock *SplitBB) { |
| 74 | assert(Insts->getEndAddr() + 1 == SplitBB->Insts->getBeginAddr() && |
| 75 | "Splitting unrelated basic blocks!"); |
| 76 | SplitBB->addPredecessor(this); |
| 77 | assert(SplitBB->Successors.empty() && |
| 78 | "Split basic block shouldn't already have successors!"); |
| 79 | SplitBB->Successors = Successors; |
| 80 | Successors.clear(); |
| 81 | addSuccessor(SplitBB); |
| 82 | } |