LLVM  14.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"
18 #include "llvm/IR/InstIterator.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/InitializePasses.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Transforms/IPO.h"
23 
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "annotation2metadata"
27 
29  // Only add !annotation metadata if the corresponding remarks pass is also
30  // enabled.
32  "annotation-remarks"))
33  return false;
34 
35  auto *Annotations = M.getGlobalVariable("llvm.global.annotations");
36  auto *C = dyn_cast_or_null<Constant>(Annotations);
37  if (!C || C->getNumOperands() != 1)
38  return false;
39 
40  C = cast<Constant>(C->getOperand(0));
41 
42  // Iterate over all entries in C and attach !annotation metadata to suitable
43  // entries.
44  for (auto &Op : C->operands()) {
45  // Look at the operands to check if we can use the entry to generate
46  // !annotation metadata.
47  auto *OpC = dyn_cast<ConstantStruct>(&Op);
48  if (!OpC || OpC->getNumOperands() != 4)
49  continue;
50  auto *StrGEP = dyn_cast<ConstantExpr>(OpC->getOperand(1));
51  if (!StrGEP || StrGEP->getNumOperands() < 2)
52  continue;
53  auto *StrC = dyn_cast<GlobalValue>(StrGEP->getOperand(0));
54  if (!StrC)
55  continue;
56  auto *StrData = dyn_cast<ConstantDataSequential>(StrC->getOperand(0));
57  if (!StrData)
58  continue;
59  // Look through bitcast.
60  auto *Bitcast = dyn_cast<ConstantExpr>(OpC->getOperand(0));
61  if (!Bitcast || Bitcast->getOpcode() != Instruction::BitCast)
62  continue;
63  auto *Fn = dyn_cast<Function>(Bitcast->getOperand(0));
64  if (!Fn)
65  continue;
66 
67  // Add annotation to all instructions in the function.
68  for (auto &I : instructions(Fn))
69  I.addAnnotationMetadata(StrData->getAsCString());
70  }
71  return true;
72 }
73 
74 namespace {
75 struct Annotation2MetadataLegacy : public ModulePass {
76  static char ID;
77 
78  Annotation2MetadataLegacy() : ModulePass(ID) {
80  }
81 
82  bool runOnModule(Module &M) override { return convertAnnotation2Metadata(M); }
83 
84  void getAnalysisUsage(AnalysisUsage &AU) const override {
85  AU.setPreservesAll();
86  }
87 };
88 
89 } // end anonymous namespace
90 
92 
93 INITIALIZE_PASS_BEGIN(Annotation2MetadataLegacy, DEBUG_TYPE,
94  "Annotation2Metadata", false, false)
95 INITIALIZE_PASS_END(Annotation2MetadataLegacy, DEBUG_TYPE,
97 
99  return new Annotation2MetadataLegacy();
100 }
101 
103  ModuleAnalysisManager &AM) {
105  return PreservedAnalyses::all();
106 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
Annotation2Metadata.h
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
InstIterator.h
Pass.h
llvm::LegacyLegalizeActions::Bitcast
@ Bitcast
Perform the operation on a different, but equivalently sized type.
Definition: LegacyLegalizerInfo.h:54
OptimizationRemarkEmitter.h
llvm::OptimizationRemarkEmitter::allowExtraAnalysis
bool allowExtraAnalysis(StringRef PassName) const
Whether we allow for extra compile-time budget to perform more analysis to produce fewer false positi...
Definition: OptimizationRemarkEmitter.h:98
llvm::initializeAnnotation2MetadataLegacyPass
void initializeAnnotation2MetadataLegacyPass(PassRegistry &)
Module.h
llvm::Annotations
Annotations lets you mark points and ranges inside source code, for tests:
Definition: Annotations.h:46
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
Constants.h
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
false
Definition: StackSlotColoring.cpp:142
Annotation2Metadata
Annotation2Metadata
Definition: Annotation2Metadata.cpp:96
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
llvm::instructions
inst_range instructions(Function *F)
Definition: InstIterator.h:133
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(Annotation2MetadataLegacy, DEBUG_TYPE, "Annotation2Metadata", false, false) INITIALIZE_PASS_END(Annotation2MetadataLegacy
IPO.h
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::Annotation2MetadataPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: Annotation2Metadata.cpp:102
DEBUG_TYPE
#define DEBUG_TYPE
Definition: Annotation2Metadata.cpp:26
convertAnnotation2Metadata
static bool convertAnnotation2Metadata(Module &M)
Definition: Annotation2Metadata.cpp:28
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:324
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
Function.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
InitializePasses.h
llvm::createAnnotation2MetadataLegacyPass
ModulePass * createAnnotation2MetadataLegacyPass()
Definition: Annotation2Metadata.cpp:98
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37