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 | // |
| 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. |
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 | |
| 15 | #include "llvm/Pass.h" |
| 16 | #include "llvm/Function.h" |
Reid Spencer | 05445e3 | 2006-08-07 23:17:24 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
| 18 | #include "llvm/Support/SlowOperationInformer.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | a5de823 | 2004-08-12 02:44:23 +0000 | [diff] [blame] | 20 | #include <iostream> |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 180e568 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 23 | namespace { |
Reid Spencer | 05445e3 | 2006-08-07 23:17:24 +0000 | [diff] [blame] | 24 | Statistic<int> HelloCounter("hellocount", |
| 25 | "Counts number of functions greeted"); |
Chris Lattner | 180e568 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 26 | // Hello - The first implementation, without getAnalysisUsage. |
| 27 | struct Hello : public FunctionPass { |
| 28 | virtual bool runOnFunction(Function &F) { |
Reid Spencer | 05445e3 | 2006-08-07 23:17:24 +0000 | [diff] [blame] | 29 | SlowOperationInformer soi("EscapeString"); |
| 30 | HelloCounter++; |
| 31 | std::string fname = F.getName(); |
| 32 | EscapeString(fname); |
| 33 | std::cerr << "Hello: " << fname << "\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 | }; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 37 | RegisterPass<Hello> X("hello", "Hello World Pass"); |
Chris Lattner | 180e568 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 38 | |
| 39 | // Hello2 - The second implementation with getAnalysisUsage implemented. |
| 40 | struct Hello2 : public FunctionPass { |
| 41 | virtual bool runOnFunction(Function &F) { |
Reid Spencer | 05445e3 | 2006-08-07 23:17:24 +0000 | [diff] [blame] | 42 | SlowOperationInformer soi("EscapeString"); |
| 43 | HelloCounter++; |
| 44 | std::string fname = F.getName(); |
| 45 | EscapeString(fname); |
| 46 | std::cerr << "Hello: " << fname << "\n"; |
Chris Lattner | 180e568 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 47 | return false; |
| 48 | } |
| 49 | |
| 50 | // We don't modify the program, so we preserve all analyses |
| 51 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 52 | AU.setPreservesAll(); |
| 53 | }; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 54 | }; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 55 | RegisterPass<Hello2> Y("hello2", |
| 56 | "Hello World Pass (with getAnalysisUsage implemented)"); |
Chris Lattner | 180e568 | 2002-08-08 20:10:38 +0000 | [diff] [blame] | 57 | } |