1 //===- TestPasses.cpp - "buggy" passes used to test bugpoint --------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains "buggy" passes that are used to test bugpoint, to check
10 // that it is narrowing down testcases correctly.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/BasicBlock.h"
15 #include "llvm/IR/Constant.h"
16 #include "llvm/IR/InstVisitor.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/Type.h"
19 #include "llvm/Pass.h"
24 /// CrashOnCalls - This pass is used to test bugpoint. It intentionally
25 /// crashes on any call instructions.
26 class CrashOnCalls
: public BasicBlockPass
{
28 static char ID
; // Pass ID, replacement for typeid
29 CrashOnCalls() : BasicBlockPass(ID
) {}
31 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
35 bool runOnBasicBlock(BasicBlock
&BB
) override
{
36 for (BasicBlock::iterator I
= BB
.begin(), E
= BB
.end(); I
!= E
; ++I
)
37 if (isa
<CallInst
>(*I
))
45 char CrashOnCalls::ID
= 0;
46 static RegisterPass
<CrashOnCalls
>
47 X("bugpoint-crashcalls",
48 "BugPoint Test Pass - Intentionally crash on CallInsts");
51 /// DeleteCalls - This pass is used to test bugpoint. It intentionally
52 /// deletes some call instructions, "misoptimizing" the program.
53 class DeleteCalls
: public BasicBlockPass
{
55 static char ID
; // Pass ID, replacement for typeid
56 DeleteCalls() : BasicBlockPass(ID
) {}
58 bool runOnBasicBlock(BasicBlock
&BB
) override
{
59 for (BasicBlock::iterator I
= BB
.begin(), E
= BB
.end(); I
!= E
; ++I
)
60 if (CallInst
*CI
= dyn_cast
<CallInst
>(I
)) {
62 CI
->replaceAllUsesWith(Constant::getNullValue(CI
->getType()));
63 CI
->getParent()->getInstList().erase(CI
);
71 char DeleteCalls::ID
= 0;
72 static RegisterPass
<DeleteCalls
>
73 Y("bugpoint-deletecalls",
74 "BugPoint Test Pass - Intentionally 'misoptimize' CallInsts");
77 /// CrashOnDeclFunc - This pass is used to test bugpoint. It intentionally
78 /// crashes if the module has an undefined function (ie a function that is
79 /// defined in an external module).
80 class CrashOnDeclFunc
: public ModulePass
{
82 static char ID
; // Pass ID, replacement for typeid
83 CrashOnDeclFunc() : ModulePass(ID
) {}
86 bool runOnModule(Module
&M
) override
{
87 for (auto &F
: M
.functions()) {
88 if (F
.isDeclaration())
96 char CrashOnDeclFunc::ID
= 0;
97 static RegisterPass
<CrashOnDeclFunc
>
98 Z("bugpoint-crash-decl-funcs",
99 "BugPoint Test Pass - Intentionally crash on declared functions");
102 /// CrashOnOneCU - This pass is used to test bugpoint. It intentionally
103 /// crashes if the Module has two or more compile units
104 class CrashOnTooManyCUs
: public ModulePass
{
107 CrashOnTooManyCUs() : ModulePass(ID
) {}
110 bool runOnModule(Module
&M
) override
{
111 NamedMDNode
*CU_Nodes
= M
.getNamedMetadata("llvm.dbg.cu");
114 if (CU_Nodes
->getNumOperands() >= 2)
121 char CrashOnTooManyCUs::ID
= 0;
122 static RegisterPass
<CrashOnTooManyCUs
>
123 A("bugpoint-crash-too-many-cus",
124 "BugPoint Test Pass - Intentionally crash on too many CUs");
127 class CrashOnFunctionAttribute
: public FunctionPass
{
129 static char ID
; // Pass ID, replacement for typeid
130 CrashOnFunctionAttribute() : FunctionPass(ID
) {}
133 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
134 AU
.setPreservesAll();
137 bool runOnFunction(Function
&F
) override
{
138 AttributeSet A
= F
.getAttributes().getFnAttributes();
139 if (A
.hasAttribute("bugpoint-crash"))
146 char CrashOnFunctionAttribute::ID
= 0;
147 static RegisterPass
<CrashOnFunctionAttribute
>
148 B("bugpoint-crashfuncattr", "BugPoint Test Pass - Intentionally crash on "
149 "function attribute 'bugpoint-crash'");