1 //===- llvm/unittest/VMCore/PassManager.cpp - Constants unit 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 #include "llvm/Module.h"
11 #include "llvm/LLVMContext.h"
12 #include "llvm/PassManager.h"
13 #include "llvm/Analysis/LoopInfo.h"
14 #include "llvm/Pass.h"
15 #include "llvm/Analysis/LoopPass.h"
16 #include "llvm/CallGraphSCCPass.h"
17 #include "llvm/Target/TargetData.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Constants.h"
21 #include "llvm/GlobalVariable.h"
22 #include "llvm/Function.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/BasicBlock.h"
25 #include "llvm/Instructions.h"
26 #include "llvm/InlineAsm.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/PassManager.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/Analysis/Verifier.h"
32 #include "llvm/Assembly/PrintModulePass.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 virtual bool runOnModule(Module
&M
) {
56 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
60 char ModuleNDNM::ID
=0;
61 char ModuleNDNM::run
=0;
63 struct ModuleNDM
: public ModulePass
{
67 ModuleNDM() : ModulePass(ID
) {}
68 virtual bool runOnModule(Module
&M
) {
74 char ModuleNDM::run
=0;
76 struct ModuleNDM2
: public ModulePass
{
80 ModuleNDM2() : ModulePass(ID
) {}
81 virtual bool runOnModule(Module
&M
) {
86 char ModuleNDM2::ID
=0;
87 char ModuleNDM2::run
=0;
89 struct ModuleDNM
: public ModulePass
{
93 ModuleDNM() : ModulePass(ID
) {
94 initializeModuleNDMPass(*PassRegistry::getPassRegistry());
96 virtual bool runOnModule(Module
&M
) {
97 EXPECT_TRUE(getAnalysisIfAvailable
<TargetData
>());
101 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
102 AU
.addRequired
<ModuleNDM
>();
103 AU
.setPreservesAll();
106 char ModuleDNM::ID
=0;
107 char ModuleDNM::run
=0;
110 struct PassTestBase
: public P
{
113 static bool initialized
;
114 static bool finalized
;
117 EXPECT_TRUE(initialized
);
118 EXPECT_FALSE(finalized
);
119 EXPECT_EQ(0, allocated
);
125 static void finishedOK(int run
) {
127 EXPECT_TRUE(initialized
);
128 EXPECT_TRUE(finalized
);
129 EXPECT_EQ(run
, runc
);
131 PassTestBase() : P(ID
), allocated(0) {
137 virtual void releaseMemory() {
139 EXPECT_GT(allocated
, 0);
143 template<typename P
> char PassTestBase
<P
>::ID
;
144 template<typename P
> int PassTestBase
<P
>::runc
;
145 template<typename P
> bool PassTestBase
<P
>::initialized
;
146 template<typename P
> bool PassTestBase
<P
>::finalized
;
148 template<typename T
, typename P
>
149 struct PassTest
: public PassTestBase
<P
> {
151 virtual bool doInitialization(T
&t
) {
152 EXPECT_FALSE(PassTestBase
<P
>::initialized
);
153 PassTestBase
<P
>::initialized
= true;
156 virtual bool doFinalization(T
&t
) {
157 EXPECT_FALSE(PassTestBase
<P
>::finalized
);
158 PassTestBase
<P
>::finalized
= true;
159 EXPECT_EQ(0, PassTestBase
<P
>::allocated
);
164 struct CGPass
: public PassTest
<CallGraph
, CallGraphSCCPass
> {
167 initializeCGPassPass(*PassRegistry::getPassRegistry());
169 virtual bool runOnSCC(CallGraphSCC
&SCMM
) {
170 EXPECT_TRUE(getAnalysisIfAvailable
<TargetData
>());
176 struct FPass
: public PassTest
<Module
, FunctionPass
> {
178 virtual bool runOnFunction(Function
&F
) {
180 // EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
186 struct LPass
: public PassTestBase
<LoopPass
> {
188 static int initcount
;
192 initializeLPassPass(*PassRegistry::getPassRegistry());
193 initcount
= 0; fincount
=0;
194 EXPECT_FALSE(initialized
);
196 static void finishedOK(int run
, int finalized
) {
197 PassTestBase
<LoopPass
>::finishedOK(run
);
198 EXPECT_EQ(run
, initcount
);
199 EXPECT_EQ(finalized
, fincount
);
201 virtual bool doInitialization(Loop
* L
, LPPassManager
&LPM
) {
206 virtual bool runOnLoop(Loop
*L
, LPPassManager
&LPM
) {
207 EXPECT_TRUE(getAnalysisIfAvailable
<TargetData
>());
211 virtual bool doFinalization() {
217 int LPass::initcount
=0;
218 int LPass::fincount
=0;
220 struct BPass
: public PassTestBase
<BasicBlockPass
> {
225 static void finishedOK(int run
, int N
) {
226 PassTestBase
<BasicBlockPass
>::finishedOK(run
);
227 EXPECT_EQ(inited
, N
);
234 virtual bool doInitialization(Module
&M
) {
235 EXPECT_FALSE(initialized
);
239 virtual bool doInitialization(Function
&F
) {
243 virtual bool runOnBasicBlock(BasicBlock
&BB
) {
244 EXPECT_TRUE(getAnalysisIfAvailable
<TargetData
>());
248 virtual bool doFinalization(Function
&F
) {
252 virtual bool doFinalization(Module
&M
) {
253 EXPECT_FALSE(finalized
);
255 EXPECT_EQ(0, allocated
);
262 struct OnTheFlyTest
: public ModulePass
{
265 OnTheFlyTest() : ModulePass(ID
) {
266 initializeFPassPass(*PassRegistry::getPassRegistry());
268 virtual bool runOnModule(Module
&M
) {
269 EXPECT_TRUE(getAnalysisIfAvailable
<TargetData
>());
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 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
280 AU
.addRequired
<FPass
>();
283 char OnTheFlyTest::ID
=0;
285 TEST(PassManager
, RunOnce
) {
286 Module
M("test-once", getGlobalContext());
287 struct ModuleNDNM
*mNDNM
= new ModuleNDNM();
288 struct ModuleDNM
*mDNM
= new ModuleDNM();
289 struct ModuleNDM
*mNDM
= new ModuleNDM();
290 struct ModuleNDM2
*mNDM2
= new ModuleNDM2();
292 mNDM
->run
= mNDNM
->run
= mDNM
->run
= mNDM2
->run
= 0;
295 Passes
.add(new TargetData(&M
));
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
) {
310 Module
M("test-rerun", getGlobalContext());
311 struct ModuleNDNM
*mNDNM
= new ModuleNDNM();
312 struct ModuleDNM
*mDNM
= new ModuleDNM();
313 struct ModuleNDM
*mNDM
= new ModuleNDM();
314 struct ModuleNDM2
*mNDM2
= new ModuleNDM2();
316 mNDM
->run
= mNDNM
->run
= mDNM
->run
= mNDM2
->run
= 0;
319 Passes
.add(new TargetData(&M
));
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 inbetween
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();
337 void MemoryTestHelper(int run
) {
338 OwningPtr
<Module
> M(makeLLVMModule());
341 Passes
.add(new TargetData(M
.get()));
348 void MemoryTestHelper(int run
, int N
) {
349 Module
*M
= makeLLVMModule();
352 Passes
.add(new TargetData(M
));
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
) {
385 Module
*M
= makeLLVMModule();
387 SCOPED_TRACE("Running OnTheFlyTest");
388 struct OnTheFlyTest
*O
= new OnTheFlyTest();
390 Passes
.add(new TargetData(M
));
394 FPass::finishedOK(4);
399 Module
* makeLLVMModule() {
400 // Module Construction
401 Module
* mod
= new Module("test-mem", getGlobalContext());
402 mod
->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
403 "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
404 "a0:0:64-s0:64:64-f80:128:128");
405 mod
->setTargetTriple("x86_64-unknown-linux-gnu");
408 std::vector
<const Type
*>FuncTy_0_args
;
409 FunctionType
* FuncTy_0
= FunctionType::get(
410 /*Result=*/IntegerType::get(getGlobalContext(), 32),
411 /*Params=*/FuncTy_0_args
,
414 std::vector
<const Type
*>FuncTy_2_args
;
415 FuncTy_2_args
.push_back(IntegerType::get(getGlobalContext(), 1));
416 FunctionType
* FuncTy_2
= FunctionType::get(
417 /*Result=*/Type::getVoidTy(getGlobalContext()),
418 /*Params=*/FuncTy_2_args
,
422 // Function Declarations
424 Function
* func_test1
= Function::Create(
426 /*Linkage=*/GlobalValue::ExternalLinkage
,
427 /*Name=*/"test1", mod
);
428 func_test1
->setCallingConv(CallingConv::C
);
429 AttrListPtr func_test1_PAL
;
430 func_test1
->setAttributes(func_test1_PAL
);
432 Function
* func_test2
= Function::Create(
434 /*Linkage=*/GlobalValue::ExternalLinkage
,
435 /*Name=*/"test2", mod
);
436 func_test2
->setCallingConv(CallingConv::C
);
437 AttrListPtr func_test2_PAL
;
438 func_test2
->setAttributes(func_test2_PAL
);
440 Function
* func_test3
= Function::Create(
442 /*Linkage=*/GlobalValue::ExternalLinkage
,
443 /*Name=*/"test3", mod
);
444 func_test3
->setCallingConv(CallingConv::C
);
445 AttrListPtr func_test3_PAL
;
446 func_test3
->setAttributes(func_test3_PAL
);
448 Function
* func_test4
= Function::Create(
450 /*Linkage=*/GlobalValue::ExternalLinkage
,
451 /*Name=*/"test4", mod
);
452 func_test4
->setCallingConv(CallingConv::C
);
453 AttrListPtr func_test4_PAL
;
454 func_test4
->setAttributes(func_test4_PAL
);
456 // Global Variable Declarations
459 // Constant Definitions
461 // Global Variable Definitions
463 // Function Definitions
465 // Function: test1 (func_test1)
468 BasicBlock
* label_entry
= BasicBlock::Create(getGlobalContext(), "entry",func_test1
,0);
470 // Block entry (label_entry)
471 CallInst
* int32_3
= CallInst::Create(func_test2
, "", label_entry
);
472 int32_3
->setCallingConv(CallingConv::C
);
473 int32_3
->setTailCall(false);AttrListPtr int32_3_PAL
;
474 int32_3
->setAttributes(int32_3_PAL
);
476 ReturnInst::Create(getGlobalContext(), int32_3
, label_entry
);
480 // Function: test2 (func_test2)
483 BasicBlock
* label_entry_5
= BasicBlock::Create(getGlobalContext(), "entry",func_test2
,0);
485 // Block entry (label_entry_5)
486 CallInst
* int32_6
= CallInst::Create(func_test3
, "", label_entry_5
);
487 int32_6
->setCallingConv(CallingConv::C
);
488 int32_6
->setTailCall(false);AttrListPtr int32_6_PAL
;
489 int32_6
->setAttributes(int32_6_PAL
);
491 ReturnInst::Create(getGlobalContext(), int32_6
, label_entry_5
);
495 // Function: test3 (func_test3)
498 BasicBlock
* label_entry_8
= BasicBlock::Create(getGlobalContext(), "entry",func_test3
,0);
500 // Block entry (label_entry_8)
501 CallInst
* int32_9
= CallInst::Create(func_test1
, "", label_entry_8
);
502 int32_9
->setCallingConv(CallingConv::C
);
503 int32_9
->setTailCall(false);AttrListPtr int32_9_PAL
;
504 int32_9
->setAttributes(int32_9_PAL
);
506 ReturnInst::Create(getGlobalContext(), int32_9
, label_entry_8
);
510 // Function: test4 (func_test4)
512 Function::arg_iterator args
= func_test4
->arg_begin();
513 Value
* int1_f
= args
++;
514 int1_f
->setName("f");
516 BasicBlock
* label_entry_11
= BasicBlock::Create(getGlobalContext(), "entry",func_test4
,0);
517 BasicBlock
* label_bb
= BasicBlock::Create(getGlobalContext(), "bb",func_test4
,0);
518 BasicBlock
* label_bb1
= BasicBlock::Create(getGlobalContext(), "bb1",func_test4
,0);
519 BasicBlock
* label_return
= BasicBlock::Create(getGlobalContext(), "return",func_test4
,0);
521 // Block entry (label_entry_11)
522 BranchInst::Create(label_bb
, label_entry_11
);
524 // Block bb (label_bb)
525 BranchInst::Create(label_bb
, label_bb1
, int1_f
, label_bb
);
527 // Block bb1 (label_bb1)
528 BranchInst::Create(label_bb1
, label_return
, int1_f
, label_bb1
);
530 // Block return (label_return)
531 ReturnInst::Create(getGlobalContext(), label_return
);
540 INITIALIZE_PASS(ModuleNDM
, "mndm", "mndm", false, false)
541 INITIALIZE_PASS_BEGIN(CGPass
, "cgp","cgp", false, false)
542 INITIALIZE_AG_DEPENDENCY(CallGraph
)
543 INITIALIZE_PASS_END(CGPass
, "cgp","cgp", false, false)
544 INITIALIZE_PASS(FPass
, "fp","fp", false, false)
545 INITIALIZE_PASS_BEGIN(LPass
, "lp","lp", false, false)
546 INITIALIZE_PASS_DEPENDENCY(LoopInfo
)
547 INITIALIZE_PASS_END(LPass
, "lp","lp", false, false)
548 INITIALIZE_PASS(BPass
, "bp","bp", false, false)