1 //===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements two versions of the LLVM "Hello World" pass described
11 // in docs/WritingAnLLVMPass.html
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "hello"
16 #include "llvm/Pass.h"
17 #include "llvm/Function.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Support/SlowOperationInformer.h"
20 #include "llvm/Support/Streams.h"
21 #include "llvm/ADT/Statistic.h"
24 STATISTIC(HelloCounter, "Counts number of functions greeted");
27 // Hello - The first implementation, without getAnalysisUsage.
28 struct Hello : public FunctionPass {
29 virtual bool runOnFunction(Function &F) {
30 SlowOperationInformer soi("EscapeString");
32 std::string fname = F.getName();
34 cerr << "Hello: " << fname << "\n";
38 RegisterPass<Hello> X("hello", "Hello World Pass");
40 // Hello2 - The second implementation with getAnalysisUsage implemented.
41 struct Hello2 : public FunctionPass {
42 virtual bool runOnFunction(Function &F) {
43 SlowOperationInformer soi("EscapeString");
45 std::string fname = F.getName();
47 cerr << "Hello: " << fname << "\n";
51 // We don't modify the program, so we preserve all analyses
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56 RegisterPass<Hello2> Y("hello2",
57 "Hello World Pass (with getAnalysisUsage implemented)");