blob: 29b9bb8a94eaf86fc3f87008b3bb33de3f0d6482 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Reid Spencer05445e32006-08-07 23:17:24 +000015#include "llvm/ADT/Statistic.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/Function.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/Pass.h"
18#include "llvm/Support/raw_ostream.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Stephen Hinesdce4a402014-05-29 02:49:00 -070021#define DEBUG_TYPE "hello"
22
Chris Lattnercbfdd1f2006-12-19 22:24:09 +000023STATISTIC(HelloCounter, "Counts number of functions greeted");
24
Chris Lattner180e5682002-08-08 20:10:38 +000025namespace {
26 // Hello - The first implementation, without getAnalysisUsage.
27 struct Hello : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000028 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +000029 Hello() : FunctionPass(ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000030
Stephen Hines36b56882014-04-23 16:57:46 -070031 bool runOnFunction(Function &F) override {
Dan Gohmanfe601042010-06-22 15:08:57 +000032 ++HelloCounter;
Daniel Dunbar3446cf12009-10-17 20:43:19 +000033 errs() << "Hello: ";
34 errs().write_escaped(F.getName()) << '\n';
Chris Lattner180e5682002-08-08 20:10:38 +000035 return false;
36 }
Misha Brukmanfd939082005-04-21 23:48:37 +000037 };
Dan Gohman844731a2008-05-13 00:00:25 +000038}
Devang Patel794fd752007-05-01 21:15:47 +000039
Dan Gohman844731a2008-05-13 00:00:25 +000040char Hello::ID = 0;
Owen Andersoncce7f7c2010-10-07 00:31:16 +000041static RegisterPass<Hello> X("hello", "Hello World Pass");
Chris Lattner180e5682002-08-08 20:10:38 +000042
Dan Gohman844731a2008-05-13 00:00:25 +000043namespace {
Chris Lattner180e5682002-08-08 20:10:38 +000044 // Hello2 - The second implementation with getAnalysisUsage implemented.
45 struct Hello2 : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000046 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +000047 Hello2() : FunctionPass(ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000048
Stephen Hines36b56882014-04-23 16:57:46 -070049 bool runOnFunction(Function &F) override {
Dan Gohmanfe601042010-06-22 15:08:57 +000050 ++HelloCounter;
Daniel Dunbar3446cf12009-10-17 20:43:19 +000051 errs() << "Hello: ";
52 errs().write_escaped(F.getName()) << '\n';
Chris Lattner180e5682002-08-08 20:10:38 +000053 return false;
54 }
55
Puyan Lotfi6b4fa222013-09-27 07:36:10 +000056 // We don't modify the program, so we preserve all analyses.
Stephen Hines36b56882014-04-23 16:57:46 -070057 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner180e5682002-08-08 20:10:38 +000058 AU.setPreservesAll();
Douglas Gregorcabdd742009-12-19 07:05:23 +000059 }
Misha Brukmanfd939082005-04-21 23:48:37 +000060 };
Chris Lattner180e5682002-08-08 20:10:38 +000061}
Dan Gohman844731a2008-05-13 00:00:25 +000062
63char Hello2::ID = 0;
Owen Andersoncce7f7c2010-10-07 00:31:16 +000064static RegisterPass<Hello2>
65Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");