Annotation2Metadata.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //===-- Annotation2Metadata.cpp - Add !annotation metadata. ---------------===//
  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. //
  9. // Add !annotation metadata for entries in @llvm.global.anotations, generated
  10. // using __attribute__((annotate("_name"))) on functions in Clang.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/IPO/Annotation2Metadata.h"
  14. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  15. #include "llvm/IR/Constants.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/InstIterator.h"
  18. #include "llvm/IR/Module.h"
  19. #include "llvm/InitializePasses.h"
  20. #include "llvm/Pass.h"
  21. #include "llvm/Transforms/IPO.h"
  22. using namespace llvm;
  23. #define DEBUG_TYPE "annotation2metadata"
  24. static bool convertAnnotation2Metadata(Module &M) {
  25. // Only add !annotation metadata if the corresponding remarks pass is also
  26. // enabled.
  27. if (!OptimizationRemarkEmitter::allowExtraAnalysis(M.getContext(),
  28. "annotation-remarks"))
  29. return false;
  30. auto *Annotations = M.getGlobalVariable("llvm.global.annotations");
  31. auto *C = dyn_cast_or_null<Constant>(Annotations);
  32. if (!C || C->getNumOperands() != 1)
  33. return false;
  34. C = cast<Constant>(C->getOperand(0));
  35. // Iterate over all entries in C and attach !annotation metadata to suitable
  36. // entries.
  37. for (auto &Op : C->operands()) {
  38. // Look at the operands to check if we can use the entry to generate
  39. // !annotation metadata.
  40. auto *OpC = dyn_cast<ConstantStruct>(&Op);
  41. if (!OpC || OpC->getNumOperands() != 4)
  42. continue;
  43. auto *StrC = dyn_cast<GlobalValue>(OpC->getOperand(1)->stripPointerCasts());
  44. if (!StrC)
  45. continue;
  46. auto *StrData = dyn_cast<ConstantDataSequential>(StrC->getOperand(0));
  47. if (!StrData)
  48. continue;
  49. auto *Fn = dyn_cast<Function>(OpC->getOperand(0)->stripPointerCasts());
  50. if (!Fn)
  51. continue;
  52. // Add annotation to all instructions in the function.
  53. for (auto &I : instructions(Fn))
  54. I.addAnnotationMetadata(StrData->getAsCString());
  55. }
  56. return true;
  57. }
  58. namespace {
  59. struct Annotation2MetadataLegacy : public ModulePass {
  60. static char ID;
  61. Annotation2MetadataLegacy() : ModulePass(ID) {
  62. initializeAnnotation2MetadataLegacyPass(*PassRegistry::getPassRegistry());
  63. }
  64. bool runOnModule(Module &M) override { return convertAnnotation2Metadata(M); }
  65. void getAnalysisUsage(AnalysisUsage &AU) const override {
  66. AU.setPreservesAll();
  67. }
  68. };
  69. } // end anonymous namespace
  70. char Annotation2MetadataLegacy::ID = 0;
  71. INITIALIZE_PASS_BEGIN(Annotation2MetadataLegacy, DEBUG_TYPE,
  72. "Annotation2Metadata", false, false)
  73. INITIALIZE_PASS_END(Annotation2MetadataLegacy, DEBUG_TYPE,
  74. "Annotation2Metadata", false, false)
  75. ModulePass *llvm::createAnnotation2MetadataLegacyPass() {
  76. return new Annotation2MetadataLegacy();
  77. }
  78. PreservedAnalyses Annotation2MetadataPass::run(Module &M,
  79. ModuleAnalysisManager &AM) {
  80. convertAnnotation2Metadata(M);
  81. return PreservedAnalyses::all();
  82. }