blob: d9af9df18b5910efa5b0246671888646f585e5db [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
Chris Lattnered7ac422002-08-08 20:10:38 +000016#include "llvm/Pass.h"
17#include "llvm/Function.h"
Reid Spencer2b6d18a2006-08-07 23:17:24 +000018#include "llvm/ADT/StringExtras.h"
Bill Wendlinga7459ca2006-11-26 09:17:06 +000019#include "llvm/Support/Streams.h"
Reid Spencer2b6d18a2006-08-07 23:17:24 +000020#include "llvm/ADT/Statistic.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000021using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000022
Chris Lattner1fa216f2006-12-19 22:24:09 +000023STATISTIC(HelloCounter, "Counts number of functions greeted");
24
Chris Lattnered7ac422002-08-08 20:10:38 +000025namespace {
26 // Hello - The first implementation, without getAnalysisUsage.
27 struct Hello : public FunctionPass {
Devang Patel09f162c2007-05-01 21:15:47 +000028 static const int ID; // Pass identifcation, replacement for typeid
29 Hello() : FunctionPass((intptr_t)&ID) {}
30
Chris Lattnered7ac422002-08-08 20:10:38 +000031 virtual bool runOnFunction(Function &F) {
Reid Spencer2b6d18a2006-08-07 23:17:24 +000032 HelloCounter++;
33 std::string fname = F.getName();
34 EscapeString(fname);
Bill Wendlingf3baad32006-12-07 01:30:32 +000035 cerr << "Hello: " << fname << "\n";
Chris Lattnered7ac422002-08-08 20:10:38 +000036 return false;
37 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000038 };
Devang Patel09f162c2007-05-01 21:15:47 +000039
40 const int Hello::ID = 0;
Chris Lattnerc2d3d312006-08-27 22:42:52 +000041 RegisterPass<Hello> X("hello", "Hello World Pass");
Chris Lattnered7ac422002-08-08 20:10:38 +000042
43 // Hello2 - The second implementation with getAnalysisUsage implemented.
44 struct Hello2 : public FunctionPass {
Devang Patel09f162c2007-05-01 21:15:47 +000045 static const int ID; // Pass identifcation, replacement for typeid
46 Hello2() : FunctionPass((intptr_t)&ID) {}
47
Chris Lattnered7ac422002-08-08 20:10:38 +000048 virtual bool runOnFunction(Function &F) {
Reid Spencer2b6d18a2006-08-07 23:17:24 +000049 HelloCounter++;
50 std::string fname = F.getName();
51 EscapeString(fname);
Bill Wendlingf3baad32006-12-07 01:30:32 +000052 cerr << "Hello: " << fname << "\n";
Chris Lattnered7ac422002-08-08 20:10:38 +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 };
Misha Brukmanb1c93172005-04-21 23:48:37 +000060 };
Devang Patel09f162c2007-05-01 21:15:47 +000061 const int Hello2::ID = 0;
Chris Lattnerc2d3d312006-08-27 22:42:52 +000062 RegisterPass<Hello2> Y("hello2",
63 "Hello World Pass (with getAnalysisUsage implemented)");
Chris Lattnered7ac422002-08-08 20:10:38 +000064}