LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Hello.cpp
Go to the documentation of this file.
1 //===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements two versions of the LLVM "Hello World" pass described
11 // in docs/WritingAnLLVMPass.html
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #define DEBUG_TYPE "hello"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/Pass.h"
20 using namespace llvm;
21 
22 STATISTIC(HelloCounter, "Counts number of functions greeted");
23 
24 namespace {
25  // Hello - The first implementation, without getAnalysisUsage.
26  struct Hello : public FunctionPass {
27  static char ID; // Pass identification, replacement for typeid
28  Hello() : FunctionPass(ID) {}
29 
30  virtual bool runOnFunction(Function &F) {
31  ++HelloCounter;
32  errs() << "Hello: ";
33  errs().write_escaped(F.getName()) << '\n';
34  return false;
35  }
36  };
37 }
38 
39 char Hello::ID = 0;
40 static RegisterPass<Hello> X("hello", "Hello World Pass");
41 
42 namespace {
43  // Hello2 - The second implementation with getAnalysisUsage implemented.
44  struct Hello2 : public FunctionPass {
45  static char ID; // Pass identification, replacement for typeid
46  Hello2() : FunctionPass(ID) {}
47 
48  virtual bool runOnFunction(Function &F) {
49  ++HelloCounter;
50  errs() << "Hello: ";
51  errs().write_escaped(F.getName()) << '\n';
52  return false;
53  }
54 
55  // We don't modify the program, so we preserve all analyses.
56  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
57  AU.setPreservesAll();
58  }
59  };
60 }
61 
62 char Hello2::ID = 0;
64 Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");
raw_ostream & errs()
F(f)
static RegisterPass< Hello2 > Y("hello2","Hello World Pass (with getAnalysisUsage implemented)")
StringRef getName() const
Definition: Value.cpp:167
STATISTIC(HelloCounter,"Counts number of functions greeted")
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")