Files
llvm-project/llvm/lib/Transforms/Hello/Hello.cpp
T

66 lines
1.9 KiB
C++
Raw Normal View History

2002-08-08 20:10:38 +00:00
//===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
2005-04-21 23:48:37 +00:00
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
2005-04-21 23:48:37 +00:00
//
//===----------------------------------------------------------------------===//
2002-08-08 20:10:38 +00:00
//
// This file implements two versions of the LLVM "Hello World" pass described
// in docs/WritingAnLLVMPass.html
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/Function.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
2004-01-09 06:12:26 +00:00
using namespace llvm;
#define DEBUG_TYPE "hello"
2006-12-19 22:24:09 +00:00
STATISTIC(HelloCounter, "Counts number of functions greeted");
2002-08-08 20:10:38 +00:00
namespace {
// Hello - The first implementation, without getAnalysisUsage.
struct Hello : public FunctionPass {
2007-05-06 13:37:16 +00:00
static char ID; // Pass identification, replacement for typeid
Hello() : FunctionPass(ID) {}
bool runOnFunction(Function &F) override {
++HelloCounter;
errs() << "Hello: ";
errs().write_escaped(F.getName()) << '\n';
2002-08-08 20:10:38 +00:00
return false;
}
2005-04-21 23:48:37 +00:00
};
}
char Hello::ID = 0;
static RegisterPass<Hello> X("hello", "Hello World Pass");
2002-08-08 20:10:38 +00:00
namespace {
2002-08-08 20:10:38 +00:00
// Hello2 - The second implementation with getAnalysisUsage implemented.
struct Hello2 : public FunctionPass {
2007-05-06 13:37:16 +00:00
static char ID; // Pass identification, replacement for typeid
Hello2() : FunctionPass(ID) {}
bool runOnFunction(Function &F) override {
++HelloCounter;
errs() << "Hello: ";
errs().write_escaped(F.getName()) << '\n';
2002-08-08 20:10:38 +00:00
return false;
}
2013-09-27 07:36:10 +00:00
// We don't modify the program, so we preserve all analyses.
void getAnalysisUsage(AnalysisUsage &AU) const override {
2002-08-08 20:10:38 +00:00
AU.setPreservesAll();
}
2005-04-21 23:48:37 +00:00
};
}
char Hello2::ID = 0;
static RegisterPass<Hello2>
Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");