LLVM  14.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/SmallString.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/GlobalAlias.h"
24 #include "llvm/IR/GlobalValue.h"
25 #include "llvm/IR/GlobalVariable.h"
26 #include "llvm/IR/InlineAsm.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/MC/MCAsmInfo.h"
29 #include "llvm/MC/MCContext.h"
30 #include "llvm/MC/MCDirectives.h"
31 #include "llvm/MC/MCInstrInfo.h"
35 #include "llvm/MC/MCRegisterInfo.h"
37 #include "llvm/MC/MCSymbol.h"
39 #include "llvm/MC/TargetRegistry.h"
41 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/CodeGen.h"
45 #include "llvm/Support/SMLoc.h"
46 #include "llvm/Support/SourceMgr.h"
48 #include <algorithm>
49 #include <cassert>
50 #include <cstdint>
51 #include <memory>
52 #include <string>
53 
54 using namespace llvm;
55 using namespace object;
56 
58  if (FirstMod)
59  assert(FirstMod->getTargetTriple() == M->getTargetTriple());
60  else
61  FirstMod = M;
62 
63  for (GlobalValue &GV : M->global_values())
64  SymTab.push_back(&GV);
65 
66  CollectAsmSymbols(*M, [this](StringRef Name, BasicSymbolRef::Flags Flags) {
67  SymTab.push_back(new (AsmSymbols.Allocate())
68  AsmSymbol(std::string(Name), Flags));
69  });
70 }
71 
72 static void
74  function_ref<void(RecordStreamer &)> Init) {
75  StringRef InlineAsm = M.getModuleInlineAsm();
76  if (InlineAsm.empty())
77  return;
78 
79  std::string Err;
80  const Triple TT(M.getTargetTriple());
81  const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
82  assert(T && T->hasMCAsmParser());
83 
84  std::unique_ptr<MCRegisterInfo> MRI(T->createMCRegInfo(TT.str()));
85  if (!MRI)
86  return;
87 
88  MCTargetOptions MCOptions;
89  std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(*MRI, TT.str(), MCOptions));
90  if (!MAI)
91  return;
92 
93  std::unique_ptr<MCSubtargetInfo> STI(
94  T->createMCSubtargetInfo(TT.str(), "", ""));
95  if (!STI)
96  return;
97 
98  std::unique_ptr<MCInstrInfo> MCII(T->createMCInstrInfo());
99  if (!MCII)
100  return;
101 
102  std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
105 
106  MCContext MCCtx(TT, MAI.get(), MRI.get(), STI.get(), &SrcMgr);
107  std::unique_ptr<MCObjectFileInfo> MOFI(
108  T->createMCObjectFileInfo(MCCtx, /*PIC=*/false));
109  MOFI->setSDKVersion(M.getSDKVersion());
110  MCCtx.setObjectFileInfo(MOFI.get());
111  RecordStreamer Streamer(MCCtx, M);
112  T->createNullTargetStreamer(Streamer);
113 
114  std::unique_ptr<MCAsmParser> Parser(
115  createMCAsmParser(SrcMgr, MCCtx, Streamer, *MAI));
116 
117  std::unique_ptr<MCTargetAsmParser> TAP(
118  T->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
119  if (!TAP)
120  return;
121 
122  // Module-level inline asm is assumed to use At&t syntax (see
123  // AsmPrinter::doInitialization()).
124  Parser->setAssemblerDialect(InlineAsm::AD_ATT);
125 
126  Parser->setTargetParser(*TAP);
127  if (Parser->Run(false))
128  return;
129 
130  Init(Streamer);
131 }
132 
134  const Module &M,
136  initializeRecordStreamer(M, [&](RecordStreamer &Streamer) {
137  Streamer.flushSymverDirectives();
138 
139  for (auto &KV : Streamer) {
140  StringRef Key = KV.first();
141  RecordStreamer::State Value = KV.second;
142  // FIXME: For now we just assume that all asm symbols are executable.
144  switch (Value) {
146  llvm_unreachable("NeverSeen should have been replaced earlier");
149  break;
151  break;
156  break;
160  break;
164  }
166  }
167  });
168 }
169 
171  const Module &M, function_ref<void(StringRef, StringRef)> AsmSymver) {
172  initializeRecordStreamer(M, [&](RecordStreamer &Streamer) {
173  for (auto &KV : Streamer.symverAliases())
174  for (auto &Alias : KV.second)
175  AsmSymver(KV.first->getName(), Alias);
176  });
177 }
178 
180  if (S.is<AsmSymbol *>()) {
181  OS << S.get<AsmSymbol *>()->first;
182  return;
183  }
184 
185  auto *GV = S.get<GlobalValue *>();
186  if (GV->hasDLLImportStorageClass())
187  OS << "__imp_";
188 
189  Mang.getNameWithPrefix(OS, GV, false);
190 }
191 
193  if (S.is<AsmSymbol *>())
194  return S.get<AsmSymbol *>()->second;
195 
196  auto *GV = S.get<GlobalValue *>();
197 
199  if (GV->isDeclarationForLinker())
201  else if (GV->hasHiddenVisibility() && !GV->hasLocalLinkage())
203  if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
204  if (GVar->isConstant())
206  }
207  if (isa_and_nonnull<Function>(GV->getAliaseeObject()) || isa<GlobalIFunc>(GV))
209  if (isa<GlobalAlias>(GV))
211  if (GV->hasPrivateLinkage())
213  if (!GV->hasLocalLinkage())
215  if (GV->hasCommonLinkage())
217  if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
218  GV->hasExternalWeakLinkage())
220 
221  if (GV->getName().startswith("llvm."))
223  else if (auto *Var = dyn_cast<GlobalVariable>(GV)) {
224  if (Var->getSection() == "llvm.metadata")
226  }
227 
228  return Res;
229 }
MemoryBuffer.h
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
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:113
T
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
StringRef.h
llvm::object::BasicSymbolRef::SF_Common
@ SF_Common
Definition: SymbolicFile.h:112
MCDirectives.h
llvm::InlineAsm::AD_ATT
@ AD_ATT
Definition: InlineAsm.h:34
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:137
InlineAsm.h
ErrorHandling.h
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
MCObjectFileInfo.h
Module.h
llvm::RecordStreamer::State
State
Definition: RecordStreamer.h:26
llvm::object::BasicSymbolRef::Flags
Flags
Definition: SymbolicFile.h:106
STLExtras.h
llvm::RecordStreamer::DefinedGlobal
@ DefinedGlobal
Definition: RecordStreamer.h:26
llvm::object::BasicSymbolRef::SF_Weak
@ SF_Weak
Definition: SymbolicFile.h:110
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:120
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:113
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
SmallString.h
MCContext.h
llvm::SrcMgr
SourceMgr SrcMgr
Definition: Error.cpp:24
RecordStreamer.h
llvm::MCContext::setObjectFileInfo
void setObjectFileInfo(const MCObjectFileInfo *Mofi)
Definition: MCContext.h:421
MCInstrInfo.h
MCSymbol.h
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:481
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:53
llvm::ModuleSymbolTable::printSymbolName
void printSymbolName(raw_ostream &OS, Symbol S) const
Definition: ModuleSymbolTable.cpp:179
StringMap.h
llvm::ModuleSymbolTable::addModule
void addModule(Module *M)
Definition: ModuleSymbolTable.cpp:57
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: STLExtras.h:168
llvm::InlineAsm
Definition: InlineAsm.h:31
llvm::object::BasicSymbolRef::SF_Const
@ SF_Const
Definition: SymbolicFile.h:119
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:115
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:107
llvm::RecordStreamer::DefinedWeak
@ DefinedWeak
Definition: RecordStreamer.h:26
llvm::object::BasicSymbolRef::SF_Undefined
@ SF_Undefined
Definition: SymbolicFile.h:108
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:67
llvm::MCTargetOptions
Definition: MCTargetOptions.h:36
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:73
MCAsmInfo.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
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
for
this could be done in SelectionDAGISel along with other special for
Definition: README.txt:104
llvm::Init
Definition: Record.h:271
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:120
llvm::ModuleSymbolTable::getSymbolFlags
uint32_t getSymbolFlags(Symbol S) const
Definition: ModuleSymbolTable.cpp:192
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
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:170
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:141
GlobalAlias.h
CodeGen.h
llvm::object::BasicSymbolRef::SF_Global
@ SF_Global
Definition: SymbolicFile.h:109
llvm::object::BasicSymbolRef::SF_Hidden
@ SF_Hidden
Definition: SymbolicFile.h:118
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:6408
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:75
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:133