LLVM 24.0.0git
Annotation2Metadata.cpp
Go to the documentation of this file.
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
16#include "llvm/IR/Constants.h"
17#include "llvm/IR/Function.h"
19#include "llvm/IR/Module.h"
20
21using namespace llvm;
22
23#define DEBUG_TYPE "annotation2metadata"
24
26 // Only add !annotation metadata if the corresponding remarks pass is also
27 // enabled.
29 "annotation-remarks"))
30 return false;
31
32 auto *Annotations = M.getGlobalVariable("llvm.global.annotations");
34 if (!C || C->getNumOperands() != 1)
35 return false;
36
37 C = cast<Constant>(C->getOperand(0));
38
39 // Iterate over all entries in C and attach !annotation metadata to suitable
40 // entries.
41 for (auto &Op : C->operands()) {
42 // Look at the operands to check if we can use the entry to generate
43 // !annotation metadata.
44 auto *OpC = dyn_cast<ConstantStruct>(&Op);
45 if (!OpC || OpC->getNumOperands() != 4)
46 continue;
47 auto *StrC = dyn_cast<GlobalValue>(OpC->getOperand(1)->stripPointerCasts());
48 if (!StrC)
49 continue;
50 auto *StrData = dyn_cast<ConstantDataSequential>(StrC->getOperand(0));
51 if (!StrData)
52 continue;
53 auto *Fn = dyn_cast<Function>(OpC->getOperand(0)->stripPointerCasts());
54 if (!Fn)
55 continue;
56
57 // Add annotation to all instructions in the function.
58 for (auto &I : instructions(Fn))
59 I.addAnnotationMetadata(StrData->getAsCString());
60 }
61 return true;
62}
63
static bool convertAnnotation2Metadata(Module &M)
Expand Atomic instructions
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition MD5.cpp:57
Annotations lets you mark points and ranges inside source code, for tests:
Definition Annotations.h:67
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
bool allowExtraAnalysis(StringRef PassName) const
Whether we allow for extra compile-time budget to perform more analysis to produce fewer false positi...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)