41 std::string getModuleId(
Module *M) {
43 bool ExportsSymbols =
false;
45 if (GV.isDeclaration() || GV.getName().startswith(
"llvm.") ||
46 !GV.hasExternalLinkage())
48 ExportsSymbols =
true;
55 for (
auto &GV : M->globals())
57 for (
auto &GA : M->aliases())
59 for (
auto &IF : M->ifuncs())
70 return (
"$" + Str).str();
77 if (!ExportGV.hasLocalLinkage())
84 std::string NewName = (ExportGV.getName() + ModuleId).str();
94 for (
auto &
F : ExportM)
96 for (
auto &GV : ExportM.globals())
98 for (
auto &GA : ExportM.aliases())
100 for (
auto &IF : ExportM.ifuncs())
111 auto ExternalizeTypeId = [&](
CallInst *CI,
unsigned ArgNo) {
113 cast<MetadataAsValue>(CI->
getArgOperand(ArgNo))->getMetadata();
115 if (isa<MDNode>(MD) && cast<MDNode>(MD)->isDistinct()) {
116 Metadata *&GlobalMD = LocalToGlobal[MD];
118 std::string NewName =
130 for (
const Use &U : TypeTestFunc->
uses()) {
131 auto CI = cast<CallInst>(U.getUser());
132 ExternalizeTypeId(CI, 1);
138 for (
const Use &U : TypeCheckedLoadFunc->
uses()) {
139 auto CI = cast<CallInst>(U.getUser());
140 ExternalizeTypeId(CI, 2);
149 for (
auto MD : MDs) {
150 auto I = LocalToGlobal.
find(MD->getOperand(1));
151 if (
I == LocalToGlobal.
end()) {
165 void simplifyExternals(
Module &M) {
199 if (ShouldKeepDefinition(&F))
207 if (ShouldKeepDefinition(&GV))
218 if (ShouldKeepDefinition(GA))
222 if (
I->getValueType()->isFunctionTy())
229 GA->getThreadLocalMode(), GA->
getType()->getAddressSpace());
231 GA->replaceAllUsesWith(GO);
232 GA->eraseFromParent();
240 std::string ModuleId = getModuleId(&M);
241 if (ModuleId.empty()) {
248 promoteTypeIds(M, ModuleId);
261 std::unique_ptr<Module> MergedM(
CloneModule(&M, VMap, IsInMergedM));
263 filterModule(&M, [&](
const GlobalValue *GV) {
return !IsInMergedM(GV); });
265 promoteInternals(*MergedM, M, ModuleId);
266 promoteInternals(M, *MergedM, ModuleId);
268 simplifyExternals(*MergedM);
275 W.writeModule(&M,
false, &Index,
278 W.writeModule(MergedM.get());
284 bool requiresSplit(
Module &M) {
298 if (requiresSplit(M))
299 return splitAndWriteThinLTOBitcode(OS, M);
306 class WriteThinLTOBitcode :
public ModulePass {
320 StringRef getPassName()
const override {
return "ThinLTO Bitcode Writer"; }
322 bool runOnModule(
Module &M)
override {
324 &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex());
325 writeThinLTOBitcode(OS, M, Index);
337 "Write ThinLTO Bitcode",
false,
true)
343 return new WriteThinLTOBitcode(Str);
void setVisibility(VisibilityTypes V)
iterator_range< use_iterator > uses()
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
VisibilityTypes getVisibility() const
A Module instance is used to store all the information related to an LLVM module. ...
static MDString * get(LLVMContext &Context, StringRef Str)
This is the interface to build a ModuleSummaryIndex for a module.
Type * getValueType() const
This class represents a function call, abstracting a target machine's calling convention.
Externally visible function.
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
Return the function type for an intrinsic.
ModuleSummaryIndex buildModuleSummaryIndex(const Module &M, std::function< BlockFrequencyInfo *(const Function &F)> GetBFICallback, ProfileSummaryInfo *PSI)
Direct function to compute a ModuleSummaryIndex from a given module.
iterator_range< global_object_iterator > global_objects()
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
A Use represents the edge between a Value definition and its users.
void setName(const Twine &Name)
Change the name of the value.
void eraseFromParent() override
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
LLVM_NODISCARD bool empty() const
global_iterator global_begin()
Class to represent function types.
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
void takeName(Value *V)
Transfer the name from V to this value.
Class to hold module path string table and global value map, and encapsulate methods for operating on...
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
write thinlto Write ThinLTO Bitcode
void deleteBody()
deleteBody - This method deletes the body of the function, and converts the linkage to external...
This is an important base class in LLVM.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static void stringifyResult(MD5Result &Result, SmallString< 32 > &Str)
Translates the bytes in Res to a hex string that is deposited into Str.
Represent the analysis usage information of a pass.
static Type * getVoidTy(LLVMContext &C)
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
std::unique_ptr< Module > CloneModule(const Module *M)
Return an exact copy of the specified module.
static void write(bool isBE, void *P, T V)
global_iterator global_end()
Iterator for intrusive lists based on ilist_node.
void initializeWriteThinLTOBitcodePass(PassRegistry &)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
void setLinkage(LinkageTypes LT)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
void setPreservesAll()
Set by analyses that do not transform their input at all.
const GlobalObject * getBaseObject() const
void eraseFromParent() override
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
PointerType * getType() const
Global values are always pointers.
void WriteBitcodeToFile(const Module *M, raw_ostream &Out, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false)
Write the specified module to the specified raw output stream.
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
FunctionType * getFunctionType() const
Returns the FunctionType for me.
void setArgOperand(unsigned i, Value *v)
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
iterator find(const KeyT &Val)
ModulePass * createWriteThinLTOBitcodePass(raw_ostream &Str)
Write ThinLTO-ready bitcode to Str.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
const std::string to_string(const T &Value)
write thinlto Write ThinLTO false
This class implements an extremely fast bulk output stream that can only output to a stream...
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=nullptr)
This header defines various interfaces for pass management in LLVM.
GlobalValue * getNamedValue(StringRef Name) const
Return the global value in the module with the specified name, of arbitrary type. ...
INITIALIZE_PASS_BEGIN(WriteThinLTOBitcode,"write-thinlto-bitcode","Write ThinLTO Bitcode", false, true) INITIALIZE_PASS_END(WriteThinLTOBitcode
LLVMContext & getContext() const
Get the global data context.
Legacy wrapper pass to provide the ModuleSummaryIndex object.