LLVM API Documentation

SymbolTableListTraitsImpl.h
Go to the documentation of this file.
00001 //===-- llvm/SymbolTableListTraitsImpl.h - Implementation ------*- C++ -*--===//
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 // This file implements the stickier parts of the SymbolTableListTraits class,
00011 // and is explicitly instantiated where needed to avoid defining all this code
00012 // in a widely used header.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H
00017 #define LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H
00018 
00019 #include "llvm/IR/SymbolTableListTraits.h"
00020 #include "llvm/IR/ValueSymbolTable.h"
00021 
00022 namespace llvm {
00023 
00024 /// setSymTabObject - This is called when (f.e.) the parent of a basic block
00025 /// changes.  This requires us to remove all the instruction symtab entries from
00026 /// the current function and reinsert them into the new function.
00027 template<typename ValueSubClass, typename ItemParentClass>
00028 template<typename TPtr>
00029 void SymbolTableListTraits<ValueSubClass,ItemParentClass>
00030 ::setSymTabObject(TPtr *Dest, TPtr Src) {
00031   // Get the old symtab and value list before doing the assignment.
00032   ValueSymbolTable *OldST = TraitsClass::getSymTab(getListOwner());
00033 
00034   // Do it.
00035   *Dest = Src;
00036   
00037   // Get the new SymTab object.
00038   ValueSymbolTable *NewST = TraitsClass::getSymTab(getListOwner());
00039   
00040   // If there is nothing to do, quick exit.
00041   if (OldST == NewST) return;
00042   
00043   // Move all the elements from the old symtab to the new one.
00044   iplist<ValueSubClass> &ItemList = TraitsClass::getList(getListOwner());
00045   if (ItemList.empty()) return;
00046   
00047   if (OldST) {
00048     // Remove all entries from the previous symtab.
00049     for (typename iplist<ValueSubClass>::iterator I = ItemList.begin();
00050          I != ItemList.end(); ++I)
00051       if (I->hasName())
00052         OldST->removeValueName(I->getValueName());
00053   }
00054 
00055   if (NewST) {
00056     // Add all of the items to the new symtab.
00057     for (typename iplist<ValueSubClass>::iterator I = ItemList.begin();
00058          I != ItemList.end(); ++I)
00059       if (I->hasName())
00060         NewST->reinsertValue(I);
00061   }
00062   
00063 }
00064 
00065 template<typename ValueSubClass, typename ItemParentClass>
00066 void SymbolTableListTraits<ValueSubClass,ItemParentClass>
00067 ::addNodeToList(ValueSubClass *V) {
00068   assert(!V->getParent() && "Value already in a container!!");
00069   ItemParentClass *Owner = getListOwner();
00070   V->setParent(Owner);
00071   if (V->hasName())
00072     if (ValueSymbolTable *ST = TraitsClass::getSymTab(Owner))
00073       ST->reinsertValue(V);
00074 }
00075 
00076 template<typename ValueSubClass, typename ItemParentClass>
00077 void SymbolTableListTraits<ValueSubClass,ItemParentClass>
00078 ::removeNodeFromList(ValueSubClass *V) {
00079   V->setParent(nullptr);
00080   if (V->hasName())
00081     if (ValueSymbolTable *ST = TraitsClass::getSymTab(getListOwner()))
00082       ST->removeValueName(V->getValueName());
00083 }
00084 
00085 template<typename ValueSubClass, typename ItemParentClass>
00086 void SymbolTableListTraits<ValueSubClass,ItemParentClass>
00087 ::transferNodesFromList(ilist_traits<ValueSubClass> &L2,
00088                         ilist_iterator<ValueSubClass> first,
00089                         ilist_iterator<ValueSubClass> last) {
00090   // We only have to do work here if transferring instructions between BBs
00091   ItemParentClass *NewIP = getListOwner(), *OldIP = L2.getListOwner();
00092   if (NewIP == OldIP) return;  // No work to do at all...
00093 
00094   // We only have to update symbol table entries if we are transferring the
00095   // instructions to a different symtab object...
00096   ValueSymbolTable *NewST = TraitsClass::getSymTab(NewIP);
00097   ValueSymbolTable *OldST = TraitsClass::getSymTab(OldIP);
00098   if (NewST != OldST) {
00099     for (; first != last; ++first) {
00100       ValueSubClass &V = *first;
00101       bool HasName = V.hasName();
00102       if (OldST && HasName)
00103         OldST->removeValueName(V.getValueName());
00104       V.setParent(NewIP);
00105       if (NewST && HasName)
00106         NewST->reinsertValue(&V);
00107     }
00108   } else {
00109     // Just transferring between blocks in the same function, simply update the
00110     // parent fields in the instructions...
00111     for (; first != last; ++first)
00112       first->setParent(NewIP);
00113   }
00114 }
00115 
00116 } // End llvm namespace
00117 
00118 #endif