LLVM API Documentation

MCModule.cpp
Go to the documentation of this file.
00001 //===- lib/MC/MCModule.cpp - MCModule implementation ----------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/ADT/STLExtras.h"
00011 #include "llvm/MC/MCModule.h"
00012 #include "llvm/MC/MCAtom.h"
00013 #include "llvm/MC/MCFunction.h"
00014 #include <algorithm>
00015 
00016 using namespace llvm;
00017 
00018 static bool AtomComp(const MCAtom *L, uint64_t Addr) {
00019   return L->getEndAddr() < Addr;
00020 }
00021 
00022 static bool AtomCompInv(uint64_t Addr, const MCAtom *R) {
00023   return Addr < R->getEndAddr();
00024 }
00025 
00026 void MCModule::map(MCAtom *NewAtom) {
00027   uint64_t Begin = NewAtom->Begin;
00028 
00029   assert(Begin <= NewAtom->End && "Creating MCAtom with endpoints reversed?");
00030 
00031   // Check for atoms already covering this range.
00032   AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(),
00033                                             Begin, AtomComp);
00034   assert((I == atom_end() || (*I)->getBeginAddr() > NewAtom->End)
00035          && "Offset range already occupied!");
00036 
00037   // Insert the new atom to the list.
00038   Atoms.insert(I, NewAtom);
00039 }
00040 
00041 MCTextAtom *MCModule::createTextAtom(uint64_t Begin, uint64_t End) {
00042   MCTextAtom *NewAtom = new MCTextAtom(this, Begin, End);
00043   map(NewAtom);
00044   return NewAtom;
00045 }
00046 
00047 MCDataAtom *MCModule::createDataAtom(uint64_t Begin, uint64_t End) {
00048   MCDataAtom *NewAtom = new MCDataAtom(this, Begin, End);
00049   map(NewAtom);
00050   return NewAtom;
00051 }
00052 
00053 // remap - Update the interval mapping for an atom.
00054 void MCModule::remap(MCAtom *Atom, uint64_t NewBegin, uint64_t NewEnd) {
00055   // Find and erase the old mapping.
00056   AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(),
00057                                             Atom->Begin, AtomComp);
00058   assert(I != atom_end() && "Atom offset not found in module!");
00059   assert(*I == Atom && "Previous atom mapping was invalid!");
00060   Atoms.erase(I);
00061 
00062   // FIXME: special case NewBegin == Atom->Begin
00063 
00064   // Insert the new mapping.
00065   AtomListTy::iterator NewI = std::lower_bound(atom_begin(), atom_end(),
00066                                                NewBegin, AtomComp);
00067   assert((NewI == atom_end() || (*NewI)->getBeginAddr() > Atom->End)
00068          && "Offset range already occupied!");
00069   Atoms.insert(NewI, Atom);
00070 
00071   // Update the atom internal bounds.
00072   Atom->Begin = NewBegin;
00073   Atom->End = NewEnd;
00074 }
00075 
00076 const MCAtom *MCModule::findAtomContaining(uint64_t Addr) const {
00077   AtomListTy::const_iterator I = std::lower_bound(atom_begin(), atom_end(),
00078                                                   Addr, AtomComp);
00079   if (I != atom_end() && (*I)->getBeginAddr() <= Addr)
00080     return *I;
00081   return nullptr;
00082 }
00083 
00084 MCAtom *MCModule::findAtomContaining(uint64_t Addr) {
00085   return const_cast<MCAtom*>(
00086     const_cast<const MCModule *>(this)->findAtomContaining(Addr));
00087 }
00088 
00089 const MCAtom *MCModule::findFirstAtomAfter(uint64_t Addr) const {
00090   AtomListTy::const_iterator I = std::upper_bound(atom_begin(), atom_end(),
00091                                                   Addr, AtomCompInv);
00092   if (I != atom_end())
00093     return *I;
00094   return nullptr;
00095 }
00096 
00097 MCAtom *MCModule::findFirstAtomAfter(uint64_t Addr) {
00098   return const_cast<MCAtom*>(
00099     const_cast<const MCModule *>(this)->findFirstAtomAfter(Addr));
00100 }
00101 
00102 MCFunction *MCModule::createFunction(StringRef Name) {
00103   std::unique_ptr<MCFunction> MCF(new MCFunction(Name, this));
00104   Functions.push_back(std::move(MCF));
00105   return Functions.back().get();
00106 }
00107 
00108 static bool CompBBToAtom(MCBasicBlock *BB, const MCTextAtom *Atom) {
00109   return BB->getInsts() < Atom;
00110 }
00111 
00112 void MCModule::splitBasicBlocksForAtom(const MCTextAtom *TA,
00113                                        const MCTextAtom *NewTA) {
00114   BBsByAtomTy::iterator
00115     I = std::lower_bound(BBsByAtom.begin(), BBsByAtom.end(),
00116                          TA, CompBBToAtom);
00117   for (; I != BBsByAtom.end() && (*I)->getInsts() == TA; ++I) {
00118     MCBasicBlock *BB = *I;
00119     MCBasicBlock *NewBB = &BB->getParent()->createBlock(*NewTA);
00120     BB->splitBasicBlock(NewBB);
00121   }
00122 }
00123 
00124 void MCModule::trackBBForAtom(const MCTextAtom *Atom, MCBasicBlock *BB) {
00125   assert(Atom == BB->getInsts() && "Text atom doesn't back the basic block!");
00126   BBsByAtomTy::iterator I = std::lower_bound(BBsByAtom.begin(),
00127                                              BBsByAtom.end(),
00128                                              Atom, CompBBToAtom);
00129   for (; I != BBsByAtom.end() && (*I)->getInsts() == Atom; ++I)
00130     if (*I == BB)
00131       return;
00132   BBsByAtom.insert(I, BB);
00133 }
00134 
00135 MCModule::MCModule() : Entrypoint(0) { }
00136 
00137 MCModule::~MCModule() {
00138   for (AtomListTy::iterator AI = atom_begin(),
00139                             AE = atom_end();
00140                             AI != AE; ++AI)
00141     delete *AI;
00142 }