Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Scalar/AnnotationRemarks.cpp
35269 views
//===-- AnnotationRemarks.cpp - Generate remarks for annotated instrs. ----===//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// Generate remarks for instructions marked with !annotation.9//10//===----------------------------------------------------------------------===//1112#include "llvm/Transforms/Scalar/AnnotationRemarks.h"13#include "llvm/ADT/MapVector.h"14#include "llvm/Analysis/OptimizationRemarkEmitter.h"15#include "llvm/Analysis/TargetLibraryInfo.h"16#include "llvm/IR/Function.h"17#include "llvm/IR/InstIterator.h"18#include "llvm/Transforms/Utils/MemoryOpRemark.h"1920using namespace llvm;21using namespace llvm::ore;2223#define DEBUG_TYPE "annotation-remarks"24#define REMARK_PASS DEBUG_TYPE2526static void tryEmitAutoInitRemark(ArrayRef<Instruction *> Instructions,27OptimizationRemarkEmitter &ORE,28const TargetLibraryInfo &TLI) {29// For every auto-init annotation generate a separate remark.30for (Instruction *I : Instructions) {31if (!AutoInitRemark::canHandle(I))32continue;3334Function &F = *I->getParent()->getParent();35const DataLayout &DL = F.getDataLayout();36AutoInitRemark Remark(ORE, REMARK_PASS, DL, TLI);37Remark.visit(I);38}39}4041static void runImpl(Function &F, const TargetLibraryInfo &TLI) {42if (!OptimizationRemarkEmitter::allowExtraAnalysis(F, REMARK_PASS))43return;4445// Track all annotated instructions aggregated based on their debug location.46DenseMap<MDNode *, SmallVector<Instruction *, 4>> DebugLoc2Annotated;4748OptimizationRemarkEmitter ORE(&F);49// First, generate a summary of the annotated instructions.50MapVector<StringRef, unsigned> Mapping;51for (Instruction &I : instructions(F)) {52if (!I.hasMetadata(LLVMContext::MD_annotation))53continue;54auto Iter = DebugLoc2Annotated.insert({I.getDebugLoc().getAsMDNode(), {}});55Iter.first->second.push_back(&I);5657for (const MDOperand &Op :58I.getMetadata(LLVMContext::MD_annotation)->operands()) {59StringRef AnnotationStr =60isa<MDString>(Op.get())61? cast<MDString>(Op.get())->getString()62: cast<MDString>(cast<MDTuple>(Op.get())->getOperand(0).get())63->getString();64auto Iter = Mapping.insert({AnnotationStr, 0});65Iter.first->second++;66}67}6869for (const auto &KV : Mapping)70ORE.emit(OptimizationRemarkAnalysis(REMARK_PASS, "AnnotationSummary",71F.getSubprogram(), &F.front())72<< "Annotated " << NV("count", KV.second) << " instructions with "73<< NV("type", KV.first));7475// For each debug location, look for all the instructions with annotations and76// generate more detailed remarks to be displayed at that location.77for (auto &KV : DebugLoc2Annotated) {78// Don't generate remarks with no debug location.79if (!KV.first)80continue;8182tryEmitAutoInitRemark(KV.second, ORE, TLI);83}84}8586PreservedAnalyses AnnotationRemarksPass::run(Function &F,87FunctionAnalysisManager &AM) {88auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);89runImpl(F, TLI);90return PreservedAnalyses::all();91}929394