1 //===-- llvm/SymbolTableListTraitsImpl.h - Implementation ------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the stickier parts of the SymbolTableListTraits class,
11 // and is explicitly instantiated where needed to avoid defining all this code
12 // in a widely used header.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H
17 #define LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H
19 #include "llvm/IR/SymbolTableListTraits.h"
20 #include "llvm/IR/ValueSymbolTable.h"
24 /// setSymTabObject - This is called when (f.e.) the parent of a basic block
25 /// changes. This requires us to remove all the instruction symtab entries from
26 /// the current function and reinsert them into the new function.
27 template <typename ValueSubClass>
28 template <typename TPtr>
29 void SymbolTableListTraits<ValueSubClass>::setSymTabObject(TPtr *Dest,
31 // Get the old symtab and value list before doing the assignment.
32 ValueSymbolTable *OldST = getSymTab(getListOwner());
37 // Get the new SymTab object.
38 ValueSymbolTable *NewST = getSymTab(getListOwner());
40 // If there is nothing to do, quick exit.
41 if (OldST == NewST) return;
43 // Move all the elements from the old symtab to the new one.
44 ListTy &ItemList = getList(getListOwner());
45 if (ItemList.empty()) return;
48 // Remove all entries from the previous symtab.
49 for (auto I = ItemList.begin(); I != ItemList.end(); ++I)
51 OldST->removeValueName(I->getValueName());
55 // Add all of the items to the new symtab.
56 for (auto I = ItemList.begin(); I != ItemList.end(); ++I)
58 NewST->reinsertValue(&*I);
63 template <typename ValueSubClass>
64 void SymbolTableListTraits<ValueSubClass>::addNodeToList(ValueSubClass *V) {
65 assert(!V->getParent() && "Value already in a container!!");
66 ItemParentClass *Owner = getListOwner();
69 if (ValueSymbolTable *ST = getSymTab(Owner))
73 template <typename ValueSubClass>
74 void SymbolTableListTraits<ValueSubClass>::removeNodeFromList(
76 V->setParent(nullptr);
78 if (ValueSymbolTable *ST = getSymTab(getListOwner()))
79 ST->removeValueName(V->getValueName());
82 template <typename ValueSubClass>
83 void SymbolTableListTraits<ValueSubClass>::transferNodesFromList(
84 SymbolTableListTraits &L2, ilist_iterator<ValueSubClass> first,
85 ilist_iterator<ValueSubClass> last) {
86 // We only have to do work here if transferring instructions between BBs
87 ItemParentClass *NewIP = getListOwner(), *OldIP = L2.getListOwner();
88 if (NewIP == OldIP) return; // No work to do at all...
90 // We only have to update symbol table entries if we are transferring the
91 // instructions to a different symtab object...
92 ValueSymbolTable *NewST = getSymTab(NewIP);
93 ValueSymbolTable *OldST = getSymTab(OldIP);
95 for (; first != last; ++first) {
96 ValueSubClass &V = *first;
97 bool HasName = V.hasName();
99 OldST->removeValueName(V.getValueName());
101 if (NewST && HasName)
102 NewST->reinsertValue(&V);
105 // Just transferring between blocks in the same function, simply update the
106 // parent fields in the instructions...
107 for (; first != last; ++first)
108 first->setParent(NewIP);
112 } // End llvm namespace