blob: 470f97b8ba610d56a2629e95d79316b5558551f4 [file] [log] [blame]
Chandler Carruth3a040e62015-12-27 08:41:34 +00001//===- InferFunctionAttrs.cpp - Infer implicit function attributes --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000011#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000012#include "llvm/IR/Function.h"
13#include "llvm/IR/LLVMContext.h"
14#include "llvm/IR/Module.h"
15#include "llvm/Support/Debug.h"
16#include "llvm/Support/raw_ostream.h"
Ahmed Bougachab0624a22016-04-27 19:04:40 +000017#include "llvm/Transforms/Utils/BuildLibCalls.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000018using namespace llvm;
19
20#define DEBUG_TYPE "inferattrs"
21
Chandler Carruth3a040e62015-12-27 08:41:34 +000022static bool inferAllPrototypeAttributes(Module &M,
23 const TargetLibraryInfo &TLI) {
24 bool Changed = false;
25
26 for (Function &F : M.functions())
Ahmed Bougachab0624a22016-04-27 19:04:40 +000027 // We only infer things using the prototype and the name; we don't need
28 // definitions.
29 if (F.isDeclaration() && !F.hasFnAttribute((Attribute::OptimizeNone)))
30 Changed |= inferLibFuncAttributes(F, TLI);
Chandler Carruth3a040e62015-12-27 08:41:34 +000031
32 return Changed;
33}
34
35PreservedAnalyses InferFunctionAttrsPass::run(Module &M,
Sean Silvafd03ac62016-08-09 00:28:38 +000036 ModuleAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +000037 auto &TLI = AM.getResult<TargetLibraryAnalysis>(M);
Chandler Carruth3a040e62015-12-27 08:41:34 +000038
39 if (!inferAllPrototypeAttributes(M, TLI))
40 // If we didn't infer anything, preserve all analyses.
41 return PreservedAnalyses::all();
42
43 // Otherwise, we may have changed fundamental function attributes, so clear
44 // out all the passes.
45 return PreservedAnalyses::none();
46}
47
48namespace {
49struct InferFunctionAttrsLegacyPass : public ModulePass {
50 static char ID; // Pass identification, replacement for typeid
51 InferFunctionAttrsLegacyPass() : ModulePass(ID) {
52 initializeInferFunctionAttrsLegacyPassPass(
53 *PassRegistry::getPassRegistry());
54 }
55
56 void getAnalysisUsage(AnalysisUsage &AU) const override {
57 AU.addRequired<TargetLibraryInfoWrapperPass>();
58 }
59
60 bool runOnModule(Module &M) override {
Andrew Kayloraa641a52016-04-22 22:06:11 +000061 if (skipModule(M))
62 return false;
63
Chandler Carruth3a040e62015-12-27 08:41:34 +000064 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
65 return inferAllPrototypeAttributes(M, TLI);
66 }
67};
68}
69
70char InferFunctionAttrsLegacyPass::ID = 0;
71INITIALIZE_PASS_BEGIN(InferFunctionAttrsLegacyPass, "inferattrs",
72 "Infer set function attributes", false, false)
73INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
74INITIALIZE_PASS_END(InferFunctionAttrsLegacyPass, "inferattrs",
75 "Infer set function attributes", false, false)
76
77Pass *llvm::createInferFunctionAttrsLegacyPass() {
78 return new InferFunctionAttrsLegacyPass();
79}