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/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         run++;
    102         return false;
    103       }
    104       void getAnalysisUsage(AnalysisUsage &AU) const override {
    105         AU.addRequired<ModuleNDM>();
    106         AU.setPreservesAll();
    107       }
    108     };
    109     char ModuleDNM::ID=0;
    110     char ModuleDNM::run=0;
    111 
    112     template<typename P>
    113     struct PassTestBase : public P {
    114     protected:
    115       static int runc;
    116       static bool initialized;
    117       static bool finalized;
    118       int allocated;
    119       void run() {
    120         EXPECT_TRUE(initialized);
    121         EXPECT_FALSE(finalized);
    122         EXPECT_EQ(0, allocated);
    123         allocated++;
    124         runc++;
    125       }
    126     public:
    127       static char ID;
    128       static void finishedOK(int run) {
    129         EXPECT_GT(runc, 0);
    130         EXPECT_TRUE(initialized);
    131         EXPECT_TRUE(finalized);
    132         EXPECT_EQ(run, runc);
    133       }
    134       PassTestBase() : P(ID), allocated(0) {
    135         initialized = false;
    136         finalized = false;
    137         runc = 0;
    138       }
    139 
    140       void releaseMemory() override {
    141         EXPECT_GT(runc, 0);
    142         EXPECT_GT(allocated, 0);
    143         allocated--;
    144       }
    145     };
    146     template<typename P> char PassTestBase<P>::ID;
    147     template<typename P> int PassTestBase<P>::runc;
    148     template<typename P> bool PassTestBase<P>::initialized;
    149     template<typename P> bool PassTestBase<P>::finalized;
    150 
    151     template<typename T, typename P>
    152     struct PassTest : public PassTestBase<P> {
    153     public:
    154 #ifndef _MSC_VER // MSVC complains that Pass is not base class.
    155       using llvm::Pass::doInitialization;
    156       using llvm::Pass::doFinalization;
    157 #endif
    158       bool doInitialization(T &t) override {
    159         EXPECT_FALSE(PassTestBase<P>::initialized);
    160         PassTestBase<P>::initialized = true;
    161         return false;
    162       }
    163       bool doFinalization(T &t) override {
    164         EXPECT_FALSE(PassTestBase<P>::finalized);
    165         PassTestBase<P>::finalized = true;
    166         EXPECT_EQ(0, PassTestBase<P>::allocated);
    167         return false;
    168       }
    169     };
    170 
    171     struct CGPass : public PassTest<CallGraph, CallGraphSCCPass> {
    172     public:
    173       CGPass() {
    174         initializeCGPassPass(*PassRegistry::getPassRegistry());
    175       }
    176       bool runOnSCC(CallGraphSCC &SCMM) override {
    177         run();
    178         return false;
    179       }
    180     };
    181 
    182     struct FPass : public PassTest<Module, FunctionPass> {
    183     public:
    184       bool runOnFunction(Function &F) override {
    185         // FIXME: PR4112
    186         // EXPECT_TRUE(getAnalysisIfAvailable<DataLayout>());
    187         run();
    188         return false;
    189       }
    190     };
    191 
    192     struct LPass : public PassTestBase<LoopPass> {
    193     private:
    194       static int initcount;
    195       static int fincount;
    196     public:
    197       LPass() {
    198         initializeLPassPass(*PassRegistry::getPassRegistry());
    199         initcount = 0; fincount=0;
    200         EXPECT_FALSE(initialized);
    201       }
    202       static void finishedOK(int run, int finalized) {
    203         PassTestBase<LoopPass>::finishedOK(run);
    204         EXPECT_EQ(run, initcount);
    205         EXPECT_EQ(finalized, fincount);
    206       }
    207       using llvm::Pass::doInitialization;
    208       using llvm::Pass::doFinalization;
    209       bool doInitialization(Loop* L, LPPassManager &LPM) override {
    210         initialized = true;
    211         initcount++;
    212         return false;
    213       }
    214       bool runOnLoop(Loop *L, LPPassManager &LPM) override {
    215         run();
    216         return false;
    217       }
    218       bool doFinalization() override {
    219         fincount++;
    220         finalized = true;
    221         return false;
    222       }
    223     };
    224     int LPass::initcount=0;
    225     int LPass::fincount=0;
    226 
    227     struct BPass : public PassTestBase<BasicBlockPass> {
    228     private:
    229       static int inited;
    230       static int fin;
    231     public:
    232       static void finishedOK(int run, int N) {
    233         PassTestBase<BasicBlockPass>::finishedOK(run);
    234         EXPECT_EQ(inited, N);
    235         EXPECT_EQ(fin, N);
    236       }
    237       BPass() {
    238         inited = 0;
    239         fin = 0;
    240       }
    241       bool doInitialization(Module &M) override {
    242         EXPECT_FALSE(initialized);
    243         initialized = true;
    244         return false;
    245       }
    246       bool doInitialization(Function &F) override {
    247         inited++;
    248         return false;
    249       }
    250       bool runOnBasicBlock(BasicBlock &BB) override {
    251         run();
    252         return false;
    253       }
    254       bool doFinalization(Function &F) override {
    255         fin++;
    256         return false;
    257       }
    258       bool doFinalization(Module &M) override {
    259         EXPECT_FALSE(finalized);
    260         finalized = true;
    261         EXPECT_EQ(0, allocated);
    262         return false;
    263       }
    264     };
    265     int BPass::inited=0;
    266     int BPass::fin=0;
    267 
    268     struct OnTheFlyTest: public ModulePass {
    269     public:
    270       static char ID;
    271       OnTheFlyTest() : ModulePass(ID) {
    272         initializeFPassPass(*PassRegistry::getPassRegistry());
    273       }
    274       bool runOnModule(Module &M) override {
    275         for (Module::iterator I=M.begin(),E=M.end(); I != E; ++I) {
    276           Function &F = *I;
    277           {
    278             SCOPED_TRACE("Running on the fly function pass");
    279             getAnalysis<FPass>(F);
    280           }
    281         }
    282         return false;
    283       }
    284       void getAnalysisUsage(AnalysisUsage &AU) const override {
    285         AU.addRequired<FPass>();
    286       }
    287     };
    288     char OnTheFlyTest::ID=0;
    289 
    290     TEST(PassManager, RunOnce) {
    291       Module M("test-once", getGlobalContext());
    292       struct ModuleNDNM *mNDNM = new ModuleNDNM();
    293       struct ModuleDNM *mDNM = new ModuleDNM();
    294       struct ModuleNDM *mNDM = new ModuleNDM();
    295       struct ModuleNDM2 *mNDM2 = new ModuleNDM2();
    296 
    297       mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
    298 
    299       legacy::PassManager Passes;
    300       Passes.add(mNDM2);
    301       Passes.add(mNDM);
    302       Passes.add(mNDNM);
    303       Passes.add(mDNM);
    304 
    305       Passes.run(M);
    306       // each pass must be run exactly once, since nothing invalidates them
    307       EXPECT_EQ(1, mNDM->run);
    308       EXPECT_EQ(1, mNDNM->run);
    309       EXPECT_EQ(1, mDNM->run);
    310       EXPECT_EQ(1, mNDM2->run);
    311     }
    312 
    313     TEST(PassManager, ReRun) {
    314       Module M("test-rerun", getGlobalContext());
    315       struct ModuleNDNM *mNDNM = new ModuleNDNM();
    316       struct ModuleDNM *mDNM = new ModuleDNM();
    317       struct ModuleNDM *mNDM = new ModuleNDM();
    318       struct ModuleNDM2 *mNDM2 = new ModuleNDM2();
    319 
    320       mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
    321 
    322       legacy::PassManager Passes;
    323       Passes.add(mNDM);
    324       Passes.add(mNDNM);
    325       Passes.add(mNDM2);// invalidates mNDM needed by mDNM
    326       Passes.add(mDNM);
    327 
    328       Passes.run(M);
    329       // Some passes must be rerun because a pass that modified the
    330       // module/function was run in between
    331       EXPECT_EQ(2, mNDM->run);
    332       EXPECT_EQ(1, mNDNM->run);
    333       EXPECT_EQ(1, mNDM2->run);
    334       EXPECT_EQ(1, mDNM->run);
    335     }
    336 
    337     Module* makeLLVMModule();
    338 
    339     template<typename T>
    340     void MemoryTestHelper(int run) {
    341       std::unique_ptr<Module> M(makeLLVMModule());
    342       T *P = new T();
    343       legacy::PassManager Passes;
    344       Passes.add(P);
    345       Passes.run(*M);
    346       T::finishedOK(run);
    347     }
    348 
    349     template<typename T>
    350     void MemoryTestHelper(int run, int N) {
    351       Module *M = makeLLVMModule();
    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       Module *M = makeLLVMModule();
    387       {
    388         SCOPED_TRACE("Running OnTheFlyTest");
    389         struct OnTheFlyTest *O = new OnTheFlyTest();
    390         legacy::PassManager Passes;
    391         Passes.add(O);
    392         Passes.run(*M);
    393 
    394         FPass::finishedOK(4);
    395       }
    396       delete M;
    397     }
    398 
    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                          "a:0:64-s:64:64-f80:128:128");
    405       mod->setTargetTriple("x86_64-unknown-linux-gnu");
    406 
    407       // Type Definitions
    408       std::vector<Type*>FuncTy_0_args;
    409       FunctionType* FuncTy_0 = FunctionType::get(
    410         /*Result=*/IntegerType::get(getGlobalContext(), 32),
    411         /*Params=*/FuncTy_0_args,
    412         /*isVarArg=*/false);
    413 
    414       std::vector<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,
    419         /*isVarArg=*/false);
    420 
    421 
    422       // Function Declarations
    423 
    424       Function* func_test1 = Function::Create(
    425         /*Type=*/FuncTy_0,
    426         /*Linkage=*/GlobalValue::ExternalLinkage,
    427         /*Name=*/"test1", mod);
    428       func_test1->setCallingConv(CallingConv::C);
    429       AttributeSet func_test1_PAL;
    430       func_test1->setAttributes(func_test1_PAL);
    431 
    432       Function* func_test2 = Function::Create(
    433         /*Type=*/FuncTy_0,
    434         /*Linkage=*/GlobalValue::ExternalLinkage,
    435         /*Name=*/"test2", mod);
    436       func_test2->setCallingConv(CallingConv::C);
    437       AttributeSet func_test2_PAL;
    438       func_test2->setAttributes(func_test2_PAL);
    439 
    440       Function* func_test3 = Function::Create(
    441         /*Type=*/FuncTy_0,
    442         /*Linkage=*/GlobalValue::ExternalLinkage,
    443         /*Name=*/"test3", mod);
    444       func_test3->setCallingConv(CallingConv::C);
    445       AttributeSet func_test3_PAL;
    446       func_test3->setAttributes(func_test3_PAL);
    447 
    448       Function* func_test4 = Function::Create(
    449         /*Type=*/FuncTy_2,
    450         /*Linkage=*/GlobalValue::ExternalLinkage,
    451         /*Name=*/"test4", mod);
    452       func_test4->setCallingConv(CallingConv::C);
    453       AttributeSet func_test4_PAL;
    454       func_test4->setAttributes(func_test4_PAL);
    455 
    456       // Global Variable Declarations
    457 
    458 
    459       // Constant Definitions
    460 
    461       // Global Variable Definitions
    462 
    463       // Function Definitions
    464 
    465       // Function: test1 (func_test1)
    466       {
    467 
    468         BasicBlock* label_entry = BasicBlock::Create(getGlobalContext(), "entry",func_test1,nullptr);
    469 
    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);AttributeSet int32_3_PAL;
    474         int32_3->setAttributes(int32_3_PAL);
    475 
    476         ReturnInst::Create(getGlobalContext(), int32_3, label_entry);
    477 
    478       }
    479 
    480       // Function: test2 (func_test2)
    481       {
    482 
    483         BasicBlock* label_entry_5 = BasicBlock::Create(getGlobalContext(), "entry",func_test2,nullptr);
    484 
    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);AttributeSet int32_6_PAL;
    489         int32_6->setAttributes(int32_6_PAL);
    490 
    491         ReturnInst::Create(getGlobalContext(), int32_6, label_entry_5);
    492 
    493       }
    494 
    495       // Function: test3 (func_test3)
    496       {
    497 
    498         BasicBlock* label_entry_8 = BasicBlock::Create(getGlobalContext(), "entry",func_test3,nullptr);
    499 
    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);AttributeSet int32_9_PAL;
    504         int32_9->setAttributes(int32_9_PAL);
    505 
    506         ReturnInst::Create(getGlobalContext(), int32_9, label_entry_8);
    507 
    508       }
    509 
    510       // Function: test4 (func_test4)
    511       {
    512         Function::arg_iterator args = func_test4->arg_begin();
    513         Value *int1_f = &*args++;
    514         int1_f->setName("f");
    515 
    516         BasicBlock* label_entry_11 = BasicBlock::Create(getGlobalContext(), "entry",func_test4,nullptr);
    517         BasicBlock* label_bb = BasicBlock::Create(getGlobalContext(), "bb",func_test4,nullptr);
    518         BasicBlock* label_bb1 = BasicBlock::Create(getGlobalContext(), "bb1",func_test4,nullptr);
    519         BasicBlock* label_return = BasicBlock::Create(getGlobalContext(), "return",func_test4,nullptr);
    520 
    521         // Block entry (label_entry_11)
    522         BranchInst::Create(label_bb, label_entry_11);
    523 
    524         // Block bb (label_bb)
    525         BranchInst::Create(label_bb, label_bb1, int1_f, label_bb);
    526 
    527         // Block bb1 (label_bb1)
    528         BranchInst::Create(label_bb1, label_return, int1_f, label_bb1);
    529 
    530         // Block return (label_return)
    531         ReturnInst::Create(getGlobalContext(), label_return);
    532 
    533       }
    534       return mod;
    535     }
    536 
    537   }
    538 }
    539 
    540 INITIALIZE_PASS(ModuleNDM, "mndm", "mndm", false, false)
    541 INITIALIZE_PASS_BEGIN(CGPass, "cgp","cgp", false, false)
    542 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
    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(LoopInfoWrapperPass)
    547 INITIALIZE_PASS_END(LPass, "lp","lp", false, false)
    548 INITIALIZE_PASS(BPass, "bp","bp", false, false)
    549