Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/IPO/Annotation2Metadata.cpp
35266 views
//===-- Annotation2Metadata.cpp - Add !annotation metadata. ---------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Add !annotation metadata for entries in @llvm.global.anotations, generated9// using __attribute__((annotate("_name"))) on functions in Clang.10//11//===----------------------------------------------------------------------===//1213#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/Transforms/IPO.h"2021using namespace llvm;2223#define DEBUG_TYPE "annotation2metadata"2425static bool convertAnnotation2Metadata(Module &M) {26// Only add !annotation metadata if the corresponding remarks pass is also27// enabled.28if (!OptimizationRemarkEmitter::allowExtraAnalysis(M.getContext(),29"annotation-remarks"))30return false;3132auto *Annotations = M.getGlobalVariable("llvm.global.annotations");33auto *C = dyn_cast_or_null<Constant>(Annotations);34if (!C || C->getNumOperands() != 1)35return false;3637C = cast<Constant>(C->getOperand(0));3839// Iterate over all entries in C and attach !annotation metadata to suitable40// entries.41for (auto &Op : C->operands()) {42// Look at the operands to check if we can use the entry to generate43// !annotation metadata.44auto *OpC = dyn_cast<ConstantStruct>(&Op);45if (!OpC || OpC->getNumOperands() != 4)46continue;47auto *StrC = dyn_cast<GlobalValue>(OpC->getOperand(1)->stripPointerCasts());48if (!StrC)49continue;50auto *StrData = dyn_cast<ConstantDataSequential>(StrC->getOperand(0));51if (!StrData)52continue;53auto *Fn = dyn_cast<Function>(OpC->getOperand(0)->stripPointerCasts());54if (!Fn)55continue;5657// Add annotation to all instructions in the function.58for (auto &I : instructions(Fn))59I.addAnnotationMetadata(StrData->getAsCString());60}61return true;62}6364PreservedAnalyses Annotation2MetadataPass::run(Module &M,65ModuleAnalysisManager &AM) {66return convertAnnotation2Metadata(M) ? PreservedAnalyses::none()67: PreservedAnalyses::all();68}697071