Annotation2Metadata.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 *StrGEP = dyn_cast<ConstantExpr>(OpC->getOperand(1));
  44. if (!StrGEP || StrGEP->getNumOperands() < 2)
  45. continue;
  46. auto *StrC = dyn_cast<GlobalValue>(StrGEP->getOperand(0));
  47. if (!StrC)
  48. continue;
  49. auto *StrData = dyn_cast<ConstantDataSequential>(StrC->getOperand(0));
  50. if (!StrData)
  51. continue;
  52. // Look through bitcast.
  53. auto *Bitcast = dyn_cast<ConstantExpr>(OpC->getOperand(0));
  54. if (!Bitcast || Bitcast->getOpcode() != Instruction::BitCast)
  55. continue;
  56. auto *Fn = dyn_cast<Function>(Bitcast->getOperand(0));
  57. if (!Fn)
  58. continue;
  59. // Add annotation to all instructions in the function.
  60. for (auto &I : instructions(Fn))
  61. I.addAnnotationMetadata(StrData->getAsCString());
  62. }
  63. return true;
  64. }
  65. namespace {
  66. struct Annotation2MetadataLegacy : public ModulePass {
  67. static char ID;
  68. Annotation2MetadataLegacy() : ModulePass(ID) {
  69. initializeAnnotation2MetadataLegacyPass(*PassRegistry::getPassRegistry());
  70. }
  71. bool runOnModule(Module &M) override { return convertAnnotation2Metadata(M); }
  72. void getAnalysisUsage(AnalysisUsage &AU) const override {
  73. AU.setPreservesAll();
  74. }
  75. };
  76. } // end anonymous namespace
  77. char Annotation2MetadataLegacy::ID = 0;
  78. INITIALIZE_PASS_BEGIN(Annotation2MetadataLegacy, DEBUG_TYPE,
  79. "Annotation2Metadata", false, false)
  80. INITIALIZE_PASS_END(Annotation2MetadataLegacy, DEBUG_TYPE,
  81. "Annotation2Metadata", false, false)
  82. ModulePass *llvm::createAnnotation2MetadataLegacyPass() {
  83. return new Annotation2MetadataLegacy();
  84. }
  85. PreservedAnalyses Annotation2MetadataPass::run(Module &M,
  86. ModuleAnalysisManager &AM) {
  87. convertAnnotation2Metadata(M);
  88. return PreservedAnalyses::all();
  89. }