blob: 4e09d1a52dacc8b2d073f3dc2dc7b7cf4dab0e9b [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
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070010#include "llvm/MC/MCAnalysis/MCFunction.h"
11#include "llvm/MC/MCAnalysis/MCAtom.h"
12#include "llvm/MC/MCAnalysis/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
Ahmed Bougachaef993562013-05-24 01:07:04 +000023MCBasicBlock &MCFunction::createBlock(const MCTextAtom &TA) {
Stephen Hinesdce4a402014-05-29 02:49:00 -070024 std::unique_ptr<MCBasicBlock> MCBB(new MCBasicBlock(TA, this));
25 Blocks.push_back(std::move(MCBB));
26 return *Blocks.back();
Ahmed Bougachaef993562013-05-24 01:07:04 +000027}
28
Ahmed Bougacha21727672013-08-21 19:40:25 +000029MCBasicBlock *MCFunction::find(uint64_t StartAddr) {
Ahmed Bougachaf9e23482013-08-21 07:27:59 +000030 for (const_iterator I = begin(), E = end(); I != E; ++I)
31 if ((*I)->getInsts()->getBeginAddr() == StartAddr)
Stephen Hinesdce4a402014-05-29 02:49:00 -070032 return I->get();
33 return nullptr;
Ahmed Bougachaf9e23482013-08-21 07:27:59 +000034}
35
Ahmed Bougacha21727672013-08-21 19:40:25 +000036const MCBasicBlock *MCFunction::find(uint64_t StartAddr) const {
37 return const_cast<MCFunction *>(this)->find(StartAddr);
Ahmed Bougachaf9e23482013-08-21 07:27:59 +000038}
39
Ahmed Bougachaef993562013-05-24 01:07:04 +000040// MCBasicBlock
41
42MCBasicBlock::MCBasicBlock(const MCTextAtom &Insts, MCFunction *Parent)
Ahmed Bougachaaeb2bbc2013-08-21 07:28:24 +000043 : Insts(&Insts), Parent(Parent) {
44 getParent()->getParent()->trackBBForAtom(&Insts, this);
45}
Ahmed Bougachaef993562013-05-24 01:07:04 +000046
47void MCBasicBlock::addSuccessor(const MCBasicBlock *MCBB) {
Ahmed Bougachadca54ea2013-08-21 07:27:50 +000048 if (!isSuccessor(MCBB))
49 Successors.push_back(MCBB);
Ahmed Bougachaef993562013-05-24 01:07:04 +000050}
51
52bool MCBasicBlock::isSuccessor(const MCBasicBlock *MCBB) const {
53 return std::find(Successors.begin(), Successors.end(),
54 MCBB) != Successors.end();
55}
56
57void MCBasicBlock::addPredecessor(const MCBasicBlock *MCBB) {
Ahmed Bougachadca54ea2013-08-21 07:27:50 +000058 if (!isPredecessor(MCBB))
59 Predecessors.push_back(MCBB);
Ahmed Bougachaef993562013-05-24 01:07:04 +000060}
61
62bool MCBasicBlock::isPredecessor(const MCBasicBlock *MCBB) const {
63 return std::find(Predecessors.begin(), Predecessors.end(),
64 MCBB) != Predecessors.end();
65}
Ahmed Bougachaaeb2bbc2013-08-21 07:28:24 +000066
67void 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}