10b57cec5SDimitry Andric //===- InferFunctionAttrs.cpp - Infer implicit function attributes --------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
100b57cec5SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
110b57cec5SDimitry Andric #include "llvm/IR/Function.h"
120b57cec5SDimitry Andric #include "llvm/IR/Module.h"
130b57cec5SDimitry Andric #include "llvm/Transforms/Utils/BuildLibCalls.h"
14fe6060f1SDimitry Andric #include "llvm/Transforms/Utils/Local.h"
150b57cec5SDimitry Andric using namespace llvm;
160b57cec5SDimitry Andric
170b57cec5SDimitry Andric #define DEBUG_TYPE "inferattrs"
180b57cec5SDimitry Andric
inferAllPrototypeAttributes(Module & M,function_ref<TargetLibraryInfo & (Function &)> GetTLI)198bcb0991SDimitry Andric static bool inferAllPrototypeAttributes(
208bcb0991SDimitry Andric Module &M, function_ref<TargetLibraryInfo &(Function &)> GetTLI) {
210b57cec5SDimitry Andric bool Changed = false;
220b57cec5SDimitry Andric
230b57cec5SDimitry Andric for (Function &F : M.functions())
240b57cec5SDimitry Andric // We only infer things using the prototype and the name; we don't need
25fe6060f1SDimitry Andric // definitions. This ensures libfuncs are annotated and also allows our
26fe6060f1SDimitry Andric // CGSCC inference to avoid needing to duplicate the inference from other
27fe6060f1SDimitry Andric // attribute logic on all calls to declarations (as declarations aren't
28fe6060f1SDimitry Andric // explicitly visited by CGSCC passes in the new pass manager.)
29fe6060f1SDimitry Andric if (F.isDeclaration() && !F.hasOptNone()) {
30fe6060f1SDimitry Andric if (!F.hasFnAttribute(Attribute::NoBuiltin))
31*81ad6265SDimitry Andric Changed |= inferNonMandatoryLibFuncAttrs(F, GetTLI(F));
32fe6060f1SDimitry Andric Changed |= inferAttributesFromOthers(F);
33fe6060f1SDimitry Andric }
340b57cec5SDimitry Andric
350b57cec5SDimitry Andric return Changed;
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric
run(Module & M,ModuleAnalysisManager & AM)380b57cec5SDimitry Andric PreservedAnalyses InferFunctionAttrsPass::run(Module &M,
390b57cec5SDimitry Andric ModuleAnalysisManager &AM) {
408bcb0991SDimitry Andric FunctionAnalysisManager &FAM =
418bcb0991SDimitry Andric AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
428bcb0991SDimitry Andric auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
438bcb0991SDimitry Andric return FAM.getResult<TargetLibraryAnalysis>(F);
448bcb0991SDimitry Andric };
450b57cec5SDimitry Andric
468bcb0991SDimitry Andric if (!inferAllPrototypeAttributes(M, GetTLI))
470b57cec5SDimitry Andric // If we didn't infer anything, preserve all analyses.
480b57cec5SDimitry Andric return PreservedAnalyses::all();
490b57cec5SDimitry Andric
500b57cec5SDimitry Andric // Otherwise, we may have changed fundamental function attributes, so clear
510b57cec5SDimitry Andric // out all the passes.
520b57cec5SDimitry Andric return PreservedAnalyses::none();
530b57cec5SDimitry Andric }
54