blob: 29b9bb8a94eaf86fc3f87008b3bb33de3f0d6482 [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//
Chris Lattnerf3ebc3f2007-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 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
Reid Spencer2b6d18a2006-08-07 23:17:24 +000015#include "llvm/ADT/Statistic.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Function.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Pass.h"
18#include "llvm/Support/raw_ostream.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000019using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000020
Chandler Carruth964daaa2014-04-22 02:55:47 +000021#define DEBUG_TYPE "hello"
22
Chris Lattner1fa216f2006-12-19 22:24:09 +000023STATISTIC(HelloCounter, "Counts number of functions greeted");
24
Chris Lattnered7ac422002-08-08 20:10:38 +000025namespace {
26 // Hello - The first implementation, without getAnalysisUsage.
27 struct Hello : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000028 static char ID; // Pass identification, replacement for typeid
Owen Andersona7aed182010-08-06 18:33:48 +000029 Hello() : FunctionPass(ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000030
Craig Topper3e4c6972014-03-05 09:10:37 +000031 bool runOnFunction(Function &F) override {
Dan Gohmand2d1ae12010-06-22 15:08:57 +000032 ++HelloCounter;
Daniel Dunbar8eff29d2009-10-17 20:43:19 +000033 errs() << "Hello: ";
34 errs().write_escaped(F.getName()) << '\n';
Chris Lattnered7ac422002-08-08 20:10:38 +000035 return false;
36 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000037 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000038}
Devang Patel09f162c2007-05-01 21:15:47 +000039
Dan Gohmand78c4002008-05-13 00:00:25 +000040char Hello::ID = 0;
Owen Anderson6da4d822010-10-07 00:31:16 +000041static RegisterPass<Hello> X("hello", "Hello World Pass");
Chris Lattnered7ac422002-08-08 20:10:38 +000042
Dan Gohmand78c4002008-05-13 00:00:25 +000043namespace {
Chris Lattnered7ac422002-08-08 20:10:38 +000044 // Hello2 - The second implementation with getAnalysisUsage implemented.
45 struct Hello2 : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000046 static char ID; // Pass identification, replacement for typeid
Owen Andersona7aed182010-08-06 18:33:48 +000047 Hello2() : FunctionPass(ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000048
Craig Topper3e4c6972014-03-05 09:10:37 +000049 bool runOnFunction(Function &F) override {
Dan Gohmand2d1ae12010-06-22 15:08:57 +000050 ++HelloCounter;
Daniel Dunbar8eff29d2009-10-17 20:43:19 +000051 errs() << "Hello: ";
52 errs().write_escaped(F.getName()) << '\n';
Chris Lattnered7ac422002-08-08 20:10:38 +000053 return false;
54 }
55
Puyan Lotfi74e38de2013-09-27 07:36:10 +000056 // We don't modify the program, so we preserve all analyses.
Craig Topper3e4c6972014-03-05 09:10:37 +000057 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattnered7ac422002-08-08 20:10:38 +000058 AU.setPreservesAll();
Douglas Gregor740ab382009-12-19 07:05:23 +000059 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000060 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000061}
Dan Gohmand78c4002008-05-13 00:00:25 +000062
63char Hello2::ID = 0;
Owen Anderson6da4d822010-10-07 00:31:16 +000064static RegisterPass<Hello2>
65Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");