blob: d4ad1eba33c6c0960cd7687ae9639df9b5058811 [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;
Fangrui Songf78650a2018-07-30 19:41:25 +000036
Chris Lattnerb47aa542007-04-17 03:26:42 +000037 // Get the new SymTab object.
Duncan P. N. Exon Smithc77e92d2015-10-06 21:31:07 +000038 ValueSymbolTable *NewST = getSymTab(getListOwner());
Fangrui Songf78650a2018-07-30 19:41:25 +000039
Chris Lattnerb47aa542007-04-17 03:26:42 +000040 // If there is nothing to do, quick exit.
41 if (OldST == NewST) return;
Fangrui Songf78650a2018-07-30 19:41:25 +000042
Chris Lattnerb47aa542007-04-17 03:26:42 +000043 // 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;
Fangrui Songf78650a2018-07-30 19:41:25 +000046
Chris Lattnerb47aa542007-04-17 03:26:42 +000047 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 }
Fangrui Songf78650a2018-07-30 19:41:25 +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(
Duncan P. N. Exon Smith8cc24ea2016-09-03 01:22:56 +000084 SymbolTableListTraits &L2, iterator first, iterator last) {
Misha Brukmanfa100532003-10-10 17:54:14 +000085 // We only have to do work here if transferring instructions between BBs
Chris Lattner422cfcd2007-04-17 04:04:14 +000086 ItemParentClass *NewIP = getListOwner(), *OldIP = L2.getListOwner();
Duncan P. N. Exon Smithb7668d52016-08-30 18:00:45 +000087 assert(NewIP != OldIP && "Expected different list owners");
Chris Lattner113f4f42002-06-25 16:13:24 +000088
Misha Brukmanfa100532003-10-10 17:54:14 +000089 // We only have to update symbol table entries if we are transferring the
Chris Lattner113f4f42002-06-25 16:13:24 +000090 // instructions to a different symtab object...
Duncan P. N. Exon Smithc77e92d2015-10-06 21:31:07 +000091 ValueSymbolTable *NewST = getSymTab(NewIP);
92 ValueSymbolTable *OldST = getSymTab(OldIP);
Chris Lattnerb47aa542007-04-17 03:26:42 +000093 if (NewST != OldST) {
Chris Lattner113f4f42002-06-25 16:13:24 +000094 for (; first != last; ++first) {
95 ValueSubClass &V = *first;
96 bool HasName = V.hasName();
Chris Lattnerb47aa542007-04-17 03:26:42 +000097 if (OldST && HasName)
98 OldST->removeValueName(V.getValueName());
Chris Lattner113f4f42002-06-25 16:13:24 +000099 V.setParent(NewIP);
Chris Lattnerb47aa542007-04-17 03:26:42 +0000100 if (NewST && HasName)
101 NewST->reinsertValue(&V);
Chris Lattner113f4f42002-06-25 16:13:24 +0000102 }
103 } else {
Misha Brukmanfa100532003-10-10 17:54:14 +0000104 // Just transferring between blocks in the same function, simply update the
Chris Lattner113f4f42002-06-25 16:13:24 +0000105 // parent fields in the instructions...
106 for (; first != last; ++first)
107 first->setParent(NewIP);
108 }
109}
110
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000111} // End llvm namespace
Brian Gaeke960707c2003-11-11 22:41:34 +0000112
Chris Lattner113f4f42002-06-25 16:13:24 +0000113#endif