SymbolTableListTraitsImpl.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //===-- llvm/SymbolTableListTraitsImpl.h - Implementation ------*- C++ -*--===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  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.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H
  15. #define LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H
  16. #include "llvm/IR/SymbolTableListTraits.h"
  17. #include "llvm/IR/ValueSymbolTable.h"
  18. namespace llvm {
  19. /// Notify basic blocks when an instruction is inserted.
  20. template <typename ParentClass>
  21. inline void invalidateParentIListOrdering(ParentClass *Parent) {}
  22. template <> void invalidateParentIListOrdering(BasicBlock *BB);
  23. /// setSymTabObject - This is called when (f.e.) the parent of a basic block
  24. /// changes. This requires us to remove all the instruction symtab entries from
  25. /// the current function and reinsert them into the new function.
  26. template <typename ValueSubClass>
  27. template <typename TPtr>
  28. void SymbolTableListTraits<ValueSubClass>::setSymTabObject(TPtr *Dest,
  29. TPtr Src) {
  30. // Get the old symtab and value list before doing the assignment.
  31. ValueSymbolTable *OldST = getSymTab(getListOwner());
  32. // Do it.
  33. *Dest = Src;
  34. // Get the new SymTab object.
  35. ValueSymbolTable *NewST = getSymTab(getListOwner());
  36. // If there is nothing to do, quick exit.
  37. if (OldST == NewST) return;
  38. // Move all the elements from the old symtab to the new one.
  39. ListTy &ItemList = getList(getListOwner());
  40. if (ItemList.empty()) return;
  41. if (OldST) {
  42. // Remove all entries from the previous symtab.
  43. for (auto I = ItemList.begin(); I != ItemList.end(); ++I)
  44. if (I->hasName())
  45. OldST->removeValueName(I->getValueName());
  46. }
  47. if (NewST) {
  48. // Add all of the items to the new symtab.
  49. for (auto I = ItemList.begin(); I != ItemList.end(); ++I)
  50. if (I->hasName())
  51. NewST->reinsertValue(&*I);
  52. }
  53. }
  54. template <typename ValueSubClass>
  55. void SymbolTableListTraits<ValueSubClass>::addNodeToList(ValueSubClass *V) {
  56. assert(!V->getParent() && "Value already in a container!!");
  57. ItemParentClass *Owner = getListOwner();
  58. V->setParent(Owner);
  59. invalidateParentIListOrdering(Owner);
  60. if (V->hasName())
  61. if (ValueSymbolTable *ST = getSymTab(Owner))
  62. ST->reinsertValue(V);
  63. }
  64. template <typename ValueSubClass>
  65. void SymbolTableListTraits<ValueSubClass>::removeNodeFromList(
  66. ValueSubClass *V) {
  67. V->setParent(nullptr);
  68. if (V->hasName())
  69. if (ValueSymbolTable *ST = getSymTab(getListOwner()))
  70. ST->removeValueName(V->getValueName());
  71. }
  72. template <typename ValueSubClass>
  73. void SymbolTableListTraits<ValueSubClass>::transferNodesFromList(
  74. SymbolTableListTraits &L2, iterator first, iterator last) {
  75. // Transfering nodes, even within the same BB, invalidates the ordering. The
  76. // list that we removed the nodes from still has a valid ordering.
  77. ItemParentClass *NewIP = getListOwner();
  78. invalidateParentIListOrdering(NewIP);
  79. // Nothing else needs to be done if we're reording nodes within the same list.
  80. ItemParentClass *OldIP = L2.getListOwner();
  81. if (NewIP == OldIP)
  82. return;
  83. // We only have to update symbol table entries if we are transferring the
  84. // instructions to a different symtab object...
  85. ValueSymbolTable *NewST = getSymTab(NewIP);
  86. ValueSymbolTable *OldST = getSymTab(OldIP);
  87. if (NewST != OldST) {
  88. for (; first != last; ++first) {
  89. ValueSubClass &V = *first;
  90. bool HasName = V.hasName();
  91. if (OldST && HasName)
  92. OldST->removeValueName(V.getValueName());
  93. V.setParent(NewIP);
  94. if (NewST && HasName)
  95. NewST->reinsertValue(&V);
  96. }
  97. } else {
  98. // Just transferring between blocks in the same function, simply update the
  99. // parent fields in the instructions...
  100. for (; first != last; ++first)
  101. first->setParent(NewIP);
  102. }
  103. }
  104. } // End llvm namespace
  105. #endif