Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1 | //===- TestPasses.cpp - "buggy" passes used to test bugpoint --------------===// |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 345353d | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file contains "buggy" passes that are used to test bugpoint, to check |
| 11 | // that it is narrowing down testcases correctly. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/BasicBlock.h" |
| 16 | #include "llvm/IR/Constant.h" |
Chandler Carruth | 7da14f1 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 17 | #include "llvm/IR/InstVisitor.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Instructions.h" |
| 19 | #include "llvm/IR/Type.h" |
Misha Brukman | 0c2305b | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 20 | #include "llvm/Pass.h" |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 21 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | /// CrashOnCalls - This pass is used to test bugpoint. It intentionally |
| 26 | /// crashes on any call instructions. |
| 27 | class CrashOnCalls : public BasicBlockPass { |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 28 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 29 | static char ID; // Pass ID, replacement for typeid |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 30 | CrashOnCalls() : BasicBlockPass(ID) {} |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 31 | private: |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 32 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 33 | AU.setPreservesAll(); |
| 34 | } |
| 35 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 36 | bool runOnBasicBlock(BasicBlock &BB) override { |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 37 | for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) |
| 38 | if (isa<CallInst>(*I)) |
| 39 | abort(); |
| 40 | |
| 41 | return false; |
| 42 | } |
| 43 | }; |
Dan Gohman | 12bb505 | 2010-08-20 00:56:16 +0000 | [diff] [blame] | 44 | } |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 45 | |
Dan Gohman | 12bb505 | 2010-08-20 00:56:16 +0000 | [diff] [blame] | 46 | char CrashOnCalls::ID = 0; |
| 47 | static RegisterPass<CrashOnCalls> |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 48 | X("bugpoint-crashcalls", |
| 49 | "BugPoint Test Pass - Intentionally crash on CallInsts"); |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 50 | |
| 51 | namespace { |
| 52 | /// DeleteCalls - This pass is used to test bugpoint. It intentionally |
Chris Lattner | 425726d | 2004-03-17 17:29:08 +0000 | [diff] [blame] | 53 | /// deletes some call instructions, "misoptimizing" the program. |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 54 | class DeleteCalls : public BasicBlockPass { |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 55 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 56 | static char ID; // Pass ID, replacement for typeid |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 57 | DeleteCalls() : BasicBlockPass(ID) {} |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 58 | private: |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 59 | bool runOnBasicBlock(BasicBlock &BB) override { |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 60 | for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) |
Chris Lattner | e695da3 | 2003-04-23 16:38:00 +0000 | [diff] [blame] | 61 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 62 | if (!CI->use_empty()) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 63 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 64 | CI->getParent()->getInstList().erase(CI); |
Chris Lattner | 425726d | 2004-03-17 17:29:08 +0000 | [diff] [blame] | 65 | break; |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | }; |
Dan Gohman | 12bb505 | 2010-08-20 00:56:16 +0000 | [diff] [blame] | 70 | } |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 71 | |
Dan Gohman | 12bb505 | 2010-08-20 00:56:16 +0000 | [diff] [blame] | 72 | char DeleteCalls::ID = 0; |
| 73 | static RegisterPass<DeleteCalls> |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 74 | Y("bugpoint-deletecalls", |
| 75 | "BugPoint Test Pass - Intentionally 'misoptimize' CallInsts"); |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 76 | |
| 77 | namespace { |
| 78 | /// CrashOnDeclFunc - This pass is used to test bugpoint. It intentionally |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 79 | /// crashes if the module has an undefined function (ie a function that is |
| 80 | /// defined in an external module). |
| 81 | class CrashOnDeclFunc : public ModulePass { |
| 82 | public: |
| 83 | static char ID; // Pass ID, replacement for typeid |
| 84 | CrashOnDeclFunc() : ModulePass(ID) {} |
| 85 | |
| 86 | private: |
| 87 | bool runOnModule(Module &M) override { |
| 88 | for (auto &F : M.functions()) { |
| 89 | if (F.isDeclaration()) |
| 90 | abort(); |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 91 | } |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 92 | return false; |
| 93 | } |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 94 | }; |
| 95 | } |
| 96 | |
| 97 | char CrashOnDeclFunc::ID = 0; |
| 98 | static RegisterPass<CrashOnDeclFunc> |
| 99 | Z("bugpoint-crash-decl-funcs", |
| 100 | "BugPoint Test Pass - Intentionally crash on declared functions"); |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 101 | |
| 102 | #include <iostream> |
| 103 | namespace { |
| 104 | /// CrashOnOneCU - This pass is used to test bugpoint. It intentionally |
| 105 | /// crashes if the Module has two or more compile units |
| 106 | class CrashOnTooManyCUs : public ModulePass { |
| 107 | public: |
| 108 | static char ID; |
| 109 | CrashOnTooManyCUs() : ModulePass(ID) {} |
| 110 | |
| 111 | private: |
| 112 | bool runOnModule(Module &M) override { |
| 113 | NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu"); |
| 114 | if (!CU_Nodes) |
| 115 | return false; |
| 116 | if (CU_Nodes->getNumOperands() >= 2) |
| 117 | abort(); |
| 118 | return false; |
| 119 | } |
| 120 | }; |
| 121 | } |
| 122 | |
| 123 | char CrashOnTooManyCUs::ID = 0; |
| 124 | static RegisterPass<CrashOnTooManyCUs> |
| 125 | A("bugpoint-crash-too-many-cus", |
| 126 | "BugPoint Test Pass - Intentionally crash on too many CUs"); |