blob: 4d17d75859cf114ccfd42a50b5d0f574871dfda6 [file] [log] [blame]
Chris Lattner113f4f42002-06-25 16:13:24 +00001//===-- llvm/SymbolTableListTraitsImpl.h - Implementation ------*- C++ -*--===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell29265fe2003-10-21 15:17:13 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell29265fe2003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner113f4f42002-06-25 16:13:24 +00009//
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 Kramera7c40ef2014-08-13 16:26:38 +000016#ifndef LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H
17#define LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H
Chris Lattner113f4f42002-06-25 16:13:24 +000018
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/SymbolTableListTraits.h"
20#include "llvm/IR/ValueSymbolTable.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000021
Brian Gaeke960707c2003-11-11 22:41:34 +000022namespace llvm {
23
Chris Lattnerb47aa542007-04-17 03:26:42 +000024/// 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.
27template<typename ValueSubClass, typename ItemParentClass>
28template<typename TPtr>
29void SymbolTableListTraits<ValueSubClass,ItemParentClass>
30::setSymTabObject(TPtr *Dest, TPtr Src) {
31 // Get the old symtab and value list before doing the assignment.
Duncan P. N. Exon Smithc77e92d2015-10-06 21:31:07 +000032 ValueSymbolTable *OldST = getSymTab(getListOwner());
Chris Lattner113f4f42002-06-25 16:13:24 +000033
Chris Lattnerb47aa542007-04-17 03:26:42 +000034 // Do it.
35 *Dest = Src;
36
37 // Get the new SymTab object.
Duncan P. N. Exon Smithc77e92d2015-10-06 21:31:07 +000038 ValueSymbolTable *NewST = getSymTab(getListOwner());
Chris Lattnerb47aa542007-04-17 03:26:42 +000039
40 // If there is nothing to do, quick exit.
41 if (OldST == NewST) return;
42
43 // Move all the elements from the old symtab to the new one.
Duncan P. N. Exon Smithd146e7d2015-10-06 22:14:06 +000044 iplist<ValueSubClass> &ItemList = getList(getListOwner());
Chris Lattnerb47aa542007-04-17 03:26:42 +000045 if (ItemList.empty()) return;
46
47 if (OldST) {
48 // Remove all entries from the previous symtab.
Duncan P. N. Exon Smith55a0c432015-10-06 22:37:47 +000049 for (auto I = ItemList.begin(); I != ItemList.end(); ++I)
Chris Lattnerb47aa542007-04-17 03:26:42 +000050 if (I->hasName())
51 OldST->removeValueName(I->getValueName());
Chris Lattner113f4f42002-06-25 16:13:24 +000052 }
53
Chris Lattnerb47aa542007-04-17 03:26:42 +000054 if (NewST) {
55 // Add all of the items to the new symtab.
Duncan P. N. Exon Smith55a0c432015-10-06 22:37:47 +000056 for (auto I = ItemList.begin(); I != ItemList.end(); ++I)
Chris Lattnerb47aa542007-04-17 03:26:42 +000057 if (I->hasName())
58 NewST->reinsertValue(I);
Chris Lattner113f4f42002-06-25 16:13:24 +000059 }
Chris Lattnerb47aa542007-04-17 03:26:42 +000060
Chris Lattner113f4f42002-06-25 16:13:24 +000061}
62
Chris Lattnerb47aa542007-04-17 03:26:42 +000063template<typename ValueSubClass, typename ItemParentClass>
64void SymbolTableListTraits<ValueSubClass,ItemParentClass>
Chris Lattner113f4f42002-06-25 16:13:24 +000065::addNodeToList(ValueSubClass *V) {
Craig Toppere73658d2014-04-28 04:05:08 +000066 assert(!V->getParent() && "Value already in a container!!");
Chris Lattner422cfcd2007-04-17 04:04:14 +000067 ItemParentClass *Owner = getListOwner();
68 V->setParent(Owner);
Chris Lattnerb47aa542007-04-17 03:26:42 +000069 if (V->hasName())
Duncan P. N. Exon Smithc77e92d2015-10-06 21:31:07 +000070 if (ValueSymbolTable *ST = getSymTab(Owner))
Chris Lattnerb47aa542007-04-17 03:26:42 +000071 ST->reinsertValue(V);
Chris Lattner113f4f42002-06-25 16:13:24 +000072}
73
Chris Lattnerb47aa542007-04-17 03:26:42 +000074template<typename ValueSubClass, typename ItemParentClass>
75void SymbolTableListTraits<ValueSubClass,ItemParentClass>
Chris Lattner113f4f42002-06-25 16:13:24 +000076::removeNodeFromList(ValueSubClass *V) {
Craig Toppere73658d2014-04-28 04:05:08 +000077 V->setParent(nullptr);
Chris Lattnerb47aa542007-04-17 03:26:42 +000078 if (V->hasName())
Duncan P. N. Exon Smithc77e92d2015-10-06 21:31:07 +000079 if (ValueSymbolTable *ST = getSymTab(getListOwner()))
Chris Lattnerb47aa542007-04-17 03:26:42 +000080 ST->removeValueName(V->getValueName());
Chris Lattner113f4f42002-06-25 16:13:24 +000081}
82
Chris Lattnerb47aa542007-04-17 03:26:42 +000083template<typename ValueSubClass, typename ItemParentClass>
84void SymbolTableListTraits<ValueSubClass,ItemParentClass>
Dan Gohman804c95d2008-07-28 21:51:04 +000085::transferNodesFromList(ilist_traits<ValueSubClass> &L2,
Chris Lattner113f4f42002-06-25 16:13:24 +000086 ilist_iterator<ValueSubClass> first,
87 ilist_iterator<ValueSubClass> last) {
Misha Brukmanfa100532003-10-10 17:54:14 +000088 // We only have to do work here if transferring instructions between BBs
Chris Lattner422cfcd2007-04-17 04:04:14 +000089 ItemParentClass *NewIP = getListOwner(), *OldIP = L2.getListOwner();
Chris Lattner113f4f42002-06-25 16:13:24 +000090 if (NewIP == OldIP) return; // No work to do at all...
91
Misha Brukmanfa100532003-10-10 17:54:14 +000092 // We only have to update symbol table entries if we are transferring the
Chris Lattner113f4f42002-06-25 16:13:24 +000093 // instructions to a different symtab object...
Duncan P. N. Exon Smithc77e92d2015-10-06 21:31:07 +000094 ValueSymbolTable *NewST = getSymTab(NewIP);
95 ValueSymbolTable *OldST = getSymTab(OldIP);
Chris Lattnerb47aa542007-04-17 03:26:42 +000096 if (NewST != OldST) {
Chris Lattner113f4f42002-06-25 16:13:24 +000097 for (; first != last; ++first) {
98 ValueSubClass &V = *first;
99 bool HasName = V.hasName();
Chris Lattnerb47aa542007-04-17 03:26:42 +0000100 if (OldST && HasName)
101 OldST->removeValueName(V.getValueName());
Chris Lattner113f4f42002-06-25 16:13:24 +0000102 V.setParent(NewIP);
Chris Lattnerb47aa542007-04-17 03:26:42 +0000103 if (NewST && HasName)
104 NewST->reinsertValue(&V);
Chris Lattner113f4f42002-06-25 16:13:24 +0000105 }
106 } else {
Misha Brukmanfa100532003-10-10 17:54:14 +0000107 // Just transferring between blocks in the same function, simply update the
Chris Lattner113f4f42002-06-25 16:13:24 +0000108 // parent fields in the instructions...
109 for (; first != last; ++first)
110 first->setParent(NewIP);
111 }
112}
113
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000114} // End llvm namespace
Brian Gaeke960707c2003-11-11 22:41:34 +0000115
Chris Lattner113f4f42002-06-25 16:13:24 +0000116#endif