Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1 | //===-- llvm/SymbolTableListTraitsImpl.h - Implementation ------*- C++ -*--===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 29265fe | 2003-10-21 15:17:13 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 29265fe | 2003-10-21 15:17:13 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9 | // |
| 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. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 16 | #ifndef LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H |
| 17 | #define LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 18 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/SymbolTableListTraits.h" |
| 20 | #include "llvm/IR/ValueSymbolTable.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 21 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 22 | namespace llvm { |
| 23 | |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 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. |
Duncan P. N. Exon Smith | 37bf678 | 2015-10-07 20:05:10 +0000 | [diff] [blame] | 27 | template <typename ValueSubClass> |
| 28 | template <typename TPtr> |
| 29 | void SymbolTableListTraits<ValueSubClass>::setSymTabObject(TPtr *Dest, |
| 30 | TPtr Src) { |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 31 | // Get the old symtab and value list before doing the assignment. |
Duncan P. N. Exon Smith | c77e92d | 2015-10-06 21:31:07 +0000 | [diff] [blame] | 32 | ValueSymbolTable *OldST = getSymTab(getListOwner()); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 33 | |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 34 | // Do it. |
| 35 | *Dest = Src; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 36 | |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 37 | // Get the new SymTab object. |
Duncan P. N. Exon Smith | c77e92d | 2015-10-06 21:31:07 +0000 | [diff] [blame] | 38 | ValueSymbolTable *NewST = getSymTab(getListOwner()); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 39 | |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 40 | // If there is nothing to do, quick exit. |
| 41 | if (OldST == NewST) return; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 42 | |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 43 | // Move all the elements from the old symtab to the new one. |
Duncan P. N. Exon Smith | 37bf678 | 2015-10-07 20:05:10 +0000 | [diff] [blame] | 44 | ListTy &ItemList = getList(getListOwner()); |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 45 | if (ItemList.empty()) return; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 46 | |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 47 | if (OldST) { |
| 48 | // Remove all entries from the previous symtab. |
Duncan P. N. Exon Smith | 55a0c43 | 2015-10-06 22:37:47 +0000 | [diff] [blame] | 49 | for (auto I = ItemList.begin(); I != ItemList.end(); ++I) |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 50 | if (I->hasName()) |
| 51 | OldST->removeValueName(I->getValueName()); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 54 | if (NewST) { |
| 55 | // Add all of the items to the new symtab. |
Duncan P. N. Exon Smith | 55a0c43 | 2015-10-06 22:37:47 +0000 | [diff] [blame] | 56 | for (auto I = ItemList.begin(); I != ItemList.end(); ++I) |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 57 | if (I->hasName()) |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 58 | NewST->reinsertValue(&*I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 59 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Duncan P. N. Exon Smith | 37bf678 | 2015-10-07 20:05:10 +0000 | [diff] [blame] | 63 | template <typename ValueSubClass> |
| 64 | void SymbolTableListTraits<ValueSubClass>::addNodeToList(ValueSubClass *V) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 65 | assert(!V->getParent() && "Value already in a container!!"); |
Chris Lattner | 422cfcd | 2007-04-17 04:04:14 +0000 | [diff] [blame] | 66 | ItemParentClass *Owner = getListOwner(); |
| 67 | V->setParent(Owner); |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 68 | if (V->hasName()) |
Duncan P. N. Exon Smith | c77e92d | 2015-10-06 21:31:07 +0000 | [diff] [blame] | 69 | if (ValueSymbolTable *ST = getSymTab(Owner)) |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 70 | ST->reinsertValue(V); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Duncan P. N. Exon Smith | 37bf678 | 2015-10-07 20:05:10 +0000 | [diff] [blame] | 73 | template <typename ValueSubClass> |
| 74 | void SymbolTableListTraits<ValueSubClass>::removeNodeFromList( |
| 75 | ValueSubClass *V) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 76 | V->setParent(nullptr); |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 77 | if (V->hasName()) |
Duncan P. N. Exon Smith | c77e92d | 2015-10-06 21:31:07 +0000 | [diff] [blame] | 78 | if (ValueSymbolTable *ST = getSymTab(getListOwner())) |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 79 | ST->removeValueName(V->getValueName()); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Duncan P. N. Exon Smith | 37bf678 | 2015-10-07 20:05:10 +0000 | [diff] [blame] | 82 | template <typename ValueSubClass> |
| 83 | void SymbolTableListTraits<ValueSubClass>::transferNodesFromList( |
Duncan P. N. Exon Smith | 8cc24ea | 2016-09-03 01:22:56 +0000 | [diff] [blame] | 84 | SymbolTableListTraits &L2, iterator first, iterator last) { |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 85 | // We only have to do work here if transferring instructions between BBs |
Chris Lattner | 422cfcd | 2007-04-17 04:04:14 +0000 | [diff] [blame] | 86 | ItemParentClass *NewIP = getListOwner(), *OldIP = L2.getListOwner(); |
Duncan P. N. Exon Smith | b7668d5 | 2016-08-30 18:00:45 +0000 | [diff] [blame] | 87 | assert(NewIP != OldIP && "Expected different list owners"); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 88 | |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 89 | // We only have to update symbol table entries if we are transferring the |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 90 | // instructions to a different symtab object... |
Duncan P. N. Exon Smith | c77e92d | 2015-10-06 21:31:07 +0000 | [diff] [blame] | 91 | ValueSymbolTable *NewST = getSymTab(NewIP); |
| 92 | ValueSymbolTable *OldST = getSymTab(OldIP); |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 93 | if (NewST != OldST) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 94 | for (; first != last; ++first) { |
| 95 | ValueSubClass &V = *first; |
| 96 | bool HasName = V.hasName(); |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 97 | if (OldST && HasName) |
| 98 | OldST->removeValueName(V.getValueName()); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 99 | V.setParent(NewIP); |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 100 | if (NewST && HasName) |
| 101 | NewST->reinsertValue(&V); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 102 | } |
| 103 | } else { |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 104 | // Just transferring between blocks in the same function, simply update the |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 105 | // parent fields in the instructions... |
| 106 | for (; first != last; ++first) |
| 107 | first->setParent(NewIP); |
| 108 | } |
| 109 | } |
| 110 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 111 | } // End llvm namespace |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 113 | #endif |