132#define DEBUG_TYPE "mergefunc"
134STATISTIC(NumFunctionsMerged,
"Number of functions merged");
135STATISTIC(NumThunksWritten,
"Number of thunks generated");
136STATISTIC(NumAliasesWritten,
"Number of aliases generated");
137STATISTIC(NumDoubleWeak,
"Number of new functions created");
141 cl::desc(
"How many functions in a module could be used for "
142 "MergeFunctions to pass a basic correctness check. "
143 "'0' disables this check. Works only with '-debug' key."),
163 cl::desc(
"Preserve debug info in thunk when mergefunc "
164 "transformations are made."));
169 cl::desc(
"Allow mergefunc to create aliases"));
181 Function *getFunc()
const {
return F; }
186 void replaceBy(Function *
G)
const {
195class MergeFunctions {
197 MergeFunctions() : FnTree(FunctionNodeCmp(&GlobalNumbers)) {
200 template <
typename FuncContainer>
bool run(FuncContainer &Functions);
203 SmallPtrSet<GlobalValue *, 4> &getUsed();
208 class FunctionNodeCmp {
209 GlobalNumberState* GlobalNumbers;
212 FunctionNodeCmp(GlobalNumberState* GN) : GlobalNumbers(GN) {}
214 bool operator()(
const FunctionNode &
LHS,
const FunctionNode &
RHS)
const {
216 if (
LHS.getHash() !=
RHS.getHash())
217 return LHS.getHash() <
RHS.getHash();
218 FunctionComparator FCmp(
LHS.getFunc(),
RHS.getFunc(), GlobalNumbers);
219 return FCmp.compare() < 0;
222 using FnTreeType = std::set<FunctionNode, FunctionNodeCmp>;
224 GlobalNumberState GlobalNumbers;
228 std::vector<WeakTrackingVH> Deferred;
231 SmallPtrSet<GlobalValue *, 4> Used;
236 bool doFunctionalCheck(std::vector<WeakTrackingVH> &Worklist);
241 bool insert(Function *NewFunction);
249 void removeUsers(
Value *V);
253 void replaceDirectCallers(Function *Old, Function *New);
258 void mergeTwoFunctions(Function *
F, Function *
G);
264 filterInstsUnrelatedToPDI(BasicBlock *GEntryBlock,
265 std::vector<Instruction *> &PDIUnrelatedWL,
266 std::vector<DbgVariableRecord *> &PDVRUnrelatedWL);
269 void eraseTail(Function *
G);
276 eraseInstsUnrelatedToPDI(std::vector<Instruction *> &PDIUnrelatedWL,
277 std::vector<DbgVariableRecord *> &PDVRUnrelatedWL);
282 void writeThunk(Function *
F, Function *
G);
285 void writeAlias(Function *
F, Function *
G);
290 bool writeThunkOrAliasIfNeeded(Function *
F, Function *
G);
293 void replaceFunctionInTree(
const FunctionNode &FN, Function *
G);
304 DenseMap<AssertingVH<Function>, FnTreeType::iterator> FNodesInTree;
307 DenseMap<Function *, Function *> DelToNewMap;
325 MF.getUsed().insert_range(UsedV);
332 return MF.runOnFunctions(
F);
336bool MergeFunctions::doFunctionalCheck(std::vector<WeakTrackingVH> &Worklist) {
338 unsigned TripleNumber = 0;
341 dbgs() <<
"MERGEFUNC-VERIFY: Started for first " << Max <<
" functions.\n";
344 for (std::vector<WeakTrackingVH>::iterator
I = Worklist.begin(),
346 I != E && i < Max; ++
I, ++i) {
348 for (std::vector<WeakTrackingVH>::iterator J =
I; J != E && j < Max;
357 dbgs() <<
"MERGEFUNC-VERIFY: Non-symmetric; triple: " << TripleNumber
359 dbgs() << *F1 <<
'\n' << *F2 <<
'\n';
367 for (std::vector<WeakTrackingVH>::iterator K = J; K !=
E && k < Max;
368 ++k, ++K, ++TripleNumber) {
376 bool Transitive =
true;
378 if (Res1 != 0 && Res1 == Res4) {
380 Transitive = Res3 == Res1;
381 }
else if (Res3 != 0 && Res3 == -Res4) {
383 Transitive = Res3 == Res1;
384 }
else if (Res4 != 0 && -Res3 == Res4) {
386 Transitive = Res4 == -Res1;
390 dbgs() <<
"MERGEFUNC-VERIFY: Non-transitive; triple: "
391 << TripleNumber <<
"\n";
392 dbgs() <<
"Res1, Res3, Res4: " << Res1 <<
", " << Res3 <<
", "
394 dbgs() << *F1 <<
'\n' << *F2 <<
'\n' << *F3 <<
'\n';
401 dbgs() <<
"MERGEFUNC-VERIFY: " << (
Valid ?
"Passed." :
"Failed.") <<
"\n";
432 return !
F.isDeclaration() && !
F.hasAvailableExternallyLinkage() &&
433 !
F.hasFnAttribute(Attribute::NoIPA) &&
440template <
typename FuncContainer>
bool MergeFunctions::run(FuncContainer &M) {
445 std::vector<std::pair<stable_hash, Function *>> HashedFuncs;
446 for (
auto &Func : M) {
455 auto S = HashedFuncs.begin();
456 for (
auto I = HashedFuncs.begin(), IE = HashedFuncs.end();
I != IE; ++
I) {
459 if ((
I != S && std::prev(
I)->first ==
I->first) ||
460 (std::next(
I) != IE && std::next(
I)->first ==
I->first)) {
466 std::vector<WeakTrackingVH> Worklist;
467 Deferred.swap(Worklist);
472 LLVM_DEBUG(
dbgs() <<
"size of worklist: " << Worklist.size() <<
'\n');
479 if (!
F->isDeclaration() && !
F->hasAvailableExternallyLinkage() &&
480 !
F->hasFnAttribute(Attribute::NoIPA)) {
484 LLVM_DEBUG(
dbgs() <<
"size of FnTree: " << FnTree.size() <<
'\n');
485 }
while (!Deferred.empty());
488 FNodesInTree.clear();
489 GlobalNumbers.
clear();
497 [[maybe_unused]]
bool MergeResult = this->
run(
F);
498 assert(MergeResult == !DelToNewMap.empty());
499 return this->DelToNewMap;
519void MergeFunctions::eraseInstsUnrelatedToPDI(
520 std::vector<Instruction *> &PDIUnrelatedWL,
521 std::vector<DbgVariableRecord *> &PDVRUnrelatedWL) {
523 dbgs() <<
" Erasing instructions (in reverse order of appearance in "
524 "entry block) unrelated to parameter debug info from entry "
526 while (!PDIUnrelatedWL.empty()) {
531 I->eraseFromParent();
532 PDIUnrelatedWL.pop_back();
535 while (!PDVRUnrelatedWL.empty()) {
541 PDVRUnrelatedWL.pop_back();
544 LLVM_DEBUG(
dbgs() <<
" } // Done erasing instructions unrelated to parameter "
545 "debug info from entry block. \n");
549void MergeFunctions::eraseTail(
Function *
G) {
550 std::vector<BasicBlock *> WorklistBB;
552 BB.dropAllReferences();
553 WorklistBB.push_back(&BB);
555 while (!WorklistBB.empty()) {
558 WorklistBB.pop_back();
571void MergeFunctions::filterInstsUnrelatedToPDI(
572 BasicBlock *GEntryBlock, std::vector<Instruction *> &PDIUnrelatedWL,
573 std::vector<DbgVariableRecord *> &PDVRUnrelatedWL) {
574 std::set<Instruction *> PDIRelated;
575 std::set<DbgVariableRecord *> PDVRRelated;
588 PDVRRelated.insert(DbgVal);
596 auto ExamineDbgDeclare = [&PDIRelated,
611 if (
Value *Arg =
SI->getValueOperand()) {
616 PDIRelated.insert(AI);
620 PDIRelated.insert(
SI);
624 PDVRRelated.insert(DbgDecl);
655 ExamineDbgValue(&DVR);
658 ExamineDbgDeclare(&DVR);
662 if (BI->isTerminator() && &*BI == GEntryBlock->
getTerminator()) {
666 PDIRelated.insert(&*BI);
675 <<
" Report parameter debug info related/related instructions: {\n");
677 auto IsPDIRelated = [](
auto *Rec,
auto &Container,
auto &UnrelatedCont) {
678 if (Container.find(Rec) == Container.end()) {
682 UnrelatedCont.push_back(Rec);
693 IsPDIRelated(&DVR, PDVRRelated, PDVRUnrelatedWL);
694 IsPDIRelated(&
I, PDIRelated, PDIUnrelatedWL);
704 if (
F->hasKernelCallingConv())
709 if (
F->size() == 1) {
710 if (
F->front().size() < 2) {
712 <<
" is too small to bother creating a thunk for\n");
737 std::optional<uint64_t> GEC =
G->getEntryCount();
739 std::vector<Instruction *> PDIUnrelatedWL;
740 std::vector<DbgVariableRecord *> PDVRUnrelatedWL;
744 LLVM_DEBUG(
dbgs() <<
"writeThunk: (MergeFunctionsPDI) Do not create a new "
745 "function as thunk; retain original: "
746 <<
G->getName() <<
"()\n");
747 GEntryBlock = &
G->getEntryBlock();
749 dbgs() <<
"writeThunk: (MergeFunctionsPDI) filter parameter related "
751 <<
G->getName() <<
"() {\n");
752 filterInstsUnrelatedToPDI(GEntryBlock, PDIUnrelatedWL, PDVRUnrelatedWL);
757 G->getAddressSpace(),
"",
G->getParent());
768 Args.push_back(Builder.CreateAggregateCast(&AI, FFTy->getParamType(i)));
772 CallInst *CI = Builder.CreateCall(
F, Args);
780 if (
H->getReturnType()->isVoidTy()) {
781 RI = Builder.CreateRetVoid();
783 RI = Builder.CreateRet(Builder.CreateAggregateCast(CI,
H->getReturnType()));
797 dbgs() <<
"writeThunk: (MergeFunctionsPDI) No DISubprogram for "
798 <<
G->getName() <<
"()\n");
801 eraseInstsUnrelatedToPDI(PDIUnrelatedWL, PDVRUnrelatedWL);
803 dbgs() <<
"} // End of parameter related debug info filtering for: "
804 <<
G->getName() <<
"()\n");
815 G->replaceAllUsesWith(NewG);
816 G->eraseFromParent();
829 assert(
F->hasLocalLinkage() ||
F->hasExternalLinkage()
830 ||
F->hasWeakLinkage() ||
F->hasLinkOnceLinkage());
839 G->getLinkage(),
"",
F,
G->getParent());
843 if (FAlign || GAlign)
846 F->setAlignment(std::nullopt);
848 GA->setVisibility(
G->getVisibility());
852 G->replaceAllUsesWith(GA);
853 G->eraseFromParent();
864 G->eraseFromParent();
880 return F->hasWeakODRLinkage() ||
F->hasLinkOnceODRLinkage();
884 std::optional<uint64_t> GC) {
888 F->setEntryCount(Sum);
894 std::optional<uint64_t> FEC =
F->getEntryCount();
895 std::optional<uint64_t> GEC =
G->getEntryCount();
902 "if G is ODR, F must also be ODR due to ordering");
914 F->getAddressSpace(),
"",
F->getParent());
918 F->setComdat(
nullptr);
924 F->replaceAllUsesWith(NewF);
929 replaceDirectCallers(
G,
F);
931 replaceDirectCallers(NewF,
F);
938 writeThunkOrAliasIfNeeded(
F,
G);
941 writeThunkOrAliasIfNeeded(
F, NewF);
943 if (NewFAlign || GAlign)
946 F->setAlignment(std::nullopt);
952 ++NumFunctionsMerged;
960 if (
G->hasGlobalUnnamedAddr() && !
Used.contains(
G)) {
966 G->replaceAllUsesWith(
F);
970 replaceDirectCallers(
G,
F);
979 G->eraseFromParent();
980 ++NumFunctionsMerged;
984 if (writeThunkOrAliasIfNeeded(
F,
G)) {
986 ++NumFunctionsMerged;
992void MergeFunctions::replaceFunctionInTree(
const FunctionNode &FN,
996 "The two functions must be equal");
998 auto I = FNodesInTree.find(
F);
999 assert(
I != FNodesInTree.end() &&
"F should be in FNodesInTree");
1000 assert(FNodesInTree.count(
G) == 0 &&
"FNodesInTree should not contain G");
1002 FnTreeType::iterator IterToFNInFnTree =
I->second;
1003 assert(&(*IterToFNInFnTree) == &FN &&
"F should map to FN in FNodesInTree.");
1005 FNodesInTree.erase(
I);
1006 FNodesInTree.insert({
G, IterToFNInFnTree});
1019 if (
F->isInterposable() !=
G->isInterposable()) {
1022 return !
F->isInterposable();
1025 if (
F->hasLocalLinkage() !=
G->hasLocalLinkage()) {
1028 return !
F->hasLocalLinkage();
1034 return F->getName() <=
G->getName();
1039bool MergeFunctions::insert(
Function *NewFunction) {
1040 std::pair<FnTreeType::iterator, bool>
Result =
1041 FnTree.insert(FunctionNode(NewFunction));
1044 assert(FNodesInTree.count(NewFunction) == 0);
1045 FNodesInTree.insert({NewFunction,
Result.first});
1051 const FunctionNode &OldF = *
Result.first;
1056 replaceFunctionInTree(*
Result.first, NewFunction);
1058 assert(OldF.getFunc() !=
F &&
"Must have swapped the functions.");
1063 Function *OldFunc = OldF.getFunc();
1066 <<
" == " << NewFunction->
getName() <<
'\n');
1069 mergeTwoFunctions(OldFunc, DeleteF);
1070 this->DelToNewMap.insert({DeleteF, OldFunc});
1076void MergeFunctions::remove(
Function *
F) {
1077 auto I = FNodesInTree.find(
F);
1078 if (
I != FNodesInTree.end()) {
1080 FnTree.erase(
I->second);
1083 FNodesInTree.erase(
I);
1084 Deferred.emplace_back(
F);
1090void MergeFunctions::removeUsers(
Value *V) {
1091 for (
User *U :
V->users())
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Module.h This file contains the declarations for the Module class.
This defines the Use class.
static bool canCreateAliasFor(Function *F)
static bool isEligibleForMerging(Function &F)
Check whether F is eligible for function merging.
static bool isODR(const Function *F)
Returns true if F is either weak_odr or linkonce_odr.
static cl::opt< unsigned > NumFunctionsForVerificationCheck("mergefunc-verify", cl::desc("How many functions in a module could be used for " "MergeFunctions to pass a basic correctness check. " "'0' disables this check. Works only with '-debug' key."), cl::init(0), cl::Hidden)
static bool canCreateThunkFor(Function *F)
Whether this function may be replaced by a forwarding thunk.
static void mergeEntryCountsInto(Function *F, std::optional< uint64_t > FC, std::optional< uint64_t > GC)
static cl::opt< bool > MergeFunctionsPDI("mergefunc-preserve-debug-info", cl::Hidden, cl::init(false), cl::desc("Preserve debug info in thunk when mergefunc " "transformations are made."))
static bool hasDistinctMetadataIntrinsic(const Function &F)
Check whether F has an intrinsic which references distinct metadata as an operand.
Function * asPtr(Function *Fn)
static void copyMetadataIfPresent(Function *From, Function *To, StringRef Kind)
Copy all metadata of a specific kind from one function to another.
static cl::opt< bool > MergeFunctionsAliases("mergefunc-use-aliases", cl::Hidden, cl::init(false), cl::desc("Allow mergefunc to create aliases"))
static bool isFuncOrderCorrect(const Function *F, const Function *G)
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Value handle that asserts if the Value is deleted.
LLVM Basic Block Representation.
iterator begin()
Instruction iterator methods.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
LLVM_ABI SymbolTableList< BasicBlock >::iterator eraseFromParent()
Unlink 'this' from the containing function and delete it.
InstListType::iterator iterator
Instruction iterators...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
void setCallingConv(CallingConv::ID CC)
bool isCallee(Value::const_user_iterator UI) const
Determine whether the passed iterator points to the callee operand's Use.
void setAttributes(AttributeList A)
Set the attributes for this call.
This class represents a function call, abstracting a target machine's calling convention.
void setTailCallKind(TailCallKind TCK)
LLVM_ABI DISubprogram * getSubprogram() const
Get the subprogram for this scope.
Subprogram description. Uses SubclassData1.
LLVM_ABI void eraseFromParent()
Record of a variable value-assignment, aka a non instruction representation of the dbg....
LLVM_ABI void print(raw_ostream &O, bool IsForDebug=false) const
bool isDbgDeclare() const
FunctionComparator - Compares two functions to determine whether or not they will generate machine co...
LLVM_ABI int compare()
Test whether the two functions have equivalent behaviour.
Class to represent function types.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
MaybeAlign getAlign() const
Returns the alignment of the given function.
void setEntryCount(uint64_t Count, const DenseSet< GlobalValue::GUID > *Imports=nullptr)
Set the entry count for this function.
void copyAttributesFrom(const Function *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a Function) from the ...
static LLVM_ABI GlobalAlias * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
void erase(GlobalValue *Global)
LLVM_ABI void setComdat(Comdat *C)
LLVM_ABI void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this GlobalObject.
@ PrivateLinkage
Like Internal, but omit from symbol table.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
LLVMContext & getContext() const
static LLVM_ABI DenseMap< Function *, Function * > runOnFunctions(ArrayRef< Function * > F)
static LLVM_ABI bool runOnModule(Module &M)
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
A Module instance is used to store all the information related to an LLVM module.
Class to represent pointers.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Return a value (possibly void), from a function.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
Represent a constant reference to a string, i.e.
A Use represents the edge between a Value definition and its users.
LLVM Value Representation.
LLVM_ABI void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
iterator_range< user_iterator > users()
iterator_range< use_iterator > uses()
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Value handle that is nullable, but tries to track the Value.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
@ SwiftTail
This follows the Swift calling convention in how arguments are passed but guarantees tail calls will ...
int compare(DigitsT LDigits, int16_t LScale, DigitsT RDigits, int16_t RScale)
Compare two scaled numbers.
@ Valid
The data is already valid.
initializer< Ty > init(const Ty &Val)
DXILDebugInfoMap run(Module &M)
LLVM_ABI std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
void stable_sort(R &&Range)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
uint64_t stable_hash
An opaque object representing a stable hash code.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
auto dyn_cast_or_null(const Y &Val)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
std::enable_if_t< std::is_unsigned_v< T >, T > SaturatingAdd(T X, T Y, bool *ResultOverflowed=nullptr)
Add two unsigned integers, X and Y, of type T.
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
LLVM_ABI stable_hash StructuralHash(const Function &F, bool DetailedHash=false)
Returns a hash of the function F.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
LLVM_ABI GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallVectorImpl< GlobalValue * > &Vec, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Function object to check whether the first component of a container supported by std::get (like std::...