blob: 36a60fa9d7ca52f8a5b698771dccb938887e7678 [file] [log] [blame]
Chris Lattnered7ac422002-08-08 20:10:38 +00001//===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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"
Chris Lattnera7ba90e2004-08-12 02:44:23 +000017#include <iostream>
Chris Lattnerdf3c3422004-01-09 06:12:26 +000018using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000019
Chris Lattnered7ac422002-08-08 20:10:38 +000020namespace {
21 // Hello - The first implementation, without getAnalysisUsage.
22 struct Hello : public FunctionPass {
23 virtual bool runOnFunction(Function &F) {
24 std::cerr << "Hello: " << F.getName() << "\n";
25 return false;
26 }
27 };
28 RegisterOpt<Hello> X("hello", "Hello World Pass");
29
30 // Hello2 - The second implementation with getAnalysisUsage implemented.
31 struct Hello2 : public FunctionPass {
32 virtual bool runOnFunction(Function &F) {
33 std::cerr << "Hello: " << F.getName() << "\n";
34 return false;
35 }
36
37 // We don't modify the program, so we preserve all analyses
38 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
39 AU.setPreservesAll();
40 };
41 };
42 RegisterOpt<Hello2> Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");
43}