1*9880d681SAndroid Build Coastguard Worker //===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===// 2*9880d681SAndroid Build Coastguard Worker // 3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure 4*9880d681SAndroid Build Coastguard Worker // 5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source 6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details. 7*9880d681SAndroid Build Coastguard Worker // 8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 9*9880d681SAndroid Build Coastguard Worker // 10*9880d681SAndroid Build Coastguard Worker // This file implements two versions of the LLVM "Hello World" pass described 11*9880d681SAndroid Build Coastguard Worker // in docs/WritingAnLLVMPass.html 12*9880d681SAndroid Build Coastguard Worker // 13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 14*9880d681SAndroid Build Coastguard Worker 15*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h" 16*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h" 17*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h" 18*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h" 19*9880d681SAndroid Build Coastguard Worker using namespace llvm; 20*9880d681SAndroid Build Coastguard Worker 21*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "hello" 22*9880d681SAndroid Build Coastguard Worker 23*9880d681SAndroid Build Coastguard Worker STATISTIC(HelloCounter, "Counts number of functions greeted"); 24*9880d681SAndroid Build Coastguard Worker 25*9880d681SAndroid Build Coastguard Worker namespace { 26*9880d681SAndroid Build Coastguard Worker // Hello - The first implementation, without getAnalysisUsage. 27*9880d681SAndroid Build Coastguard Worker struct Hello : public FunctionPass { 28*9880d681SAndroid Build Coastguard Worker static char ID; // Pass identification, replacement for typeid Hello__anonf22d077d0111::Hello29*9880d681SAndroid Build Coastguard Worker Hello() : FunctionPass(ID) {} 30*9880d681SAndroid Build Coastguard Worker runOnFunction__anonf22d077d0111::Hello31*9880d681SAndroid Build Coastguard Worker bool runOnFunction(Function &F) override { 32*9880d681SAndroid Build Coastguard Worker ++HelloCounter; 33*9880d681SAndroid Build Coastguard Worker errs() << "Hello: "; 34*9880d681SAndroid Build Coastguard Worker errs().write_escaped(F.getName()) << '\n'; 35*9880d681SAndroid Build Coastguard Worker return false; 36*9880d681SAndroid Build Coastguard Worker } 37*9880d681SAndroid Build Coastguard Worker }; 38*9880d681SAndroid Build Coastguard Worker } 39*9880d681SAndroid Build Coastguard Worker 40*9880d681SAndroid Build Coastguard Worker char Hello::ID = 0; 41*9880d681SAndroid Build Coastguard Worker static RegisterPass<Hello> X("hello", "Hello World Pass"); 42*9880d681SAndroid Build Coastguard Worker 43*9880d681SAndroid Build Coastguard Worker namespace { 44*9880d681SAndroid Build Coastguard Worker // Hello2 - The second implementation with getAnalysisUsage implemented. 45*9880d681SAndroid Build Coastguard Worker struct Hello2 : public FunctionPass { 46*9880d681SAndroid Build Coastguard Worker static char ID; // Pass identification, replacement for typeid Hello2__anonf22d077d0211::Hello247*9880d681SAndroid Build Coastguard Worker Hello2() : FunctionPass(ID) {} 48*9880d681SAndroid Build Coastguard Worker runOnFunction__anonf22d077d0211::Hello249*9880d681SAndroid Build Coastguard Worker bool runOnFunction(Function &F) override { 50*9880d681SAndroid Build Coastguard Worker ++HelloCounter; 51*9880d681SAndroid Build Coastguard Worker errs() << "Hello: "; 52*9880d681SAndroid Build Coastguard Worker errs().write_escaped(F.getName()) << '\n'; 53*9880d681SAndroid Build Coastguard Worker return false; 54*9880d681SAndroid Build Coastguard Worker } 55*9880d681SAndroid Build Coastguard Worker 56*9880d681SAndroid Build Coastguard Worker // We don't modify the program, so we preserve all analyses. getAnalysisUsage__anonf22d077d0211::Hello257*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override { 58*9880d681SAndroid Build Coastguard Worker AU.setPreservesAll(); 59*9880d681SAndroid Build Coastguard Worker } 60*9880d681SAndroid Build Coastguard Worker }; 61*9880d681SAndroid Build Coastguard Worker } 62*9880d681SAndroid Build Coastguard Worker 63*9880d681SAndroid Build Coastguard Worker char Hello2::ID = 0; 64*9880d681SAndroid Build Coastguard Worker static RegisterPass<Hello2> 65*9880d681SAndroid Build Coastguard Worker Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)"); 66