Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1 | //===- TestPasses.cpp - "buggy" passes used to test bugpoint --------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 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 | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | afade92 | 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 | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/BasicBlock.h" |
| 16 | #include "llvm/IR/Constant.h" |
Chandler Carruth | 67f6bf7 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 17 | #include "llvm/IR/InstVisitor.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Instructions.h" |
| 19 | #include "llvm/IR/Type.h" |
Misha Brukman | e49603d | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 20 | #include "llvm/Pass.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 21 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Chris Lattner | afade92 | 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 | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 28 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 29 | static char ID; // Pass ID, replacement for typeid |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 30 | CrashOnCalls() : BasicBlockPass(ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 31 | private: |
Craig Topper | c83e68f | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 32 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 33 | AU.setPreservesAll(); |
| 34 | } |
| 35 | |
Craig Topper | c83e68f | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 36 | bool runOnBasicBlock(BasicBlock &BB) override { |
Chris Lattner | afade92 | 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 | a2a3bbc | 2010-08-20 00:56:16 +0000 | [diff] [blame] | 44 | } |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 45 | |
Dan Gohman | a2a3bbc | 2010-08-20 00:56:16 +0000 | [diff] [blame] | 46 | char CrashOnCalls::ID = 0; |
| 47 | static RegisterPass<CrashOnCalls> |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 48 | X("bugpoint-crashcalls", |
| 49 | "BugPoint Test Pass - Intentionally crash on CallInsts"); |
Chris Lattner | afade92 | 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 | 9d5968d | 2004-03-17 17:29:08 +0000 | [diff] [blame] | 53 | /// deletes some call instructions, "misoptimizing" the program. |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 54 | class DeleteCalls : public BasicBlockPass { |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 55 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 56 | static char ID; // Pass ID, replacement for typeid |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 57 | DeleteCalls() : BasicBlockPass(ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 58 | private: |
Craig Topper | c83e68f | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 59 | bool runOnBasicBlock(BasicBlock &BB) override { |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 60 | for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) |
Chris Lattner | 6e96a99c | 2003-04-23 16:38:00 +0000 | [diff] [blame] | 61 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 62 | if (!CI->use_empty()) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 63 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 64 | CI->getParent()->getInstList().erase(CI); |
Chris Lattner | 9d5968d | 2004-03-17 17:29:08 +0000 | [diff] [blame] | 65 | break; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | }; |
Dan Gohman | a2a3bbc | 2010-08-20 00:56:16 +0000 | [diff] [blame] | 70 | } |
JF Bastien | 7b862ec | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 71 | |
Dan Gohman | a2a3bbc | 2010-08-20 00:56:16 +0000 | [diff] [blame] | 72 | char DeleteCalls::ID = 0; |
| 73 | static RegisterPass<DeleteCalls> |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 74 | Y("bugpoint-deletecalls", |
| 75 | "BugPoint Test Pass - Intentionally 'misoptimize' CallInsts"); |
JF Bastien | 7b862ec | 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 | 11e0c31 | 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 | 7b862ec | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 91 | } |
Keno Fischer | 11e0c31 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 92 | return false; |
| 93 | } |
JF Bastien | 7b862ec | 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 | 11e0c31 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 101 | |
Keno Fischer | 11e0c31 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 102 | namespace { |
| 103 | /// CrashOnOneCU - This pass is used to test bugpoint. It intentionally |
| 104 | /// crashes if the Module has two or more compile units |
| 105 | class CrashOnTooManyCUs : public ModulePass { |
| 106 | public: |
| 107 | static char ID; |
| 108 | CrashOnTooManyCUs() : ModulePass(ID) {} |
| 109 | |
| 110 | private: |
| 111 | bool runOnModule(Module &M) override { |
| 112 | NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu"); |
| 113 | if (!CU_Nodes) |
| 114 | return false; |
| 115 | if (CU_Nodes->getNumOperands() >= 2) |
| 116 | abort(); |
| 117 | return false; |
| 118 | } |
| 119 | }; |
| 120 | } |
| 121 | |
| 122 | char CrashOnTooManyCUs::ID = 0; |
| 123 | static RegisterPass<CrashOnTooManyCUs> |
| 124 | A("bugpoint-crash-too-many-cus", |
| 125 | "BugPoint Test Pass - Intentionally crash on too many CUs"); |
Brian Gesiak | 79550f6 | 2018-12-19 03:42:19 +0000 | [diff] [blame] | 126 | |
| 127 | namespace { |
| 128 | class CrashOnFunctionAttribute : public FunctionPass { |
| 129 | public: |
| 130 | static char ID; // Pass ID, replacement for typeid |
| 131 | CrashOnFunctionAttribute() : FunctionPass(ID) {} |
| 132 | |
| 133 | private: |
| 134 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 135 | AU.setPreservesAll(); |
| 136 | } |
| 137 | |
| 138 | bool runOnFunction(Function &F) override { |
| 139 | AttributeSet A = F.getAttributes().getFnAttributes(); |
| 140 | if (A.hasAttribute("bugpoint-crash")) |
| 141 | abort(); |
| 142 | return false; |
| 143 | } |
| 144 | }; |
| 145 | } // namespace |
| 146 | |
| 147 | char CrashOnFunctionAttribute::ID = 0; |
| 148 | static RegisterPass<CrashOnFunctionAttribute> |
| 149 | B("bugpoint-crashfuncattr", "BugPoint Test Pass - Intentionally crash on " |
| 150 | "function attribute 'bugpoint-crash'"); |