13a040e6dSChandler Carruth //===- InferFunctionAttrs.cpp - Infer implicit function attributes --------===//
23a040e6dSChandler Carruth //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63a040e6dSChandler Carruth //
73a040e6dSChandler Carruth //===----------------------------------------------------------------------===//
83a040e6dSChandler Carruth 
93a040e6dSChandler Carruth #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
106bda14b3SChandler Carruth #include "llvm/Analysis/TargetLibraryInfo.h"
113a040e6dSChandler Carruth #include "llvm/IR/Function.h"
123a040e6dSChandler Carruth #include "llvm/IR/Module.h"
1305da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
14b0624a2cSAhmed Bougacha #include "llvm/Transforms/Utils/BuildLibCalls.h"
15dd985551SPhilip Reames #include "llvm/Transforms/Utils/Local.h"
163a040e6dSChandler Carruth using namespace llvm;
173a040e6dSChandler Carruth 
183a040e6dSChandler Carruth #define DEBUG_TYPE "inferattrs"
193a040e6dSChandler Carruth 
inferAllPrototypeAttributes(Module & M,function_ref<TargetLibraryInfo & (Function &)> GetTLI)209c27b59cSTeresa Johnson static bool inferAllPrototypeAttributes(
219c27b59cSTeresa Johnson     Module &M, function_ref<TargetLibraryInfo &(Function &)> GetTLI) {
223a040e6dSChandler Carruth   bool Changed = false;
233a040e6dSChandler Carruth 
243a040e6dSChandler Carruth   for (Function &F : M.functions())
25b0624a2cSAhmed Bougacha     // We only infer things using the prototype and the name; we don't need
26dd985551SPhilip Reames     // definitions.  This ensures libfuncs are annotated and also allows our
27dd985551SPhilip Reames     // CGSCC inference to avoid needing to duplicate the inference from other
28dd985551SPhilip Reames     // attribute logic on all calls to declarations (as declarations aren't
29dd985551SPhilip Reames     // explicitly visited by CGSCC passes in the new pass manager.)
30dd985551SPhilip Reames     if (F.isDeclaration() && !F.hasOptNone()) {
3111707435SPhilip Reames       if (!F.hasFnAttribute(Attribute::NoBuiltin))
32*304378fdSJonas Paulsson         Changed |= inferNonMandatoryLibFuncAttrs(F, GetTLI(F));
33dd985551SPhilip Reames       Changed |= inferAttributesFromOthers(F);
34dd985551SPhilip Reames     }
353a040e6dSChandler Carruth 
363a040e6dSChandler Carruth   return Changed;
373a040e6dSChandler Carruth }
383a040e6dSChandler Carruth 
run(Module & M,ModuleAnalysisManager & AM)393a040e6dSChandler Carruth PreservedAnalyses InferFunctionAttrsPass::run(Module &M,
40fd03ac6aSSean Silva                                               ModuleAnalysisManager &AM) {
419c27b59cSTeresa Johnson   FunctionAnalysisManager &FAM =
429c27b59cSTeresa Johnson       AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
439c27b59cSTeresa Johnson   auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
449c27b59cSTeresa Johnson     return FAM.getResult<TargetLibraryAnalysis>(F);
459c27b59cSTeresa Johnson   };
463a040e6dSChandler Carruth 
479c27b59cSTeresa Johnson   if (!inferAllPrototypeAttributes(M, GetTLI))
483a040e6dSChandler Carruth     // If we didn't infer anything, preserve all analyses.
493a040e6dSChandler Carruth     return PreservedAnalyses::all();
503a040e6dSChandler Carruth 
513a040e6dSChandler Carruth   // Otherwise, we may have changed fundamental function attributes, so clear
523a040e6dSChandler Carruth   // out all the passes.
533a040e6dSChandler Carruth   return PreservedAnalyses::none();
543a040e6dSChandler Carruth }
553a040e6dSChandler Carruth 
563a040e6dSChandler Carruth namespace {
573a040e6dSChandler Carruth struct InferFunctionAttrsLegacyPass : public ModulePass {
583a040e6dSChandler Carruth   static char ID; // Pass identification, replacement for typeid
InferFunctionAttrsLegacyPass__anon21b9165d0211::InferFunctionAttrsLegacyPass593a040e6dSChandler Carruth   InferFunctionAttrsLegacyPass() : ModulePass(ID) {
603a040e6dSChandler Carruth     initializeInferFunctionAttrsLegacyPassPass(
613a040e6dSChandler Carruth         *PassRegistry::getPassRegistry());
623a040e6dSChandler Carruth   }
633a040e6dSChandler Carruth 
getAnalysisUsage__anon21b9165d0211::InferFunctionAttrsLegacyPass643a040e6dSChandler Carruth   void getAnalysisUsage(AnalysisUsage &AU) const override {
653a040e6dSChandler Carruth     AU.addRequired<TargetLibraryInfoWrapperPass>();
663a040e6dSChandler Carruth   }
673a040e6dSChandler Carruth 
runOnModule__anon21b9165d0211::InferFunctionAttrsLegacyPass683a040e6dSChandler Carruth   bool runOnModule(Module &M) override {
69aa641a51SAndrew Kaylor     if (skipModule(M))
70aa641a51SAndrew Kaylor       return false;
71aa641a51SAndrew Kaylor 
729c27b59cSTeresa Johnson     auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
739c27b59cSTeresa Johnson       return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
749c27b59cSTeresa Johnson     };
759c27b59cSTeresa Johnson     return inferAllPrototypeAttributes(M, GetTLI);
763a040e6dSChandler Carruth   }
773a040e6dSChandler Carruth };
783a040e6dSChandler Carruth }
793a040e6dSChandler Carruth 
803a040e6dSChandler Carruth char InferFunctionAttrsLegacyPass::ID = 0;
813a040e6dSChandler Carruth INITIALIZE_PASS_BEGIN(InferFunctionAttrsLegacyPass, "inferattrs",
823a040e6dSChandler Carruth                       "Infer set function attributes", false, false)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)833a040e6dSChandler Carruth INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
843a040e6dSChandler Carruth INITIALIZE_PASS_END(InferFunctionAttrsLegacyPass, "inferattrs",
853a040e6dSChandler Carruth                     "Infer set function attributes", false, false)
863a040e6dSChandler Carruth 
873a040e6dSChandler Carruth Pass *llvm::createInferFunctionAttrsLegacyPass() {
883a040e6dSChandler Carruth   return new InferFunctionAttrsLegacyPass();
893a040e6dSChandler Carruth }
90