blob: b7fc870d882294ee8b51b3817c3a59c785c3d7ee [file] [log] [blame]
Chris Lattner180e5682002-08-08 20:10:38 +00001//===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner180e5682002-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 Lattnera5de8232004-08-12 02:44:23 +000017#include <iostream>
Chris Lattnerf7703df2004-01-09 06:12:26 +000018using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000019
Chris Lattner180e5682002-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 }
Misha Brukmanfd939082005-04-21 23:48:37 +000027 };
Chris Lattner180e5682002-08-08 20:10:38 +000028 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 };
Misha Brukmanfd939082005-04-21 23:48:37 +000041 };
Chris Lattner180e5682002-08-08 20:10:38 +000042 RegisterOpt<Hello2> Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");
43}