Chris Lattner | ed7ac42 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 1 | //===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | ed7ac42 | 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 | |
Reid Spencer | 2b6d18a | 2006-08-07 23:17:24 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Function.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/Pass.h" |
| 18 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | df3c342 | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 19 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 20 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 21 | #define DEBUG_TYPE "hello" |
| 22 | |
Chris Lattner | 1fa216f | 2006-12-19 22:24:09 +0000 | [diff] [blame] | 23 | STATISTIC(HelloCounter, "Counts number of functions greeted"); |
| 24 | |
Chris Lattner | ed7ac42 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 25 | namespace { |
| 26 | // Hello - The first implementation, without getAnalysisUsage. |
| 27 | struct Hello : public FunctionPass { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 28 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 29 | Hello() : FunctionPass(ID) {} |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 30 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 31 | bool runOnFunction(Function &F) override { |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 32 | ++HelloCounter; |
Daniel Dunbar | 8eff29d | 2009-10-17 20:43:19 +0000 | [diff] [blame] | 33 | errs() << "Hello: "; |
| 34 | errs().write_escaped(F.getName()) << '\n'; |
Chris Lattner | ed7ac42 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 35 | return false; |
| 36 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 37 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 38 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 39 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 40 | char Hello::ID = 0; |
Owen Anderson | 6da4d82 | 2010-10-07 00:31:16 +0000 | [diff] [blame] | 41 | static RegisterPass<Hello> X("hello", "Hello World Pass"); |
Chris Lattner | ed7ac42 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 42 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 43 | namespace { |
Chris Lattner | ed7ac42 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 44 | // Hello2 - The second implementation with getAnalysisUsage implemented. |
| 45 | struct Hello2 : public FunctionPass { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 46 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 47 | Hello2() : FunctionPass(ID) {} |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 48 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 49 | bool runOnFunction(Function &F) override { |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 50 | ++HelloCounter; |
Daniel Dunbar | 8eff29d | 2009-10-17 20:43:19 +0000 | [diff] [blame] | 51 | errs() << "Hello: "; |
| 52 | errs().write_escaped(F.getName()) << '\n'; |
Chris Lattner | ed7ac42 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 53 | return false; |
| 54 | } |
| 55 | |
Puyan Lotfi | 74e38de | 2013-09-27 07:36:10 +0000 | [diff] [blame] | 56 | // We don't modify the program, so we preserve all analyses. |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 57 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | ed7ac42 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 58 | AU.setPreservesAll(); |
Douglas Gregor | 740ab38 | 2009-12-19 07:05:23 +0000 | [diff] [blame] | 59 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 60 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 61 | } |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 62 | |
| 63 | char Hello2::ID = 0; |
Owen Anderson | 6da4d82 | 2010-10-07 00:31:16 +0000 | [diff] [blame] | 64 | static RegisterPass<Hello2> |
| 65 | Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)"); |