blob: 838d5505490f5841dc2fd1b09cb0ea09d13ab861 [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
Owen Anderson75693222010-08-06 18:33:48 +000028 Hello() : FunctionPass(ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029
30 virtual bool runOnFunction(Function &F) {
Dan Gohman559d5132010-06-22 15:08:57 +000031 ++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;
Owen Anderson6374c3d2010-07-21 22:09:45 +000040INITIALIZE_PASS(Hello, "hello", "Hello World Pass", false, false);
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
Owen Anderson75693222010-08-06 18:33:48 +000046 Hello2() : FunctionPass(ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047
48 virtual bool runOnFunction(Function &F) {
Dan Gohman559d5132010-06-22 15:08:57 +000049 ++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;
Owen Anderson6374c3d2010-07-21 22:09:45 +000063INITIALIZE_PASS(Hello2, "hello2",
64 "Hello World Pass (with getAnalysisUsage implemented)",
65 false, false);