blob: 804c3a70b2dd6921bbf276885dc31a52715f427e [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"
19#include "llvm/ADT/Statistic.h"
Chris Lattnera7ba90e2004-08-12 02:44:23 +000020#include <iostream>
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 {
Reid Spencer2b6d18a2006-08-07 23:17:24 +000024 Statistic<int> HelloCounter("hellocount",
25 "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);
33 std::cerr << "Hello: " << fname << "\n";
Chris Lattnered7ac422002-08-08 20:10:38 +000034 return false;
35 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000036 };
Chris Lattnered7ac422002-08-08 20:10:38 +000037 RegisterOpt<Hello> X("hello", "Hello World Pass");
38
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);
46 std::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 Lattnered7ac422002-08-08 20:10:38 +000055 RegisterOpt<Hello2> Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");
56}