blob: 91534a754a13e1d00c9b6deab59bfa55e4b31f9a [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Benjamin Kramercfa6ec92009-08-23 11:37:21 +000019#include "llvm/Support/raw_ostream.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 {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000028 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000029 Hello() : FunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000030
Chris Lattner180e5682002-08-08 20:10:38 +000031 virtual bool runOnFunction(Function &F) {
Reid Spencer05445e32006-08-07 23:17:24 +000032 HelloCounter++;
Daniel Dunbar3446cf12009-10-17 20:43:19 +000033 errs() << "Hello: ";
34 errs().write_escaped(F.getName()) << '\n';
Chris Lattner180e5682002-08-08 20:10:38 +000035 return false;
36 }
Misha Brukmanfd939082005-04-21 23:48:37 +000037 };
Dan Gohman844731a2008-05-13 00:00:25 +000038}
Devang Patel794fd752007-05-01 21:15:47 +000039
Dan Gohman844731a2008-05-13 00:00:25 +000040char Hello::ID = 0;
41static RegisterPass<Hello> X("hello", "Hello World Pass");
Chris Lattner180e5682002-08-08 20:10:38 +000042
Dan Gohman844731a2008-05-13 00:00:25 +000043namespace {
Chris Lattner180e5682002-08-08 20:10:38 +000044 // Hello2 - The second implementation with getAnalysisUsage implemented.
45 struct Hello2 : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000046 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000047 Hello2() : FunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000048
Chris Lattner180e5682002-08-08 20:10:38 +000049 virtual bool runOnFunction(Function &F) {
Reid Spencer05445e32006-08-07 23:17:24 +000050 HelloCounter++;
Daniel Dunbar3446cf12009-10-17 20:43:19 +000051 errs() << "Hello: ";
52 errs().write_escaped(F.getName()) << '\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 };
Chris Lattner180e5682002-08-08 20:10:38 +000061}
Dan Gohman844731a2008-05-13 00:00:25 +000062
63char Hello2::ID = 0;
64static RegisterPass<Hello2>
65Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");