blob: 9f2343b3b3138b7ce7b5b8861f99c78b0e8f35b0 [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"
Reid Spencer05445e32006-08-07 23:17:24 +000016#include "llvm/ADT/Statistic.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/Function.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/Pass.h"
19#include "llvm/Support/raw_ostream.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattnercbfdd1f2006-12-19 22:24:09 +000022STATISTIC(HelloCounter, "Counts number of functions greeted");
23
Chris Lattner180e5682002-08-08 20:10:38 +000024namespace {
25 // Hello - The first implementation, without getAnalysisUsage.
26 struct Hello : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000027 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +000028 Hello() : FunctionPass(ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000029
Chris Lattner180e5682002-08-08 20:10:38 +000030 virtual bool runOnFunction(Function &F) {
Dan Gohmanfe601042010-06-22 15:08:57 +000031 ++HelloCounter;
Daniel Dunbar3446cf12009-10-17 20:43:19 +000032 errs() << "Hello: ";
33 errs().write_escaped(F.getName()) << '\n';
Chris Lattner180e5682002-08-08 20:10:38 +000034 return false;
35 }
Misha Brukmanfd939082005-04-21 23:48:37 +000036 };
Dan Gohman844731a2008-05-13 00:00:25 +000037}
Devang Patel794fd752007-05-01 21:15:47 +000038
Dan Gohman844731a2008-05-13 00:00:25 +000039char Hello::ID = 0;
Owen Andersoncce7f7c2010-10-07 00:31:16 +000040static RegisterPass<Hello> X("hello", "Hello World Pass");
Chris Lattner180e5682002-08-08 20:10:38 +000041
Dan Gohman844731a2008-05-13 00:00:25 +000042namespace {
Chris Lattner180e5682002-08-08 20:10:38 +000043 // Hello2 - The second implementation with getAnalysisUsage implemented.
44 struct Hello2 : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000045 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +000046 Hello2() : FunctionPass(ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000047
Chris Lattner180e5682002-08-08 20:10:38 +000048 virtual bool runOnFunction(Function &F) {
Dan Gohmanfe601042010-06-22 15:08:57 +000049 ++HelloCounter;
Daniel Dunbar3446cf12009-10-17 20:43:19 +000050 errs() << "Hello: ";
51 errs().write_escaped(F.getName()) << '\n';
Chris Lattner180e5682002-08-08 20:10:38 +000052 return false;
53 }
54
55 // We don't modify the program, so we preserve all analyses
56 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
57 AU.setPreservesAll();
Douglas Gregorcabdd742009-12-19 07:05:23 +000058 }
Misha Brukmanfd939082005-04-21 23:48:37 +000059 };
Chris Lattner180e5682002-08-08 20:10:38 +000060}
Dan Gohman844731a2008-05-13 00:00:25 +000061
62char Hello2::ID = 0;
Owen Andersoncce7f7c2010-10-07 00:31:16 +000063static RegisterPass<Hello2>
64Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");