blob: f399c823d6fb0bf7638fc0bd30f9bcf398352493 [file] [log] [blame]
Chris Lattner113f4f42002-06-25 16:13:24 +00001//===-- llvm/SymbolTableListTraitsImpl.h - Implementation ------*- C++ -*--===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukmanb1c93172005-04-21 23:48:37 +00006//
John Criswell29265fe2003-10-21 15:17:13 +00007//===----------------------------------------------------------------------===//
Chris Lattner113f4f42002-06-25 16:13:24 +00008//
9// This file implements the stickier parts of the SymbolTableListTraits class,
10// and is explicitly instantiated where needed to avoid defining all this code
11// in a widely used header.
12//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H
16#define LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H
Chris Lattner113f4f42002-06-25 16:13:24 +000017
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/SymbolTableListTraits.h"
19#include "llvm/IR/ValueSymbolTable.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000020
Brian Gaeke960707c2003-11-11 22:41:34 +000021namespace llvm {
22
Chris Lattnerb47aa542007-04-17 03:26:42 +000023/// setSymTabObject - This is called when (f.e.) the parent of a basic block
24/// changes. This requires us to remove all the instruction symtab entries from
25/// the current function and reinsert them into the new function.
Duncan P. N. Exon Smith37bf6782015-10-07 20:05:10 +000026template <typename ValueSubClass>
27template <typename TPtr>
28void SymbolTableListTraits<ValueSubClass>::setSymTabObject(TPtr *Dest,
29 TPtr Src) {
Chris Lattnerb47aa542007-04-17 03:26:42 +000030 // Get the old symtab and value list before doing the assignment.
Duncan P. N. Exon Smithc77e92d2015-10-06 21:31:07 +000031 ValueSymbolTable *OldST = getSymTab(getListOwner());
Chris Lattner113f4f42002-06-25 16:13:24 +000032
Chris Lattnerb47aa542007-04-17 03:26:42 +000033 // Do it.
34 *Dest = Src;
Fangrui Songf78650a2018-07-30 19:41:25 +000035
Chris Lattnerb47aa542007-04-17 03:26:42 +000036 // Get the new SymTab object.
Duncan P. N. Exon Smithc77e92d2015-10-06 21:31:07 +000037 ValueSymbolTable *NewST = getSymTab(getListOwner());
Fangrui Songf78650a2018-07-30 19:41:25 +000038
Chris Lattnerb47aa542007-04-17 03:26:42 +000039 // If there is nothing to do, quick exit.
40 if (OldST == NewST) return;
Fangrui Songf78650a2018-07-30 19:41:25 +000041
Chris Lattnerb47aa542007-04-17 03:26:42 +000042 // Move all the elements from the old symtab to the new one.
Duncan P. N. Exon Smith37bf6782015-10-07 20:05:10 +000043 ListTy &ItemList = getList(getListOwner());
Chris Lattnerb47aa542007-04-17 03:26:42 +000044 if (ItemList.empty()) return;
Fangrui Songf78650a2018-07-30 19:41:25 +000045
Chris Lattnerb47aa542007-04-17 03:26:42 +000046 if (OldST) {
47 // Remove all entries from the previous symtab.
Duncan P. N. Exon Smith55a0c432015-10-06 22:37:47 +000048 for (auto I = ItemList.begin(); I != ItemList.end(); ++I)
Chris Lattnerb47aa542007-04-17 03:26:42 +000049 if (I->hasName())
50 OldST->removeValueName(I->getValueName());
Chris Lattner113f4f42002-06-25 16:13:24 +000051 }
52
Chris Lattnerb47aa542007-04-17 03:26:42 +000053 if (NewST) {
54 // Add all of the items to the new symtab.
Duncan P. N. Exon Smith55a0c432015-10-06 22:37:47 +000055 for (auto I = ItemList.begin(); I != ItemList.end(); ++I)
Chris Lattnerb47aa542007-04-17 03:26:42 +000056 if (I->hasName())
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +000057 NewST->reinsertValue(&*I);
Chris Lattner113f4f42002-06-25 16:13:24 +000058 }
Fangrui Songf78650a2018-07-30 19:41:25 +000059
Chris Lattner113f4f42002-06-25 16:13:24 +000060}
61
Duncan P. N. Exon Smith37bf6782015-10-07 20:05:10 +000062template <typename ValueSubClass>
63void SymbolTableListTraits<ValueSubClass>::addNodeToList(ValueSubClass *V) {
Craig Toppere73658d2014-04-28 04:05:08 +000064 assert(!V->getParent() && "Value already in a container!!");
Chris Lattner422cfcd2007-04-17 04:04:14 +000065 ItemParentClass *Owner = getListOwner();
66 V->setParent(Owner);
Chris Lattnerb47aa542007-04-17 03:26:42 +000067 if (V->hasName())
Duncan P. N. Exon Smithc77e92d2015-10-06 21:31:07 +000068 if (ValueSymbolTable *ST = getSymTab(Owner))
Chris Lattnerb47aa542007-04-17 03:26:42 +000069 ST->reinsertValue(V);
Chris Lattner113f4f42002-06-25 16:13:24 +000070}
71
Duncan P. N. Exon Smith37bf6782015-10-07 20:05:10 +000072template <typename ValueSubClass>
73void SymbolTableListTraits<ValueSubClass>::removeNodeFromList(
74 ValueSubClass *V) {
Craig Toppere73658d2014-04-28 04:05:08 +000075 V->setParent(nullptr);
Chris Lattnerb47aa542007-04-17 03:26:42 +000076 if (V->hasName())
Duncan P. N. Exon Smithc77e92d2015-10-06 21:31:07 +000077 if (ValueSymbolTable *ST = getSymTab(getListOwner()))
Chris Lattnerb47aa542007-04-17 03:26:42 +000078 ST->removeValueName(V->getValueName());
Chris Lattner113f4f42002-06-25 16:13:24 +000079}
80
Duncan P. N. Exon Smith37bf6782015-10-07 20:05:10 +000081template <typename ValueSubClass>
82void SymbolTableListTraits<ValueSubClass>::transferNodesFromList(
Duncan P. N. Exon Smith8cc24ea2016-09-03 01:22:56 +000083 SymbolTableListTraits &L2, iterator first, iterator last) {
Misha Brukmanfa100532003-10-10 17:54:14 +000084 // We only have to do work here if transferring instructions between BBs
Chris Lattner422cfcd2007-04-17 04:04:14 +000085 ItemParentClass *NewIP = getListOwner(), *OldIP = L2.getListOwner();
Reid Klecknere80799e2019-01-23 22:59:52 +000086 if (NewIP == OldIP)
87 return;
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