1 //===- InferFunctionAttrs.cpp - Infer implicit function attributes --------===//
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 #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
10 #include "llvm/Analysis/TargetLibraryInfo.h"
11 #include "llvm/IR/Function.h"
12 #include "llvm/IR/LLVMContext.h"
13 #include "llvm/IR/Module.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include "llvm/Transforms/Utils/BuildLibCalls.h"
19 #define DEBUG_TYPE "inferattrs"
21 static bool inferAllPrototypeAttributes(Module
&M
,
22 const TargetLibraryInfo
&TLI
) {
25 for (Function
&F
: M
.functions())
26 // We only infer things using the prototype and the name; we don't need
28 if (F
.isDeclaration() && !F
.hasOptNone())
29 Changed
|= inferLibFuncAttributes(F
, TLI
);
34 PreservedAnalyses
InferFunctionAttrsPass::run(Module
&M
,
35 ModuleAnalysisManager
&AM
) {
36 auto &TLI
= AM
.getResult
<TargetLibraryAnalysis
>(M
);
38 if (!inferAllPrototypeAttributes(M
, TLI
))
39 // If we didn't infer anything, preserve all analyses.
40 return PreservedAnalyses::all();
42 // Otherwise, we may have changed fundamental function attributes, so clear
43 // out all the passes.
44 return PreservedAnalyses::none();
48 struct InferFunctionAttrsLegacyPass
: public ModulePass
{
49 static char ID
; // Pass identification, replacement for typeid
50 InferFunctionAttrsLegacyPass() : ModulePass(ID
) {
51 initializeInferFunctionAttrsLegacyPassPass(
52 *PassRegistry::getPassRegistry());
55 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
56 AU
.addRequired
<TargetLibraryInfoWrapperPass
>();
59 bool runOnModule(Module
&M
) override
{
63 auto &TLI
= getAnalysis
<TargetLibraryInfoWrapperPass
>().getTLI();
64 return inferAllPrototypeAttributes(M
, TLI
);
69 char InferFunctionAttrsLegacyPass::ID
= 0;
70 INITIALIZE_PASS_BEGIN(InferFunctionAttrsLegacyPass
, "inferattrs",
71 "Infer set function attributes", false, false)
72 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass
)
73 INITIALIZE_PASS_END(InferFunctionAttrsLegacyPass
, "inferattrs",
74 "Infer set function attributes", false, false)
76 Pass
*llvm::createInferFunctionAttrsLegacyPass() {
77 return new InferFunctionAttrsLegacyPass();