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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6 | // |
John Criswell | 29265fe | 2003-10-21 15:17:13 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 15 | #ifndef LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H |
| 16 | #define LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 17 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/SymbolTableListTraits.h" |
| 19 | #include "llvm/IR/ValueSymbolTable.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 20 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 21 | namespace llvm { |
| 22 | |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 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. |
Duncan P. N. Exon Smith | 37bf678 | 2015-10-07 20:05:10 +0000 | [diff] [blame] | 26 | template <typename ValueSubClass> |
| 27 | template <typename TPtr> |
| 28 | void SymbolTableListTraits<ValueSubClass>::setSymTabObject(TPtr *Dest, |
| 29 | TPtr Src) { |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 30 | // 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] | 31 | ValueSymbolTable *OldST = getSymTab(getListOwner()); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 32 | |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 33 | // Do it. |
| 34 | *Dest = Src; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 35 | |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 36 | // Get the new SymTab object. |
Duncan P. N. Exon Smith | c77e92d | 2015-10-06 21:31:07 +0000 | [diff] [blame] | 37 | ValueSymbolTable *NewST = getSymTab(getListOwner()); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 38 | |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 39 | // If there is nothing to do, quick exit. |
| 40 | if (OldST == NewST) return; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 41 | |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 42 | // 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] | 43 | ListTy &ItemList = getList(getListOwner()); |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 44 | if (ItemList.empty()) return; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 45 | |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 46 | if (OldST) { |
| 47 | // Remove all entries from the previous symtab. |
Duncan P. N. Exon Smith | 55a0c43 | 2015-10-06 22:37:47 +0000 | [diff] [blame] | 48 | for (auto I = ItemList.begin(); I != ItemList.end(); ++I) |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 49 | if (I->hasName()) |
| 50 | OldST->removeValueName(I->getValueName()); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 53 | if (NewST) { |
| 54 | // Add all of the items to the new symtab. |
Duncan P. N. Exon Smith | 55a0c43 | 2015-10-06 22:37:47 +0000 | [diff] [blame] | 55 | for (auto I = ItemList.begin(); I != ItemList.end(); ++I) |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 56 | if (I->hasName()) |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 57 | NewST->reinsertValue(&*I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 58 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Duncan P. N. Exon Smith | 37bf678 | 2015-10-07 20:05:10 +0000 | [diff] [blame] | 62 | template <typename ValueSubClass> |
| 63 | void SymbolTableListTraits<ValueSubClass>::addNodeToList(ValueSubClass *V) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 64 | assert(!V->getParent() && "Value already in a container!!"); |
Chris Lattner | 422cfcd | 2007-04-17 04:04:14 +0000 | [diff] [blame] | 65 | ItemParentClass *Owner = getListOwner(); |
| 66 | V->setParent(Owner); |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 67 | if (V->hasName()) |
Duncan P. N. Exon Smith | c77e92d | 2015-10-06 21:31:07 +0000 | [diff] [blame] | 68 | if (ValueSymbolTable *ST = getSymTab(Owner)) |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 69 | ST->reinsertValue(V); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Duncan P. N. Exon Smith | 37bf678 | 2015-10-07 20:05:10 +0000 | [diff] [blame] | 72 | template <typename ValueSubClass> |
| 73 | void SymbolTableListTraits<ValueSubClass>::removeNodeFromList( |
| 74 | ValueSubClass *V) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 75 | V->setParent(nullptr); |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 76 | if (V->hasName()) |
Duncan P. N. Exon Smith | c77e92d | 2015-10-06 21:31:07 +0000 | [diff] [blame] | 77 | if (ValueSymbolTable *ST = getSymTab(getListOwner())) |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 78 | ST->removeValueName(V->getValueName()); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Duncan P. N. Exon Smith | 37bf678 | 2015-10-07 20:05:10 +0000 | [diff] [blame] | 81 | template <typename ValueSubClass> |
| 82 | void SymbolTableListTraits<ValueSubClass>::transferNodesFromList( |
Duncan P. N. Exon Smith | 8cc24ea | 2016-09-03 01:22:56 +0000 | [diff] [blame] | 83 | SymbolTableListTraits &L2, iterator first, iterator last) { |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 84 | // We only have to do work here if transferring instructions between BBs |
Chris Lattner | 422cfcd | 2007-04-17 04:04:14 +0000 | [diff] [blame] | 85 | ItemParentClass *NewIP = getListOwner(), *OldIP = L2.getListOwner(); |
Reid Kleckner | e80799e | 2019-01-23 22:59:52 +0000 | [diff] [blame] | 86 | if (NewIP == OldIP) |
| 87 | return; |
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 |