1 //===- llvm/unittest/IR/LegacyPassManager.cpp - Legacy PassManager tests --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
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"
42 void initializeModuleNDMPass(PassRegistry&);
43 void initializeFPassPass(PassRegistry&);
44 void initializeCGPassPass(PassRegistry&);
45 void initializeLPassPass(PassRegistry&);
46 void initializeBPassPass(PassRegistry&);
50 // NM = no modifications
51 struct ModuleNDNM: public ModulePass {
55 ModuleNDNM() : ModulePass(ID) { }
56 bool runOnModule(Module &M) override {
60 void getAnalysisUsage(AnalysisUsage &AU) const override {
64 char ModuleNDNM::ID=0;
65 char ModuleNDNM::run=0;
67 struct ModuleNDM : public ModulePass {
71 ModuleNDM() : ModulePass(ID) {}
72 bool runOnModule(Module &M) override {
78 char ModuleNDM::run=0;
80 struct ModuleNDM2 : public ModulePass {
84 ModuleNDM2() : ModulePass(ID) {}
85 bool runOnModule(Module &M) override {
90 char ModuleNDM2::ID=0;
91 char ModuleNDM2::run=0;
93 struct ModuleDNM : public ModulePass {
97 ModuleDNM() : ModulePass(ID) {
98 initializeModuleNDMPass(*PassRegistry::getPassRegistry());
100 bool runOnModule(Module &M) override {
101 EXPECT_TRUE(getAnalysisIfAvailable<DataLayoutPass>());
105 void getAnalysisUsage(AnalysisUsage &AU) const override {
106 AU.addRequired<ModuleNDM>();
107 AU.setPreservesAll();
110 char ModuleDNM::ID=0;
111 char ModuleDNM::run=0;
114 struct PassTestBase : public P {
117 static bool initialized;
118 static bool finalized;
121 EXPECT_TRUE(initialized);
122 EXPECT_FALSE(finalized);
123 EXPECT_EQ(0, allocated);
129 static void finishedOK(int run) {
131 EXPECT_TRUE(initialized);
132 EXPECT_TRUE(finalized);
133 EXPECT_EQ(run, runc);
135 PassTestBase() : P(ID), allocated(0) {
141 void releaseMemory() override {
143 EXPECT_GT(allocated, 0);
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;
152 template<typename T, typename P>
153 struct PassTest : public PassTestBase<P> {
155 #ifndef _MSC_VER // MSVC complains that Pass is not base class.
156 using llvm::Pass::doInitialization;
157 using llvm::Pass::doFinalization;
159 bool doInitialization(T &t) override {
160 EXPECT_FALSE(PassTestBase<P>::initialized);
161 PassTestBase<P>::initialized = true;
164 bool doFinalization(T &t) override {
165 EXPECT_FALSE(PassTestBase<P>::finalized);
166 PassTestBase<P>::finalized = true;
167 EXPECT_EQ(0, PassTestBase<P>::allocated);
172 struct CGPass : public PassTest<CallGraph, CallGraphSCCPass> {
175 initializeCGPassPass(*PassRegistry::getPassRegistry());
177 bool runOnSCC(CallGraphSCC &SCMM) override {
178 EXPECT_TRUE(getAnalysisIfAvailable<DataLayoutPass>());
184 struct FPass : public PassTest<Module, FunctionPass> {
186 bool runOnFunction(Function &F) override {
188 // EXPECT_TRUE(getAnalysisIfAvailable<DataLayout>());
194 struct LPass : public PassTestBase<LoopPass> {
196 static int initcount;
200 initializeLPassPass(*PassRegistry::getPassRegistry());
201 initcount = 0; fincount=0;
202 EXPECT_FALSE(initialized);
204 static void finishedOK(int run, int finalized) {
205 PassTestBase<LoopPass>::finishedOK(run);
206 EXPECT_EQ(run, initcount);
207 EXPECT_EQ(finalized, fincount);
209 using llvm::Pass::doInitialization;
210 using llvm::Pass::doFinalization;
211 bool doInitialization(Loop* L, LPPassManager &LPM) override {
216 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
217 EXPECT_TRUE(getAnalysisIfAvailable<DataLayoutPass>());
221 bool doFinalization() override {
227 int LPass::initcount=0;
228 int LPass::fincount=0;
230 struct BPass : public PassTestBase<BasicBlockPass> {
235 static void finishedOK(int run, int N) {
236 PassTestBase<BasicBlockPass>::finishedOK(run);
237 EXPECT_EQ(inited, N);
244 bool doInitialization(Module &M) override {
245 EXPECT_FALSE(initialized);
249 bool doInitialization(Function &F) override {
253 bool runOnBasicBlock(BasicBlock &BB) override {
254 EXPECT_TRUE(getAnalysisIfAvailable<DataLayoutPass>());
258 bool doFinalization(Function &F) override {
262 bool doFinalization(Module &M) override {
263 EXPECT_FALSE(finalized);
265 EXPECT_EQ(0, allocated);
272 struct OnTheFlyTest: public ModulePass {
275 OnTheFlyTest() : ModulePass(ID) {
276 initializeFPassPass(*PassRegistry::getPassRegistry());
278 bool runOnModule(Module &M) override {
279 EXPECT_TRUE(getAnalysisIfAvailable<DataLayoutPass>());
280 for (Module::iterator I=M.begin(),E=M.end(); I != E; ++I) {
283 SCOPED_TRACE("Running on the fly function pass");
284 getAnalysis<FPass>(F);
289 void getAnalysisUsage(AnalysisUsage &AU) const override {
290 AU.addRequired<FPass>();
293 char OnTheFlyTest::ID=0;
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();
302 mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
304 legacy::PassManager Passes;
305 Passes.add(new DataLayoutPass());
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);
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();
326 mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
328 legacy::PassManager Passes;
329 Passes.add(new DataLayoutPass());
332 Passes.add(mNDM2);// invalidates mNDM needed by mDNM
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);
344 Module* makeLLVMModule();
347 void MemoryTestHelper(int run) {
348 std::unique_ptr<Module> M(makeLLVMModule());
350 legacy::PassManager Passes;
351 Passes.add(new DataLayoutPass());
358 void MemoryTestHelper(int run, int N) {
359 Module *M = makeLLVMModule();
361 legacy::PassManager Passes;
362 Passes.add(new DataLayoutPass());
365 T::finishedOK(run, N);
369 TEST(PassManager, Memory) {
370 // SCC#1: test1->test2->test3->test1
372 // SCC#3: indirect call node
374 SCOPED_TRACE("Callgraph pass");
375 MemoryTestHelper<CGPass>(3);
379 SCOPED_TRACE("Function pass");
380 MemoryTestHelper<FPass>(4);// 4 functions
384 SCOPED_TRACE("Loop pass");
385 MemoryTestHelper<LPass>(2, 1); //2 loops, 1 function
388 SCOPED_TRACE("Basic block pass");
389 MemoryTestHelper<BPass>(7, 4); //9 basic blocks
394 TEST(PassManager, MemoryOnTheFly) {
395 Module *M = makeLLVMModule();
397 SCOPED_TRACE("Running OnTheFlyTest");
398 struct OnTheFlyTest *O = new OnTheFlyTest();
399 legacy::PassManager Passes;
400 Passes.add(new DataLayoutPass());
404 FPass::finishedOK(4);
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");
418 std::vector<Type*>FuncTy_0_args;
419 FunctionType* FuncTy_0 = FunctionType::get(
420 /*Result=*/IntegerType::get(getGlobalContext(), 32),
421 /*Params=*/FuncTy_0_args,
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,
432 // Function Declarations
434 Function* func_test1 = Function::Create(
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);
442 Function* func_test2 = Function::Create(
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);
450 Function* func_test3 = Function::Create(
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);
458 Function* func_test4 = Function::Create(
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);
466 // Global Variable Declarations
469 // Constant Definitions
471 // Global Variable Definitions
473 // Function Definitions
475 // Function: test1 (func_test1)
478 BasicBlock* label_entry = BasicBlock::Create(getGlobalContext(), "entry",func_test1,nullptr);
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);
486 ReturnInst::Create(getGlobalContext(), int32_3, label_entry);
490 // Function: test2 (func_test2)
493 BasicBlock* label_entry_5 = BasicBlock::Create(getGlobalContext(), "entry",func_test2,nullptr);
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);
501 ReturnInst::Create(getGlobalContext(), int32_6, label_entry_5);
505 // Function: test3 (func_test3)
508 BasicBlock* label_entry_8 = BasicBlock::Create(getGlobalContext(), "entry",func_test3,nullptr);
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);
516 ReturnInst::Create(getGlobalContext(), int32_9, label_entry_8);
520 // Function: test4 (func_test4)
522 Function::arg_iterator args = func_test4->arg_begin();
523 Value* int1_f = args++;
524 int1_f->setName("f");
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);
531 // Block entry (label_entry_11)
532 BranchInst::Create(label_bb, label_entry_11);
534 // Block bb (label_bb)
535 BranchInst::Create(label_bb, label_bb1, int1_f, label_bb);
537 // Block bb1 (label_bb1)
538 BranchInst::Create(label_bb1, label_return, int1_f, label_bb1);
540 // Block return (label_return)
541 ReturnInst::Create(getGlobalContext(), label_return);
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)