1ed7ac42cSChris Lattner //===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
2ed7ac42cSChris Lattner //
3482202a6SJohn Criswell //                     The LLVM Compiler Infrastructure
4482202a6SJohn Criswell //
5482202a6SJohn Criswell // This file was developed by the LLVM research group and is distributed under
6482202a6SJohn Criswell // the University of Illinois Open Source License. See LICENSE.TXT for details.
7482202a6SJohn Criswell //
8482202a6SJohn Criswell //===----------------------------------------------------------------------===//
9482202a6SJohn Criswell //
10ed7ac42cSChris Lattner // This file implements two versions of the LLVM "Hello World" pass described
11ed7ac42cSChris Lattner // in docs/WritingAnLLVMPass.html
12ed7ac42cSChris Lattner //
13ed7ac42cSChris Lattner //===----------------------------------------------------------------------===//
14ed7ac42cSChris Lattner 
15ed7ac42cSChris Lattner #include "llvm/Pass.h"
16ed7ac42cSChris Lattner #include "llvm/Function.h"
172b6d18a6SReid Spencer #include "llvm/ADT/StringExtras.h"
182b6d18a6SReid Spencer #include "llvm/Support/SlowOperationInformer.h"
19*a7459ca8SBill Wendling #include "llvm/Support/Streams.h"
202b6d18a6SReid Spencer #include "llvm/ADT/Statistic.h"
21df3c342aSChris Lattner using namespace llvm;
22960707c3SBrian Gaeke 
23ed7ac42cSChris Lattner namespace {
242b6d18a6SReid Spencer   Statistic<int> HelloCounter("hellocount",
252b6d18a6SReid Spencer       "Counts number of functions greeted");
26ed7ac42cSChris Lattner   // Hello - The first implementation, without getAnalysisUsage.
27ed7ac42cSChris Lattner   struct Hello : public FunctionPass {
28ed7ac42cSChris Lattner     virtual bool runOnFunction(Function &F) {
292b6d18a6SReid Spencer       SlowOperationInformer soi("EscapeString");
302b6d18a6SReid Spencer       HelloCounter++;
312b6d18a6SReid Spencer       std::string fname = F.getName();
322b6d18a6SReid Spencer       EscapeString(fname);
33*a7459ca8SBill Wendling       llvm_cerr << "Hello: " << fname << "\n";
34ed7ac42cSChris Lattner       return false;
35ed7ac42cSChris Lattner     }
36ed7ac42cSChris Lattner   };
37c2d3d311SChris Lattner   RegisterPass<Hello> X("hello", "Hello World Pass");
38ed7ac42cSChris Lattner 
39ed7ac42cSChris Lattner   // Hello2 - The second implementation with getAnalysisUsage implemented.
40ed7ac42cSChris Lattner   struct Hello2 : public FunctionPass {
41ed7ac42cSChris Lattner     virtual bool runOnFunction(Function &F) {
422b6d18a6SReid Spencer       SlowOperationInformer soi("EscapeString");
432b6d18a6SReid Spencer       HelloCounter++;
442b6d18a6SReid Spencer       std::string fname = F.getName();
452b6d18a6SReid Spencer       EscapeString(fname);
46*a7459ca8SBill Wendling       llvm_cerr << "Hello: " << fname << "\n";
47ed7ac42cSChris Lattner       return false;
48ed7ac42cSChris Lattner     }
49ed7ac42cSChris Lattner 
50ed7ac42cSChris Lattner     // We don't modify the program, so we preserve all analyses
51ed7ac42cSChris Lattner     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52ed7ac42cSChris Lattner       AU.setPreservesAll();
53ed7ac42cSChris Lattner     };
54ed7ac42cSChris Lattner   };
55c2d3d311SChris Lattner   RegisterPass<Hello2> Y("hello2",
56c2d3d311SChris Lattner                         "Hello World Pass (with getAnalysisUsage implemented)");
57ed7ac42cSChris Lattner }
58