opnsense-src/lib/Transforms/Hello/Hello.cpp

66 lines
1.9 KiB
C++
Raw Normal View History

2009-06-02 13:52:33 -04:00
//===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// 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"
2009-06-02 13:52:33 -04:00
#include "llvm/Pass.h"
2009-10-14 13:57:32 -04:00
#include "llvm/Support/raw_ostream.h"
2009-06-02 13:52:33 -04:00
using namespace llvm;
#define DEBUG_TYPE "hello"
2009-06-02 13:52:33 -04:00
STATISTIC(HelloCounter, "Counts number of functions greeted");
namespace {
// Hello - The first implementation, without getAnalysisUsage.
struct Hello : public FunctionPass {
static char ID; // Pass identification, replacement for typeid
Hello() : FunctionPass(ID) {}
2009-06-02 13:52:33 -04:00
bool runOnFunction(Function &F) override {
2010-07-13 13:19:57 -04:00
++HelloCounter;
2009-10-23 10:19:52 -04:00
errs() << "Hello: ";
errs().write_escaped(F.getName()) << '\n';
2009-06-02 13:52:33 -04:00
return false;
}
};
}
2009-06-02 13:52:33 -04:00
char Hello::ID = 0;
static RegisterPass<Hello> X("hello", "Hello World Pass");
2009-06-02 13:52:33 -04:00
namespace {
// Hello2 - The second implementation with getAnalysisUsage implemented.
struct Hello2 : public FunctionPass {
static char ID; // Pass identification, replacement for typeid
Hello2() : FunctionPass(ID) {}
2009-06-02 13:52:33 -04:00
bool runOnFunction(Function &F) override {
2010-07-13 13:19:57 -04:00
++HelloCounter;
2009-10-23 10:19:52 -04:00
errs() << "Hello: ";
errs().write_escaped(F.getName()) << '\n';
2009-06-02 13:52:33 -04:00
return false;
}
// We don't modify the program, so we preserve all analyses.
void getAnalysisUsage(AnalysisUsage &AU) const override {
2009-06-02 13:52:33 -04:00
AU.setPreservesAll();
2010-01-01 05:31:22 -05:00
}
2009-06-02 13:52:33 -04:00
};
}
2009-06-02 13:52:33 -04:00
char Hello2::ID = 0;
static RegisterPass<Hello2>
Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");