1 //===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements two versions of the LLVM "Hello World" pass described
11 // in docs/WritingAnLLVMPass.html
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Support/raw_ostream.h"
21 #define DEBUG_TYPE "hello"
23 STATISTIC(HelloCounter, "Counts number of functions greeted");
26 // Hello - The first implementation, without getAnalysisUsage.
27 struct Hello : public FunctionPass {
28 static char ID; // Pass identification, replacement for typeid
29 Hello() : FunctionPass(ID) {}
31 bool runOnFunction(Function &F) override {
34 errs().write_escaped(F.getName()) << '\n';
41 static RegisterPass<Hello> X("hello", "Hello World Pass");
44 // Hello2 - The second implementation with getAnalysisUsage implemented.
45 struct Hello2 : public FunctionPass {
46 static char ID; // Pass identification, replacement for typeid
47 Hello2() : FunctionPass(ID) {}
49 bool runOnFunction(Function &F) override {
52 errs().write_escaped(F.getName()) << '\n';
56 // We don't modify the program, so we preserve all analyses.
57 void getAnalysisUsage(AnalysisUsage &AU) const override {
64 static RegisterPass<Hello2>
65 Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");