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 | |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 10 | #include "llvm/MC/MCAnalysis/MCFunction.h" |
| 11 | #include "llvm/MC/MCAnalysis/MCAtom.h" |
| 12 | #include "llvm/MC/MCAnalysis/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 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 23 | MCBasicBlock &MCFunction::createBlock(const MCTextAtom &TA) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 24 | std::unique_ptr<MCBasicBlock> MCBB(new MCBasicBlock(TA, this)); |
| 25 | Blocks.push_back(std::move(MCBB)); |
| 26 | return *Blocks.back(); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Ahmed Bougacha | 2172767 | 2013-08-21 19:40:25 +0000 | [diff] [blame] | 29 | MCBasicBlock *MCFunction::find(uint64_t StartAddr) { |
Ahmed Bougacha | f9e2348 | 2013-08-21 07:27:59 +0000 | [diff] [blame] | 30 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
| 31 | if ((*I)->getInsts()->getBeginAddr() == StartAddr) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 32 | return I->get(); |
| 33 | return nullptr; |
Ahmed Bougacha | f9e2348 | 2013-08-21 07:27:59 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Ahmed Bougacha | 2172767 | 2013-08-21 19:40:25 +0000 | [diff] [blame] | 36 | const MCBasicBlock *MCFunction::find(uint64_t StartAddr) const { |
| 37 | return const_cast<MCFunction *>(this)->find(StartAddr); |
Ahmed Bougacha | f9e2348 | 2013-08-21 07:27:59 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 40 | // MCBasicBlock |
| 41 | |
| 42 | MCBasicBlock::MCBasicBlock(const MCTextAtom &Insts, MCFunction *Parent) |
Ahmed Bougacha | aeb2bbc | 2013-08-21 07:28:24 +0000 | [diff] [blame] | 43 | : Insts(&Insts), Parent(Parent) { |
| 44 | getParent()->getParent()->trackBBForAtom(&Insts, this); |
| 45 | } |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 46 | |
| 47 | void MCBasicBlock::addSuccessor(const MCBasicBlock *MCBB) { |
Ahmed Bougacha | dca54ea | 2013-08-21 07:27:50 +0000 | [diff] [blame] | 48 | if (!isSuccessor(MCBB)) |
| 49 | Successors.push_back(MCBB); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | bool MCBasicBlock::isSuccessor(const MCBasicBlock *MCBB) const { |
| 53 | return std::find(Successors.begin(), Successors.end(), |
| 54 | MCBB) != Successors.end(); |
| 55 | } |
| 56 | |
| 57 | void MCBasicBlock::addPredecessor(const MCBasicBlock *MCBB) { |
Ahmed Bougacha | dca54ea | 2013-08-21 07:27:50 +0000 | [diff] [blame] | 58 | if (!isPredecessor(MCBB)) |
| 59 | Predecessors.push_back(MCBB); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | bool MCBasicBlock::isPredecessor(const MCBasicBlock *MCBB) const { |
| 63 | return std::find(Predecessors.begin(), Predecessors.end(), |
| 64 | MCBB) != Predecessors.end(); |
| 65 | } |
Ahmed Bougacha | aeb2bbc | 2013-08-21 07:28:24 +0000 | [diff] [blame] | 66 | |
| 67 | void MCBasicBlock::splitBasicBlock(MCBasicBlock *SplitBB) { |
| 68 | assert(Insts->getEndAddr() + 1 == SplitBB->Insts->getBeginAddr() && |
| 69 | "Splitting unrelated basic blocks!"); |
| 70 | SplitBB->addPredecessor(this); |
| 71 | assert(SplitBB->Successors.empty() && |
| 72 | "Split basic block shouldn't already have successors!"); |
| 73 | SplitBB->Successors = Successors; |
| 74 | Successors.clear(); |
| 75 | addSuccessor(SplitBB); |
| 76 | } |