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/Transforms/IPO.h" 21e8d8bef9SDimitry Andric 22e8d8bef9SDimitry Andric using namespace llvm; 23e8d8bef9SDimitry Andric 24e8d8bef9SDimitry Andric #define DEBUG_TYPE "annotation2metadata" 25e8d8bef9SDimitry Andric convertAnnotation2Metadata(Module & M)26e8d8bef9SDimitry Andricstatic bool convertAnnotation2Metadata(Module &M) { 27e8d8bef9SDimitry Andric // Only add !annotation metadata if the corresponding remarks pass is also 28e8d8bef9SDimitry Andric // enabled. 29e8d8bef9SDimitry Andric if (!OptimizationRemarkEmitter::allowExtraAnalysis(M.getContext(), 30e8d8bef9SDimitry Andric "annotation-remarks")) 31e8d8bef9SDimitry Andric return false; 32e8d8bef9SDimitry Andric 33e8d8bef9SDimitry Andric auto *Annotations = M.getGlobalVariable("llvm.global.annotations"); 34e8d8bef9SDimitry Andric auto *C = dyn_cast_or_null<Constant>(Annotations); 35e8d8bef9SDimitry Andric if (!C || C->getNumOperands() != 1) 36e8d8bef9SDimitry Andric return false; 37e8d8bef9SDimitry Andric 38e8d8bef9SDimitry Andric C = cast<Constant>(C->getOperand(0)); 39e8d8bef9SDimitry Andric 40e8d8bef9SDimitry Andric // Iterate over all entries in C and attach !annotation metadata to suitable 41e8d8bef9SDimitry Andric // entries. 42e8d8bef9SDimitry Andric for (auto &Op : C->operands()) { 43e8d8bef9SDimitry Andric // Look at the operands to check if we can use the entry to generate 44e8d8bef9SDimitry Andric // !annotation metadata. 45e8d8bef9SDimitry Andric auto *OpC = dyn_cast<ConstantStruct>(&Op); 46e8d8bef9SDimitry Andric if (!OpC || OpC->getNumOperands() != 4) 47e8d8bef9SDimitry Andric continue; 48bdd1243dSDimitry Andric auto *StrC = dyn_cast<GlobalValue>(OpC->getOperand(1)->stripPointerCasts()); 49e8d8bef9SDimitry Andric if (!StrC) 50e8d8bef9SDimitry Andric continue; 51e8d8bef9SDimitry Andric auto *StrData = dyn_cast<ConstantDataSequential>(StrC->getOperand(0)); 52e8d8bef9SDimitry Andric if (!StrData) 53e8d8bef9SDimitry Andric continue; 54bdd1243dSDimitry Andric auto *Fn = dyn_cast<Function>(OpC->getOperand(0)->stripPointerCasts()); 55e8d8bef9SDimitry Andric if (!Fn) 56e8d8bef9SDimitry Andric continue; 57e8d8bef9SDimitry Andric 58e8d8bef9SDimitry Andric // Add annotation to all instructions in the function. 59e8d8bef9SDimitry Andric for (auto &I : instructions(Fn)) 60e8d8bef9SDimitry Andric I.addAnnotationMetadata(StrData->getAsCString()); 61e8d8bef9SDimitry Andric } 62e8d8bef9SDimitry Andric return true; 63e8d8bef9SDimitry Andric } 64e8d8bef9SDimitry Andric run(Module & M,ModuleAnalysisManager & AM)65e8d8bef9SDimitry AndricPreservedAnalyses Annotation2MetadataPass::run(Module &M, 66e8d8bef9SDimitry Andric ModuleAnalysisManager &AM) { 67*06c3fb27SDimitry Andric return convertAnnotation2Metadata(M) ? PreservedAnalyses::none() 68*06c3fb27SDimitry Andric : PreservedAnalyses::all(); 69e8d8bef9SDimitry Andric } 70