1 //===- llvm/unittest/IR/LegacyPassManager.cpp - Legacy PassManager tests --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This unit test exercises the legacy pass manager infrastructure. We use the
11 // old names as well to ensure that the source-level compatibility is preserved
14 //===----------------------------------------------------------------------===//
16 #include "llvm/IR/LegacyPassManager.h"
17 #include "llvm/Analysis/CallGraphSCCPass.h"
18 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/Analysis/LoopPass.h"
20 #include "llvm/IR/BasicBlock.h"
21 #include "llvm/IR/CallingConv.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/GlobalVariable.h"
26 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/LLVMContext.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/OptBisect.h"
30 #include "llvm/Pass.h"
31 #include "llvm/Support/MathExtras.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "gtest/gtest.h"
38 void initializeModuleNDMPass(PassRegistry
&);
39 void initializeFPassPass(PassRegistry
&);
40 void initializeCGPassPass(PassRegistry
&);
41 void initializeLPassPass(PassRegistry
&);
42 void initializeBPassPass(PassRegistry
&);
46 // NM = no modifications
47 struct ModuleNDNM
: public ModulePass
{
51 ModuleNDNM() : ModulePass(ID
) { }
52 bool runOnModule(Module
&M
) override
{
56 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
60 char ModuleNDNM::ID
=0;
61 char ModuleNDNM::run
=0;
63 struct ModuleNDM
: public ModulePass
{
67 ModuleNDM() : ModulePass(ID
) {}
68 bool runOnModule(Module
&M
) override
{
74 char ModuleNDM::run
=0;
76 struct ModuleNDM2
: public ModulePass
{
80 ModuleNDM2() : ModulePass(ID
) {}
81 bool runOnModule(Module
&M
) override
{
86 char ModuleNDM2::ID
=0;
87 char ModuleNDM2::run
=0;
89 struct ModuleDNM
: public ModulePass
{
93 ModuleDNM() : ModulePass(ID
) {
94 initializeModuleNDMPass(*PassRegistry::getPassRegistry());
96 bool runOnModule(Module
&M
) override
{
100 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
101 AU
.addRequired
<ModuleNDM
>();
102 AU
.setPreservesAll();
105 char ModuleDNM::ID
=0;
106 char ModuleDNM::run
=0;
109 struct PassTestBase
: public P
{
112 static bool initialized
;
113 static bool finalized
;
116 EXPECT_TRUE(initialized
);
117 EXPECT_FALSE(finalized
);
118 EXPECT_EQ(0, allocated
);
124 static void finishedOK(int run
) {
126 EXPECT_TRUE(initialized
);
127 EXPECT_TRUE(finalized
);
128 EXPECT_EQ(run
, runc
);
130 PassTestBase() : P(ID
), allocated(0) {
136 void releaseMemory() override
{
138 EXPECT_GT(allocated
, 0);
142 template<typename P
> char PassTestBase
<P
>::ID
;
143 template<typename P
> int PassTestBase
<P
>::runc
;
144 template<typename P
> bool PassTestBase
<P
>::initialized
;
145 template<typename P
> bool PassTestBase
<P
>::finalized
;
147 template<typename T
, typename P
>
148 struct PassTest
: public PassTestBase
<P
> {
150 #ifndef _MSC_VER // MSVC complains that Pass is not base class.
151 using llvm::Pass::doInitialization
;
152 using llvm::Pass::doFinalization
;
154 bool doInitialization(T
&t
) override
{
155 EXPECT_FALSE(PassTestBase
<P
>::initialized
);
156 PassTestBase
<P
>::initialized
= true;
159 bool doFinalization(T
&t
) override
{
160 EXPECT_FALSE(PassTestBase
<P
>::finalized
);
161 PassTestBase
<P
>::finalized
= true;
162 EXPECT_EQ(0, PassTestBase
<P
>::allocated
);
167 struct CGPass
: public PassTest
<CallGraph
, CallGraphSCCPass
> {
170 initializeCGPassPass(*PassRegistry::getPassRegistry());
172 bool runOnSCC(CallGraphSCC
&SCMM
) override
{
178 struct FPass
: public PassTest
<Module
, FunctionPass
> {
180 bool runOnFunction(Function
&F
) override
{
182 // EXPECT_TRUE(getAnalysisIfAvailable<DataLayout>());
188 struct LPass
: public PassTestBase
<LoopPass
> {
190 static int initcount
;
194 initializeLPassPass(*PassRegistry::getPassRegistry());
195 initcount
= 0; fincount
=0;
196 EXPECT_FALSE(initialized
);
198 static void finishedOK(int run
, int finalized
) {
199 PassTestBase
<LoopPass
>::finishedOK(run
);
200 EXPECT_EQ(run
, initcount
);
201 EXPECT_EQ(finalized
, fincount
);
203 using llvm::Pass::doInitialization
;
204 using llvm::Pass::doFinalization
;
205 bool doInitialization(Loop
* L
, LPPassManager
&LPM
) override
{
210 bool runOnLoop(Loop
*L
, LPPassManager
&LPM
) override
{
214 bool doFinalization() override
{
220 int LPass::initcount
=0;
221 int LPass::fincount
=0;
223 struct BPass
: public PassTestBase
<BasicBlockPass
> {
228 static void finishedOK(int run
, int N
) {
229 PassTestBase
<BasicBlockPass
>::finishedOK(run
);
230 EXPECT_EQ(inited
, N
);
237 bool doInitialization(Module
&M
) override
{
238 EXPECT_FALSE(initialized
);
242 bool doInitialization(Function
&F
) override
{
246 bool runOnBasicBlock(BasicBlock
&BB
) override
{
250 bool doFinalization(Function
&F
) override
{
254 bool doFinalization(Module
&M
) override
{
255 EXPECT_FALSE(finalized
);
257 EXPECT_EQ(0, allocated
);
264 struct OnTheFlyTest
: public ModulePass
{
267 OnTheFlyTest() : ModulePass(ID
) {
268 initializeFPassPass(*PassRegistry::getPassRegistry());
270 bool runOnModule(Module
&M
) override
{
271 for (Module::iterator I
=M
.begin(),E
=M
.end(); I
!= E
; ++I
) {
274 SCOPED_TRACE("Running on the fly function pass");
275 getAnalysis
<FPass
>(F
);
280 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
281 AU
.addRequired
<FPass
>();
284 char OnTheFlyTest::ID
=0;
286 TEST(PassManager
, RunOnce
) {
288 Module
M("test-once", Context
);
289 struct ModuleNDNM
*mNDNM
= new ModuleNDNM();
290 struct ModuleDNM
*mDNM
= new ModuleDNM();
291 struct ModuleNDM
*mNDM
= new ModuleNDM();
292 struct ModuleNDM2
*mNDM2
= new ModuleNDM2();
294 mNDM
->run
= mNDNM
->run
= mDNM
->run
= mNDM2
->run
= 0;
296 legacy::PassManager Passes
;
303 // each pass must be run exactly once, since nothing invalidates them
304 EXPECT_EQ(1, mNDM
->run
);
305 EXPECT_EQ(1, mNDNM
->run
);
306 EXPECT_EQ(1, mDNM
->run
);
307 EXPECT_EQ(1, mNDM2
->run
);
310 TEST(PassManager
, ReRun
) {
312 Module
M("test-rerun", Context
);
313 struct ModuleNDNM
*mNDNM
= new ModuleNDNM();
314 struct ModuleDNM
*mDNM
= new ModuleDNM();
315 struct ModuleNDM
*mNDM
= new ModuleNDM();
316 struct ModuleNDM2
*mNDM2
= new ModuleNDM2();
318 mNDM
->run
= mNDNM
->run
= mDNM
->run
= mNDM2
->run
= 0;
320 legacy::PassManager Passes
;
323 Passes
.add(mNDM2
);// invalidates mNDM needed by mDNM
327 // Some passes must be rerun because a pass that modified the
328 // module/function was run in between
329 EXPECT_EQ(2, mNDM
->run
);
330 EXPECT_EQ(1, mNDNM
->run
);
331 EXPECT_EQ(1, mNDM2
->run
);
332 EXPECT_EQ(1, mDNM
->run
);
335 Module
*makeLLVMModule(LLVMContext
&Context
);
338 void MemoryTestHelper(int run
) {
340 std::unique_ptr
<Module
> M(makeLLVMModule(Context
));
342 legacy::PassManager Passes
;
349 void MemoryTestHelper(int run
, int N
) {
351 Module
*M
= makeLLVMModule(Context
);
353 legacy::PassManager Passes
;
356 T::finishedOK(run
, N
);
360 TEST(PassManager
, Memory
) {
361 // SCC#1: test1->test2->test3->test1
363 // SCC#3: indirect call node
365 SCOPED_TRACE("Callgraph pass");
366 MemoryTestHelper
<CGPass
>(3);
370 SCOPED_TRACE("Function pass");
371 MemoryTestHelper
<FPass
>(4);// 4 functions
375 SCOPED_TRACE("Loop pass");
376 MemoryTestHelper
<LPass
>(2, 1); //2 loops, 1 function
379 SCOPED_TRACE("Basic block pass");
380 MemoryTestHelper
<BPass
>(7, 4); //9 basic blocks
385 TEST(PassManager
, MemoryOnTheFly
) {
387 Module
*M
= makeLLVMModule(Context
);
389 SCOPED_TRACE("Running OnTheFlyTest");
390 struct OnTheFlyTest
*O
= new OnTheFlyTest();
391 legacy::PassManager Passes
;
395 FPass::finishedOK(4);
400 // Skips or runs optional passes.
401 struct CustomOptPassGate
: public OptPassGate
{
403 CustomOptPassGate(bool Skip
) : Skip(Skip
) { }
404 bool shouldRunPass(const Pass
*P
, const Module
&U
) { return !Skip
; }
407 // Optional module pass.
408 struct ModuleOpt
: public ModulePass
{
411 ModuleOpt() : ModulePass(ID
) { }
412 bool runOnModule(Module
&M
) override
{
418 char ModuleOpt::ID
=0;
420 TEST(PassManager
, CustomOptPassGate
) {
421 LLVMContext Context0
;
422 LLVMContext Context1
;
423 LLVMContext Context2
;
424 CustomOptPassGate
SkipOptionalPasses(true);
425 CustomOptPassGate
RunOptionalPasses(false);
427 Module
M0("custom-opt-bisect", Context0
);
428 Module
M1("custom-opt-bisect", Context1
);
429 Module
M2("custom-opt-bisect2", Context2
);
430 struct ModuleOpt
*mOpt0
= new ModuleOpt();
431 struct ModuleOpt
*mOpt1
= new ModuleOpt();
432 struct ModuleOpt
*mOpt2
= new ModuleOpt();
434 mOpt0
->run
= mOpt1
->run
= mOpt2
->run
= 0;
436 legacy::PassManager Passes0
;
437 legacy::PassManager Passes1
;
438 legacy::PassManager Passes2
;
444 Context1
.setOptPassGate(SkipOptionalPasses
);
445 Context2
.setOptPassGate(RunOptionalPasses
);
451 // By default optional passes are run.
452 EXPECT_EQ(1, mOpt0
->run
);
454 // The first context skips optional passes.
455 EXPECT_EQ(0, mOpt1
->run
);
457 // The second context runs optional passes.
458 EXPECT_EQ(1, mOpt2
->run
);
461 Module
*makeLLVMModule(LLVMContext
&Context
) {
462 // Module Construction
463 Module
*mod
= new Module("test-mem", Context
);
464 mod
->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
465 "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
466 "a:0:64-s:64:64-f80:128:128");
467 mod
->setTargetTriple("x86_64-unknown-linux-gnu");
470 std::vector
<Type
*>FuncTy_0_args
;
471 FunctionType
*FuncTy_0
= FunctionType::get(
472 /*Result=*/IntegerType::get(Context
, 32),
473 /*Params=*/FuncTy_0_args
,
476 std::vector
<Type
*>FuncTy_2_args
;
477 FuncTy_2_args
.push_back(IntegerType::get(Context
, 1));
478 FunctionType
*FuncTy_2
= FunctionType::get(
479 /*Result=*/Type::getVoidTy(Context
),
480 /*Params=*/FuncTy_2_args
,
483 // Function Declarations
485 Function
* func_test1
= Function::Create(
487 /*Linkage=*/GlobalValue::ExternalLinkage
,
488 /*Name=*/"test1", mod
);
489 func_test1
->setCallingConv(CallingConv::C
);
490 AttributeList func_test1_PAL
;
491 func_test1
->setAttributes(func_test1_PAL
);
493 Function
* func_test2
= Function::Create(
495 /*Linkage=*/GlobalValue::ExternalLinkage
,
496 /*Name=*/"test2", mod
);
497 func_test2
->setCallingConv(CallingConv::C
);
498 AttributeList func_test2_PAL
;
499 func_test2
->setAttributes(func_test2_PAL
);
501 Function
* func_test3
= Function::Create(
503 /*Linkage=*/GlobalValue::ExternalLinkage
,
504 /*Name=*/"test3", mod
);
505 func_test3
->setCallingConv(CallingConv::C
);
506 AttributeList func_test3_PAL
;
507 func_test3
->setAttributes(func_test3_PAL
);
509 Function
* func_test4
= Function::Create(
511 /*Linkage=*/GlobalValue::ExternalLinkage
,
512 /*Name=*/"test4", mod
);
513 func_test4
->setCallingConv(CallingConv::C
);
514 AttributeList func_test4_PAL
;
515 func_test4
->setAttributes(func_test4_PAL
);
517 // Global Variable Declarations
520 // Constant Definitions
522 // Global Variable Definitions
524 // Function Definitions
526 // Function: test1 (func_test1)
529 BasicBlock
*label_entry
=
530 BasicBlock::Create(Context
, "entry", func_test1
, nullptr);
532 // Block entry (label_entry)
533 CallInst
* int32_3
= CallInst::Create(func_test2
, "", label_entry
);
534 int32_3
->setCallingConv(CallingConv::C
);
535 int32_3
->setTailCall(false);
536 AttributeList int32_3_PAL
;
537 int32_3
->setAttributes(int32_3_PAL
);
539 ReturnInst::Create(Context
, int32_3
, label_entry
);
542 // Function: test2 (func_test2)
545 BasicBlock
*label_entry_5
=
546 BasicBlock::Create(Context
, "entry", func_test2
, nullptr);
548 // Block entry (label_entry_5)
549 CallInst
* int32_6
= CallInst::Create(func_test3
, "", label_entry_5
);
550 int32_6
->setCallingConv(CallingConv::C
);
551 int32_6
->setTailCall(false);
552 AttributeList int32_6_PAL
;
553 int32_6
->setAttributes(int32_6_PAL
);
555 ReturnInst::Create(Context
, int32_6
, label_entry_5
);
558 // Function: test3 (func_test3)
561 BasicBlock
*label_entry_8
=
562 BasicBlock::Create(Context
, "entry", func_test3
, nullptr);
564 // Block entry (label_entry_8)
565 CallInst
* int32_9
= CallInst::Create(func_test1
, "", label_entry_8
);
566 int32_9
->setCallingConv(CallingConv::C
);
567 int32_9
->setTailCall(false);
568 AttributeList int32_9_PAL
;
569 int32_9
->setAttributes(int32_9_PAL
);
571 ReturnInst::Create(Context
, int32_9
, label_entry_8
);
574 // Function: test4 (func_test4)
576 Function::arg_iterator args
= func_test4
->arg_begin();
577 Value
*int1_f
= &*args
++;
578 int1_f
->setName("f");
580 BasicBlock
*label_entry_11
=
581 BasicBlock::Create(Context
, "entry", func_test4
, nullptr);
582 BasicBlock
*label_bb
=
583 BasicBlock::Create(Context
, "bb", func_test4
, nullptr);
584 BasicBlock
*label_bb1
=
585 BasicBlock::Create(Context
, "bb1", func_test4
, nullptr);
586 BasicBlock
*label_return
=
587 BasicBlock::Create(Context
, "return", func_test4
, nullptr);
589 // Block entry (label_entry_11)
590 BranchInst::Create(label_bb
, label_entry_11
);
592 // Block bb (label_bb)
593 BranchInst::Create(label_bb
, label_bb1
, int1_f
, label_bb
);
595 // Block bb1 (label_bb1)
596 BranchInst::Create(label_bb1
, label_return
, int1_f
, label_bb1
);
598 // Block return (label_return)
599 ReturnInst::Create(Context
, label_return
);
607 INITIALIZE_PASS(ModuleNDM
, "mndm", "mndm", false, false)
608 INITIALIZE_PASS_BEGIN(CGPass
, "cgp","cgp", false, false)
609 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass
)
610 INITIALIZE_PASS_END(CGPass
, "cgp","cgp", false, false)
611 INITIALIZE_PASS(FPass
, "fp","fp", false, false)
612 INITIALIZE_PASS_BEGIN(LPass
, "lp","lp", false, false)
613 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass
)
614 INITIALIZE_PASS_END(LPass
, "lp","lp", false, false)
615 INITIALIZE_PASS(BPass
, "bp","bp", false, false)