MC CFG: Add a few needed methods, mainly MCModule::findFirstAtomAfter.
authorAhmed Bougacha <ahmed.bougacha@gmail.com>
Wed, 21 Aug 2013 07:28:17 +0000 (07:28 +0000)
committerAhmed Bougacha <ahmed.bougacha@gmail.com>
Wed, 21 Aug 2013 07:28:17 +0000 (07:28 +0000)
While there, do some minor cleanup.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@188880 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/MC/MCFunction.h
include/llvm/MC/MCModule.h
lib/MC/MCFunction.cpp
lib/MC/MCModule.cpp

index a87b9488d3680027e2e37cc5722501fc5e54c830..697b5006a9a9c00fb4a892c0ee9c8fe96e0de5a0 100644 (file)
@@ -97,6 +97,12 @@ public:
 
   StringRef getName() const { return Name; }
 
+  /// \name Get the owning MC Module.
+  /// @{
+  const MCModule *getParent() const { return ParentModule; }
+        MCModule *getParent()       { return ParentModule; }
+  /// @}
+
   /// \name Access to the function's basic blocks. No ordering is enforced,
   /// except that the first block is the entry block.
   /// @{
index a145653af79c726e521d861cfd66edb7012cd812..c0c242c21a6df8963ac81c80807b46a952e4fc86 100644 (file)
@@ -43,7 +43,9 @@ class MCModule {
   typedef std::vector<MCAtom*> AtomListTy;
   AtomListTy Atoms;
 
+  // For access to map/remap.
   friend class MCAtom;
+
   /// \brief Remap \p Atom to the given range, and update its Begin/End fields.
   /// \param Atom An atom belonging to this module.
   /// An atom should always use this method to update its bounds, because this
@@ -83,6 +85,8 @@ public:
   /// @{
   const MCAtom *findAtomContaining(uint64_t Addr) const;
         MCAtom *findAtomContaining(uint64_t Addr);
+  const MCAtom *findFirstAtomAfter(uint64_t Addr) const;
+        MCAtom *findFirstAtomAfter(uint64_t Addr);
 
   typedef AtomListTy::const_iterator const_atom_iterator;
   typedef AtomListTy::      iterator       atom_iterator;
index 5011d5fd6b8ef022edbb01e7dc9451fe8b8567fa..300ab5b1a0ea19904fbee59c61caca7abbc45a82 100644 (file)
@@ -26,8 +26,9 @@ MCFunction::~MCFunction() {
 }
 
 MCBasicBlock &MCFunction::createBlock(const MCTextAtom &TA) {
-  Blocks.push_back(new MCBasicBlock(TA, this));
-  return *Blocks.back();
+  MCBasicBlock *MCBB = new MCBasicBlock(TA, this);
+  Blocks.push_back(MCBB);
+  return *MCBB;
 }
 
 const MCBasicBlock *MCFunction::find(uint64_t StartAddr) const {
index 9a9d90e5b6a71027d09888f49c0008ae9480393b..bdd5cc6b1cd1b7138b3c7c2fb21249c012e08b3f 100644 (file)
@@ -18,6 +18,10 @@ static bool AtomComp(const MCAtom *L, uint64_t Addr) {
   return L->getEndAddr() < Addr;
 }
 
+static bool AtomCompInv(uint64_t Addr, const MCAtom *R) {
+  return Addr < R->getEndAddr();
+}
+
 void MCModule::map(MCAtom *NewAtom) {
   uint64_t Begin = NewAtom->Begin;
 
@@ -77,13 +81,23 @@ const MCAtom *MCModule::findAtomContaining(uint64_t Addr) const {
 }
 
 MCAtom *MCModule::findAtomContaining(uint64_t Addr) {
-  AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(),
-                                            Addr, AtomComp);
-  if (I != atom_end() && (*I)->getBeginAddr() <= Addr)
+  return const_cast<MCAtom*>(
+    const_cast<const MCModule *>(this)->findAtomContaining(Addr));
+}
+
+const MCAtom *MCModule::findFirstAtomAfter(uint64_t Addr) const {
+  AtomListTy::const_iterator I = std::upper_bound(atom_begin(), atom_end(),
+                                                  Addr, AtomCompInv);
+  if (I != atom_end())
     return *I;
   return 0;
 }
 
+MCAtom *MCModule::findFirstAtomAfter(uint64_t Addr) {
+  return const_cast<MCAtom*>(
+    const_cast<const MCModule *>(this)->findFirstAtomAfter(Addr));
+}
+
 MCFunction *MCModule::createFunction(StringRef Name) {
   Functions.push_back(new MCFunction(Name, this));
   return Functions.back();