blob: d07f6135257f193f7549328b943aba137747eb00 [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"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/Support/Streams.h"
20#include "llvm/ADT/Statistic.h"
21using namespace llvm;
22
23STATISTIC(HelloCounter, "Counts number of functions greeted");
24
25namespace {
26 // Hello - The first implementation, without getAnalysisUsage.
27 struct Hello : public FunctionPass {
28 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000029 Hello() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030
31 virtual bool runOnFunction(Function &F) {
32 HelloCounter++;
33 std::string fname = F.getName();
34 EscapeString(fname);
35 cerr << "Hello: " << fname << "\n";
36 return false;
37 }
38 };
Dan Gohman089efff2008-05-13 00:00:25 +000039}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040
Dan Gohman089efff2008-05-13 00:00:25 +000041char Hello::ID = 0;
42static RegisterPass<Hello> X("hello", "Hello World Pass");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043
Dan Gohman089efff2008-05-13 00:00:25 +000044namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 // Hello2 - The second implementation with getAnalysisUsage implemented.
46 struct Hello2 : public FunctionPass {
47 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000048 Hello2() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049
50 virtual bool runOnFunction(Function &F) {
51 HelloCounter++;
52 std::string fname = F.getName();
53 EscapeString(fname);
54 cerr << "Hello: " << fname << "\n";
55 return false;
56 }
57
58 // We don't modify the program, so we preserve all analyses
59 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.setPreservesAll();
61 };
62 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063}
Dan Gohman089efff2008-05-13 00:00:25 +000064
65char Hello2::ID = 0;
66static RegisterPass<Hello2>
67Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");