blob: 6b1463685b2ca8d3fea995998906366e63e90600 [file] [log] [blame]
Chris Lattnerafade922002-11-20 22:28:10 +00001//===- TestPasses.cpp - "buggy" passes used to test bugpoint --------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerafade922002-11-20 22:28:10 +00009//
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 Carruth0b8c9a82013-01-02 11:36:10 +000015#include "llvm/IR/BasicBlock.h"
16#include "llvm/IR/Constant.h"
Chandler Carruth67f6bf72014-03-06 03:23:41 +000017#include "llvm/IR/InstVisitor.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/Instructions.h"
19#include "llvm/IR/Type.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000020#include "llvm/Pass.h"
Chris Lattnerafade922002-11-20 22:28:10 +000021
Brian Gaeked0fde302003-11-11 22:41:34 +000022using namespace llvm;
23
Chris Lattnerafade922002-11-20 22:28:10 +000024namespace {
25 /// CrashOnCalls - This pass is used to test bugpoint. It intentionally
26 /// crashes on any call instructions.
27 class CrashOnCalls : public BasicBlockPass {
Devang Patel794fd752007-05-01 21:15:47 +000028 public:
Devang Patel19974732007-05-03 01:11:54 +000029 static char ID; // Pass ID, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +000030 CrashOnCalls() : BasicBlockPass(ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000031 private:
Craig Topperc83e68f2014-03-08 08:27:28 +000032 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattnerafade922002-11-20 22:28:10 +000033 AU.setPreservesAll();
34 }
35
Craig Topperc83e68f2014-03-08 08:27:28 +000036 bool runOnBasicBlock(BasicBlock &BB) override {
Chris Lattnerafade922002-11-20 22:28:10 +000037 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 Gohmana2a3bbc2010-08-20 00:56:16 +000044}
Chris Lattnerafade922002-11-20 22:28:10 +000045
Dan Gohmana2a3bbc2010-08-20 00:56:16 +000046char CrashOnCalls::ID = 0;
47static RegisterPass<CrashOnCalls>
Chris Lattnerafade922002-11-20 22:28:10 +000048 X("bugpoint-crashcalls",
49 "BugPoint Test Pass - Intentionally crash on CallInsts");
Chris Lattnerafade922002-11-20 22:28:10 +000050
51namespace {
52 /// DeleteCalls - This pass is used to test bugpoint. It intentionally
Chris Lattner9d5968d2004-03-17 17:29:08 +000053 /// deletes some call instructions, "misoptimizing" the program.
Chris Lattnerafade922002-11-20 22:28:10 +000054 class DeleteCalls : public BasicBlockPass {
Devang Patel794fd752007-05-01 21:15:47 +000055 public:
Devang Patel19974732007-05-03 01:11:54 +000056 static char ID; // Pass ID, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +000057 DeleteCalls() : BasicBlockPass(ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000058 private:
Craig Topperc83e68f2014-03-08 08:27:28 +000059 bool runOnBasicBlock(BasicBlock &BB) override {
Chris Lattnerafade922002-11-20 22:28:10 +000060 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
Chris Lattner6e96a99c2003-04-23 16:38:00 +000061 if (CallInst *CI = dyn_cast<CallInst>(I)) {
Chris Lattnerafade922002-11-20 22:28:10 +000062 if (!CI->use_empty())
Owen Andersona7235ea2009-07-31 20:28:14 +000063 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
Chris Lattnerafade922002-11-20 22:28:10 +000064 CI->getParent()->getInstList().erase(CI);
Chris Lattner9d5968d2004-03-17 17:29:08 +000065 break;
Chris Lattnerafade922002-11-20 22:28:10 +000066 }
67 return false;
68 }
69 };
Dan Gohmana2a3bbc2010-08-20 00:56:16 +000070}
JF Bastien7b862ec2015-04-20 23:42:22 +000071
Dan Gohmana2a3bbc2010-08-20 00:56:16 +000072char DeleteCalls::ID = 0;
73static RegisterPass<DeleteCalls>
Chris Lattnerafade922002-11-20 22:28:10 +000074 Y("bugpoint-deletecalls",
75 "BugPoint Test Pass - Intentionally 'misoptimize' CallInsts");
JF Bastien7b862ec2015-04-20 23:42:22 +000076
77namespace {
78 /// CrashOnDeclFunc - This pass is used to test bugpoint. It intentionally
Keno Fischer11e0c312015-11-06 00:12:50 +000079/// crashes if the module has an undefined function (ie a function that is
80/// defined in an external module).
81class CrashOnDeclFunc : public ModulePass {
82public:
83 static char ID; // Pass ID, replacement for typeid
84 CrashOnDeclFunc() : ModulePass(ID) {}
85
86private:
87 bool runOnModule(Module &M) override {
88 for (auto &F : M.functions()) {
89 if (F.isDeclaration())
90 abort();
JF Bastien7b862ec2015-04-20 23:42:22 +000091 }
Keno Fischer11e0c312015-11-06 00:12:50 +000092 return false;
93 }
JF Bastien7b862ec2015-04-20 23:42:22 +000094 };
95}
96
97char CrashOnDeclFunc::ID = 0;
98static RegisterPass<CrashOnDeclFunc>
99 Z("bugpoint-crash-decl-funcs",
100 "BugPoint Test Pass - Intentionally crash on declared functions");
Keno Fischer11e0c312015-11-06 00:12:50 +0000101
Keno Fischer11e0c312015-11-06 00:12:50 +0000102namespace {
103/// CrashOnOneCU - This pass is used to test bugpoint. It intentionally
104/// crashes if the Module has two or more compile units
105class CrashOnTooManyCUs : public ModulePass {
106public:
107 static char ID;
108 CrashOnTooManyCUs() : ModulePass(ID) {}
109
110private:
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
122char CrashOnTooManyCUs::ID = 0;
123static RegisterPass<CrashOnTooManyCUs>
124 A("bugpoint-crash-too-many-cus",
125 "BugPoint Test Pass - Intentionally crash on too many CUs");
Brian Gesiak79550f62018-12-19 03:42:19 +0000126
127namespace {
128class CrashOnFunctionAttribute : public FunctionPass {
129public:
130 static char ID; // Pass ID, replacement for typeid
131 CrashOnFunctionAttribute() : FunctionPass(ID) {}
132
133private:
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
147char CrashOnFunctionAttribute::ID = 0;
148static RegisterPass<CrashOnFunctionAttribute>
149 B("bugpoint-crashfuncattr", "BugPoint Test Pass - Intentionally crash on "
150 "function attribute 'bugpoint-crash'");