LLVM 22.0.0git
SymbolTableListTraits.h
Go to the documentation of this file.
1//===- llvm/SymbolTableListTraits.h - Traits for iplist ---------*- C++ -*-===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines a generic class that is used to implement the automatic
10// symbol table manipulation that occurs when you put (for example) a named
11// instruction into a basic block.
12//
13// The way that this is implemented is by using a special traits class with the
14// intrusive list that makes up the list of instructions in a basic block. When
15// a new element is added to the list of instructions, the traits class is
16// notified, allowing the symbol table to be updated.
17//
18// This generic class implements the traits class. It must be generic so that
19// it can work for all uses it, which include lists of instructions, basic
20// blocks, arguments, functions, global variables, etc...
21//
22//===----------------------------------------------------------------------===//
23
24#ifndef LLVM_IR_SYMBOLTABLELISTTRAITS_H
25#define LLVM_IR_SYMBOLTABLELISTTRAITS_H
26
27#include "llvm/ADT/ilist.h"
30#include <cstddef>
31
32namespace llvm {
33
34class Argument;
35class BasicBlock;
36class Function;
37class GlobalAlias;
38class GlobalIFunc;
39class GlobalVariable;
40class Instruction;
41class Module;
43
44/// Template metafunction to get the parent type for a symbol table list.
45///
46/// Implementations create a typedef called \c type so that we only need a
47/// single template parameter for the list and traits.
48template <typename NodeTy> struct SymbolTableListParentType {};
49
50#define DEFINE_SYMBOL_TABLE_PARENT_TYPE(NODE, PARENT) \
51 template <> struct SymbolTableListParentType<NODE> { using type = PARENT; };
52DEFINE_SYMBOL_TABLE_PARENT_TYPE(Instruction, BasicBlock)
53DEFINE_SYMBOL_TABLE_PARENT_TYPE(BasicBlock, Function)
54DEFINE_SYMBOL_TABLE_PARENT_TYPE(Argument, Function)
59#undef DEFINE_SYMBOL_TABLE_PARENT_TYPE
60
61template <typename NodeTy, typename... Args> class SymbolTableList;
62
63// ValueSubClass - The type of objects that I hold, e.g. Instruction.
64// ItemParentClass - The type of object that owns the list, e.g. BasicBlock.
65// OptionsT - Extra options to ilist nodes.
66//
67template <typename ValueSubClass, typename... Args>
68class SymbolTableListTraits : public ilist_alloc_traits<ValueSubClass> {
69 using ListTy = SymbolTableList<ValueSubClass, Args...>;
70 using iterator = typename simple_ilist<ValueSubClass, Args...>::iterator;
71 using ItemParentClass =
73
74public:
76
77private:
78 /// getListOwner - Return the object that owns this list. If this is a list
79 /// of instructions, it returns the BasicBlock that owns them.
80 ItemParentClass *getListOwner() {
81 size_t Offset = reinterpret_cast<size_t>(
82 &((ItemParentClass *)nullptr->*ItemParentClass::getSublistAccess(
83 static_cast<ValueSubClass *>(
84 nullptr))));
85 ListTy *Anchor = static_cast<ListTy *>(this);
86 return reinterpret_cast<ItemParentClass*>(reinterpret_cast<char*>(Anchor)-
87 Offset);
88 }
89
90 static ListTy &getList(ItemParentClass *Par) {
91 return Par->*(Par->getSublistAccess((ValueSubClass*)nullptr));
92 }
93
94 static ValueSymbolTable *getSymTab(ItemParentClass *Par) {
95 return Par ? toPtr(Par->getValueSymbolTable()) : nullptr;
96 }
97
98public:
99 void addNodeToList(ValueSubClass *V);
100 void removeNodeFromList(ValueSubClass *V);
101 void transferNodesFromList(SymbolTableListTraits &L2, iterator first,
102 iterator last);
103 // private:
104 template<typename TPtr>
105 void setSymTabObject(TPtr *, TPtr);
107 static ValueSymbolTable *toPtr(ValueSymbolTable &R) { return &R; }
108};
109
110// The SymbolTableListTraits template is explicitly instantiated for the
111// following data types, so add extern template statements to prevent implicit
112// instantiation.
118
119/// List that automatically updates parent links and symbol tables.
120///
121/// When nodes are inserted into and removed from this list, the associated
122/// symbol table will be automatically updated. Similarly, parent links get
123/// updated automatically.
124template <class T, typename... Args>
125class SymbolTableList : public iplist_impl<simple_ilist<T, Args...>,
126 SymbolTableListTraits<T, Args...>> {
127};
128
129} // end namespace llvm
130
131#endif // LLVM_IR_SYMBOLTABLELISTTRAITS_H
#define LLVM_TEMPLATE_ABI
Definition Compiler.h:214
Machine Check Debug Module
#define T
#define P(N)
static bool getSymTab(Value *V, ValueSymbolTable *&ST)
Definition Value.cpp:272
#define DEFINE_SYMBOL_TABLE_PARENT_TYPE(NODE, PARENT)
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
LLVM Basic Block Representation.
Definition BasicBlock.h:62
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static ValueSymbolTable * toPtr(ValueSymbolTable *P)
void setSymTabObject(TPtr *, TPtr)
setSymTabObject - This is called when (f.e.) the parent of a basic block changes.
void transferNodesFromList(SymbolTableListTraits &L2, iterator first, iterator last)
static ValueSymbolTable * toPtr(ValueSymbolTable &R)
void removeNodeFromList(ValueSubClass *V)
List that automatically updates parent links and symbol tables.
This class provides a symbol table of name/value pairs.
A simple intrusive list implementation.
This file defines classes to implement an intrusive doubly linked list class (i.e.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
template class LLVM_TEMPLATE_ABI SymbolTableListTraits< Function >
Definition Function.h:111
template class LLVM_TEMPLATE_ABI SymbolTableListTraits< GlobalAlias >
Definition GlobalAlias.h:30
template class LLVM_TEMPLATE_ABI SymbolTableListTraits< GlobalVariable >
template class LLVM_TEMPLATE_ABI SymbolTableListTraits< BasicBlock >
Definition BasicBlock.h:74
template class LLVM_TEMPLATE_ABI SymbolTableListTraits< GlobalIFunc >
Definition GlobalIFunc.h:36
Template metafunction to get the parent type for a symbol table list.
Use delete by default for iplist and ilist.
Definition ilist.h:41