blob: a12a8a68cd9b918ccf85dda7003a22857582fb18 [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
Chris Lattnercbfdd1f2006-12-19 22:24:09 +000015#define DEBUG_TYPE "hello"
Chris Lattner180e5682002-08-08 20:10:38 +000016#include "llvm/Pass.h"
17#include "llvm/Function.h"
Reid Spencer05445e32006-08-07 23:17:24 +000018#include "llvm/ADT/StringExtras.h"
19#include "llvm/Support/SlowOperationInformer.h"
Bill Wendling62c804a2006-11-26 09:17:06 +000020#include "llvm/Support/Streams.h"
Reid Spencer05445e32006-08-07 23:17:24 +000021#include "llvm/ADT/Statistic.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattnercbfdd1f2006-12-19 22:24:09 +000024STATISTIC(HelloCounter, "Counts number of functions greeted");
25
Chris Lattner180e5682002-08-08 20:10:38 +000026namespace {
27 // Hello - The first implementation, without getAnalysisUsage.
28 struct Hello : public FunctionPass {
29 virtual bool runOnFunction(Function &F) {
Reid Spencer05445e32006-08-07 23:17:24 +000030 SlowOperationInformer soi("EscapeString");
31 HelloCounter++;
32 std::string fname = F.getName();
33 EscapeString(fname);
Bill Wendlinge8156192006-12-07 01:30:32 +000034 cerr << "Hello: " << fname << "\n";
Chris Lattner180e5682002-08-08 20:10:38 +000035 return false;
36 }
Misha Brukmanfd939082005-04-21 23:48:37 +000037 };
Chris Lattner7f8897f2006-08-27 22:42:52 +000038 RegisterPass<Hello> X("hello", "Hello World Pass");
Chris Lattner180e5682002-08-08 20:10:38 +000039
40 // Hello2 - The second implementation with getAnalysisUsage implemented.
41 struct Hello2 : public FunctionPass {
42 virtual bool runOnFunction(Function &F) {
Reid Spencer05445e32006-08-07 23:17:24 +000043 SlowOperationInformer soi("EscapeString");
44 HelloCounter++;
45 std::string fname = F.getName();
46 EscapeString(fname);
Bill Wendlinge8156192006-12-07 01:30:32 +000047 cerr << "Hello: " << fname << "\n";
Chris Lattner180e5682002-08-08 20:10:38 +000048 return false;
49 }
50
51 // We don't modify the program, so we preserve all analyses
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53 AU.setPreservesAll();
54 };
Misha Brukmanfd939082005-04-21 23:48:37 +000055 };
Chris Lattner7f8897f2006-08-27 22:42:52 +000056 RegisterPass<Hello2> Y("hello2",
57 "Hello World Pass (with getAnalysisUsage implemented)");
Chris Lattner180e5682002-08-08 20:10:38 +000058}