blob: ca8f89dd8378a8bd86beab58997f3e2198bcdd90 [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"
Bill Wendling62c804a2006-11-26 09:17:06 +000019#include "llvm/Support/Streams.h"
Reid Spencer05445e32006-08-07 23:17:24 +000020#include "llvm/ADT/Statistic.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Chris Lattnercbfdd1f2006-12-19 22:24:09 +000023STATISTIC(HelloCounter, "Counts number of functions greeted");
24
Chris Lattner180e5682002-08-08 20:10:38 +000025namespace {
26 // Hello - The first implementation, without getAnalysisUsage.
27 struct Hello : public FunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000028 static char ID; // Pass identifcation, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000029 Hello() : FunctionPass((intptr_t)&ID) {}
30
Chris Lattner180e5682002-08-08 20:10:38 +000031 virtual bool runOnFunction(Function &F) {
Reid Spencer05445e32006-08-07 23:17:24 +000032 HelloCounter++;
33 std::string fname = F.getName();
34 EscapeString(fname);
Bill Wendlinge8156192006-12-07 01:30:32 +000035 cerr << "Hello: " << fname << "\n";
Chris Lattner180e5682002-08-08 20:10:38 +000036 return false;
37 }
Misha Brukmanfd939082005-04-21 23:48:37 +000038 };
Devang Patel794fd752007-05-01 21:15:47 +000039
Devang Patel19974732007-05-03 01:11:54 +000040 char Hello::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000041 RegisterPass<Hello> X("hello", "Hello World Pass");
Chris Lattner180e5682002-08-08 20:10:38 +000042
43 // Hello2 - The second implementation with getAnalysisUsage implemented.
44 struct Hello2 : public FunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000045 static char ID; // Pass identifcation, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000046 Hello2() : FunctionPass((intptr_t)&ID) {}
47
Chris Lattner180e5682002-08-08 20:10:38 +000048 virtual bool runOnFunction(Function &F) {
Reid Spencer05445e32006-08-07 23:17:24 +000049 HelloCounter++;
50 std::string fname = F.getName();
51 EscapeString(fname);
Bill Wendlinge8156192006-12-07 01:30:32 +000052 cerr << "Hello: " << fname << "\n";
Chris Lattner180e5682002-08-08 20:10:38 +000053 return false;
54 }
55
56 // We don't modify the program, so we preserve all analyses
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
58 AU.setPreservesAll();
59 };
Misha Brukmanfd939082005-04-21 23:48:37 +000060 };
Devang Patel19974732007-05-03 01:11:54 +000061 char Hello2::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000062 RegisterPass<Hello2> Y("hello2",
63 "Hello World Pass (with getAnalysisUsage implemented)");
Chris Lattner180e5682002-08-08 20:10:38 +000064}