Lines Matching defs:F

23 // Functions are kept on binary tree. For each new function F we perform
32 // cheap to compute, and has the property that if function F == G according to
33 // the comparison function, then hash(F) == hash(G). This consistency property
174 mutable AssertingVH<Function> F;
179 FunctionNode(Function *F) : F(F), Hash(StructuralHash(*F)) {}
181 Function *getFunc() const { return F; }
184 /// Replace the reference to the function F by the function G, assuming their
187 F = G;
242 void remove(Function *F);
255 void mergeTwoFunctions(Function *F, Function *G);
276 /// Replace G with a simple tail call to bitcast(F). Also (unless
277 /// MergeFunctionsPDI holds) replace direct uses of G with bitcast(F),
279 void writeThunk(Function *F, Function *G);
281 // Replace G with an alias to F (deleting function G)
282 void writeAlias(Function *F, Function *G);
284 // Replace G with an alias to F if possible, or a thunk to F if possible.
286 bool writeThunkOrAlias(Function *F, Function *G);
288 /// Replace function F with function G in the function tree.
299 // there is exactly one mapping F -> FN for each FunctionNode FN in FnTree.
385 /// Check whether \p F has an intrinsic which references
388 static bool hasDistinctMetadataIntrinsic(const Function &F) {
389 for (const BasicBlock &BB : F) {
407 /// Check whether \p F is eligible for function merging.
408 static bool isEligibleForMerging(Function &F) {
409 return !F.isDeclaration() && !F.hasAvailableExternallyLinkage() &&
410 !hasDistinctMetadataIntrinsic(F);
455 Function *F = cast<Function>(I);
456 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage()) {
457 Changed |= insert(F);
699 static bool canCreateThunkFor(Function *F) {
700 if (F->isVarArg())
705 if (F->size() == 1) {
706 if (F->front().sizeWithoutDebug() < 2) {
707 LLVM_DEBUG(dbgs() << "canCreateThunkFor: " << F->getName()
724 // Replace G with a simple tail call to bitcast(F). Also (unless
725 // MergeFunctionsPDI holds) replace direct uses of G with bitcast(F),
729 // passed on as corresponding arguments in the call that G makes to F.
731 // call sites to point to F even when within the same translation unit.
732 void MergeFunctions::writeThunk(Function *F, Function *G) {
755 BB = BasicBlock::Create(F->getContext(), "", NewG);
762 FunctionType *FFTy = F->getFunctionType();
768 CallInst *CI = Builder.CreateCall(F, Args);
770 bool isSwiftTailCall = F->getCallingConv() == CallingConv::SwiftTail &&
774 CI->setCallingConv(F->getCallingConv());
775 CI->setAttributes(F->getAttributes());
817 static bool canCreateAliasFor(Function *F) {
818 if (!MergeFunctionsAliases || !F->hasGlobalUnnamedAddr())
822 assert(F->hasLocalLinkage() || F->hasExternalLinkage()
823 || F->hasWeakLinkage() || F->hasLinkOnceLinkage());
827 // Replace G with an alias to F (deleting function G)
828 void MergeFunctions::writeAlias(Function *F, Function *G) {
831 G->getLinkage(), "", F, G->getParent());
833 const MaybeAlign FAlign = F->getAlign();
836 F->setAlignment(std::max(FAlign.valueOrOne(), GAlign.valueOrOne()));
838 F->setAlignment(std::nullopt);
851 // Replace G with an alias to F if possible, or a thunk to F if
853 bool MergeFunctions::writeThunkOrAlias(Function *F, Function *G) {
855 writeAlias(F, G);
858 if (canCreateThunkFor(F)) {
859 writeThunk(F, G);
866 void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) {
867 if (F->isInterposable()) {
871 // create aliases for G and NewF, or because a thunk for F is profitable.
872 // F here has the same signature as NewF below, so that's what we check.
873 if (!canCreateThunkFor(F) &&
874 (!canCreateAliasFor(F) || !canCreateAliasFor(G)))
878 Function *NewF = Function::Create(F->getFunctionType(), F->getLinkage(),
879 F->getAddressSpace(), "", F->getParent());
880 NewF->copyAttributesFrom(F);
881 NewF->takeName(F);
882 NewF->IsNewDbgInfoFormat = F->IsNewDbgInfoFormat;
884 copyMetadataIfPresent(F, NewF, "type");
885 copyMetadataIfPresent(F, NewF, "kcfi_type");
886 removeUsers(F);
887 F->replaceAllUsesWith(NewF);
894 writeThunkOrAlias(F, G);
895 writeThunkOrAlias(F, NewF);
898 F->setAlignment(std::max(NewFAlign.valueOrOne(), GAlign.valueOrOne()));
900 F->setAlignment(std::nullopt);
901 F->setLinkage(GlobalValue::PrivateLinkage);
906 // call sites to point to F even when within the same translation unit.
917 G->replaceAllUsesWith(F);
919 // Redirect direct callers of G to F. (See note on MergeFunctionsPDI
921 replaceDirectCallers(G, F);
925 // If G was internal then we may have replaced all uses of G with F. If so,
934 if (writeThunkOrAlias(F, G)) {
940 /// Replace function F by function G.
943 Function *F = FN.getFunc();
944 assert(FunctionComparator(F, G, &GlobalNumbers).compare() == 0 &&
947 auto I = FNodesInTree.find(F);
948 assert(I != FNodesInTree.end() && "F should be in FNodesInTree");
952 assert(&(*IterToFNInFnTree) == &FN && "F should map to FN in FNodesInTree.");
953 // Remove F -> FN and insert G -> FN
956 // Replace F with G in FN, which is stored inside the FnTree.
961 static bool isFuncOrderCorrect(const Function *F, const Function *G) {
962 if (F->isInterposable() != G->isInterposable()) {
965 return !F->isInterposable();
967 if (F->hasLocalLinkage() != G->hasLocalLinkage()) {
970 return !F->hasLocalLinkage();
975 return F->getName() <= G->getName();
996 Function *F = OldF.getFunc();
998 NewFunction = F;
999 assert(OldF.getFunc() != F && "Must have swapped the functions.");
1012 void MergeFunctions::remove(Function *F) {
1013 auto I = FNodesInTree.find(F);
1015 LLVM_DEBUG(dbgs() << "Deferred " << F->getName() << ".\n");
1020 Deferred.emplace_back(F);