InferFunctionAttrs.cpp 3.3 KB

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