1 //===-- llvm/SymbolTableListTraitsImpl.h - Implementation ------*- C++ -*--===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the stickier parts of the SymbolTableListTraits class,
10 // and is explicitly instantiated where needed to avoid defining all this code
11 // in a widely used header.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H
16 #define LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H
18 #include "llvm/IR/SymbolTableListTraits.h"
19 #include "llvm/IR/ValueSymbolTable.h"
23 /// Notify basic blocks when an instruction is inserted.
24 template <typename ParentClass
>
25 inline void invalidateParentIListOrdering(ParentClass
*Parent
) {}
26 template <> void invalidateParentIListOrdering(BasicBlock
*BB
);
28 /// setSymTabObject - This is called when (f.e.) the parent of a basic block
29 /// changes. This requires us to remove all the instruction symtab entries from
30 /// the current function and reinsert them into the new function.
31 template <typename ValueSubClass
>
32 template <typename TPtr
>
33 void SymbolTableListTraits
<ValueSubClass
>::setSymTabObject(TPtr
*Dest
,
35 // Get the old symtab and value list before doing the assignment.
36 ValueSymbolTable
*OldST
= getSymTab(getListOwner());
41 // Get the new SymTab object.
42 ValueSymbolTable
*NewST
= getSymTab(getListOwner());
44 // If there is nothing to do, quick exit.
45 if (OldST
== NewST
) return;
47 // Move all the elements from the old symtab to the new one.
48 ListTy
&ItemList
= getList(getListOwner());
49 if (ItemList
.empty()) return;
52 // Remove all entries from the previous symtab.
53 for (auto I
= ItemList
.begin(); I
!= ItemList
.end(); ++I
)
55 OldST
->removeValueName(I
->getValueName());
59 // Add all of the items to the new symtab.
60 for (auto I
= ItemList
.begin(); I
!= ItemList
.end(); ++I
)
62 NewST
->reinsertValue(&*I
);
67 template <typename ValueSubClass
>
68 void SymbolTableListTraits
<ValueSubClass
>::addNodeToList(ValueSubClass
*V
) {
69 assert(!V
->getParent() && "Value already in a container!!");
70 ItemParentClass
*Owner
= getListOwner();
72 invalidateParentIListOrdering(Owner
);
74 if (ValueSymbolTable
*ST
= getSymTab(Owner
))
78 template <typename ValueSubClass
>
79 void SymbolTableListTraits
<ValueSubClass
>::removeNodeFromList(
81 V
->setParent(nullptr);
83 if (ValueSymbolTable
*ST
= getSymTab(getListOwner()))
84 ST
->removeValueName(V
->getValueName());
87 template <typename ValueSubClass
>
88 void SymbolTableListTraits
<ValueSubClass
>::transferNodesFromList(
89 SymbolTableListTraits
&L2
, iterator first
, iterator last
) {
90 // Transfering nodes, even within the same BB, invalidates the ordering. The
91 // list that we removed the nodes from still has a valid ordering.
92 ItemParentClass
*NewIP
= getListOwner();
93 invalidateParentIListOrdering(NewIP
);
95 // Nothing else needs to be done if we're reording nodes within the same list.
96 ItemParentClass
*OldIP
= L2
.getListOwner();
100 // We only have to update symbol table entries if we are transferring the
101 // instructions to a different symtab object...
102 ValueSymbolTable
*NewST
= getSymTab(NewIP
);
103 ValueSymbolTable
*OldST
= getSymTab(OldIP
);
104 if (NewST
!= OldST
) {
105 for (; first
!= last
; ++first
) {
106 ValueSubClass
&V
= *first
;
107 bool HasName
= V
.hasName();
108 if (OldST
&& HasName
)
109 OldST
->removeValueName(V
.getValueName());
111 if (NewST
&& HasName
)
112 NewST
->reinsertValue(&V
);
115 // Just transferring between blocks in the same function, simply update the
116 // parent fields in the instructions...
117 for (; first
!= last
; ++first
)
118 first
->setParent(NewIP
);
122 } // End llvm namespace