blob: 5a383eee56c5dfc2ab0a1a91e78aa7d443d9e58a [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
16#ifndef LLVM_SYMBOLTABLELISTTRAITS_IMPL_H
17#define LLVM_SYMBOLTABLELISTTRAITS_IMPL_H
18
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.
Chris Lattner422cfcd2007-04-17 04:04:14 +000032 ValueSymbolTable *OldST = TraitsClass::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.
Chris Lattner422cfcd2007-04-17 04:04:14 +000038 ValueSymbolTable *NewST = TraitsClass::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.
Chris Lattner422cfcd2007-04-17 04:04:14 +000044 iplist<ValueSubClass> &ItemList = TraitsClass::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.
49 for (typename iplist<ValueSubClass>::iterator I = ItemList.begin();
50 I != ItemList.end(); ++I)
51 if (I->hasName())
52 OldST->removeValueName(I->getValueName());
Chris Lattner113f4f42002-06-25 16:13:24 +000053 }
54
Chris Lattnerb47aa542007-04-17 03:26:42 +000055 if (NewST) {
56 // Add all of the items to the new symtab.
57 for (typename iplist<ValueSubClass>::iterator I = ItemList.begin();
58 I != ItemList.end(); ++I)
59 if (I->hasName())
60 NewST->reinsertValue(I);
Chris Lattner113f4f42002-06-25 16:13:24 +000061 }
Chris Lattnerb47aa542007-04-17 03:26:42 +000062
Chris Lattner113f4f42002-06-25 16:13:24 +000063}
64
Chris Lattnerb47aa542007-04-17 03:26:42 +000065template<typename ValueSubClass, typename ItemParentClass>
66void SymbolTableListTraits<ValueSubClass,ItemParentClass>
Chris Lattner113f4f42002-06-25 16:13:24 +000067::addNodeToList(ValueSubClass *V) {
68 assert(V->getParent() == 0 && "Value already in a container!!");
Chris Lattner422cfcd2007-04-17 04:04:14 +000069 ItemParentClass *Owner = getListOwner();
70 V->setParent(Owner);
Chris Lattnerb47aa542007-04-17 03:26:42 +000071 if (V->hasName())
Chris Lattner422cfcd2007-04-17 04:04:14 +000072 if (ValueSymbolTable *ST = TraitsClass::getSymTab(Owner))
Chris Lattnerb47aa542007-04-17 03:26:42 +000073 ST->reinsertValue(V);
Chris Lattner113f4f42002-06-25 16:13:24 +000074}
75
Chris Lattnerb47aa542007-04-17 03:26:42 +000076template<typename ValueSubClass, typename ItemParentClass>
77void SymbolTableListTraits<ValueSubClass,ItemParentClass>
Chris Lattner113f4f42002-06-25 16:13:24 +000078::removeNodeFromList(ValueSubClass *V) {
79 V->setParent(0);
Chris Lattnerb47aa542007-04-17 03:26:42 +000080 if (V->hasName())
Chris Lattner422cfcd2007-04-17 04:04:14 +000081 if (ValueSymbolTable *ST = TraitsClass::getSymTab(getListOwner()))
Chris Lattnerb47aa542007-04-17 03:26:42 +000082 ST->removeValueName(V->getValueName());
Chris Lattner113f4f42002-06-25 16:13:24 +000083}
84
Chris Lattnerb47aa542007-04-17 03:26:42 +000085template<typename ValueSubClass, typename ItemParentClass>
86void SymbolTableListTraits<ValueSubClass,ItemParentClass>
Dan Gohman804c95d2008-07-28 21:51:04 +000087::transferNodesFromList(ilist_traits<ValueSubClass> &L2,
Chris Lattner113f4f42002-06-25 16:13:24 +000088 ilist_iterator<ValueSubClass> first,
89 ilist_iterator<ValueSubClass> last) {
Misha Brukmanfa100532003-10-10 17:54:14 +000090 // We only have to do work here if transferring instructions between BBs
Chris Lattner422cfcd2007-04-17 04:04:14 +000091 ItemParentClass *NewIP = getListOwner(), *OldIP = L2.getListOwner();
Chris Lattner113f4f42002-06-25 16:13:24 +000092 if (NewIP == OldIP) return; // No work to do at all...
93
Misha Brukmanfa100532003-10-10 17:54:14 +000094 // We only have to update symbol table entries if we are transferring the
Chris Lattner113f4f42002-06-25 16:13:24 +000095 // instructions to a different symtab object...
Chris Lattner422cfcd2007-04-17 04:04:14 +000096 ValueSymbolTable *NewST = TraitsClass::getSymTab(NewIP);
Chris Lattnerb47aa542007-04-17 03:26:42 +000097 ValueSymbolTable *OldST = TraitsClass::getSymTab(OldIP);
98 if (NewST != OldST) {
Chris Lattner113f4f42002-06-25 16:13:24 +000099 for (; first != last; ++first) {
100 ValueSubClass &V = *first;
101 bool HasName = V.hasName();
Chris Lattnerb47aa542007-04-17 03:26:42 +0000102 if (OldST && HasName)
103 OldST->removeValueName(V.getValueName());
Chris Lattner113f4f42002-06-25 16:13:24 +0000104 V.setParent(NewIP);
Chris Lattnerb47aa542007-04-17 03:26:42 +0000105 if (NewST && HasName)
106 NewST->reinsertValue(&V);
Chris Lattner113f4f42002-06-25 16:13:24 +0000107 }
108 } else {
Misha Brukmanfa100532003-10-10 17:54:14 +0000109 // Just transferring between blocks in the same function, simply update the
Chris Lattner113f4f42002-06-25 16:13:24 +0000110 // parent fields in the instructions...
111 for (; first != last; ++first)
112 first->setParent(NewIP);
113 }
114}
115
Brian Gaeke960707c2003-11-11 22:41:34 +0000116} // End llvm namespace
117
Chris Lattner113f4f42002-06-25 16:13:24 +0000118#endif