blob: c1eb0fbb67c16ad8ffa00f65f8d9fba4b8031f69 [file] [log] [blame]
Chris Lattner73a6bdd2002-11-20 22:28:10 +00001//===- TestPasses.cpp - "buggy" passes used to test bugpoint --------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-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 Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner73a6bdd2002-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 Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/BasicBlock.h"
16#include "llvm/IR/Constant.h"
Chandler Carruth7da14f12014-03-06 03:23:41 +000017#include "llvm/IR/InstVisitor.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Instructions.h"
19#include "llvm/IR/Type.h"
Misha Brukman0c2305b2003-08-07 21:19:30 +000020#include "llvm/Pass.h"
Chris Lattner73a6bdd2002-11-20 22:28:10 +000021
Brian Gaeke960707c2003-11-11 22:41:34 +000022using namespace llvm;
23
Chris Lattner73a6bdd2002-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 Patel09f162c2007-05-01 21:15:47 +000028 public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000029 static char ID; // Pass ID, replacement for typeid
Owen Andersona7aed182010-08-06 18:33:48 +000030 CrashOnCalls() : BasicBlockPass(ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000031 private:
Craig Toppere56917c2014-03-08 08:27:28 +000032 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner73a6bdd2002-11-20 22:28:10 +000033 AU.setPreservesAll();
34 }
35
Craig Toppere56917c2014-03-08 08:27:28 +000036 bool runOnBasicBlock(BasicBlock &BB) override {
Chris Lattner73a6bdd2002-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 Gohman12bb5052010-08-20 00:56:16 +000044}
Chris Lattner73a6bdd2002-11-20 22:28:10 +000045
Dan Gohman12bb5052010-08-20 00:56:16 +000046char CrashOnCalls::ID = 0;
47static RegisterPass<CrashOnCalls>
Chris Lattner73a6bdd2002-11-20 22:28:10 +000048 X("bugpoint-crashcalls",
49 "BugPoint Test Pass - Intentionally crash on CallInsts");
Chris Lattner73a6bdd2002-11-20 22:28:10 +000050
51namespace {
52 /// DeleteCalls - This pass is used to test bugpoint. It intentionally
Chris Lattner425726d2004-03-17 17:29:08 +000053 /// deletes some call instructions, "misoptimizing" the program.
Chris Lattner73a6bdd2002-11-20 22:28:10 +000054 class DeleteCalls : public BasicBlockPass {
Devang Patel09f162c2007-05-01 21:15:47 +000055 public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000056 static char ID; // Pass ID, replacement for typeid
Owen Andersona7aed182010-08-06 18:33:48 +000057 DeleteCalls() : BasicBlockPass(ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000058 private:
Craig Toppere56917c2014-03-08 08:27:28 +000059 bool runOnBasicBlock(BasicBlock &BB) override {
Chris Lattner73a6bdd2002-11-20 22:28:10 +000060 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
Chris Lattnere695da32003-04-23 16:38:00 +000061 if (CallInst *CI = dyn_cast<CallInst>(I)) {
Chris Lattner73a6bdd2002-11-20 22:28:10 +000062 if (!CI->use_empty())
Owen Anderson5a1acd92009-07-31 20:28:14 +000063 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
Chris Lattner73a6bdd2002-11-20 22:28:10 +000064 CI->getParent()->getInstList().erase(CI);
Chris Lattner425726d2004-03-17 17:29:08 +000065 break;
Chris Lattner73a6bdd2002-11-20 22:28:10 +000066 }
67 return false;
68 }
69 };
Dan Gohman12bb5052010-08-20 00:56:16 +000070}
JF Bastienf87e20d2015-04-20 23:42:22 +000071
Dan Gohman12bb5052010-08-20 00:56:16 +000072char DeleteCalls::ID = 0;
73static RegisterPass<DeleteCalls>
Chris Lattner73a6bdd2002-11-20 22:28:10 +000074 Y("bugpoint-deletecalls",
75 "BugPoint Test Pass - Intentionally 'misoptimize' CallInsts");
JF Bastienf87e20d2015-04-20 23:42:22 +000076
77namespace {
78 /// CrashOnDeclFunc - This pass is used to test bugpoint. It intentionally
Keno Fischer34ca8312015-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 Bastienf87e20d2015-04-20 23:42:22 +000091 }
Keno Fischer34ca8312015-11-06 00:12:50 +000092 return false;
93 }
JF Bastienf87e20d2015-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 Fischer34ca8312015-11-06 00:12:50 +0000101
102#include <iostream>
103namespace {
104/// CrashOnOneCU - This pass is used to test bugpoint. It intentionally
105/// crashes if the Module has two or more compile units
106class CrashOnTooManyCUs : public ModulePass {
107public:
108 static char ID;
109 CrashOnTooManyCUs() : ModulePass(ID) {}
110
111private:
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
123char CrashOnTooManyCUs::ID = 0;
124static RegisterPass<CrashOnTooManyCUs>
125 A("bugpoint-crash-too-many-cus",
126 "BugPoint Test Pass - Intentionally crash on too many CUs");