Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 1 | //===- InferFunctionAttrs.cpp - Infer implicit function attributes --------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/Transforms/IPO/InferFunctionAttrs.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 10 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 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" |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Utils/BuildLibCalls.h" |
Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
| 19 | #define DEBUG_TYPE "inferattrs" |
| 20 | |
Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 21 | static bool inferAllPrototypeAttributes(Module &M, |
| 22 | const TargetLibraryInfo &TLI) { |
| 23 | bool Changed = false; |
| 24 | |
| 25 | for (Function &F : M.functions()) |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 26 | // We only infer things using the prototype and the name; we don't need |
| 27 | // definitions. |
Evandro Menezes | 85bd397 | 2019-04-04 22:40:06 +0000 | [diff] [blame] | 28 | if (F.isDeclaration() && !F.hasOptNone()) |
David Bolvansky | 7c7760d | 2018-10-16 21:18:31 +0000 | [diff] [blame] | 29 | Changed |= inferLibFuncAttributes(F, TLI); |
Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 30 | |
| 31 | return Changed; |
| 32 | } |
| 33 | |
| 34 | PreservedAnalyses InferFunctionAttrsPass::run(Module &M, |
Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 35 | ModuleAnalysisManager &AM) { |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 36 | auto &TLI = AM.getResult<TargetLibraryAnalysis>(M); |
Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 37 | |
| 38 | if (!inferAllPrototypeAttributes(M, TLI)) |
| 39 | // If we didn't infer anything, preserve all analyses. |
| 40 | return PreservedAnalyses::all(); |
| 41 | |
| 42 | // Otherwise, we may have changed fundamental function attributes, so clear |
| 43 | // out all the passes. |
| 44 | return PreservedAnalyses::none(); |
| 45 | } |
| 46 | |
| 47 | namespace { |
| 48 | struct InferFunctionAttrsLegacyPass : public ModulePass { |
| 49 | static char ID; // Pass identification, replacement for typeid |
| 50 | InferFunctionAttrsLegacyPass() : ModulePass(ID) { |
| 51 | initializeInferFunctionAttrsLegacyPassPass( |
| 52 | *PassRegistry::getPassRegistry()); |
| 53 | } |
| 54 | |
| 55 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 56 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
| 57 | } |
| 58 | |
| 59 | bool runOnModule(Module &M) override { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 60 | if (skipModule(M)) |
| 61 | return false; |
| 62 | |
Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 63 | auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
| 64 | return inferAllPrototypeAttributes(M, TLI); |
| 65 | } |
| 66 | }; |
| 67 | } |
| 68 | |
| 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) |
| 75 | |
| 76 | Pass *llvm::createInferFunctionAttrsLegacyPass() { |
| 77 | return new InferFunctionAttrsLegacyPass(); |
| 78 | } |