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