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