blob: df8866279f2463a461c24e2cea63483f027ea347 [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 {
28 virtual bool runOnFunction(Function &F) {
Reid Spencer2b6d18a2006-08-07 23:17:24 +000029 HelloCounter++;
30 std::string fname = F.getName();
31 EscapeString(fname);
Bill Wendlingf3baad32006-12-07 01:30:32 +000032 cerr << "Hello: " << fname << "\n";
Chris Lattnered7ac422002-08-08 20:10:38 +000033 return false;
34 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000035 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +000036 RegisterPass<Hello> X("hello", "Hello World Pass");
Chris Lattnered7ac422002-08-08 20:10:38 +000037
38 // Hello2 - The second implementation with getAnalysisUsage implemented.
39 struct Hello2 : public FunctionPass {
40 virtual bool runOnFunction(Function &F) {
Reid Spencer2b6d18a2006-08-07 23:17:24 +000041 HelloCounter++;
42 std::string fname = F.getName();
43 EscapeString(fname);
Bill Wendlingf3baad32006-12-07 01:30:32 +000044 cerr << "Hello: " << fname << "\n";
Chris Lattnered7ac422002-08-08 20:10:38 +000045 return false;
46 }
47
48 // We don't modify the program, so we preserve all analyses
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
50 AU.setPreservesAll();
51 };
Misha Brukmanb1c93172005-04-21 23:48:37 +000052 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +000053 RegisterPass<Hello2> Y("hello2",
54 "Hello World Pass (with getAnalysisUsage implemented)");
Chris Lattnered7ac422002-08-08 20:10:38 +000055}