Chris Lattner | 180e568 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 1 | //===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 180e568 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 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 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 18 | namespace llvm { |
| 19 | |
Chris Lattner | 180e568 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 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 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 44 | |
| 45 | } // End llvm namespace |