Home | History | Annotate | Download | only in IR
      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/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"
     34 
     35 using namespace llvm;
     36 
     37 namespace llvm {
     38   void initializeModuleNDMPass(PassRegistry&);
     39   void initializeFPassPass(PassRegistry&);
     40   void initializeCGPassPass(PassRegistry&);
     41   void initializeLPassPass(PassRegistry&);
     42   void initializeBPassPass(PassRegistry&);
     43 
     44   namespace {
     45     // ND = no deps
     46     // NM = no modifications
     47     struct ModuleNDNM: public ModulePass {
     48     public:
     49       static char run;
     50       static char ID;
     51       ModuleNDNM() : ModulePass(ID) { }
     52       bool runOnModule(Module &M) override {
     53         run++;
     54         return false;
     55       }
     56       void getAnalysisUsage(AnalysisUsage &AU) const override {
     57         AU.setPreservesAll();
     58       }
     59     };
     60     char ModuleNDNM::ID=0;
     61     char ModuleNDNM::run=0;
     62 
     63     struct ModuleNDM : public ModulePass {
     64     public:
     65       static char run;
     66       static char ID;
     67       ModuleNDM() : ModulePass(ID) {}
     68       bool runOnModule(Module &M) override {
     69         run++;
     70         return true;
     71       }
     72     };
     73     char ModuleNDM::ID=0;
     74     char ModuleNDM::run=0;
     75 
     76     struct ModuleNDM2 : public ModulePass {
     77     public:
     78       static char run;
     79       static char ID;
     80       ModuleNDM2() : ModulePass(ID) {}
     81       bool runOnModule(Module &M) override {
     82         run++;
     83         return true;
     84       }
     85     };
     86     char ModuleNDM2::ID=0;
     87     char ModuleNDM2::run=0;
     88 
     89     struct ModuleDNM : public ModulePass {
     90     public:
     91       static char run;
     92       static char ID;
     93       ModuleDNM() : ModulePass(ID) {
     94         initializeModuleNDMPass(*PassRegistry::getPassRegistry());
     95       }
     96       bool runOnModule(Module &M) override {
     97         run++;
     98         return false;
     99       }
    100       void getAnalysisUsage(AnalysisUsage &AU) const override {
    101         AU.addRequired<ModuleNDM>();
    102         AU.setPreservesAll();
    103       }
    104     };
    105     char ModuleDNM::ID=0;
    106     char ModuleDNM::run=0;
    107 
    108     template<typename P>
    109     struct PassTestBase : public P {
    110     protected:
    111       static int runc;
    112       static bool initialized;
    113       static bool finalized;
    114       int allocated;
    115       void run() {
    116         EXPECT_TRUE(initialized);
    117         EXPECT_FALSE(finalized);
    118         EXPECT_EQ(0, allocated);
    119         allocated++;
    120         runc++;
    121       }
    122     public:
    123       static char ID;
    124       static void finishedOK(int run) {
    125         EXPECT_GT(runc, 0);
    126         EXPECT_TRUE(initialized);
    127         EXPECT_TRUE(finalized);
    128         EXPECT_EQ(run, runc);
    129       }
    130       PassTestBase() : P(ID), allocated(0) {
    131         initialized = false;
    132         finalized = false;
    133         runc = 0;
    134       }
    135 
    136       void releaseMemory() override {
    137         EXPECT_GT(runc, 0);
    138         EXPECT_GT(allocated, 0);
    139         allocated--;
    140       }
    141     };
    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;
    146 
    147     template<typename T, typename P>
    148     struct PassTest : public PassTestBase<P> {
    149     public:
    150 #ifndef _MSC_VER // MSVC complains that Pass is not base class.
    151       using llvm::Pass::doInitialization;
    152       using llvm::Pass::doFinalization;
    153 #endif
    154       bool doInitialization(T &t) override {
    155         EXPECT_FALSE(PassTestBase<P>::initialized);
    156         PassTestBase<P>::initialized = true;
    157         return false;
    158       }
    159       bool doFinalization(T &t) override {
    160         EXPECT_FALSE(PassTestBase<P>::finalized);
    161         PassTestBase<P>::finalized = true;
    162         EXPECT_EQ(0, PassTestBase<P>::allocated);
    163         return false;
    164       }
    165     };
    166 
    167     struct CGPass : public PassTest<CallGraph, CallGraphSCCPass> {
    168     public:
    169       CGPass() {
    170         initializeCGPassPass(*PassRegistry::getPassRegistry());
    171       }
    172       bool runOnSCC(CallGraphSCC &SCMM) override {
    173         run();
    174         return false;
    175       }
    176     };
    177 
    178     struct FPass : public PassTest<Module, FunctionPass> {
    179     public:
    180       bool runOnFunction(Function &F) override {
    181         // FIXME: PR4112
    182         // EXPECT_TRUE(getAnalysisIfAvailable<DataLayout>());
    183         run();
    184         return false;
    185       }
    186     };
    187 
    188     struct LPass : public PassTestBase<LoopPass> {
    189     private:
    190       static int initcount;
    191       static int fincount;
    192     public:
    193       LPass() {
    194         initializeLPassPass(*PassRegistry::getPassRegistry());
    195         initcount = 0; fincount=0;
    196         EXPECT_FALSE(initialized);
    197       }
    198       static void finishedOK(int run, int finalized) {
    199         PassTestBase<LoopPass>::finishedOK(run);
    200         EXPECT_EQ(run, initcount);
    201         EXPECT_EQ(finalized, fincount);
    202       }
    203       using llvm::Pass::doInitialization;
    204       using llvm::Pass::doFinalization;
    205       bool doInitialization(Loop* L, LPPassManager &LPM) override {
    206         initialized = true;
    207         initcount++;
    208         return false;
    209       }
    210       bool runOnLoop(Loop *L, LPPassManager &LPM) override {
    211         run();
    212         return false;
    213       }
    214       bool doFinalization() override {
    215         fincount++;
    216         finalized = true;
    217         return false;
    218       }
    219     };
    220     int LPass::initcount=0;
    221     int LPass::fincount=0;
    222 
    223     struct BPass : public PassTestBase<BasicBlockPass> {
    224     private:
    225       static int inited;
    226       static int fin;
    227     public:
    228       static void finishedOK(int run, int N) {
    229         PassTestBase<BasicBlockPass>::finishedOK(run);
    230         EXPECT_EQ(inited, N);
    231         EXPECT_EQ(fin, N);
    232       }
    233       BPass() {
    234         inited = 0;
    235         fin = 0;
    236       }
    237       bool doInitialization(Module &M) override {
    238         EXPECT_FALSE(initialized);
    239         initialized = true;
    240         return false;
    241       }
    242       bool doInitialization(Function &F) override {
    243         inited++;
    244         return false;
    245       }
    246       bool runOnBasicBlock(BasicBlock &BB) override {
    247         run();
    248         return false;
    249       }
    250       bool doFinalization(Function &F) override {
    251         fin++;
    252         return false;
    253       }
    254       bool doFinalization(Module &M) override {
    255         EXPECT_FALSE(finalized);
    256         finalized = true;
    257         EXPECT_EQ(0, allocated);
    258         return false;
    259       }
    260     };
    261     int BPass::inited=0;
    262     int BPass::fin=0;
    263 
    264     struct OnTheFlyTest: public ModulePass {
    265     public:
    266       static char ID;
    267       OnTheFlyTest() : ModulePass(ID) {
    268         initializeFPassPass(*PassRegistry::getPassRegistry());
    269       }
    270       bool runOnModule(Module &M) override {
    271         for (Module::iterator I=M.begin(),E=M.end(); I != E; ++I) {
    272           Function &F = *I;
    273           {
    274             SCOPED_TRACE("Running on the fly function pass");
    275             getAnalysis<FPass>(F);
    276           }
    277         }
    278         return false;
    279       }
    280       void getAnalysisUsage(AnalysisUsage &AU) const override {
    281         AU.addRequired<FPass>();
    282       }
    283     };
    284     char OnTheFlyTest::ID=0;
    285 
    286     TEST(PassManager, RunOnce) {
    287       LLVMContext Context;
    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();
    293 
    294       mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
    295 
    296       legacy::PassManager Passes;
    297       Passes.add(mNDM2);
    298       Passes.add(mNDM);
    299       Passes.add(mNDNM);
    300       Passes.add(mDNM);
    301 
    302       Passes.run(M);
    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);
    308     }
    309 
    310     TEST(PassManager, ReRun) {
    311       LLVMContext Context;
    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();
    317 
    318       mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
    319 
    320       legacy::PassManager Passes;
    321       Passes.add(mNDM);
    322       Passes.add(mNDNM);
    323       Passes.add(mNDM2);// invalidates mNDM needed by mDNM
    324       Passes.add(mDNM);
    325 
    326       Passes.run(M);
    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);
    333     }
    334 
    335     Module *makeLLVMModule(LLVMContext &Context);
    336 
    337     template<typename T>
    338     void MemoryTestHelper(int run) {
    339       LLVMContext Context;
    340       std::unique_ptr<Module> M(makeLLVMModule(Context));
    341       T *P = new T();
    342       legacy::PassManager Passes;
    343       Passes.add(P);
    344       Passes.run(*M);
    345       T::finishedOK(run);
    346     }
    347 
    348     template<typename T>
    349     void MemoryTestHelper(int run, int N) {
    350       LLVMContext Context;
    351       Module *M = makeLLVMModule(Context);
    352       T *P = new T();
    353       legacy::PassManager Passes;
    354       Passes.add(P);
    355       Passes.run(*M);
    356       T::finishedOK(run, N);
    357       delete M;
    358     }
    359 
    360     TEST(PassManager, Memory) {
    361       // SCC#1: test1->test2->test3->test1
    362       // SCC#2: test4
    363       // SCC#3: indirect call node
    364       {
    365         SCOPED_TRACE("Callgraph pass");
    366         MemoryTestHelper<CGPass>(3);
    367       }
    368 
    369       {
    370         SCOPED_TRACE("Function pass");
    371         MemoryTestHelper<FPass>(4);// 4 functions
    372       }
    373 
    374       {
    375         SCOPED_TRACE("Loop pass");
    376         MemoryTestHelper<LPass>(2, 1); //2 loops, 1 function
    377       }
    378       {
    379         SCOPED_TRACE("Basic block pass");
    380         MemoryTestHelper<BPass>(7, 4); //9 basic blocks
    381       }
    382 
    383     }
    384 
    385     TEST(PassManager, MemoryOnTheFly) {
    386       LLVMContext Context;
    387       Module *M = makeLLVMModule(Context);
    388       {
    389         SCOPED_TRACE("Running OnTheFlyTest");
    390         struct OnTheFlyTest *O = new OnTheFlyTest();
    391         legacy::PassManager Passes;
    392         Passes.add(O);
    393         Passes.run(*M);
    394 
    395         FPass::finishedOK(4);
    396       }
    397       delete M;
    398     }
    399 
    400     // Skips or runs optional passes.
    401     struct CustomOptPassGate : public OptPassGate {
    402       bool Skip;
    403       CustomOptPassGate(bool Skip) : Skip(Skip) { }
    404       bool shouldRunPass(const Pass *P, const Module &U) { return !Skip; }
    405     };
    406 
    407     // Optional module pass.
    408     struct ModuleOpt: public ModulePass {
    409       char run = 0;
    410       static char ID;
    411       ModuleOpt() : ModulePass(ID) { }
    412       bool runOnModule(Module &M) override {
    413         if (!skipModule(M))
    414           run++;
    415         return false;
    416       }
    417     };
    418     char ModuleOpt::ID=0;
    419 
    420     TEST(PassManager, CustomOptPassGate) {
    421       LLVMContext Context0;
    422       LLVMContext Context1;
    423       LLVMContext Context2;
    424       CustomOptPassGate SkipOptionalPasses(true);
    425       CustomOptPassGate RunOptionalPasses(false);
    426 
    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();
    433 
    434       mOpt0->run = mOpt1->run = mOpt2->run = 0;
    435 
    436       legacy::PassManager Passes0;
    437       legacy::PassManager Passes1;
    438       legacy::PassManager Passes2;
    439 
    440       Passes0.add(mOpt0);
    441       Passes1.add(mOpt1);
    442       Passes2.add(mOpt2);
    443 
    444       Context1.setOptPassGate(SkipOptionalPasses);
    445       Context2.setOptPassGate(RunOptionalPasses);
    446 
    447       Passes0.run(M0);
    448       Passes1.run(M1);
    449       Passes2.run(M2);
    450 
    451       // By default optional passes are run.
    452       EXPECT_EQ(1, mOpt0->run);
    453 
    454       // The first context skips optional passes.
    455       EXPECT_EQ(0, mOpt1->run);
    456 
    457       // The second context runs optional passes.
    458       EXPECT_EQ(1, mOpt2->run);
    459     }
    460 
    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");
    468 
    469       // Type Definitions
    470       std::vector<Type*>FuncTy_0_args;
    471       FunctionType *FuncTy_0 = FunctionType::get(
    472           /*Result=*/IntegerType::get(Context, 32),
    473           /*Params=*/FuncTy_0_args,
    474           /*isVarArg=*/false);
    475 
    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,
    481           /*isVarArg=*/false);
    482 
    483       // Function Declarations
    484 
    485       Function* func_test1 = Function::Create(
    486         /*Type=*/FuncTy_0,
    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);
    492 
    493       Function* func_test2 = Function::Create(
    494         /*Type=*/FuncTy_0,
    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);
    500 
    501       Function* func_test3 = Function::Create(
    502         /*Type=*/FuncTy_0,
    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);
    508 
    509       Function* func_test4 = Function::Create(
    510         /*Type=*/FuncTy_2,
    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);
    516 
    517       // Global Variable Declarations
    518 
    519 
    520       // Constant Definitions
    521 
    522       // Global Variable Definitions
    523 
    524       // Function Definitions
    525 
    526       // Function: test1 (func_test1)
    527       {
    528 
    529         BasicBlock *label_entry =
    530             BasicBlock::Create(Context, "entry", func_test1, nullptr);
    531 
    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);
    538 
    539         ReturnInst::Create(Context, int32_3, label_entry);
    540       }
    541 
    542       // Function: test2 (func_test2)
    543       {
    544 
    545         BasicBlock *label_entry_5 =
    546             BasicBlock::Create(Context, "entry", func_test2, nullptr);
    547 
    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);
    554 
    555         ReturnInst::Create(Context, int32_6, label_entry_5);
    556       }
    557 
    558       // Function: test3 (func_test3)
    559       {
    560 
    561         BasicBlock *label_entry_8 =
    562             BasicBlock::Create(Context, "entry", func_test3, nullptr);
    563 
    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);
    570 
    571         ReturnInst::Create(Context, int32_9, label_entry_8);
    572       }
    573 
    574       // Function: test4 (func_test4)
    575       {
    576         Function::arg_iterator args = func_test4->arg_begin();
    577         Value *int1_f = &*args++;
    578         int1_f->setName("f");
    579 
    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);
    588 
    589         // Block entry (label_entry_11)
    590         BranchInst::Create(label_bb, label_entry_11);
    591 
    592         // Block bb (label_bb)
    593         BranchInst::Create(label_bb, label_bb1, int1_f, label_bb);
    594 
    595         // Block bb1 (label_bb1)
    596         BranchInst::Create(label_bb1, label_return, int1_f, label_bb1);
    597 
    598         // Block return (label_return)
    599         ReturnInst::Create(Context, label_return);
    600       }
    601       return mod;
    602     }
    603 
    604   }
    605 }
    606 
    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)
    616