LLVM  16.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/StringMap.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/GlobalAlias.h"
23 #include "llvm/IR/GlobalValue.h"
24 #include "llvm/IR/GlobalVariable.h"
25 #include "llvm/IR/InlineAsm.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/MC/MCAsmInfo.h"
28 #include "llvm/MC/MCContext.h"
29 #include "llvm/MC/MCInstrInfo.h"
33 #include "llvm/MC/MCRegisterInfo.h"
35 #include "llvm/MC/MCSymbol.h"
37 #include "llvm/MC/TargetRegistry.h"
39 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/SMLoc.h"
43 #include "llvm/Support/SourceMgr.h"
45 #include <algorithm>
46 #include <cassert>
47 #include <cstdint>
48 #include <memory>
49 #include <string>
50 
51 using namespace llvm;
52 using namespace object;
53 
55  if (FirstMod)
56  assert(FirstMod->getTargetTriple() == M->getTargetTriple());
57  else
58  FirstMod = M;
59 
60  for (GlobalValue &GV : M->global_values())
61  SymTab.push_back(&GV);
62 
63  CollectAsmSymbols(*M, [this](StringRef Name, BasicSymbolRef::Flags Flags) {
64  SymTab.push_back(new (AsmSymbols.Allocate())
65  AsmSymbol(std::string(Name), Flags));
66  });
67 }
68 
69 static void
71  function_ref<void(RecordStreamer &)> Init) {
72  StringRef InlineAsm = M.getModuleInlineAsm();
73  if (InlineAsm.empty())
74  return;
75 
76  std::string Err;
77  const Triple TT(M.getTargetTriple());
78  const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
79  assert(T && T->hasMCAsmParser());
80 
81  std::unique_ptr<MCRegisterInfo> MRI(T->createMCRegInfo(TT.str()));
82  if (!MRI)
83  return;
84 
85  MCTargetOptions MCOptions;
86  std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(*MRI, TT.str(), MCOptions));
87  if (!MAI)
88  return;
89 
90  std::unique_ptr<MCSubtargetInfo> STI(
91  T->createMCSubtargetInfo(TT.str(), "", ""));
92  if (!STI)
93  return;
94 
95  std::unique_ptr<MCInstrInfo> MCII(T->createMCInstrInfo());
96  if (!MCII)
97  return;
98 
99  std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
102 
103  MCContext MCCtx(TT, MAI.get(), MRI.get(), STI.get(), &SrcMgr);
104  std::unique_ptr<MCObjectFileInfo> MOFI(
105  T->createMCObjectFileInfo(MCCtx, /*PIC=*/false));
106  MOFI->setSDKVersion(M.getSDKVersion());
107  MCCtx.setObjectFileInfo(MOFI.get());
108  RecordStreamer Streamer(MCCtx, M);
109  T->createNullTargetStreamer(Streamer);
110 
111  std::unique_ptr<MCAsmParser> Parser(
112  createMCAsmParser(SrcMgr, MCCtx, Streamer, *MAI));
113 
114  std::unique_ptr<MCTargetAsmParser> TAP(
115  T->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
116  if (!TAP)
117  return;
118 
119  // Module-level inline asm is assumed to use At&t syntax (see
120  // AsmPrinter::doInitialization()).
121  Parser->setAssemblerDialect(InlineAsm::AD_ATT);
122 
123  Parser->setTargetParser(*TAP);
124  if (Parser->Run(false))
125  return;
126 
127  Init(Streamer);
128 }
129 
131  const Module &M,
133  initializeRecordStreamer(M, [&](RecordStreamer &Streamer) {
134  Streamer.flushSymverDirectives();
135 
136  for (auto &KV : Streamer) {
137  StringRef Key = KV.first();
138  RecordStreamer::State Value = KV.second;
139  // FIXME: For now we just assume that all asm symbols are executable.
141  switch (Value) {
143  llvm_unreachable("NeverSeen should have been replaced earlier");
146  break;
148  break;
153  break;
157  break;
161  }
163  }
164  });
165 }
166 
168  const Module &M, function_ref<void(StringRef, StringRef)> AsmSymver) {
169  initializeRecordStreamer(M, [&](RecordStreamer &Streamer) {
170  for (auto &KV : Streamer.symverAliases())
171  for (auto &Alias : KV.second)
172  AsmSymver(KV.first->getName(), Alias);
173  });
174 }
175 
177  if (S.is<AsmSymbol *>()) {
178  OS << S.get<AsmSymbol *>()->first;
179  return;
180  }
181 
182  auto *GV = S.get<GlobalValue *>();
183  if (GV->hasDLLImportStorageClass())
184  OS << "__imp_";
185 
186  Mang.getNameWithPrefix(OS, GV, false);
187 }
188 
190  if (S.is<AsmSymbol *>())
191  return S.get<AsmSymbol *>()->second;
192 
193  auto *GV = S.get<GlobalValue *>();
194 
196  if (GV->isDeclarationForLinker())
198  else if (GV->hasHiddenVisibility() && !GV->hasLocalLinkage())
200  if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
201  if (GVar->isConstant())
203  }
204  if (const GlobalObject *GO = GV->getAliaseeObject())
205  if (isa<Function>(GO) || isa<GlobalIFunc>(GO))
207  if (isa<GlobalAlias>(GV))
209  if (GV->hasPrivateLinkage())
211  if (!GV->hasLocalLinkage())
213  if (GV->hasCommonLinkage())
215  if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
216  GV->hasExternalWeakLinkage())
218 
219  if (GV->getName().startswith("llvm."))
221  else if (auto *Var = dyn_cast<GlobalVariable>(GV)) {
222  if (Var->getSection() == "llvm.metadata")
224  }
225 
226  return Res;
227 }
MemoryBuffer.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::ModuleSymbolTable::AsmSymbol
std::pair< std::string, uint32_t > AsmSymbol
Definition: ModuleSymbolTable.h:35
MCTargetOptions.h
llvm::object::BasicSymbolRef::SF_Indirect
@ SF_Indirect
Definition: SymbolicFile.h:115
T
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:76
StringRef.h
llvm::object::BasicSymbolRef::SF_Common
@ SF_Common
Definition: SymbolicFile.h:114
llvm::InlineAsm::AD_ATT
@ AD_ATT
Definition: InlineAsm.h:35
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:145
InlineAsm.h
ErrorHandling.h
llvm::GlobalVariable
Definition: GlobalVariable.h:39
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
MCObjectFileInfo.h
Module.h
llvm::RecordStreamer::State
State
Definition: RecordStreamer.h:26
llvm::object::BasicSymbolRef::Flags
Flags
Definition: SymbolicFile.h:108
STLExtras.h
llvm::RecordStreamer::DefinedGlobal
@ DefinedGlobal
Definition: RecordStreamer.h:26
llvm::object::BasicSymbolRef::SF_Weak
@ SF_Weak
Definition: SymbolicFile.h:112
SymbolicFile.h
llvm::TargetRegistry::lookupTarget
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
Definition: TargetRegistry.cpp:62
MCAsmParser.h
MCTargetAsmParser.h
llvm::object::BasicSymbolRef::SF_Executable
@ SF_Executable
Definition: SymbolicFile.h:122
llvm::RecordStreamer::Defined
@ Defined
Definition: RecordStreamer.h:26
GlobalValue.h
llvm::MemoryBuffer::getMemBuffer
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
Definition: MemoryBuffer.cpp:115
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
llvm::GlobalObject
Definition: GlobalObject.h:27
MCContext.h
llvm::SrcMgr
SourceMgr SrcMgr
Definition: Error.cpp:24
RecordStreamer.h
llvm::MCContext::setObjectFileInfo
void setObjectFileInfo(const MCObjectFileInfo *Mofi)
Definition: MCContext.h:445
MCInstrInfo.h
MCSymbol.h
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:486
MCSubtargetInfo.h
SMLoc.h
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
llvm::ModuleSymbolTable::printSymbolName
void printSymbolName(raw_ostream &OS, Symbol S) const
Definition: ModuleSymbolTable.cpp:176
llvm::ModuleSymbolTable::addModule
void addModule(Module *M)
Definition: ModuleSymbolTable.cpp:54
SourceMgr.h
llvm::RecordStreamer::symverAliases
iterator_range< const_symver_iterator > symverAliases()
Definition: RecordStreamer.cpp:133
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
llvm::InlineAsm
Definition: InlineAsm.h:32
llvm::object::BasicSymbolRef::SF_Const
@ SF_Const
Definition: SymbolicFile.h:121
llvm::GlobalValue
Definition: GlobalValue.h:44
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
llvm::object::BasicSymbolRef::SF_FormatSpecific
@ SF_FormatSpecific
Definition: SymbolicFile.h:117
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::object::BasicSymbolRef::SF_None
@ SF_None
Definition: SymbolicFile.h:109
llvm::RecordStreamer::DefinedWeak
@ DefinedWeak
Definition: RecordStreamer.h:26
llvm::object::BasicSymbolRef::SF_Undefined
@ SF_Undefined
Definition: SymbolicFile.h:110
MCRegisterInfo.h
llvm::RecordStreamer
Definition: RecordStreamer.h:24
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::MCTargetOptions
Definition: MCTargetOptions.h:37
Triple.h
ModuleSymbolTable.h
llvm::RecordStreamer::UndefinedWeak
@ UndefinedWeak
Definition: RecordStreamer.h:27
initializeRecordStreamer
static void initializeRecordStreamer(const Module &M, function_ref< void(RecordStreamer &)> Init)
Definition: ModuleSymbolTable.cpp:70
MCAsmInfo.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::SourceMgr
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
uint32_t
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::GlobalValue::getAliaseeObject
const GlobalObject * getAliaseeObject() const
Definition: Globals.cpp:365
for
this could be done in SelectionDAGISel along with other special for
Definition: README.txt:104
llvm::Init
Definition: Record.h:281
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
llvm::PointerUnion
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:112
llvm::ModuleSymbolTable::getSymbolFlags
uint32_t getSymbolFlags(Symbol S) const
Definition: ModuleSymbolTable.cpp:189
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::ModuleSymbolTable::CollectAsmSymvers
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.
Definition: ModuleSymbolTable.cpp:167
GlobalVariable.h
Casting.h
Function.h
llvm::RecordStreamer::Global
@ Global
Definition: RecordStreamer.h:26
llvm::SourceMgr::AddNewSourceBuffer
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:144
GlobalAlias.h
llvm::object::BasicSymbolRef::SF_Global
@ SF_Global
Definition: SymbolicFile.h:111
llvm::object::BasicSymbolRef::SF_Hidden
@ SF_Hidden
Definition: SymbolicFile.h:120
llvm::RecordStreamer::Used
@ Used
Definition: RecordStreamer.h:26
llvm::createMCAsmParser
MCAsmParser * createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &, const MCAsmInfo &, unsigned CB=0)
Create an MCAsmParser instance for parsing assembly similar to gas syntax.
Definition: AsmParser.cpp:6437
StringMap.h
raw_ostream.h
llvm::RecordStreamer::NeverSeen
@ NeverSeen
Definition: RecordStreamer.h:26
llvm::RecordStreamer::flushSymverDirectives
void flushSymverDirectives()
Definition: RecordStreamer.cpp:137
TargetRegistry.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::ModuleSymbolTable::CollectAsmSymbols
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.
Definition: ModuleSymbolTable.cpp:130