Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | #define DEBUG_TYPE "hello" |
| 16 | #include "llvm/Pass.h" |
| 17 | #include "llvm/Function.h" |
| 18 | #include "llvm/ADT/StringExtras.h" |
| 19 | #include "llvm/Support/Streams.h" |
| 20 | #include "llvm/ADT/Statistic.h" |
| 21 | using namespace llvm; |
| 22 | |
| 23 | STATISTIC(HelloCounter, "Counts number of functions greeted"); |
| 24 | |
| 25 | namespace { |
| 26 | // Hello - The first implementation, without getAnalysisUsage. |
| 27 | struct Hello : public FunctionPass { |
| 28 | static char ID; // Pass identification, replacement for typeid |
| 29 | Hello() : FunctionPass((intptr_t)&ID) {} |
| 30 | |
| 31 | virtual bool runOnFunction(Function &F) { |
| 32 | HelloCounter++; |
| 33 | std::string fname = F.getName(); |
| 34 | EscapeString(fname); |
| 35 | cerr << "Hello: " << fname << "\n"; |
| 36 | return false; |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | char Hello::ID = 0; |
| 41 | RegisterPass<Hello> X("hello", "Hello World Pass"); |
| 42 | |
| 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((intptr_t)&ID) {} |
| 47 | |
| 48 | virtual bool runOnFunction(Function &F) { |
| 49 | HelloCounter++; |
| 50 | std::string fname = F.getName(); |
| 51 | EscapeString(fname); |
| 52 | cerr << "Hello: " << fname << "\n"; |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | // We don't modify the program, so we preserve all analyses |
| 57 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 58 | AU.setPreservesAll(); |
| 59 | }; |
| 60 | }; |
| 61 | char Hello2::ID = 0; |
| 62 | RegisterPass<Hello2> Y("hello2", |
| 63 | "Hello World Pass (with getAnalysisUsage implemented)"); |
| 64 | } |