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" |
Benjamin Kramer | 0588d2d | 2009-08-23 11:37:21 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Statistic.h" |
| 20 | using namespace llvm; |
| 21 | |
| 22 | STATISTIC(HelloCounter, "Counts number of functions greeted"); |
| 23 | |
| 24 | namespace { |
| 25 | // Hello - The first implementation, without getAnalysisUsage. |
| 26 | struct Hello : public FunctionPass { |
| 27 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 7569322 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 28 | Hello() : FunctionPass(ID) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 29 | |
| 30 | virtual bool runOnFunction(Function &F) { |
Dan Gohman | 559d513 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 31 | ++HelloCounter; |
Daniel Dunbar | f89f4be | 2009-10-17 20:43:19 +0000 | [diff] [blame] | 32 | errs() << "Hello: "; |
| 33 | errs().write_escaped(F.getName()) << '\n'; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 34 | return false; |
| 35 | } |
| 36 | }; |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 37 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 38 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 39 | char Hello::ID = 0; |
Owen Anderson | 6374c3d | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 40 | INITIALIZE_PASS(Hello, "hello", "Hello World Pass", false, false); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 41 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 42 | namespace { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 43 | // Hello2 - The second implementation with getAnalysisUsage implemented. |
| 44 | struct Hello2 : public FunctionPass { |
| 45 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 7569322 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 46 | Hello2() : FunctionPass(ID) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 47 | |
| 48 | virtual bool runOnFunction(Function &F) { |
Dan Gohman | 559d513 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 49 | ++HelloCounter; |
Daniel Dunbar | f89f4be | 2009-10-17 20:43:19 +0000 | [diff] [blame] | 50 | errs() << "Hello: "; |
| 51 | errs().write_escaped(F.getName()) << '\n'; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 52 | return false; |
| 53 | } |
| 54 | |
| 55 | // We don't modify the program, so we preserve all analyses |
| 56 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 57 | AU.setPreservesAll(); |
Douglas Gregor | 8cb4138 | 2009-12-19 07:05:23 +0000 | [diff] [blame] | 58 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 59 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 60 | } |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 61 | |
| 62 | char Hello2::ID = 0; |
Owen Anderson | 6374c3d | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 63 | INITIALIZE_PASS(Hello2, "hello2", |
| 64 | "Hello World Pass (with getAnalysisUsage implemented)", |
| 65 | false, false); |