LLVM  8.0.1
ModuleSymbolTable.cpp
Go to the documentation of this file.
1 //===- ModuleSymbolTable.cpp - symbol table for in-memory IR --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class represents a symbol table built from in-memory IR. It provides
11 // access to GlobalValues and should only be used if such access is required
12 // (e.g. in the LTO implementation).
13 //
14 //===----------------------------------------------------------------------===//
15 
17 #include "RecordStreamer.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/GlobalAlias.h"
25 #include "llvm/IR/GlobalValue.h"
26 #include "llvm/IR/GlobalVariable.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"
40 #include "llvm/Support/Casting.h"
41 #include "llvm/Support/CodeGen.h"
44 #include "llvm/Support/SMLoc.h"
45 #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()) AsmSymbol(Name, Flags));
68  });
69 }
70 
71 static void
73  function_ref<void(RecordStreamer &)> Init) {
75  if (InlineAsm.empty())
76  return;
77 
78  std::string Err;
79  const Triple TT(M.getTargetTriple());
80  const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
81  assert(T && T->hasMCAsmParser());
82 
83  std::unique_ptr<MCRegisterInfo> MRI(T->createMCRegInfo(TT.str()));
84  if (!MRI)
85  return;
86 
87  std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(*MRI, TT.str()));
88  if (!MAI)
89  return;
90 
91  std::unique_ptr<MCSubtargetInfo> STI(
92  T->createMCSubtargetInfo(TT.str(), "", ""));
93  if (!STI)
94  return;
95 
96  std::unique_ptr<MCInstrInfo> MCII(T->createMCInstrInfo());
97  if (!MCII)
98  return;
99 
100  MCObjectFileInfo MOFI;
101  MCContext MCCtx(MAI.get(), MRI.get(), &MOFI);
102  MOFI.InitMCObjectFileInfo(TT, /*PIC*/ false, MCCtx);
103  MOFI.setSDKVersion(M.getSDKVersion());
104  RecordStreamer Streamer(MCCtx, M);
105  T->createNullTargetStreamer(Streamer);
106 
107  std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
109  SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
110  std::unique_ptr<MCAsmParser> Parser(
111  createMCAsmParser(SrcMgr, MCCtx, Streamer, *MAI));
112 
113  MCTargetOptions MCOptions;
114  std::unique_ptr<MCTargetAsmParser> TAP(
115  T->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
116  if (!TAP)
117  return;
118 
119  Parser->setTargetParser(*TAP);
120  if (Parser->Run(false))
121  return;
122 
123  Init(Streamer);
124 }
125 
127  const Module &M,
129  initializeRecordStreamer(M, [&](RecordStreamer &Streamer) {
130  Streamer.flushSymverDirectives();
131 
132  for (auto &KV : Streamer) {
133  StringRef Key = KV.first();
134  RecordStreamer::State Value = KV.second;
135  // FIXME: For now we just assume that all asm symbols are executable.
137  switch (Value) {
139  llvm_unreachable("NeverSeen should have been replaced earlier");
142  break;
144  break;
149  break;
153  break;
157  }
158  AsmSymbol(Key, BasicSymbolRef::Flags(Res));
159  }
160  });
161 }
162 
164  const Module &M, function_ref<void(StringRef, StringRef)> AsmSymver) {
165  initializeRecordStreamer(M, [&](RecordStreamer &Streamer) {
166  for (auto &KV : Streamer.symverAliases())
167  for (auto &Alias : KV.second)
168  AsmSymver(KV.first->getName(), Alias);
169  });
170 }
171 
173  if (S.is<AsmSymbol *>()) {
174  OS << S.get<AsmSymbol *>()->first;
175  return;
176  }
177 
178  auto *GV = S.get<GlobalValue *>();
179  if (GV->hasDLLImportStorageClass())
180  OS << "__imp_";
181 
182  Mang.getNameWithPrefix(OS, GV, false);
183 }
184 
186  if (S.is<AsmSymbol *>())
187  return S.get<AsmSymbol *>()->second;
188 
189  auto *GV = S.get<GlobalValue *>();
190 
192  if (GV->isDeclarationForLinker())
194  else if (GV->hasHiddenVisibility() && !GV->hasLocalLinkage())
196  if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
197  if (GVar->isConstant())
199  }
200  if (dyn_cast_or_null<Function>(GV->getBaseObject()))
202  if (isa<GlobalAlias>(GV))
204  if (GV->hasPrivateLinkage())
206  if (!GV->hasLocalLinkage())
208  if (GV->hasCommonLinkage())
210  if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
211  GV->hasExternalWeakLinkage())
213 
214  if (GV->getName().startswith("llvm."))
216  else if (auto *Var = dyn_cast<GlobalVariable>(GV)) {
217  if (Var->getSection() == "llvm.metadata")
219  }
220 
221  return Res;
222 }
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:240
uint32_t getSymbolFlags(Symbol S) const
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
SourceMgr SrcMgr
Definition: Error.cpp:24
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:117
unsigned second
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
MCAsmParser * createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &, const MCAsmInfo &, unsigned CB=0)
Create an MCAsmParser instance.
Definition: AsmParser.cpp:5932
amdgpu Simplify well known AMD library false Value Value const Twine & Name
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...
Context object for machine code objects.
Definition: MCContext.h:63
Key
PAL metadata keys.
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:152
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
std::pair< std::string, uint32_t > AsmSymbol
static void initializeRecordStreamer(const Module &M, function_ref< void(RecordStreamer &)> Init)
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...
unsigned const MachineRegisterInfo * MRI
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:42
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
unsigned first
Module.h This file contains the declarations for the Module class.
void setSDKVersion(const VersionTuple &TheSDKVersion)
Target - Wrapper for Target specific information.
void printSymbolName(raw_ostream &OS, Symbol S) const
void InitMCObjectFileInfo(const Triple &TT, bool PIC, MCContext &ctx, bool LargeCodeModel=false)
const std::string & getModuleInlineAsm() const
Get any module-scope inline assembly blocks.
Definition: Module.h:248
T get() const
Returns the value of the specified pointer type.
Definition: PointerUnion.h:135
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
iterator_range< const_symver_iterator > symverAliases()
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
int is() const
Test if the Union currently holds the type matching T.
Definition: PointerUnion.h:123
Represents a location in source code.
Definition: SMLoc.h:24
VersionTuple getSDKVersion() const
Get the build SDK version metadata.
Definition: Module.cpp:571
iterator_range< global_value_iterator > global_values()
Definition: Module.h:685
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...
Definition: PointerUnion.h:87