blob: 91534a754a13e1d00c9b6deab59bfa55e4b31f9a [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements two versions of the LLVM "Hello World" pass described
11// in docs/WritingAnLLVMPass.html
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "hello"
16#include "llvm/Pass.h"
17#include "llvm/Function.h"
18#include "llvm/ADT/StringExtras.h"
Benjamin Kramer0588d2d2009-08-23 11:37:21 +000019#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/ADT/Statistic.h"
21using namespace llvm;
22
23STATISTIC(HelloCounter, "Counts number of functions greeted");
24
25namespace {
26 // Hello - The first implementation, without getAnalysisUsage.
27 struct Hello : public FunctionPass {
28 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000029 Hello() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030
31 virtual bool runOnFunction(Function &F) {
32 HelloCounter++;
Daniel Dunbarf89f4be2009-10-17 20:43:19 +000033 errs() << "Hello: ";
34 errs().write_escaped(F.getName()) << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035 return false;
36 }
37 };
Dan Gohman089efff2008-05-13 00:00:25 +000038}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039
Dan Gohman089efff2008-05-13 00:00:25 +000040char Hello::ID = 0;
41static RegisterPass<Hello> X("hello", "Hello World Pass");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042
Dan Gohman089efff2008-05-13 00:00:25 +000043namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 // Hello2 - The second implementation with getAnalysisUsage implemented.
45 struct Hello2 : public FunctionPass {
46 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000047 Hello2() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048
49 virtual bool runOnFunction(Function &F) {
50 HelloCounter++;
Daniel Dunbarf89f4be2009-10-17 20:43:19 +000051 errs() << "Hello: ";
52 errs().write_escaped(F.getName()) << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 };
60 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061}
Dan Gohman089efff2008-05-13 00:00:25 +000062
63char Hello2::ID = 0;
64static RegisterPass<Hello2>
65Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");