Chris Lattner | 180e568 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 1 | //===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | |
Chris Lattner | cbfdd1f | 2006-12-19 22:24:09 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "hello" |
Chris Lattner | 180e568 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 16 | #include "llvm/Pass.h" |
| 17 | #include "llvm/Function.h" |
Benjamin Kramer | cfa6ec9 | 2009-08-23 11:37:21 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 05445e3 | 2006-08-07 23:17:24 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 20 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 21 | |
Chris Lattner | cbfdd1f | 2006-12-19 22:24:09 +0000 | [diff] [blame] | 22 | STATISTIC(HelloCounter, "Counts number of functions greeted"); |
| 23 | |
Chris Lattner | 180e568 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | // Hello - The first implementation, without getAnalysisUsage. |
| 26 | struct Hello : public FunctionPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 27 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame^] | 28 | Hello() : FunctionPass(ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 180e568 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 30 | virtual bool runOnFunction(Function &F) { |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 31 | ++HelloCounter; |
Daniel Dunbar | 3446cf1 | 2009-10-17 20:43:19 +0000 | [diff] [blame] | 32 | errs() << "Hello: "; |
| 33 | errs().write_escaped(F.getName()) << '\n'; |
Chris Lattner | 180e568 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 34 | return false; |
| 35 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 36 | }; |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 37 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 38 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 39 | char Hello::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 40 | INITIALIZE_PASS(Hello, "hello", "Hello World Pass", false, false); |
Chris Lattner | 180e568 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 41 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 42 | namespace { |
Chris Lattner | 180e568 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 43 | // Hello2 - The second implementation with getAnalysisUsage implemented. |
| 44 | struct Hello2 : public FunctionPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 45 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame^] | 46 | Hello2() : FunctionPass(ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 180e568 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 48 | virtual bool runOnFunction(Function &F) { |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 49 | ++HelloCounter; |
Daniel Dunbar | 3446cf1 | 2009-10-17 20:43:19 +0000 | [diff] [blame] | 50 | errs() << "Hello: "; |
| 51 | errs().write_escaped(F.getName()) << '\n'; |
Chris Lattner | 180e568 | 2002-08-08 20:10:38 +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 | cabdd74 | 2009-12-19 07:05:23 +0000 | [diff] [blame] | 58 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 59 | }; |
Chris Lattner | 180e568 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 60 | } |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 61 | |
| 62 | char Hello2::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 63 | INITIALIZE_PASS(Hello2, "hello2", |
| 64 | "Hello World Pass (with getAnalysisUsage implemented)", |
| 65 | false, false); |