1 //===- llvm/unittest/IR/LegacyPassManager.cpp - Legacy PassManager tests --===//
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 unit test exercises the legacy pass manager infrastructure. We use the
10 // old names as well to ensure that the source-level compatibility is preserved
13 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/LegacyPassManager.h"
16 #include "llvm/Analysis/CallGraphSCCPass.h"
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/Analysis/LoopPass.h"
19 #include "llvm/IR/BasicBlock.h"
20 #include "llvm/IR/CallingConv.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/GlobalVariable.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/OptBisect.h"
29 #include "llvm/Pass.h"
30 #include "llvm/Support/MathExtras.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "gtest/gtest.h"
37 void initializeModuleNDMPass(PassRegistry
&);
38 void initializeFPassPass(PassRegistry
&);
39 void initializeCGPassPass(PassRegistry
&);
40 void initializeLPassPass(PassRegistry
&);
41 void initializeBPassPass(PassRegistry
&);
45 // NM = no modifications
46 struct ModuleNDNM
: public ModulePass
{
50 ModuleNDNM() : ModulePass(ID
) { }
51 bool runOnModule(Module
&M
) override
{
55 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
59 char ModuleNDNM::ID
=0;
60 char ModuleNDNM::run
=0;
62 struct ModuleNDM
: public ModulePass
{
66 ModuleNDM() : ModulePass(ID
) {}
67 bool runOnModule(Module
&M
) override
{
73 char ModuleNDM::run
=0;
75 struct ModuleNDM2
: public ModulePass
{
79 ModuleNDM2() : ModulePass(ID
) {}
80 bool runOnModule(Module
&M
) override
{
85 char ModuleNDM2::ID
=0;
86 char ModuleNDM2::run
=0;
88 struct ModuleDNM
: public ModulePass
{
92 ModuleDNM() : ModulePass(ID
) {
93 initializeModuleNDMPass(*PassRegistry::getPassRegistry());
95 bool runOnModule(Module
&M
) override
{
99 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
100 AU
.addRequired
<ModuleNDM
>();
101 AU
.setPreservesAll();
104 char ModuleDNM::ID
=0;
105 char ModuleDNM::run
=0;
108 struct PassTestBase
: public P
{
111 static bool initialized
;
112 static bool finalized
;
115 EXPECT_TRUE(initialized
);
116 EXPECT_FALSE(finalized
);
117 EXPECT_EQ(0, allocated
);
123 static void finishedOK(int run
) {
125 EXPECT_TRUE(initialized
);
126 EXPECT_TRUE(finalized
);
127 EXPECT_EQ(run
, runc
);
129 PassTestBase() : P(ID
), allocated(0) {
135 void releaseMemory() override
{
137 EXPECT_GT(allocated
, 0);
141 template<typename P
> char PassTestBase
<P
>::ID
;
142 template<typename P
> int PassTestBase
<P
>::runc
;
143 template<typename P
> bool PassTestBase
<P
>::initialized
;
144 template<typename P
> bool PassTestBase
<P
>::finalized
;
146 template<typename T
, typename P
>
147 struct PassTest
: public PassTestBase
<P
> {
149 #ifndef _MSC_VER // MSVC complains that Pass is not base class.
150 using llvm::Pass::doInitialization
;
151 using llvm::Pass::doFinalization
;
153 bool doInitialization(T
&t
) override
{
154 EXPECT_FALSE(PassTestBase
<P
>::initialized
);
155 PassTestBase
<P
>::initialized
= true;
158 bool doFinalization(T
&t
) override
{
159 EXPECT_FALSE(PassTestBase
<P
>::finalized
);
160 PassTestBase
<P
>::finalized
= true;
161 EXPECT_EQ(0, PassTestBase
<P
>::allocated
);
166 struct CGPass
: public PassTest
<CallGraph
, CallGraphSCCPass
> {
169 initializeCGPassPass(*PassRegistry::getPassRegistry());
171 bool runOnSCC(CallGraphSCC
&SCMM
) override
{
177 struct FPass
: public PassTest
<Module
, FunctionPass
> {
179 bool runOnFunction(Function
&F
) override
{
181 // EXPECT_TRUE(getAnalysisIfAvailable<DataLayout>());
187 struct LPass
: public PassTestBase
<LoopPass
> {
189 static int initcount
;
193 initializeLPassPass(*PassRegistry::getPassRegistry());
194 initcount
= 0; fincount
=0;
195 EXPECT_FALSE(initialized
);
197 static void finishedOK(int run
, int finalized
) {
198 PassTestBase
<LoopPass
>::finishedOK(run
);
199 EXPECT_EQ(run
, initcount
);
200 EXPECT_EQ(finalized
, fincount
);
202 using llvm::Pass::doInitialization
;
203 using llvm::Pass::doFinalization
;
204 bool doInitialization(Loop
* L
, LPPassManager
&LPM
) override
{
209 bool runOnLoop(Loop
*L
, LPPassManager
&LPM
) override
{
213 bool doFinalization() override
{
219 int LPass::initcount
=0;
220 int LPass::fincount
=0;
222 struct BPass
: public PassTestBase
<BasicBlockPass
> {
227 static void finishedOK(int run
, int N
) {
228 PassTestBase
<BasicBlockPass
>::finishedOK(run
);
229 EXPECT_EQ(inited
, N
);
236 bool doInitialization(Module
&M
) override
{
237 EXPECT_FALSE(initialized
);
241 bool doInitialization(Function
&F
) override
{
245 bool runOnBasicBlock(BasicBlock
&BB
) override
{
249 bool doFinalization(Function
&F
) override
{
253 bool doFinalization(Module
&M
) override
{
254 EXPECT_FALSE(finalized
);
256 EXPECT_EQ(0, allocated
);
263 struct OnTheFlyTest
: public ModulePass
{
266 OnTheFlyTest() : ModulePass(ID
) {
267 initializeFPassPass(*PassRegistry::getPassRegistry());
269 bool runOnModule(Module
&M
) override
{
270 for (Module::iterator I
=M
.begin(),E
=M
.end(); I
!= E
; ++I
) {
273 SCOPED_TRACE("Running on the fly function pass");
274 getAnalysis
<FPass
>(F
);
279 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
280 AU
.addRequired
<FPass
>();
283 char OnTheFlyTest::ID
=0;
285 TEST(PassManager
, RunOnce
) {
287 Module
M("test-once", Context
);
288 struct ModuleNDNM
*mNDNM
= new ModuleNDNM();
289 struct ModuleDNM
*mDNM
= new ModuleDNM();
290 struct ModuleNDM
*mNDM
= new ModuleNDM();
291 struct ModuleNDM2
*mNDM2
= new ModuleNDM2();
293 mNDM
->run
= mNDNM
->run
= mDNM
->run
= mNDM2
->run
= 0;
295 legacy::PassManager Passes
;
302 // each pass must be run exactly once, since nothing invalidates them
303 EXPECT_EQ(1, mNDM
->run
);
304 EXPECT_EQ(1, mNDNM
->run
);
305 EXPECT_EQ(1, mDNM
->run
);
306 EXPECT_EQ(1, mNDM2
->run
);
309 TEST(PassManager
, ReRun
) {
311 Module
M("test-rerun", Context
);
312 struct ModuleNDNM
*mNDNM
= new ModuleNDNM();
313 struct ModuleDNM
*mDNM
= new ModuleDNM();
314 struct ModuleNDM
*mNDM
= new ModuleNDM();
315 struct ModuleNDM2
*mNDM2
= new ModuleNDM2();
317 mNDM
->run
= mNDNM
->run
= mDNM
->run
= mNDM2
->run
= 0;
319 legacy::PassManager Passes
;
322 Passes
.add(mNDM2
);// invalidates mNDM needed by mDNM
326 // Some passes must be rerun because a pass that modified the
327 // module/function was run in between
328 EXPECT_EQ(2, mNDM
->run
);
329 EXPECT_EQ(1, mNDNM
->run
);
330 EXPECT_EQ(1, mNDM2
->run
);
331 EXPECT_EQ(1, mDNM
->run
);
334 Module
*makeLLVMModule(LLVMContext
&Context
);
337 void MemoryTestHelper(int run
) {
339 std::unique_ptr
<Module
> M(makeLLVMModule(Context
));
341 legacy::PassManager Passes
;
348 void MemoryTestHelper(int run
, int N
) {
350 Module
*M
= makeLLVMModule(Context
);
352 legacy::PassManager Passes
;
355 T::finishedOK(run
, N
);
359 TEST(PassManager
, Memory
) {
360 // SCC#1: test1->test2->test3->test1
362 // SCC#3: indirect call node
364 SCOPED_TRACE("Callgraph pass");
365 MemoryTestHelper
<CGPass
>(3);
369 SCOPED_TRACE("Function pass");
370 MemoryTestHelper
<FPass
>(4);// 4 functions
374 SCOPED_TRACE("Loop pass");
375 MemoryTestHelper
<LPass
>(2, 1); //2 loops, 1 function
378 SCOPED_TRACE("Basic block pass");
379 MemoryTestHelper
<BPass
>(7, 4); //9 basic blocks
384 TEST(PassManager
, MemoryOnTheFly
) {
386 Module
*M
= makeLLVMModule(Context
);
388 SCOPED_TRACE("Running OnTheFlyTest");
389 struct OnTheFlyTest
*O
= new OnTheFlyTest();
390 legacy::PassManager Passes
;
394 FPass::finishedOK(4);
399 // Skips or runs optional passes.
400 struct CustomOptPassGate
: public OptPassGate
{
402 CustomOptPassGate(bool Skip
) : Skip(Skip
) { }
403 bool shouldRunPass(const Pass
*P
, StringRef IRDescription
) {
404 if (P
->getPassKind() == PT_Module
)
406 return OptPassGate::shouldRunPass(P
, IRDescription
);
408 bool isEnabled() const { return true; }
411 // Optional module pass.
412 struct ModuleOpt
: public ModulePass
{
415 ModuleOpt() : ModulePass(ID
) { }
416 bool runOnModule(Module
&M
) override
{
422 char ModuleOpt::ID
=0;
424 TEST(PassManager
, CustomOptPassGate
) {
425 LLVMContext Context0
;
426 LLVMContext Context1
;
427 LLVMContext Context2
;
428 CustomOptPassGate
SkipOptionalPasses(true);
429 CustomOptPassGate
RunOptionalPasses(false);
431 Module
M0("custom-opt-bisect", Context0
);
432 Module
M1("custom-opt-bisect", Context1
);
433 Module
M2("custom-opt-bisect2", Context2
);
434 struct ModuleOpt
*mOpt0
= new ModuleOpt();
435 struct ModuleOpt
*mOpt1
= new ModuleOpt();
436 struct ModuleOpt
*mOpt2
= new ModuleOpt();
438 mOpt0
->run
= mOpt1
->run
= mOpt2
->run
= 0;
440 legacy::PassManager Passes0
;
441 legacy::PassManager Passes1
;
442 legacy::PassManager Passes2
;
448 Context1
.setOptPassGate(SkipOptionalPasses
);
449 Context2
.setOptPassGate(RunOptionalPasses
);
455 // By default optional passes are run.
456 EXPECT_EQ(1, mOpt0
->run
);
458 // The first context skips optional passes.
459 EXPECT_EQ(0, mOpt1
->run
);
461 // The second context runs optional passes.
462 EXPECT_EQ(1, mOpt2
->run
);
465 Module
*makeLLVMModule(LLVMContext
&Context
) {
466 // Module Construction
467 Module
*mod
= new Module("test-mem", Context
);
468 mod
->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
469 "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
470 "a:0:64-s:64:64-f80:128:128");
471 mod
->setTargetTriple("x86_64-unknown-linux-gnu");
474 std::vector
<Type
*>FuncTy_0_args
;
475 FunctionType
*FuncTy_0
= FunctionType::get(
476 /*Result=*/IntegerType::get(Context
, 32),
477 /*Params=*/FuncTy_0_args
,
480 std::vector
<Type
*>FuncTy_2_args
;
481 FuncTy_2_args
.push_back(IntegerType::get(Context
, 1));
482 FunctionType
*FuncTy_2
= FunctionType::get(
483 /*Result=*/Type::getVoidTy(Context
),
484 /*Params=*/FuncTy_2_args
,
487 // Function Declarations
489 Function
* func_test1
= Function::Create(
491 /*Linkage=*/GlobalValue::ExternalLinkage
,
492 /*Name=*/"test1", mod
);
493 func_test1
->setCallingConv(CallingConv::C
);
494 AttributeList func_test1_PAL
;
495 func_test1
->setAttributes(func_test1_PAL
);
497 Function
* func_test2
= Function::Create(
499 /*Linkage=*/GlobalValue::ExternalLinkage
,
500 /*Name=*/"test2", mod
);
501 func_test2
->setCallingConv(CallingConv::C
);
502 AttributeList func_test2_PAL
;
503 func_test2
->setAttributes(func_test2_PAL
);
505 Function
* func_test3
= Function::Create(
507 /*Linkage=*/GlobalValue::ExternalLinkage
,
508 /*Name=*/"test3", mod
);
509 func_test3
->setCallingConv(CallingConv::C
);
510 AttributeList func_test3_PAL
;
511 func_test3
->setAttributes(func_test3_PAL
);
513 Function
* func_test4
= Function::Create(
515 /*Linkage=*/GlobalValue::ExternalLinkage
,
516 /*Name=*/"test4", mod
);
517 func_test4
->setCallingConv(CallingConv::C
);
518 AttributeList func_test4_PAL
;
519 func_test4
->setAttributes(func_test4_PAL
);
521 // Global Variable Declarations
524 // Constant Definitions
526 // Global Variable Definitions
528 // Function Definitions
530 // Function: test1 (func_test1)
533 BasicBlock
*label_entry
=
534 BasicBlock::Create(Context
, "entry", func_test1
, nullptr);
536 // Block entry (label_entry)
537 CallInst
* int32_3
= CallInst::Create(func_test2
, "", label_entry
);
538 int32_3
->setCallingConv(CallingConv::C
);
539 int32_3
->setTailCall(false);
540 AttributeList int32_3_PAL
;
541 int32_3
->setAttributes(int32_3_PAL
);
543 ReturnInst::Create(Context
, int32_3
, label_entry
);
546 // Function: test2 (func_test2)
549 BasicBlock
*label_entry_5
=
550 BasicBlock::Create(Context
, "entry", func_test2
, nullptr);
552 // Block entry (label_entry_5)
553 CallInst
* int32_6
= CallInst::Create(func_test3
, "", label_entry_5
);
554 int32_6
->setCallingConv(CallingConv::C
);
555 int32_6
->setTailCall(false);
556 AttributeList int32_6_PAL
;
557 int32_6
->setAttributes(int32_6_PAL
);
559 ReturnInst::Create(Context
, int32_6
, label_entry_5
);
562 // Function: test3 (func_test3)
565 BasicBlock
*label_entry_8
=
566 BasicBlock::Create(Context
, "entry", func_test3
, nullptr);
568 // Block entry (label_entry_8)
569 CallInst
* int32_9
= CallInst::Create(func_test1
, "", label_entry_8
);
570 int32_9
->setCallingConv(CallingConv::C
);
571 int32_9
->setTailCall(false);
572 AttributeList int32_9_PAL
;
573 int32_9
->setAttributes(int32_9_PAL
);
575 ReturnInst::Create(Context
, int32_9
, label_entry_8
);
578 // Function: test4 (func_test4)
580 Function::arg_iterator args
= func_test4
->arg_begin();
581 Value
*int1_f
= &*args
++;
582 int1_f
->setName("f");
584 BasicBlock
*label_entry_11
=
585 BasicBlock::Create(Context
, "entry", func_test4
, nullptr);
586 BasicBlock
*label_bb
=
587 BasicBlock::Create(Context
, "bb", func_test4
, nullptr);
588 BasicBlock
*label_bb1
=
589 BasicBlock::Create(Context
, "bb1", func_test4
, nullptr);
590 BasicBlock
*label_return
=
591 BasicBlock::Create(Context
, "return", func_test4
, nullptr);
593 // Block entry (label_entry_11)
594 BranchInst::Create(label_bb
, label_entry_11
);
596 // Block bb (label_bb)
597 BranchInst::Create(label_bb
, label_bb1
, int1_f
, label_bb
);
599 // Block bb1 (label_bb1)
600 BranchInst::Create(label_bb1
, label_return
, int1_f
, label_bb1
);
602 // Block return (label_return)
603 ReturnInst::Create(Context
, label_return
);
611 INITIALIZE_PASS(ModuleNDM
, "mndm", "mndm", false, false)
612 INITIALIZE_PASS_BEGIN(CGPass
, "cgp","cgp", false, false)
613 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass
)
614 INITIALIZE_PASS_END(CGPass
, "cgp","cgp", false, false)
615 INITIALIZE_PASS(FPass
, "fp","fp", false, false)
616 INITIALIZE_PASS_BEGIN(LPass
, "lp","lp", false, false)
617 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass
)
618 INITIALIZE_PASS_END(LPass
, "lp","lp", false, false)
619 INITIALIZE_PASS(BPass
, "bp","bp", false, false)