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