1//===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements two versions of the LLVM "Hello World" pass described 11// in docs/WritingAnLLVMPass.html 12// 13//===----------------------------------------------------------------------===// 14 15#include "llvm/ADT/Statistic.h" 16#include "llvm/IR/Function.h" 17#include "llvm/Pass.h" 18#include "llvm/Support/raw_ostream.h" 19using namespace llvm; 20 21#define DEBUG_TYPE "hello" 22 23STATISTIC(HelloCounter, "Counts number of functions greeted"); 24 25namespace { 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) {} 30 31 bool runOnFunction(Function &F) override { 32 ++HelloCounter; 33 errs() << "Hello: "; 34 errs().write_escaped(F.getName()) << '\n'; 35 return false; 36 } 37 }; 38} 39 40char Hello::ID = 0; 41static RegisterPass<Hello> X("hello", "Hello World Pass"); 42 43namespace { 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) {} 48 49 bool runOnFunction(Function &F) override { 50 ++HelloCounter; 51 errs() << "Hello: "; 52 errs().write_escaped(F.getName()) << '\n'; 53 return false; 54 } 55 56 // We don't modify the program, so we preserve all analyses. 57 void getAnalysisUsage(AnalysisUsage &AU) const override { 58 AU.setPreservesAll(); 59 } 60 }; 61} 62 63char Hello2::ID = 0; 64static RegisterPass<Hello2> 65Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)"); 66