1 //===-- ModuleUtils.h - Functions to manipulate Modules ---------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This family of functions perform manipulations on Modules.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
14 #define LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
16 #include "llvm/ADT/StringRef.h"
17 #include <utility> // for std::pair
21 template <typename T
> class ArrayRef
;
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
36 void appendToGlobalCtors(Module
&M
, Function
*F
, int Priority
,
37 Constant
*Data
= nullptr);
39 /// Same as appendToGlobalCtors(), but for global dtors.
40 void appendToGlobalDtors(Module
&M
, Function
*F
, int Priority
,
41 Constant
*Data
= nullptr);
43 FunctionCallee
declareSanitizerInitFunction(Module
&M
, StringRef InitName
,
44 ArrayRef
<Type
*> InitArgTypes
);
46 /// Creates sanitizer constructor function, and calls sanitizer's init
48 /// \return Returns pair of pointers to constructor, and init functions
50 std::pair
<Function
*, FunctionCallee
> createSanitizerCtorAndInitFunctions(
51 Module
&M
, StringRef CtorName
, StringRef InitName
,
52 ArrayRef
<Type
*> InitArgTypes
, ArrayRef
<Value
*> InitArgs
,
53 StringRef VersionCheckName
= StringRef());
55 /// Creates sanitizer constructor function lazily. If a constructor and init
56 /// function already exist, this function returns it. Otherwise it calls \c
57 /// createSanitizerCtorAndInitFunctions. The FunctionsCreatedCallback is invoked
58 /// in that case, passing the new Ctor and Init function.
60 /// \return Returns pair of pointers to constructor, and init functions
62 std::pair
<Function
*, FunctionCallee
> getOrCreateSanitizerCtorAndInitFunctions(
63 Module
&M
, StringRef CtorName
, StringRef InitName
,
64 ArrayRef
<Type
*> InitArgTypes
, ArrayRef
<Value
*> InitArgs
,
65 function_ref
<void(Function
*, FunctionCallee
)> FunctionsCreatedCallback
,
66 StringRef VersionCheckName
= StringRef());
68 // Creates and returns a sanitizer init function without argument if it doesn't
69 // exist, and adds it to the global constructors list. Otherwise it returns the
71 Function
*getOrCreateInitFunction(Module
&M
, StringRef Name
);
73 /// Rename all the anon globals in the module using a hash computed from
74 /// the list of public globals in the module.
75 bool nameUnamedGlobals(Module
&M
);
77 /// Adds global values to the llvm.used list.
78 void appendToUsed(Module
&M
, ArrayRef
<GlobalValue
*> Values
);
80 /// Adds global values to the llvm.compiler.used list.
81 void appendToCompilerUsed(Module
&M
, ArrayRef
<GlobalValue
*> Values
);
83 /// Filter out potentially dead comdat functions where other entries keep the
84 /// entire comdat group alive.
86 /// This is designed for cases where functions appear to become dead but remain
87 /// alive due to other live entries in their comdat group.
89 /// The \p DeadComdatFunctions container should only have pointers to
90 /// `Function`s which are members of a comdat group and are believed to be
93 /// After this routine finishes, the only remaining `Function`s in \p
94 /// DeadComdatFunctions are those where every member of the comdat is listed
95 /// and thus removing them is safe (provided *all* are removed).
96 void filterDeadComdatFunctions(
97 Module
&M
, SmallVectorImpl
<Function
*> &DeadComdatFunctions
);
99 /// Produce a unique identifier for this module by taking the MD5 sum of
100 /// the names of the module's strong external symbols that are not comdat
103 /// This identifier is normally guaranteed to be unique, or the program would
104 /// fail to link due to multiply defined symbols.
106 /// If the module has no strong external symbols (such a module may still have a
107 /// semantic effect if it performs global initialization), we cannot produce a
108 /// unique identifier for this module, so we return the empty string.
109 std::string
getUniqueModuleId(Module
*M
);
111 } // End llvm namespace
113 #endif // LLVM_TRANSFORMS_UTILS_MODULEUTILS_H