blob: b0adb5401f8912427c0c69ee1ccaed299e521ab2 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukmanb1c93172005-04-21 23:48:37 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattnered7ac422002-08-08 20:10:38 +00008//
9// This file implements two versions of the LLVM "Hello World" pass described
10// in docs/WritingAnLLVMPass.html
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer2b6d18a2006-08-07 23:17:24 +000014#include "llvm/ADT/Statistic.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Function.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/Pass.h"
17#include "llvm/Support/raw_ostream.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000018using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000019
Chandler Carruth964daaa2014-04-22 02:55:47 +000020#define DEBUG_TYPE "hello"
21
Chris Lattner1fa216f2006-12-19 22:24:09 +000022STATISTIC(HelloCounter, "Counts number of functions greeted");
23
Chris Lattnered7ac422002-08-08 20:10:38 +000024namespace {
25 // Hello - The first implementation, without getAnalysisUsage.
26 struct Hello : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000027 static char ID; // Pass identification, replacement for typeid
Owen Andersona7aed182010-08-06 18:33:48 +000028 Hello() : FunctionPass(ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000029
Craig Topper3e4c6972014-03-05 09:10:37 +000030 bool runOnFunction(Function &F) override {
Dan Gohmand2d1ae12010-06-22 15:08:57 +000031 ++HelloCounter;
Daniel Dunbar8eff29d2009-10-17 20:43:19 +000032 errs() << "Hello: ";
33 errs().write_escaped(F.getName()) << '\n';
Chris Lattnered7ac422002-08-08 20:10:38 +000034 return false;
35 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000036 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000037}
Devang Patel09f162c2007-05-01 21:15:47 +000038
Dan Gohmand78c4002008-05-13 00:00:25 +000039char Hello::ID = 0;
Owen Anderson6da4d822010-10-07 00:31:16 +000040static RegisterPass<Hello> X("hello", "Hello World Pass");
Chris Lattnered7ac422002-08-08 20:10:38 +000041
Dan Gohmand78c4002008-05-13 00:00:25 +000042namespace {
Chris Lattnered7ac422002-08-08 20:10:38 +000043 // Hello2 - The second implementation with getAnalysisUsage implemented.
44 struct Hello2 : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000045 static char ID; // Pass identification, replacement for typeid
Owen Andersona7aed182010-08-06 18:33:48 +000046 Hello2() : FunctionPass(ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000047
Craig Topper3e4c6972014-03-05 09:10:37 +000048 bool runOnFunction(Function &F) override {
Dan Gohmand2d1ae12010-06-22 15:08:57 +000049 ++HelloCounter;
Daniel Dunbar8eff29d2009-10-17 20:43:19 +000050 errs() << "Hello: ";
51 errs().write_escaped(F.getName()) << '\n';
Chris Lattnered7ac422002-08-08 20:10:38 +000052 return false;
53 }
54
Puyan Lotfi74e38de2013-09-27 07:36:10 +000055 // We don't modify the program, so we preserve all analyses.
Craig Topper3e4c6972014-03-05 09:10:37 +000056 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattnered7ac422002-08-08 20:10:38 +000057 AU.setPreservesAll();
Douglas Gregor740ab382009-12-19 07:05:23 +000058 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000059 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000060}
Dan Gohmand78c4002008-05-13 00:00:25 +000061
62char Hello2::ID = 0;
Owen Anderson6da4d822010-10-07 00:31:16 +000063static RegisterPass<Hello2>
64Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");