blob: 50573d8d688af190a1eebf5a05fe664862940ff3 [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.
Duncan P. N. Exon Smith37bf6782015-10-07 20:05:10 +000027template <typename ValueSubClass>
28template <typename TPtr>
29void SymbolTableListTraits<ValueSubClass>::setSymTabObject(TPtr *Dest,
30 TPtr Src) {
Chris Lattnerb47aa542007-04-17 03:26:42 +000031 // 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 Smith37bf6782015-10-07 20:05:10 +000044 ListTy &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())
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +000058 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
Duncan P. N. Exon Smith37bf6782015-10-07 20:05:10 +000063template <typename ValueSubClass>
64void SymbolTableListTraits<ValueSubClass>::addNodeToList(ValueSubClass *V) {
Craig Toppere73658d2014-04-28 04:05:08 +000065 assert(!V->getParent() && "Value already in a container!!");
Chris Lattner422cfcd2007-04-17 04:04:14 +000066 ItemParentClass *Owner = getListOwner();
67 V->setParent(Owner);
Chris Lattnerb47aa542007-04-17 03:26:42 +000068 if (V->hasName())
Duncan P. N. Exon Smithc77e92d2015-10-06 21:31:07 +000069 if (ValueSymbolTable *ST = getSymTab(Owner))
Chris Lattnerb47aa542007-04-17 03:26:42 +000070 ST->reinsertValue(V);
Chris Lattner113f4f42002-06-25 16:13:24 +000071}
72
Duncan P. N. Exon Smith37bf6782015-10-07 20:05:10 +000073template <typename ValueSubClass>
74void SymbolTableListTraits<ValueSubClass>::removeNodeFromList(
75 ValueSubClass *V) {
Craig Toppere73658d2014-04-28 04:05:08 +000076 V->setParent(nullptr);
Chris Lattnerb47aa542007-04-17 03:26:42 +000077 if (V->hasName())
Duncan P. N. Exon Smithc77e92d2015-10-06 21:31:07 +000078 if (ValueSymbolTable *ST = getSymTab(getListOwner()))
Chris Lattnerb47aa542007-04-17 03:26:42 +000079 ST->removeValueName(V->getValueName());
Chris Lattner113f4f42002-06-25 16:13:24 +000080}
81
Duncan P. N. Exon Smith37bf6782015-10-07 20:05:10 +000082template <typename ValueSubClass>
83void SymbolTableListTraits<ValueSubClass>::transferNodesFromList(
84 SymbolTableListTraits &L2, ilist_iterator<ValueSubClass> first,
85 ilist_iterator<ValueSubClass> last) {
Misha Brukmanfa100532003-10-10 17:54:14 +000086 // We only have to do work here if transferring instructions between BBs
Chris Lattner422cfcd2007-04-17 04:04:14 +000087 ItemParentClass *NewIP = getListOwner(), *OldIP = L2.getListOwner();
Chris Lattner113f4f42002-06-25 16:13:24 +000088 if (NewIP == OldIP) return; // No work to do at all...
89
Misha Brukmanfa100532003-10-10 17:54:14 +000090 // We only have to update symbol table entries if we are transferring the
Chris Lattner113f4f42002-06-25 16:13:24 +000091 // instructions to a different symtab object...
Duncan P. N. Exon Smithc77e92d2015-10-06 21:31:07 +000092 ValueSymbolTable *NewST = getSymTab(NewIP);
93 ValueSymbolTable *OldST = getSymTab(OldIP);
Chris Lattnerb47aa542007-04-17 03:26:42 +000094 if (NewST != OldST) {
Chris Lattner113f4f42002-06-25 16:13:24 +000095 for (; first != last; ++first) {
96 ValueSubClass &V = *first;
97 bool HasName = V.hasName();
Chris Lattnerb47aa542007-04-17 03:26:42 +000098 if (OldST && HasName)
99 OldST->removeValueName(V.getValueName());
Chris Lattner113f4f42002-06-25 16:13:24 +0000100 V.setParent(NewIP);
Chris Lattnerb47aa542007-04-17 03:26:42 +0000101 if (NewST && HasName)
102 NewST->reinsertValue(&V);
Chris Lattner113f4f42002-06-25 16:13:24 +0000103 }
104 } else {
Misha Brukmanfa100532003-10-10 17:54:14 +0000105 // Just transferring between blocks in the same function, simply update the
Chris Lattner113f4f42002-06-25 16:13:24 +0000106 // parent fields in the instructions...
107 for (; first != last; ++first)
108 first->setParent(NewIP);
109 }
110}
111
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000112} // End llvm namespace
Brian Gaeke960707c2003-11-11 22:41:34 +0000113
Chris Lattner113f4f42002-06-25 16:13:24 +0000114#endif