InferFunctionAttrs.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //===- InferFunctionAttrs.cpp - Infer implicit function attributes --------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
  9. #include "llvm/Analysis/TargetLibraryInfo.h"
  10. #include "llvm/IR/Function.h"
  11. #include "llvm/IR/Module.h"
  12. #include "llvm/InitializePasses.h"
  13. #include "llvm/Transforms/Utils/BuildLibCalls.h"
  14. #include "llvm/Transforms/Utils/Local.h"
  15. using namespace llvm;
  16. #define DEBUG_TYPE "inferattrs"
  17. static bool inferAllPrototypeAttributes(
  18. Module &M, function_ref<TargetLibraryInfo &(Function &)> GetTLI) {
  19. bool Changed = false;
  20. for (Function &F : M.functions())
  21. // We only infer things using the prototype and the name; we don't need
  22. // definitions. This ensures libfuncs are annotated and also allows our
  23. // CGSCC inference to avoid needing to duplicate the inference from other
  24. // attribute logic on all calls to declarations (as declarations aren't
  25. // explicitly visited by CGSCC passes in the new pass manager.)
  26. if (F.isDeclaration() && !F.hasOptNone()) {
  27. if (!F.hasFnAttribute(Attribute::NoBuiltin))
  28. Changed |= inferNonMandatoryLibFuncAttrs(F, GetTLI(F));
  29. Changed |= inferAttributesFromOthers(F);
  30. }
  31. return Changed;
  32. }
  33. PreservedAnalyses InferFunctionAttrsPass::run(Module &M,
  34. ModuleAnalysisManager &AM) {
  35. FunctionAnalysisManager &FAM =
  36. AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
  37. auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
  38. return FAM.getResult<TargetLibraryAnalysis>(F);
  39. };
  40. if (!inferAllPrototypeAttributes(M, GetTLI))
  41. // If we didn't infer anything, preserve all analyses.
  42. return PreservedAnalyses::all();
  43. // Otherwise, we may have changed fundamental function attributes, so clear
  44. // out all the passes.
  45. return PreservedAnalyses::none();
  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. void getAnalysisUsage(AnalysisUsage &AU) const override {
  55. AU.addRequired<TargetLibraryInfoWrapperPass>();
  56. }
  57. bool runOnModule(Module &M) override {
  58. if (skipModule(M))
  59. return false;
  60. auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
  61. return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
  62. };
  63. return inferAllPrototypeAttributes(M, GetTLI);
  64. }
  65. };
  66. }
  67. char InferFunctionAttrsLegacyPass::ID = 0;
  68. INITIALIZE_PASS_BEGIN(InferFunctionAttrsLegacyPass, "inferattrs",
  69. "Infer set function attributes", false, false)
  70. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  71. INITIALIZE_PASS_END(InferFunctionAttrsLegacyPass, "inferattrs",
  72. "Infer set function attributes", false, false)
  73. Pass *llvm::createInferFunctionAttrsLegacyPass() {
  74. return new InferFunctionAttrsLegacyPass();
  75. }