blob: 37d7a00396dc30ba69b35b71328ef963a246f88d [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"
Benjamin Kramer0588d2d2009-08-23 11:37:21 +000018#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/ADT/Statistic.h"
20using namespace llvm;
21
22STATISTIC(HelloCounter, "Counts number of functions greeted");
23
24namespace {
25 // Hello - The first implementation, without getAnalysisUsage.
26 struct Hello : public FunctionPass {
27 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000028 Hello() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029
30 virtual bool runOnFunction(Function &F) {
31 HelloCounter++;
Daniel Dunbarf89f4be2009-10-17 20:43:19 +000032 errs() << "Hello: ";
33 errs().write_escaped(F.getName()) << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034 return false;
35 }
36 };
Dan Gohman089efff2008-05-13 00:00:25 +000037}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038
Dan Gohman089efff2008-05-13 00:00:25 +000039char Hello::ID = 0;
40static RegisterPass<Hello> X("hello", "Hello World Pass");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041
Dan Gohman089efff2008-05-13 00:00:25 +000042namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043 // Hello2 - The second implementation with getAnalysisUsage implemented.
44 struct Hello2 : public FunctionPass {
45 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000046 Hello2() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047
48 virtual bool runOnFunction(Function &F) {
49 HelloCounter++;
Daniel Dunbarf89f4be2009-10-17 20:43:19 +000050 errs() << "Hello: ";
51 errs().write_escaped(F.getName()) << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Gregor8cb41382009-12-19 07:05:23 +000058 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060}
Dan Gohman089efff2008-05-13 00:00:25 +000061
62char Hello2::ID = 0;
63static RegisterPass<Hello2>
64Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");