LLVM 19.0.0git
Hello.cpp
Go to the documentation of this file.
1//===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
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// This file implements two versions of the LLVM "Hello World" pass described
10// in docs/WritingAnLLVMPass.html
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/Statistic.h"
15#include "llvm/IR/Function.h"
16#include "llvm/Pass.h"
18using namespace llvm;
19
20#define DEBUG_TYPE "hello"
21
22STATISTIC(HelloCounter, "Counts number of functions greeted");
23
24namespace {
25 // Hello - The first implementation, without getAnalysisUsage.
26 struct Hello : public FunctionPass {
27 static char ID; // Pass identification, replacement for typeid
28 Hello() : FunctionPass(ID) {}
29
30 bool runOnFunction(Function &F) override {
31 ++HelloCounter;
32 errs() << "Hello: ";
33 errs().write_escaped(F.getName()) << '\n';
34 return false;
35 }
36 };
37}
38
39char Hello::ID = 0;
40static RegisterPass<Hello> X("hello", "Hello World Pass");
41
42namespace {
43 // Hello2 - The second implementation with getAnalysisUsage implemented.
44 struct Hello2 : public FunctionPass {
45 static char ID; // Pass identification, replacement for typeid
46 Hello2() : FunctionPass(ID) {}
47
48 bool runOnFunction(Function &F) override {
49 ++HelloCounter;
50 errs() << "Hello: ";
51 errs().write_escaped(F.getName()) << '\n';
52 return false;
53 }
54
55 // We don't modify the program, so we preserve all analyses.
56 void getAnalysisUsage(AnalysisUsage &AU) const override {
57 AU.setPreservesAll();
58 }
59 };
60}
61
62char Hello2::ID = 0;
64Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");
static RegisterPass< Hello2 > Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)")
static RegisterPass< Hello > X("hello", "Hello World Pass")
#define F(x, y, z)
Definition: MD5.cpp:55
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
RegisterPass<t> template - This template class is used to notify the system that a Pass is available ...
Definition: PassSupport.h:109