LLVM 18.0.0git
ModuleSymbolTable.cpp
Go to the documentation of this file.
1//===- ModuleSymbolTable.cpp - symbol table for in-memory IR --------------===//
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 class represents a symbol table built from in-memory IR. It provides
10// access to GlobalValues and should only be used if such access is required
11// (e.g. in the LTO implementation).
12//
13//===----------------------------------------------------------------------===//
14
16#include "RecordStreamer.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/GlobalAlias.h"
21#include "llvm/IR/GlobalValue.h"
23#include "llvm/IR/InlineAsm.h"
24#include "llvm/IR/Module.h"
25#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCContext.h"
27#include "llvm/MC/MCInstrInfo.h"
33#include "llvm/MC/MCSymbol.h"
40#include "llvm/Support/SMLoc.h"
44#include <algorithm>
45#include <cassert>
46#include <cstdint>
47#include <memory>
48#include <string>
49
50using namespace llvm;
51using namespace object;
52
54 if (FirstMod)
55 assert(FirstMod->getTargetTriple() == M->getTargetTriple());
56 else
57 FirstMod = M;
58
59 for (GlobalValue &GV : M->global_values())
60 SymTab.push_back(&GV);
61
63 SymTab.push_back(new (AsmSymbols.Allocate())
64 AsmSymbol(std::string(Name), Flags));
65 });
66}
67
68static void
71 StringRef InlineAsm = M.getModuleInlineAsm();
72 if (InlineAsm.empty())
73 return;
74
75 std::string Err;
76 const Triple TT(M.getTargetTriple());
77 const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
78 assert(T && T->hasMCAsmParser());
79
80 std::unique_ptr<MCRegisterInfo> MRI(T->createMCRegInfo(TT.str()));
81 if (!MRI)
82 return;
83
84 MCTargetOptions MCOptions;
85 std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(*MRI, TT.str(), MCOptions));
86 if (!MAI)
87 return;
88
89 std::unique_ptr<MCSubtargetInfo> STI(
90 T->createMCSubtargetInfo(TT.str(), "", ""));
91 if (!STI)
92 return;
93
94 std::unique_ptr<MCInstrInfo> MCII(T->createMCInstrInfo());
95 if (!MCII)
96 return;
97
98 std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
100 SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
101
102 MCContext MCCtx(TT, MAI.get(), MRI.get(), STI.get(), &SrcMgr);
103 std::unique_ptr<MCObjectFileInfo> MOFI(
104 T->createMCObjectFileInfo(MCCtx, /*PIC=*/false));
105 MOFI->setSDKVersion(M.getSDKVersion());
106 MCCtx.setObjectFileInfo(MOFI.get());
107 RecordStreamer Streamer(MCCtx, M);
108 T->createNullTargetStreamer(Streamer);
109
110 std::unique_ptr<MCAsmParser> Parser(
111 createMCAsmParser(SrcMgr, MCCtx, Streamer, *MAI));
112
113 std::unique_ptr<MCTargetAsmParser> TAP(
114 T->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
115 if (!TAP)
116 return;
117
118 // Module-level inline asm is assumed to use At&t syntax (see
119 // AsmPrinter::doInitialization()).
120 Parser->setAssemblerDialect(InlineAsm::AD_ATT);
121
122 Parser->setTargetParser(*TAP);
123 if (Parser->Run(false))
124 return;
125
126 Init(Streamer);
127}
128
130 const Module &M,
132 initializeRecordStreamer(M, [&](RecordStreamer &Streamer) {
133 Streamer.flushSymverDirectives();
134
135 for (auto &KV : Streamer) {
136 StringRef Key = KV.first();
137 RecordStreamer::State Value = KV.second;
138 // FIXME: For now we just assume that all asm symbols are executable.
140 switch (Value) {
142 llvm_unreachable("NeverSeen should have been replaced earlier");
145 break;
147 break;
152 break;
156 break;
160 }
162 }
163 });
164}
165
167 const Module &M, function_ref<void(StringRef, StringRef)> AsmSymver) {
168 initializeRecordStreamer(M, [&](RecordStreamer &Streamer) {
169 for (auto &KV : Streamer.symverAliases())
170 for (auto &Alias : KV.second)
171 AsmSymver(KV.first->getName(), Alias);
172 });
173}
174
176 if (isa<AsmSymbol *>(S)) {
177 OS << cast<AsmSymbol *>(S)->first;
178 return;
179 }
180
181 auto *GV = cast<GlobalValue *>(S);
182 if (GV->hasDLLImportStorageClass())
183 OS << "__imp_";
184
185 Mang.getNameWithPrefix(OS, GV, false);
186}
187
189 if (isa<AsmSymbol *>(S))
190 return cast<AsmSymbol *>(S)->second;
191
192 auto *GV = cast<GlobalValue *>(S);
193
195 if (GV->isDeclarationForLinker())
197 else if (GV->hasHiddenVisibility() && !GV->hasLocalLinkage())
199 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
200 if (GVar->isConstant())
202 }
203 if (const GlobalObject *GO = GV->getAliaseeObject())
204 if (isa<Function>(GO) || isa<GlobalIFunc>(GO))
206 if (isa<GlobalAlias>(GV))
208 if (GV->hasPrivateLinkage())
210 if (!GV->hasLocalLinkage())
212 if (GV->hasCommonLinkage())
214 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
215 GV->hasExternalWeakLinkage())
217
218 if (GV->getName().startswith("llvm."))
220 else if (auto *Var = dyn_cast<GlobalVariable>(GV)) {
221 if (Var->getSection() == "llvm.metadata")
223 }
224
225 return Res;
226}
unsigned const MachineRegisterInfo * MRI
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
std::string Name
static void initializeRecordStreamer(const Module &M, function_ref< void(RecordStreamer &)> Init)
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
const GlobalObject * getAliaseeObject() const
Definition: Globals.cpp:367
Context object for machine code objects.
Definition: MCContext.h:76
void setObjectFileInfo(const MCObjectFileInfo *Mofi)
Definition: MCContext.h:444
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:119
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
static void CollectAsmSymvers(const Module &M, function_ref< void(StringRef, StringRef)> AsmSymver)
Parse inline ASM and collect the symvers directives that are defined in the current module.
std::pair< std::string, uint32_t > AsmSymbol
void printSymbolName(raw_ostream &OS, Symbol S) const
uint32_t getSymbolFlags(Symbol S) const
static void CollectAsmSymbols(const Module &M, function_ref< void(StringRef, object::BasicSymbolRef::Flags)> AsmSymbol)
Parse inline ASM and collect the symbols that are defined or referenced in the current module.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:279
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:118
iterator_range< const_symver_iterator > symverAliases()
Represents a location in source code.
Definition: SMLoc.h:23
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:144
T * Allocate(size_t num=1)
Allocate space for an array of objects without constructing them.
Definition: Allocator.h:432
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
LLVM Value Representation.
Definition: Value.h:74
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
SourceMgr SrcMgr
Definition: Error.cpp:24
MCAsmParser * createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &, const MCAsmInfo &, unsigned CB=0)
Create an MCAsmParser instance for parsing assembly similar to gas syntax.
Definition: AsmParser.cpp:6456
static const Target * lookupTarget(StringRef Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.