1 //===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements two versions of the LLVM "Hello World" pass described
10 // in docs/WritingAnLLVMPass.html
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/Statistic.h"
15 #include "llvm/IR/Function.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Support/raw_ostream.h"
20 #define DEBUG_TYPE "hello"
22 STATISTIC(HelloCounter
, "Counts number of functions greeted");
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
) {}
30 bool runOnFunction(Function
&F
) override
{
33 errs().write_escaped(F
.getName()) << '\n';
40 static RegisterPass
<Hello
> X("hello", "Hello World Pass");
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
) {}
48 bool runOnFunction(Function
&F
) override
{
51 errs().write_escaped(F
.getName()) << '\n';
55 // We don't modify the program, so we preserve all analyses.
56 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
63 static RegisterPass
<Hello2
>
64 Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");