blob: d0b146b4e9f688e933847aad3becd70cd7c9f109 [file] [log] [blame]
Chris Lattnered7ac422002-08-08 20:10:38 +00001//===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnered7ac422002-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 Lattner1fa216f2006-12-19 22:24:09 +000015#define DEBUG_TYPE "hello"
Reid Spencer2b6d18a2006-08-07 23:17:24 +000016#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Function.h"
18#include "llvm/Pass.h"
19#include "llvm/Support/raw_ostream.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000020using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000021
Chris Lattner1fa216f2006-12-19 22:24:09 +000022STATISTIC(HelloCounter, "Counts number of functions greeted");
23
Chris Lattnered7ac422002-08-08 20:10:38 +000024namespace {
25 // Hello - The first implementation, without getAnalysisUsage.
26 struct Hello : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000027 static char ID; // Pass identification, replacement for typeid
Owen Andersona7aed182010-08-06 18:33:48 +000028 Hello() : FunctionPass(ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000029
Chris Lattnered7ac422002-08-08 20:10:38 +000030 virtual bool runOnFunction(Function &F) {
Dan Gohmand2d1ae12010-06-22 15:08:57 +000031 ++HelloCounter;
Daniel Dunbar8eff29d2009-10-17 20:43:19 +000032 errs() << "Hello: ";
33 errs().write_escaped(F.getName()) << '\n';
Chris Lattnered7ac422002-08-08 20:10:38 +000034 return false;
35 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000036 };
Dan Gohmand78c4002008-05-13 00:00:25 +000037}
Devang Patel09f162c2007-05-01 21:15:47 +000038
Dan Gohmand78c4002008-05-13 00:00:25 +000039char Hello::ID = 0;
Owen Anderson6da4d822010-10-07 00:31:16 +000040static RegisterPass<Hello> X("hello", "Hello World Pass");
Chris Lattnered7ac422002-08-08 20:10:38 +000041
Dan Gohmand78c4002008-05-13 00:00:25 +000042namespace {
Chris Lattnered7ac422002-08-08 20:10:38 +000043 // Hello2 - The second implementation with getAnalysisUsage implemented.
44 struct Hello2 : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000045 static char ID; // Pass identification, replacement for typeid
Owen Andersona7aed182010-08-06 18:33:48 +000046 Hello2() : FunctionPass(ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000047
Chris Lattnered7ac422002-08-08 20:10:38 +000048 virtual bool runOnFunction(Function &F) {
Dan Gohmand2d1ae12010-06-22 15:08:57 +000049 ++HelloCounter;
Daniel Dunbar8eff29d2009-10-17 20:43:19 +000050 errs() << "Hello: ";
51 errs().write_escaped(F.getName()) << '\n';
Chris Lattnered7ac422002-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 Gregor740ab382009-12-19 07:05:23 +000058 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000059 };
Chris Lattnered7ac422002-08-08 20:10:38 +000060}
Dan Gohmand78c4002008-05-13 00:00:25 +000061
62char Hello2::ID = 0;
Owen Anderson6da4d822010-10-07 00:31:16 +000063static RegisterPass<Hello2>
64Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");