Stop building RTTI information for *most* llvm libraries. Notable
[oota-llvm.git] / unittests / VMCore / PassManagerTest.cpp
1 //===- llvm/unittest/VMCore/PassManager.cpp - Constants unit tests ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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"
34
35 int dummy;
36
37 #if 0
38
39 namespace llvm {
40   namespace {
41     // ND = no deps
42     // NM = no modifications
43     struct ModuleNDNM: public ModulePass {
44     public:
45       static char run;
46       static char ID;
47       ModuleNDNM() : ModulePass(&ID) {}
48       virtual bool runOnModule(Module &M) {
49         run++;
50         return false;
51       }
52       virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53         AU.setPreservesAll();
54       }
55     };
56     char ModuleNDNM::ID=0;
57     char ModuleNDNM::run=0;
58
59     struct ModuleNDM : public ModulePass {
60     public:
61       static char run;
62       static char ID;
63       ModuleNDM() : ModulePass(&ID) {}
64       virtual bool runOnModule(Module &M) {
65         run++;
66         return true;
67       }
68     };
69     char ModuleNDM::ID=0;
70     char ModuleNDM::run=0;
71     RegisterPass<ModuleNDM> X("mndm","mndm",false,false);
72
73     struct ModuleNDM2 : public ModulePass {
74     public:
75       static char run;
76       static char ID;
77       ModuleNDM2() : ModulePass(&ID) {}
78       virtual bool runOnModule(Module &M) {
79         run++;
80         return true;
81       }
82     };
83     char ModuleNDM2::ID=0;
84     char ModuleNDM2::run=0;
85
86     struct ModuleDNM : public ModulePass {
87     public:
88       static char run;
89       static char ID;
90       ModuleDNM() : ModulePass(&ID) {}
91       virtual bool runOnModule(Module &M) {
92         EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
93         run++;
94         return false;
95       }
96       virtual void getAnalysisUsage(AnalysisUsage &AU) const {
97         AU.addRequired<ModuleNDM>();
98         AU.setPreservesAll();
99       }
100     };
101     char ModuleDNM::ID=0;
102     char ModuleDNM::run=0;
103
104     template<typename P>
105     struct PassTestBase : public P {
106     protected:
107       static int runc;
108       static bool initialized;
109       static bool finalized;
110       int allocated;
111       void run() {
112         EXPECT_EQ(true, initialized);
113         EXPECT_EQ(false, finalized);
114         EXPECT_EQ(0, allocated);
115         allocated++;
116         runc++;
117       }
118     public:
119       static char ID;
120       static void finishedOK(int run) {
121         EXPECT_GT(runc, 0);
122         EXPECT_EQ(true, initialized);
123         EXPECT_EQ(true, finalized);
124         EXPECT_EQ(run, runc);
125       }
126       PassTestBase() : P(&ID), allocated(0) {
127         initialized = false;
128         finalized = false;
129         runc = 0;
130       }
131
132       virtual void releaseMemory() {
133         EXPECT_GT(runc, 0);
134         EXPECT_GT(allocated, 0);
135         allocated--;
136       }
137     };
138     template<typename P> char PassTestBase<P>::ID;
139     template<typename P> int PassTestBase<P>::runc;
140     template<typename P> bool PassTestBase<P>::initialized;
141     template<typename P> bool PassTestBase<P>::finalized;
142
143     template<typename T, typename P>
144     struct PassTest : public PassTestBase<P> {
145     public:
146       virtual bool doInitialization(T &t) {
147         EXPECT_EQ(false, PassTestBase<P>::initialized);
148         PassTestBase<P>::initialized = true;
149         return false;
150       }
151       virtual bool doFinalization(T &t) {
152         EXPECT_EQ(false, PassTestBase<P>::finalized);
153         PassTestBase<P>::finalized = true;
154         EXPECT_EQ(0, PassTestBase<P>::allocated);
155         return false;
156       }
157     };
158
159     struct CGPass : public PassTest<CallGraph, CallGraphSCCPass> {
160     public:
161       virtual bool runOnSCC(std::vector<CallGraphNode*> &SCMM) {
162         EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
163         run();
164         return false;
165       }
166     };
167     RegisterPass<CGPass> X1("cgp","cgp");
168
169     struct FPass : public PassTest<Module, FunctionPass> {
170     public:
171       virtual bool runOnFunction(Function &F) {
172         // FIXME: PR4112
173         // EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
174         run();
175         return false;
176       }
177     };
178     RegisterPass<FPass> X2("fp","fp");
179
180     struct LPass : public PassTestBase<LoopPass> {
181     private:
182       static int initcount;
183       static int fincount;
184     public:
185       LPass() {
186         initcount = 0; fincount=0;
187         EXPECT_EQ(false, initialized);
188       }
189       static void finishedOK(int run, int finalized) {
190         PassTestBase<LoopPass>::finishedOK(run);
191         EXPECT_EQ(run, initcount);
192         EXPECT_EQ(finalized, fincount);
193       }
194       virtual bool doInitialization(Loop* L, LPPassManager &LPM) {
195         initialized = true;
196         initcount++;
197         return false;
198       }
199       virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
200         EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
201         run();
202         return false;
203       }
204       virtual bool doFinalization() {
205         fincount++;
206         finalized = true;
207         return false;
208       }
209     };
210     int LPass::initcount=0;
211     int LPass::fincount=0;
212     RegisterPass<LPass> X3("lp","lp");
213
214     struct BPass : public PassTestBase<BasicBlockPass> {
215     private:
216       static int inited;
217       static int fin;
218     public:
219       static void finishedOK(int run, int N) {
220         PassTestBase<BasicBlockPass>::finishedOK(run);
221         EXPECT_EQ(inited, N);
222         EXPECT_EQ(fin, N);
223       }
224       BPass() {
225         inited = 0;
226         fin = 0;
227       }
228       virtual bool doInitialization(Module &M) {
229         EXPECT_EQ(false, initialized);
230         initialized = true;
231         return false;
232       }
233       virtual bool doInitialization(Function &F) {
234         inited++;
235         return false;
236       }
237       virtual bool runOnBasicBlock(BasicBlock &BB) {
238         EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
239         run();
240         return false;
241       }
242       virtual bool doFinalization(Function &F) {
243         fin++;
244         return false;
245       }
246       virtual bool doFinalization(Module &M) {
247         EXPECT_EQ(false, finalized);
248         finalized = true;
249         EXPECT_EQ(0, allocated);
250         return false;
251       }
252     };
253     int BPass::inited=0;
254     int BPass::fin=0;
255     RegisterPass<BPass> X4("bp","bp");
256
257     struct OnTheFlyTest: public ModulePass {
258     public:
259       static char ID;
260       OnTheFlyTest() : ModulePass(&ID) {}
261       virtual bool runOnModule(Module &M) {
262         EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
263         for (Module::iterator I=M.begin(),E=M.end(); I != E; ++I) {
264           Function &F = *I;
265           {
266             SCOPED_TRACE("Running on the fly function pass");
267             getAnalysis<FPass>(F);
268           }
269         }
270         return false;
271       }
272       virtual void getAnalysisUsage(AnalysisUsage &AU) const {
273         AU.addRequired<FPass>();
274       }
275     };
276     char OnTheFlyTest::ID=0;
277
278     TEST(PassManager, RunOnce) {
279       Module M("test-once", getGlobalContext());
280       struct ModuleNDNM *mNDNM = new ModuleNDNM();
281       struct ModuleDNM *mDNM = new ModuleDNM();
282       struct ModuleNDM *mNDM = new ModuleNDM();
283       struct ModuleNDM2 *mNDM2 = new ModuleNDM2();
284
285       mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
286
287       PassManager Passes;
288       Passes.add(new TargetData(&M));
289       Passes.add(mNDM2);
290       Passes.add(mNDM);
291       Passes.add(mNDNM);
292       Passes.add(mDNM);
293
294       Passes.run(M);
295       // each pass must be run exactly once, since nothing invalidates them
296       EXPECT_EQ(1, mNDM->run);
297       EXPECT_EQ(1, mNDNM->run);
298       EXPECT_EQ(1, mDNM->run);
299       EXPECT_EQ(1, mNDM2->run);
300     }
301
302     TEST(PassManager, ReRun) {
303       Module M("test-rerun", getGlobalContext());
304       struct ModuleNDNM *mNDNM = new ModuleNDNM();
305       struct ModuleDNM *mDNM = new ModuleDNM();
306       struct ModuleNDM *mNDM = new ModuleNDM();
307       struct ModuleNDM2 *mNDM2 = new ModuleNDM2();
308
309       mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
310
311       PassManager Passes;
312       Passes.add(new TargetData(&M));
313       Passes.add(mNDM);
314       Passes.add(mNDNM);
315       Passes.add(mNDM2);// invalidates mNDM needed by mDNM
316       Passes.add(mDNM);
317
318       Passes.run(M);
319       // Some passes must be rerun because a pass that modified the
320       // module/function was run inbetween
321       EXPECT_EQ(2, mNDM->run);
322       EXPECT_EQ(1, mNDNM->run);
323       EXPECT_EQ(1, mNDM2->run);
324       EXPECT_EQ(1, mDNM->run);
325     }
326
327     Module* makeLLVMModule();
328
329     template<typename T>
330     void MemoryTestHelper(int run) {
331       Module *M = makeLLVMModule();
332       T *P = new T();
333       PassManager Passes;
334       Passes.add(new TargetData(M));
335       Passes.add(P);
336       Passes.run(*M);
337       T::finishedOK(run);
338     }
339
340     template<typename T>
341     void MemoryTestHelper(int run, int N) {
342       Module *M = makeLLVMModule();
343       T *P = new T();
344       PassManager Passes;
345       Passes.add(new TargetData(M));
346       Passes.add(P);
347       Passes.run(*M);
348       T::finishedOK(run, N);
349       delete M;
350     }
351
352     TEST(PassManager, Memory) {
353       // SCC#1: test1->test2->test3->test1
354       // SCC#2: test4
355       // SCC#3: indirect call node
356       {
357         SCOPED_TRACE("Callgraph pass");
358         MemoryTestHelper<CGPass>(3);
359       }
360
361       {
362         SCOPED_TRACE("Function pass");
363         MemoryTestHelper<FPass>(4);// 4 functions
364       }
365
366       {
367         SCOPED_TRACE("Loop pass");
368         MemoryTestHelper<LPass>(2, 1); //2 loops, 1 function
369       }
370       {
371         SCOPED_TRACE("Basic block pass");
372         MemoryTestHelper<BPass>(7, 4); //9 basic blocks
373       }
374
375     }
376
377     TEST(PassManager, MemoryOnTheFly) {
378       Module *M = makeLLVMModule();
379       {
380         SCOPED_TRACE("Running OnTheFlyTest");
381         struct OnTheFlyTest *O = new OnTheFlyTest();
382         PassManager Passes;
383         Passes.add(new TargetData(M));
384         Passes.add(O);
385         Passes.run(*M);
386
387         FPass::finishedOK(4);
388       }
389       delete M;
390     }
391
392     Module* makeLLVMModule() {
393       // Module Construction
394       Module* mod = new Module("test-mem", getGlobalContext());
395       mod->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
396                          "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
397                          "a0:0:64-s0:64:64-f80:128:128");
398       mod->setTargetTriple("x86_64-unknown-linux-gnu");
399
400       // Type Definitions
401       std::vector<const Type*>FuncTy_0_args;
402       FunctionType* FuncTy_0 = FunctionType::get(
403         /*Result=*/IntegerType::get(getGlobalContext(), 32),
404         /*Params=*/FuncTy_0_args,
405         /*isVarArg=*/false);
406
407       std::vector<const Type*>FuncTy_2_args;
408       FuncTy_2_args.push_back(IntegerType::get(getGlobalContext(), 1));
409       FunctionType* FuncTy_2 = FunctionType::get(
410         /*Result=*/Type::getVoidTy(getGlobalContext()),
411         /*Params=*/FuncTy_2_args,
412         /*isVarArg=*/false);
413
414
415       // Function Declarations
416
417       Function* func_test1 = Function::Create(
418         /*Type=*/FuncTy_0,
419         /*Linkage=*/GlobalValue::ExternalLinkage,
420         /*Name=*/"test1", mod);
421       func_test1->setCallingConv(CallingConv::C);
422       AttrListPtr func_test1_PAL;
423       func_test1->setAttributes(func_test1_PAL);
424
425       Function* func_test2 = Function::Create(
426         /*Type=*/FuncTy_0,
427         /*Linkage=*/GlobalValue::ExternalLinkage,
428         /*Name=*/"test2", mod);
429       func_test2->setCallingConv(CallingConv::C);
430       AttrListPtr func_test2_PAL;
431       func_test2->setAttributes(func_test2_PAL);
432
433       Function* func_test3 = Function::Create(
434         /*Type=*/FuncTy_0,
435         /*Linkage=*/GlobalValue::ExternalLinkage,
436         /*Name=*/"test3", mod);
437       func_test3->setCallingConv(CallingConv::C);
438       AttrListPtr func_test3_PAL;
439       func_test3->setAttributes(func_test3_PAL);
440
441       Function* func_test4 = Function::Create(
442         /*Type=*/FuncTy_2,
443         /*Linkage=*/GlobalValue::ExternalLinkage,
444         /*Name=*/"test4", mod);
445       func_test4->setCallingConv(CallingConv::C);
446       AttrListPtr func_test4_PAL;
447       func_test4->setAttributes(func_test4_PAL);
448
449       // Global Variable Declarations
450
451
452       // Constant Definitions
453
454       // Global Variable Definitions
455
456       // Function Definitions
457
458       // Function: test1 (func_test1)
459       {
460
461         BasicBlock* label_entry = BasicBlock::Create(getGlobalContext(), "entry",func_test1,0);
462
463         // Block entry (label_entry)
464         CallInst* int32_3 = CallInst::Create(func_test2, "", label_entry);
465         int32_3->setCallingConv(CallingConv::C);
466         int32_3->setTailCall(false);AttrListPtr int32_3_PAL;
467         int32_3->setAttributes(int32_3_PAL);
468
469         ReturnInst::Create(getGlobalContext(), int32_3, label_entry);
470
471       }
472
473       // Function: test2 (func_test2)
474       {
475
476         BasicBlock* label_entry_5 = BasicBlock::Create(getGlobalContext(), "entry",func_test2,0);
477
478         // Block entry (label_entry_5)
479         CallInst* int32_6 = CallInst::Create(func_test3, "", label_entry_5);
480         int32_6->setCallingConv(CallingConv::C);
481         int32_6->setTailCall(false);AttrListPtr int32_6_PAL;
482         int32_6->setAttributes(int32_6_PAL);
483
484         ReturnInst::Create(getGlobalContext(), int32_6, label_entry_5);
485
486       }
487
488       // Function: test3 (func_test3)
489       {
490
491         BasicBlock* label_entry_8 = BasicBlock::Create(getGlobalContext(), "entry",func_test3,0);
492
493         // Block entry (label_entry_8)
494         CallInst* int32_9 = CallInst::Create(func_test1, "", label_entry_8);
495         int32_9->setCallingConv(CallingConv::C);
496         int32_9->setTailCall(false);AttrListPtr int32_9_PAL;
497         int32_9->setAttributes(int32_9_PAL);
498
499         ReturnInst::Create(getGlobalContext(), int32_9, label_entry_8);
500
501       }
502
503       // Function: test4 (func_test4)
504       {
505         Function::arg_iterator args = func_test4->arg_begin();
506         Value* int1_f = args++;
507         int1_f->setName("f");
508
509         BasicBlock* label_entry_11 = BasicBlock::Create(getGlobalContext(), "entry",func_test4,0);
510         BasicBlock* label_bb = BasicBlock::Create(getGlobalContext(), "bb",func_test4,0);
511         BasicBlock* label_bb1 = BasicBlock::Create(getGlobalContext(), "bb1",func_test4,0);
512         BasicBlock* label_return = BasicBlock::Create(getGlobalContext(), "return",func_test4,0);
513
514         // Block entry (label_entry_11)
515         BranchInst::Create(label_bb, label_entry_11);
516
517         // Block bb (label_bb)
518         BranchInst::Create(label_bb, label_bb1, int1_f, label_bb);
519
520         // Block bb1 (label_bb1)
521         BranchInst::Create(label_bb1, label_return, int1_f, label_bb1);
522
523         // Block return (label_return)
524         ReturnInst::Create(getGlobalContext(), label_return);
525
526       }
527       return mod;
528     }
529
530   }
531 }
532 #endif