SymbolTableListTraitsImpl.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. /// setSymTabObject - This is called when (f.e.) the parent of a basic block
  20. /// changes. This requires us to remove all the instruction symtab entries from
  21. /// the current function and reinsert them into the new function.
  22. template <typename ValueSubClass>
  23. template <typename TPtr>
  24. void SymbolTableListTraits<ValueSubClass>::setSymTabObject(TPtr *Dest,
  25. TPtr Src) {
  26. // Get the old symtab and value list before doing the assignment.
  27. ValueSymbolTable *OldST = getSymTab(getListOwner());
  28. // Do it.
  29. *Dest = Src;
  30. // Get the new SymTab object.
  31. ValueSymbolTable *NewST = getSymTab(getListOwner());
  32. // If there is nothing to do, quick exit.
  33. if (OldST == NewST) return;
  34. // Move all the elements from the old symtab to the new one.
  35. ListTy &ItemList = getList(getListOwner());
  36. if (ItemList.empty()) return;
  37. if (OldST) {
  38. // Remove all entries from the previous symtab.
  39. for (auto I = ItemList.begin(); I != ItemList.end(); ++I)
  40. if (I->hasName())
  41. OldST->removeValueName(I->getValueName());
  42. }
  43. if (NewST) {
  44. // Add all of the items to the new symtab.
  45. for (auto I = ItemList.begin(); I != ItemList.end(); ++I)
  46. if (I->hasName())
  47. NewST->reinsertValue(&*I);
  48. }
  49. }
  50. template <typename ValueSubClass>
  51. void SymbolTableListTraits<ValueSubClass>::addNodeToList(ValueSubClass *V) {
  52. assert(!V->getParent() && "Value already in a container!!");
  53. ItemParentClass *Owner = getListOwner();
  54. V->setParent(Owner);
  55. if (V->hasName())
  56. if (ValueSymbolTable *ST = getSymTab(Owner))
  57. ST->reinsertValue(V);
  58. }
  59. template <typename ValueSubClass>
  60. void SymbolTableListTraits<ValueSubClass>::removeNodeFromList(
  61. ValueSubClass *V) {
  62. V->setParent(nullptr);
  63. if (V->hasName())
  64. if (ValueSymbolTable *ST = getSymTab(getListOwner()))
  65. ST->removeValueName(V->getValueName());
  66. }
  67. template <typename ValueSubClass>
  68. void SymbolTableListTraits<ValueSubClass>::transferNodesFromList(
  69. SymbolTableListTraits &L2, iterator first, iterator last) {
  70. // We only have to do work here if transferring instructions between BBs
  71. ItemParentClass *NewIP = getListOwner(), *OldIP = L2.getListOwner();
  72. if (NewIP == OldIP)
  73. return;
  74. // We only have to update symbol table entries if we are transferring the
  75. // instructions to a different symtab object...
  76. ValueSymbolTable *NewST = getSymTab(NewIP);
  77. ValueSymbolTable *OldST = getSymTab(OldIP);
  78. if (NewST != OldST) {
  79. for (; first != last; ++first) {
  80. ValueSubClass &V = *first;
  81. bool HasName = V.hasName();
  82. if (OldST && HasName)
  83. OldST->removeValueName(V.getValueName());
  84. V.setParent(NewIP);
  85. if (NewST && HasName)
  86. NewST->reinsertValue(&V);
  87. }
  88. } else {
  89. // Just transferring between blocks in the same function, simply update the
  90. // parent fields in the instructions...
  91. for (; first != last; ++first)
  92. first->setParent(NewIP);
  93. }
  94. }
  95. } // End llvm namespace
  96. #endif