1//===-- ModuleUtils.h - Functions to manipulate Modules ---------*- C++ -*-===//
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 family of functions perform manipulations on Modules.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
14#define LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
15
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/MemoryBuffer.h"
19#include <utility> // for std::pair
20
21namespace llvm {
22
23template <typename T> class ArrayRef;
24class Module;
25class Function;
26class FunctionCallee;
27class GlobalValue;
28class Constant;
29class Value;
30class Type;
31
32/// Append F to the list of global ctors of module M with the given Priority.
33/// This wraps the function in the appropriate structure and stores it along
34/// side other global constructors. For details see
35/// http://llvm.org/docs/LangRef.html#intg_global_ctors
36void appendToGlobalCtors(Module &M, Function *F, int Priority,
37 Constant *Data = nullptr);
38
39/// Same as appendToGlobalCtors(), but for global dtors.
40void appendToGlobalDtors(Module &M, Function *F, int Priority,
41 Constant *Data = nullptr);
42
43FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName,
44 ArrayRef<Type *> InitArgTypes);
45
46/// Creates sanitizer constructor function.
47/// \return Returns pointer to constructor.
48Function *createSanitizerCtor(Module &M, StringRef CtorName);
49
50/// Creates sanitizer constructor function, and calls sanitizer's init
51/// function from it.
52/// \return Returns pair of pointers to constructor, and init functions
53/// respectively.
54std::pair<Function *, FunctionCallee> createSanitizerCtorAndInitFunctions(
55 Module &M, StringRef CtorName, StringRef InitName,
56 ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
57 StringRef VersionCheckName = StringRef());
58
59/// Creates sanitizer constructor function lazily. If a constructor and init
60/// function already exist, this function returns it. Otherwise it calls \c
61/// createSanitizerCtorAndInitFunctions. The FunctionsCreatedCallback is invoked
62/// in that case, passing the new Ctor and Init function.
63///
64/// \return Returns pair of pointers to constructor, and init functions
65/// respectively.
66std::pair<Function *, FunctionCallee> getOrCreateSanitizerCtorAndInitFunctions(
67 Module &M, StringRef CtorName, StringRef InitName,
68 ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
69 function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
70 StringRef VersionCheckName = StringRef());
71
72/// Rename all the anon globals in the module using a hash computed from
73/// the list of public globals in the module.
74bool nameUnamedGlobals(Module &M);
75
76/// Adds global values to the llvm.used list.
77void appendToUsed(Module &M, ArrayRef<GlobalValue *> Values);
78
79/// Adds global values to the llvm.compiler.used list.
80void appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values);
81
82/// Filter out potentially dead comdat functions where other entries keep the
83/// entire comdat group alive.
84///
85/// This is designed for cases where functions appear to become dead but remain
86/// alive due to other live entries in their comdat group.
87///
88/// The \p DeadComdatFunctions container should only have pointers to
89/// `Function`s which are members of a comdat group and are believed to be
90/// dead.
91///
92/// After this routine finishes, the only remaining `Function`s in \p
93/// DeadComdatFunctions are those where every member of the comdat is listed
94/// and thus removing them is safe (provided *all* are removed).
95void filterDeadComdatFunctions(
96 SmallVectorImpl<Function *> &DeadComdatFunctions);
97
98/// Produce a unique identifier for this module by taking the MD5 sum of
99/// the names of the module's strong external symbols that are not comdat
100/// members.
101///
102/// This identifier is normally guaranteed to be unique, or the program would
103/// fail to link due to multiply defined symbols.
104///
105/// If the module has no strong external symbols (such a module may still have a
106/// semantic effect if it performs global initialization), we cannot produce a
107/// unique identifier for this module, so we return the empty string.
108std::string getUniqueModuleId(Module *M);
109
110/// Embed the memory buffer \p Buf into the module \p M as a global using the
111/// specified section name.
112void embedBufferInModule(Module &M, MemoryBufferRef Buf, StringRef SectionName);
113
114class CallInst;
115namespace VFABI {
116/// Overwrite the Vector Function ABI variants attribute with the names provide
117/// in \p VariantMappings.
118void setVectorVariantNames(CallInst *CI,
119 const SmallVector<std::string, 8> &VariantMappings);
120} // End VFABI namespace
121} // End llvm namespace
122
123#endif // LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
124