blob: cfa879a32db8862afa82353d3e5d751a266cfa59 [file] [log] [blame]
Chris Lattner180e5682002-08-08 20:10:38 +00001//===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner180e5682002-08-08 20:10:38 +00009//
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 Spencer05445e32006-08-07 23:17:24 +000017#include "llvm/ADT/StringExtras.h"
18#include "llvm/Support/SlowOperationInformer.h"
19#include "llvm/ADT/Statistic.h"
Chris Lattnera5de8232004-08-12 02:44:23 +000020#include <iostream>
Chris Lattnerf7703df2004-01-09 06:12:26 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Chris Lattner180e5682002-08-08 20:10:38 +000023namespace {
Reid Spencer05445e32006-08-07 23:17:24 +000024 Statistic<int> HelloCounter("hellocount",
25 "Counts number of functions greeted");
Chris Lattner180e5682002-08-08 20:10:38 +000026 // Hello - The first implementation, without getAnalysisUsage.
27 struct Hello : public FunctionPass {
28 virtual bool runOnFunction(Function &F) {
Reid Spencer05445e32006-08-07 23:17:24 +000029 SlowOperationInformer soi("EscapeString");
30 HelloCounter++;
31 std::string fname = F.getName();
32 EscapeString(fname);
33 std::cerr << "Hello: " << fname << "\n";
Chris Lattner180e5682002-08-08 20:10:38 +000034 return false;
35 }
Misha Brukmanfd939082005-04-21 23:48:37 +000036 };
Chris Lattner7f8897f2006-08-27 22:42:52 +000037 RegisterPass<Hello> X("hello", "Hello World Pass");
Chris Lattner180e5682002-08-08 20:10:38 +000038
39 // Hello2 - The second implementation with getAnalysisUsage implemented.
40 struct Hello2 : public FunctionPass {
41 virtual bool runOnFunction(Function &F) {
Reid Spencer05445e32006-08-07 23:17:24 +000042 SlowOperationInformer soi("EscapeString");
43 HelloCounter++;
44 std::string fname = F.getName();
45 EscapeString(fname);
46 std::cerr << "Hello: " << fname << "\n";
Chris Lattner180e5682002-08-08 20:10:38 +000047 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 Brukmanfd939082005-04-21 23:48:37 +000054 };
Chris Lattner7f8897f2006-08-27 22:42:52 +000055 RegisterPass<Hello2> Y("hello2",
56 "Hello World Pass (with getAnalysisUsage implemented)");
Chris Lattner180e5682002-08-08 20:10:38 +000057}