1 //===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file was developed by the LLVM research group and is distributed under 6 // the University of Illinois Open Source 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/Pass.h" 16 #include "llvm/Function.h" 17 #include <iostream> 18 using namespace llvm; 19 20 namespace { 21 // Hello - The first implementation, without getAnalysisUsage. 22 struct Hello : public FunctionPass { 23 virtual bool runOnFunction(Function &F) { 24 std::cerr << "Hello: " << F.getName() << "\n"; 25 return false; 26 } 27 }; 28 RegisterOpt<Hello> X("hello", "Hello World Pass"); 29 30 // Hello2 - The second implementation with getAnalysisUsage implemented. 31 struct Hello2 : public FunctionPass { 32 virtual bool runOnFunction(Function &F) { 33 std::cerr << "Hello: " << F.getName() << "\n"; 34 return false; 35 } 36 37 // We don't modify the program, so we preserve all analyses 38 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 39 AU.setPreservesAll(); 40 }; 41 }; 42 RegisterOpt<Hello2> Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)"); 43 } 44