LLVM  7.0.0svn
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  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:238
uint32_t getSymbolFlags(Symbol S) const
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
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:93
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:5872
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.
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:246
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
iterator_range< global_value_iterator > global_values()
Definition: Module.h:667
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...
Definition: PointerUnion.h:87