blob: a91360f962d35e60479cc7a0093f16aa56301be9 [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
15#include "llvm/Pass.h"
16#include "llvm/Function.h"
Reid Spencer2b6d18a2006-08-07 23:17:24 +000017#include "llvm/ADT/StringExtras.h"
18#include "llvm/Support/SlowOperationInformer.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 Lattnered7ac422002-08-08 20:10:38 +000023namespace {
Chris Lattnerec589032006-12-06 01:50:04 +000024 Statistic<> HelloCounter("hellocount",
Reid Spencer2b6d18a2006-08-07 23:17:24 +000025 "Counts number of functions greeted");
Chris Lattnered7ac422002-08-08 20:10:38 +000026 // 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 SlowOperationInformer soi("EscapeString");
30 HelloCounter++;
31 std::string fname = F.getName();
32 EscapeString(fname);
Bill Wendlinga7459ca2006-11-26 09:17:06 +000033 llvm_cerr << "Hello: " << fname << "\n";
Chris Lattnered7ac422002-08-08 20:10:38 +000034 return false;
35 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000036 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +000037 RegisterPass<Hello> X("hello", "Hello World Pass");
Chris Lattnered7ac422002-08-08 20:10:38 +000038
39 // Hello2 - The second implementation with getAnalysisUsage implemented.
40 struct Hello2 : public FunctionPass {
41 virtual bool runOnFunction(Function &F) {
Reid Spencer2b6d18a2006-08-07 23:17:24 +000042 SlowOperationInformer soi("EscapeString");
43 HelloCounter++;
44 std::string fname = F.getName();
45 EscapeString(fname);
Bill Wendlinga7459ca2006-11-26 09:17:06 +000046 llvm_cerr << "Hello: " << fname << "\n";
Chris Lattnered7ac422002-08-08 20:10:38 +000047 return false;
48 }
49
50 // We don't modify the program, so we preserve all analyses
51 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.setPreservesAll();
53 };
Misha Brukmanb1c93172005-04-21 23:48:37 +000054 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +000055 RegisterPass<Hello2> Y("hello2",
56 "Hello World Pass (with getAnalysisUsage implemented)");
Chris Lattnered7ac422002-08-08 20:10:38 +000057}