blob: 2ef299d9a2f0e9db996c9c572f910083d6f64587 [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 Carruth3a040e62015-12-27 08:41:34 +000011#include "llvm/Analysis/TargetLibraryInfo.h"
Philip Reames24667192016-01-04 22:49:23 +000012#include "llvm/Analysis/MemoryBuiltins.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000013#include "llvm/IR/Function.h"
14#include "llvm/IR/LLVMContext.h"
15#include "llvm/IR/Module.h"
16#include "llvm/Support/Debug.h"
17#include "llvm/Support/raw_ostream.h"
Ahmed Bougachab0624a22016-04-27 19:04:40 +000018#include "llvm/Transforms/Utils/BuildLibCalls.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000019using namespace llvm;
20
21#define DEBUG_TYPE "inferattrs"
22
Chandler Carruth3a040e62015-12-27 08:41:34 +000023static bool inferAllPrototypeAttributes(Module &M,
24 const TargetLibraryInfo &TLI) {
25 bool Changed = false;
26
27 for (Function &F : M.functions())
Ahmed Bougachab0624a22016-04-27 19:04:40 +000028 // We only infer things using the prototype and the name; we don't need
29 // definitions.
30 if (F.isDeclaration() && !F.hasFnAttribute((Attribute::OptimizeNone)))
31 Changed |= inferLibFuncAttributes(F, TLI);
Chandler Carruth3a040e62015-12-27 08:41:34 +000032
33 return Changed;
34}
35
36PreservedAnalyses InferFunctionAttrsPass::run(Module &M,
Sean Silvafd03ac62016-08-09 00:28:38 +000037 ModuleAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +000038 auto &TLI = AM.getResult<TargetLibraryAnalysis>(M);
Chandler Carruth3a040e62015-12-27 08:41:34 +000039
40 if (!inferAllPrototypeAttributes(M, TLI))
41 // If we didn't infer anything, preserve all analyses.
42 return PreservedAnalyses::all();
43
44 // Otherwise, we may have changed fundamental function attributes, so clear
45 // out all the passes.
46 return PreservedAnalyses::none();
47}
48
49namespace {
50struct InferFunctionAttrsLegacyPass : public ModulePass {
51 static char ID; // Pass identification, replacement for typeid
52 InferFunctionAttrsLegacyPass() : ModulePass(ID) {
53 initializeInferFunctionAttrsLegacyPassPass(
54 *PassRegistry::getPassRegistry());
55 }
56
57 void getAnalysisUsage(AnalysisUsage &AU) const override {
58 AU.addRequired<TargetLibraryInfoWrapperPass>();
59 }
60
61 bool runOnModule(Module &M) override {
Andrew Kayloraa641a52016-04-22 22:06:11 +000062 if (skipModule(M))
63 return false;
64
Chandler Carruth3a040e62015-12-27 08:41:34 +000065 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
66 return inferAllPrototypeAttributes(M, TLI);
67 }
68};
69}
70
71char InferFunctionAttrsLegacyPass::ID = 0;
72INITIALIZE_PASS_BEGIN(InferFunctionAttrsLegacyPass, "inferattrs",
73 "Infer set function attributes", false, false)
74INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
75INITIALIZE_PASS_END(InferFunctionAttrsLegacyPass, "inferattrs",
76 "Infer set function attributes", false, false)
77
78Pass *llvm::createInferFunctionAttrsLegacyPass() {
79 return new InferFunctionAttrsLegacyPass();
80}