blob: 2a3b3d835d134e982d8af8331f45844e7846acac [file] [log] [blame]
Chris Lattner7e708292002-06-25 16:13:24 +00001//===-- llvm/SymbolTableListTraitsImpl.h - Implementation ------*- C++ -*--===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswell856ba762003-10-21 15:17:13 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner7e708292002-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
19#include "llvm/SymbolTableListTraits.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000020#include "llvm/ValueSymbolTable.h"
Chris Lattner7e708292002-06-25 16:13:24 +000021
Brian Gaeked0fde302003-11-11 22:41:34 +000022namespace llvm {
23
Chris Lattner17fcdd52007-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 Lattnerf8dfef72007-04-17 04:04:14 +000032 ValueSymbolTable *OldST = TraitsClass::getSymTab(getListOwner());
Chris Lattner7e708292002-06-25 16:13:24 +000033
Chris Lattner17fcdd52007-04-17 03:26:42 +000034 // Do it.
35 *Dest = Src;
36
37 // Get the new SymTab object.
Chris Lattnerf8dfef72007-04-17 04:04:14 +000038 ValueSymbolTable *NewST = TraitsClass::getSymTab(getListOwner());
Chris Lattner17fcdd52007-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 Lattnerf8dfef72007-04-17 04:04:14 +000044 iplist<ValueSubClass> &ItemList = TraitsClass::getList(getListOwner());
Chris Lattner17fcdd52007-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 Lattner7e708292002-06-25 16:13:24 +000053 }
54
Chris Lattner17fcdd52007-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 Lattner7e708292002-06-25 16:13:24 +000061 }
Chris Lattner17fcdd52007-04-17 03:26:42 +000062
Chris Lattner7e708292002-06-25 16:13:24 +000063}
64
Chris Lattner17fcdd52007-04-17 03:26:42 +000065template<typename ValueSubClass, typename ItemParentClass>
66void SymbolTableListTraits<ValueSubClass,ItemParentClass>
Chris Lattner7e708292002-06-25 16:13:24 +000067::addNodeToList(ValueSubClass *V) {
68 assert(V->getParent() == 0 && "Value already in a container!!");
Chris Lattnerf8dfef72007-04-17 04:04:14 +000069 ItemParentClass *Owner = getListOwner();
70 V->setParent(Owner);
Chris Lattner17fcdd52007-04-17 03:26:42 +000071 if (V->hasName())
Chris Lattnerf8dfef72007-04-17 04:04:14 +000072 if (ValueSymbolTable *ST = TraitsClass::getSymTab(Owner))
Chris Lattner17fcdd52007-04-17 03:26:42 +000073 ST->reinsertValue(V);
Chris Lattner7e708292002-06-25 16:13:24 +000074}
75
Chris Lattner17fcdd52007-04-17 03:26:42 +000076template<typename ValueSubClass, typename ItemParentClass>
77void SymbolTableListTraits<ValueSubClass,ItemParentClass>
Chris Lattner7e708292002-06-25 16:13:24 +000078::removeNodeFromList(ValueSubClass *V) {
79 V->setParent(0);
Chris Lattner17fcdd52007-04-17 03:26:42 +000080 if (V->hasName())
Chris Lattnerf8dfef72007-04-17 04:04:14 +000081 if (ValueSymbolTable *ST = TraitsClass::getSymTab(getListOwner()))
Chris Lattner17fcdd52007-04-17 03:26:42 +000082 ST->removeValueName(V->getValueName());
Chris Lattner7e708292002-06-25 16:13:24 +000083}
84
Chris Lattner17fcdd52007-04-17 03:26:42 +000085template<typename ValueSubClass, typename ItemParentClass>
86void SymbolTableListTraits<ValueSubClass,ItemParentClass>
Chris Lattner7e708292002-06-25 16:13:24 +000087::transferNodesFromList(iplist<ValueSubClass, ilist_traits<ValueSubClass> > &L2,
88 ilist_iterator<ValueSubClass> first,
89 ilist_iterator<ValueSubClass> last) {
Misha Brukman6b634522003-10-10 17:54:14 +000090 // We only have to do work here if transferring instructions between BBs
Chris Lattnerf8dfef72007-04-17 04:04:14 +000091 ItemParentClass *NewIP = getListOwner(), *OldIP = L2.getListOwner();
Chris Lattner7e708292002-06-25 16:13:24 +000092 if (NewIP == OldIP) return; // No work to do at all...
93
Misha Brukman6b634522003-10-10 17:54:14 +000094 // We only have to update symbol table entries if we are transferring the
Chris Lattner7e708292002-06-25 16:13:24 +000095 // instructions to a different symtab object...
Chris Lattnerf8dfef72007-04-17 04:04:14 +000096 ValueSymbolTable *NewST = TraitsClass::getSymTab(NewIP);
Chris Lattner17fcdd52007-04-17 03:26:42 +000097 ValueSymbolTable *OldST = TraitsClass::getSymTab(OldIP);
98 if (NewST != OldST) {
Chris Lattner7e708292002-06-25 16:13:24 +000099 for (; first != last; ++first) {
100 ValueSubClass &V = *first;
101 bool HasName = V.hasName();
Chris Lattner17fcdd52007-04-17 03:26:42 +0000102 if (OldST && HasName)
103 OldST->removeValueName(V.getValueName());
Chris Lattner7e708292002-06-25 16:13:24 +0000104 V.setParent(NewIP);
Chris Lattner17fcdd52007-04-17 03:26:42 +0000105 if (NewST && HasName)
106 NewST->reinsertValue(&V);
Chris Lattner7e708292002-06-25 16:13:24 +0000107 }
108 } else {
Misha Brukman6b634522003-10-10 17:54:14 +0000109 // Just transferring between blocks in the same function, simply update the
Chris Lattner7e708292002-06-25 16:13:24 +0000110 // parent fields in the instructions...
111 for (; first != last; ++first)
112 first->setParent(NewIP);
113 }
114}
115
Brian Gaeked0fde302003-11-11 22:41:34 +0000116} // End llvm namespace
117
Chris Lattner7e708292002-06-25 16:13:24 +0000118#endif