LLVM  9.0.0svn
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/Module.h"
27 #include "llvm/MC/MCAsmInfo.h"
28 #include "llvm/MC/MCContext.h"
29 #include "llvm/MC/MCDirectives.h"
30 #include "llvm/MC/MCInstrInfo.h"
34 #include "llvm/MC/MCRegisterInfo.h"
36 #include "llvm/MC/MCSymbol.h"
39 #include "llvm/Support/Casting.h"
40 #include "llvm/Support/CodeGen.h"
43 #include "llvm/Support/SMLoc.h"
44 #include "llvm/Support/SourceMgr.h"
47 #include <algorithm>
48 #include <cassert>
49 #include <cstdint>
50 #include <memory>
51 #include <string>
52 
53 using namespace llvm;
54 using namespace object;
55 
57  if (FirstMod)
58  assert(FirstMod->getTargetTriple() == M->getTargetTriple());
59  else
60  FirstMod = M;
61 
62  for (GlobalValue &GV : M->global_values())
63  SymTab.push_back(&GV);
64 
65  CollectAsmSymbols(*M, [this](StringRef Name, BasicSymbolRef::Flags Flags) {
66  SymTab.push_back(new (AsmSymbols.Allocate()) AsmSymbol(Name, Flags));
67  });
68 }
69 
70 static void
72  function_ref<void(RecordStreamer &)> Init) {
74  if (InlineAsm.empty())
75  return;
76 
77  std::string Err;
78  const Triple TT(M.getTargetTriple());
79  const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
80  assert(T && T->hasMCAsmParser());
81 
82  std::unique_ptr<MCRegisterInfo> MRI(T->createMCRegInfo(TT.str()));
83  if (!MRI)
84  return;
85 
86  std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(*MRI, TT.str()));
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  MCObjectFileInfo MOFI;
100  MCContext MCCtx(MAI.get(), MRI.get(), &MOFI);
101  MOFI.InitMCObjectFileInfo(TT, /*PIC*/ false, MCCtx);
102  MOFI.setSDKVersion(M.getSDKVersion());
103  RecordStreamer Streamer(MCCtx, M);
104  T->createNullTargetStreamer(Streamer);
105 
106  std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
108  SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
109  std::unique_ptr<MCAsmParser> Parser(
110  createMCAsmParser(SrcMgr, MCCtx, Streamer, *MAI));
111 
112  MCTargetOptions MCOptions;
113  std::unique_ptr<MCTargetAsmParser> TAP(
114  T->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
115  if (!TAP)
116  return;
117 
118  Parser->setTargetParser(*TAP);
119  if (Parser->Run(false))
120  return;
121 
122  Init(Streamer);
123 }
124 
126  const Module &M,
128  initializeRecordStreamer(M, [&](RecordStreamer &Streamer) {
129  Streamer.flushSymverDirectives();
130 
131  for (auto &KV : Streamer) {
132  StringRef Key = KV.first();
133  RecordStreamer::State Value = KV.second;
134  // FIXME: For now we just assume that all asm symbols are executable.
136  switch (Value) {
138  llvm_unreachable("NeverSeen should have been replaced earlier");
141  break;
143  break;
148  break;
152  break;
156  }
157  AsmSymbol(Key, BasicSymbolRef::Flags(Res));
158  }
159  });
160 }
161 
163  const Module &M, function_ref<void(StringRef, StringRef)> AsmSymver) {
164  initializeRecordStreamer(M, [&](RecordStreamer &Streamer) {
165  for (auto &KV : Streamer.symverAliases())
166  for (auto &Alias : KV.second)
167  AsmSymver(KV.first->getName(), Alias);
168  });
169 }
170 
172  if (S.is<AsmSymbol *>()) {
173  OS << S.get<AsmSymbol *>()->first;
174  return;
175  }
176 
177  auto *GV = S.get<GlobalValue *>();
178  if (GV->hasDLLImportStorageClass())
179  OS << "__imp_";
180 
181  Mang.getNameWithPrefix(OS, GV, false);
182 }
183 
185  if (S.is<AsmSymbol *>())
186  return S.get<AsmSymbol *>()->second;
187 
188  auto *GV = S.get<GlobalValue *>();
189 
191  if (GV->isDeclarationForLinker())
193  else if (GV->hasHiddenVisibility() && !GV->hasLocalLinkage())
195  if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
196  if (GVar->isConstant())
198  }
199  if (dyn_cast_or_null<Function>(GV->getBaseObject()))
201  if (isa<GlobalAlias>(GV))
203  if (GV->hasPrivateLinkage())
205  if (!GV->hasLocalLinkage())
207  if (GV->hasCommonLinkage())
209  if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
210  GV->hasExternalWeakLinkage())
212 
213  if (GV->getName().startswith("llvm."))
215  else if (auto *Var = dyn_cast<GlobalVariable>(GV)) {
216  if (Var->getSection() == "llvm.metadata")
218  }
219 
220  return Res;
221 }
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:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
SourceMgr SrcMgr
Definition: Error.cpp:23
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:116
unsigned second
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
T get() const
Returns the value of the specified pointer type.
Definition: PointerUnion.h:205
MCAsmParser * createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &, const MCAsmInfo &, unsigned CB=0)
Create an MCAsmParser instance.
Definition: AsmParser.cpp:5957
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
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:62
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:151
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:41
#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:43
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)
int is() const
Test if the Union currently holds the type matching T.
Definition: PointerUnion.h:195
const std::string & getModuleInlineAsm() const
Get any module-scope inline assembly blocks.
Definition: Module.h:248
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:72
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:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Represents a location in source code.
Definition: SMLoc.h:23
VersionTuple getSDKVersion() const
Get the build SDK version metadata.
Definition: Module.cpp:573
iterator_range< global_value_iterator > global_values()
Definition: Module.h:688
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:163