LLVM API Documentation
00001 //===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements two versions of the LLVM "Hello World" pass described 00011 // in docs/WritingAnLLVMPass.html 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "hello" 00016 #include "llvm/Pass.h" 00017 #include "llvm/Function.h" 00018 #include "llvm/ADT/StringExtras.h" 00019 #include "llvm/Support/Streams.h" 00020 #include "llvm/ADT/Statistic.h" 00021 using namespace llvm; 00022 00023 STATISTIC(HelloCounter, "Counts number of functions greeted"); 00024 00025 namespace { 00026 // Hello - The first implementation, without getAnalysisUsage. 00027 struct Hello : public FunctionPass { 00028 static char ID; // Pass identification, replacement for typeid 00029 Hello() : FunctionPass(&ID) {} 00030 00031 virtual bool runOnFunction(Function &F) { 00032 HelloCounter++; 00033 std::string fname = F.getName(); 00034 EscapeString(fname); 00035 cerr << "Hello: " << fname << "\n"; 00036 return false; 00037 } 00038 }; 00039 } 00040 00041 char Hello::ID = 0; 00042 static RegisterPass<Hello> X("hello", "Hello World Pass"); 00043 00044 namespace { 00045 // Hello2 - The second implementation with getAnalysisUsage implemented. 00046 struct Hello2 : public FunctionPass { 00047 static char ID; // Pass identification, replacement for typeid 00048 Hello2() : FunctionPass(&ID) {} 00049 00050 virtual bool runOnFunction(Function &F) { 00051 HelloCounter++; 00052 std::string fname = F.getName(); 00053 EscapeString(fname); 00054 cerr << "Hello: " << fname << "\n"; 00055 return false; 00056 } 00057 00058 // We don't modify the program, so we preserve all analyses 00059 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00060 AU.setPreservesAll(); 00061 }; 00062 }; 00063 } 00064 00065 char Hello2::ID = 0; 00066 static RegisterPass<Hello2> 00067 Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");