1*9880d681SAndroid Build Coastguard Worker //===- TestPasses.cpp - "buggy" passes used to test bugpoint --------------===// 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 contains "buggy" passes that are used to test bugpoint, to check 11*9880d681SAndroid Build Coastguard Worker // that it is narrowing down testcases correctly. 12*9880d681SAndroid Build Coastguard Worker // 13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 14*9880d681SAndroid Build Coastguard Worker 15*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/BasicBlock.h" 16*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constant.h" 17*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InstVisitor.h" 18*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h" 19*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Type.h" 20*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h" 21*9880d681SAndroid Build Coastguard Worker 22*9880d681SAndroid Build Coastguard Worker using namespace llvm; 23*9880d681SAndroid Build Coastguard Worker 24*9880d681SAndroid Build Coastguard Worker namespace { 25*9880d681SAndroid Build Coastguard Worker /// CrashOnCalls - This pass is used to test bugpoint. It intentionally 26*9880d681SAndroid Build Coastguard Worker /// crashes on any call instructions. 27*9880d681SAndroid Build Coastguard Worker class CrashOnCalls : public BasicBlockPass { 28*9880d681SAndroid Build Coastguard Worker public: 29*9880d681SAndroid Build Coastguard Worker static char ID; // Pass ID, replacement for typeid CrashOnCalls()30*9880d681SAndroid Build Coastguard Worker CrashOnCalls() : BasicBlockPass(ID) {} 31*9880d681SAndroid Build Coastguard Worker private: getAnalysisUsage(AnalysisUsage & AU) const32*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override { 33*9880d681SAndroid Build Coastguard Worker AU.setPreservesAll(); 34*9880d681SAndroid Build Coastguard Worker } 35*9880d681SAndroid Build Coastguard Worker runOnBasicBlock(BasicBlock & BB)36*9880d681SAndroid Build Coastguard Worker bool runOnBasicBlock(BasicBlock &BB) override { 37*9880d681SAndroid Build Coastguard Worker for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) 38*9880d681SAndroid Build Coastguard Worker if (isa<CallInst>(*I)) 39*9880d681SAndroid Build Coastguard Worker abort(); 40*9880d681SAndroid Build Coastguard Worker 41*9880d681SAndroid Build Coastguard Worker return false; 42*9880d681SAndroid Build Coastguard Worker } 43*9880d681SAndroid Build Coastguard Worker }; 44*9880d681SAndroid Build Coastguard Worker } 45*9880d681SAndroid Build Coastguard Worker 46*9880d681SAndroid Build Coastguard Worker char CrashOnCalls::ID = 0; 47*9880d681SAndroid Build Coastguard Worker static RegisterPass<CrashOnCalls> 48*9880d681SAndroid Build Coastguard Worker X("bugpoint-crashcalls", 49*9880d681SAndroid Build Coastguard Worker "BugPoint Test Pass - Intentionally crash on CallInsts"); 50*9880d681SAndroid Build Coastguard Worker 51*9880d681SAndroid Build Coastguard Worker namespace { 52*9880d681SAndroid Build Coastguard Worker /// DeleteCalls - This pass is used to test bugpoint. It intentionally 53*9880d681SAndroid Build Coastguard Worker /// deletes some call instructions, "misoptimizing" the program. 54*9880d681SAndroid Build Coastguard Worker class DeleteCalls : public BasicBlockPass { 55*9880d681SAndroid Build Coastguard Worker public: 56*9880d681SAndroid Build Coastguard Worker static char ID; // Pass ID, replacement for typeid DeleteCalls()57*9880d681SAndroid Build Coastguard Worker DeleteCalls() : BasicBlockPass(ID) {} 58*9880d681SAndroid Build Coastguard Worker private: runOnBasicBlock(BasicBlock & BB)59*9880d681SAndroid Build Coastguard Worker bool runOnBasicBlock(BasicBlock &BB) override { 60*9880d681SAndroid Build Coastguard Worker for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) 61*9880d681SAndroid Build Coastguard Worker if (CallInst *CI = dyn_cast<CallInst>(I)) { 62*9880d681SAndroid Build Coastguard Worker if (!CI->use_empty()) 63*9880d681SAndroid Build Coastguard Worker CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); 64*9880d681SAndroid Build Coastguard Worker CI->getParent()->getInstList().erase(CI); 65*9880d681SAndroid Build Coastguard Worker break; 66*9880d681SAndroid Build Coastguard Worker } 67*9880d681SAndroid Build Coastguard Worker return false; 68*9880d681SAndroid Build Coastguard Worker } 69*9880d681SAndroid Build Coastguard Worker }; 70*9880d681SAndroid Build Coastguard Worker } 71*9880d681SAndroid Build Coastguard Worker 72*9880d681SAndroid Build Coastguard Worker char DeleteCalls::ID = 0; 73*9880d681SAndroid Build Coastguard Worker static RegisterPass<DeleteCalls> 74*9880d681SAndroid Build Coastguard Worker Y("bugpoint-deletecalls", 75*9880d681SAndroid Build Coastguard Worker "BugPoint Test Pass - Intentionally 'misoptimize' CallInsts"); 76*9880d681SAndroid Build Coastguard Worker 77*9880d681SAndroid Build Coastguard Worker namespace { 78*9880d681SAndroid Build Coastguard Worker /// CrashOnDeclFunc - This pass is used to test bugpoint. It intentionally 79*9880d681SAndroid Build Coastguard Worker /// crashes if the module has an undefined function (ie a function that is 80*9880d681SAndroid Build Coastguard Worker /// defined in an external module). 81*9880d681SAndroid Build Coastguard Worker class CrashOnDeclFunc : public ModulePass { 82*9880d681SAndroid Build Coastguard Worker public: 83*9880d681SAndroid Build Coastguard Worker static char ID; // Pass ID, replacement for typeid CrashOnDeclFunc()84*9880d681SAndroid Build Coastguard Worker CrashOnDeclFunc() : ModulePass(ID) {} 85*9880d681SAndroid Build Coastguard Worker 86*9880d681SAndroid Build Coastguard Worker private: runOnModule(Module & M)87*9880d681SAndroid Build Coastguard Worker bool runOnModule(Module &M) override { 88*9880d681SAndroid Build Coastguard Worker for (auto &F : M.functions()) { 89*9880d681SAndroid Build Coastguard Worker if (F.isDeclaration()) 90*9880d681SAndroid Build Coastguard Worker abort(); 91*9880d681SAndroid Build Coastguard Worker } 92*9880d681SAndroid Build Coastguard Worker return false; 93*9880d681SAndroid Build Coastguard Worker } 94*9880d681SAndroid Build Coastguard Worker }; 95*9880d681SAndroid Build Coastguard Worker } 96*9880d681SAndroid Build Coastguard Worker 97*9880d681SAndroid Build Coastguard Worker char CrashOnDeclFunc::ID = 0; 98*9880d681SAndroid Build Coastguard Worker static RegisterPass<CrashOnDeclFunc> 99*9880d681SAndroid Build Coastguard Worker Z("bugpoint-crash-decl-funcs", 100*9880d681SAndroid Build Coastguard Worker "BugPoint Test Pass - Intentionally crash on declared functions"); 101*9880d681SAndroid Build Coastguard Worker 102*9880d681SAndroid Build Coastguard Worker #include <iostream> 103*9880d681SAndroid Build Coastguard Worker namespace { 104*9880d681SAndroid Build Coastguard Worker /// CrashOnOneCU - This pass is used to test bugpoint. It intentionally 105*9880d681SAndroid Build Coastguard Worker /// crashes if the Module has two or more compile units 106*9880d681SAndroid Build Coastguard Worker class CrashOnTooManyCUs : public ModulePass { 107*9880d681SAndroid Build Coastguard Worker public: 108*9880d681SAndroid Build Coastguard Worker static char ID; CrashOnTooManyCUs()109*9880d681SAndroid Build Coastguard Worker CrashOnTooManyCUs() : ModulePass(ID) {} 110*9880d681SAndroid Build Coastguard Worker 111*9880d681SAndroid Build Coastguard Worker private: runOnModule(Module & M)112*9880d681SAndroid Build Coastguard Worker bool runOnModule(Module &M) override { 113*9880d681SAndroid Build Coastguard Worker NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu"); 114*9880d681SAndroid Build Coastguard Worker if (!CU_Nodes) 115*9880d681SAndroid Build Coastguard Worker return false; 116*9880d681SAndroid Build Coastguard Worker if (CU_Nodes->getNumOperands() >= 2) 117*9880d681SAndroid Build Coastguard Worker abort(); 118*9880d681SAndroid Build Coastguard Worker return false; 119*9880d681SAndroid Build Coastguard Worker } 120*9880d681SAndroid Build Coastguard Worker }; 121*9880d681SAndroid Build Coastguard Worker } 122*9880d681SAndroid Build Coastguard Worker 123*9880d681SAndroid Build Coastguard Worker char CrashOnTooManyCUs::ID = 0; 124*9880d681SAndroid Build Coastguard Worker static RegisterPass<CrashOnTooManyCUs> 125*9880d681SAndroid Build Coastguard Worker A("bugpoint-crash-too-many-cus", 126*9880d681SAndroid Build Coastguard Worker "BugPoint Test Pass - Intentionally crash on too many CUs"); 127