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 {
104 void getAnalysisUsage(AnalysisUsage &AU) const override {
105 AU.addRequired<ModuleNDM>();
106 AU.setPreservesAll();
109 char ModuleDNM::ID=0;
110 char ModuleDNM::run=0;
113 struct PassTestBase : public P {
116 static bool initialized;
117 static bool finalized;
120 EXPECT_TRUE(initialized);
121 EXPECT_FALSE(finalized);
122 EXPECT_EQ(0, allocated);
128 static void finishedOK(int run) {
130 EXPECT_TRUE(initialized);
131 EXPECT_TRUE(finalized);
132 EXPECT_EQ(run, runc);
134 PassTestBase() : P(ID), allocated(0) {
140 void releaseMemory() override {
142 EXPECT_GT(allocated, 0);
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;
151 template<typename T, typename P>
152 struct PassTest : public PassTestBase<P> {
154 #ifndef _MSC_VER // MSVC complains that Pass is not base class.
155 using llvm::Pass::doInitialization;
156 using llvm::Pass::doFinalization;
158 bool doInitialization(T &t) override {
159 EXPECT_FALSE(PassTestBase<P>::initialized);
160 PassTestBase<P>::initialized = true;
163 bool doFinalization(T &t) override {
164 EXPECT_FALSE(PassTestBase<P>::finalized);
165 PassTestBase<P>::finalized = true;
166 EXPECT_EQ(0, PassTestBase<P>::allocated);
171 struct CGPass : public PassTest<CallGraph, CallGraphSCCPass> {
174 initializeCGPassPass(*PassRegistry::getPassRegistry());
176 bool runOnSCC(CallGraphSCC &SCMM) override {
182 struct FPass : public PassTest<Module, FunctionPass> {
184 bool runOnFunction(Function &F) override {
186 // EXPECT_TRUE(getAnalysisIfAvailable<DataLayout>());
192 struct LPass : public PassTestBase<LoopPass> {
194 static int initcount;
198 initializeLPassPass(*PassRegistry::getPassRegistry());
199 initcount = 0; fincount=0;
200 EXPECT_FALSE(initialized);
202 static void finishedOK(int run, int finalized) {
203 PassTestBase<LoopPass>::finishedOK(run);
204 EXPECT_EQ(run, initcount);
205 EXPECT_EQ(finalized, fincount);
207 using llvm::Pass::doInitialization;
208 using llvm::Pass::doFinalization;
209 bool doInitialization(Loop* L, LPPassManager &LPM) override {
214 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
218 bool doFinalization() override {
224 int LPass::initcount=0;
225 int LPass::fincount=0;
227 struct BPass : public PassTestBase<BasicBlockPass> {
232 static void finishedOK(int run, int N) {
233 PassTestBase<BasicBlockPass>::finishedOK(run);
234 EXPECT_EQ(inited, N);
241 bool doInitialization(Module &M) override {
242 EXPECT_FALSE(initialized);
246 bool doInitialization(Function &F) override {
250 bool runOnBasicBlock(BasicBlock &BB) override {
254 bool doFinalization(Function &F) override {
258 bool doFinalization(Module &M) override {
259 EXPECT_FALSE(finalized);
261 EXPECT_EQ(0, allocated);
268 struct OnTheFlyTest: public ModulePass {
271 OnTheFlyTest() : ModulePass(ID) {
272 initializeFPassPass(*PassRegistry::getPassRegistry());
274 bool runOnModule(Module &M) override {
275 for (Module::iterator I=M.begin(),E=M.end(); I != E; ++I) {
278 SCOPED_TRACE("Running on the fly function pass");
279 getAnalysis<FPass>(F);
284 void getAnalysisUsage(AnalysisUsage &AU) const override {
285 AU.addRequired<FPass>();
288 char OnTheFlyTest::ID=0;
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();
297 mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
299 legacy::PassManager Passes;
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);
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();
320 mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
322 legacy::PassManager Passes;
325 Passes.add(mNDM2);// invalidates mNDM needed by mDNM
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);
337 Module* makeLLVMModule();
340 void MemoryTestHelper(int run) {
341 std::unique_ptr<Module> M(makeLLVMModule());
343 legacy::PassManager Passes;
350 void MemoryTestHelper(int run, int N) {
351 Module *M = makeLLVMModule();
353 legacy::PassManager Passes;
356 T::finishedOK(run, N);
360 TEST(PassManager, Memory) {
361 // SCC#1: test1->test2->test3->test1
363 // SCC#3: indirect call node
365 SCOPED_TRACE("Callgraph pass");
366 MemoryTestHelper<CGPass>(3);
370 SCOPED_TRACE("Function pass");
371 MemoryTestHelper<FPass>(4);// 4 functions
375 SCOPED_TRACE("Loop pass");
376 MemoryTestHelper<LPass>(2, 1); //2 loops, 1 function
379 SCOPED_TRACE("Basic block pass");
380 MemoryTestHelper<BPass>(7, 4); //9 basic blocks
385 TEST(PassManager, MemoryOnTheFly) {
386 Module *M = makeLLVMModule();
388 SCOPED_TRACE("Running OnTheFlyTest");
389 struct OnTheFlyTest *O = new OnTheFlyTest();
390 legacy::PassManager Passes;
394 FPass::finishedOK(4);
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");
408 std::vector<Type*>FuncTy_0_args;
409 FunctionType* FuncTy_0 = FunctionType::get(
410 /*Result=*/IntegerType::get(getGlobalContext(), 32),
411 /*Params=*/FuncTy_0_args,
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,
422 // Function Declarations
424 Function* func_test1 = Function::Create(
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);
432 Function* func_test2 = Function::Create(
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);
440 Function* func_test3 = Function::Create(
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);
448 Function* func_test4 = Function::Create(
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);
456 // Global Variable Declarations
459 // Constant Definitions
461 // Global Variable Definitions
463 // Function Definitions
465 // Function: test1 (func_test1)
468 BasicBlock* label_entry = BasicBlock::Create(getGlobalContext(), "entry",func_test1,nullptr);
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);
476 ReturnInst::Create(getGlobalContext(), int32_3, label_entry);
480 // Function: test2 (func_test2)
483 BasicBlock* label_entry_5 = BasicBlock::Create(getGlobalContext(), "entry",func_test2,nullptr);
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);
491 ReturnInst::Create(getGlobalContext(), int32_6, label_entry_5);
495 // Function: test3 (func_test3)
498 BasicBlock* label_entry_8 = BasicBlock::Create(getGlobalContext(), "entry",func_test3,nullptr);
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);
506 ReturnInst::Create(getGlobalContext(), int32_9, label_entry_8);
510 // Function: test4 (func_test4)
512 Function::arg_iterator args = func_test4->arg_begin();
513 Value *int1_f = &*args++;
514 int1_f->setName("f");
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);
521 // Block entry (label_entry_11)
522 BranchInst::Create(label_bb, label_entry_11);
524 // Block bb (label_bb)
525 BranchInst::Create(label_bb, label_bb1, int1_f, label_bb);
527 // Block bb1 (label_bb1)
528 BranchInst::Create(label_bb1, label_return, int1_f, label_bb1);
530 // Block return (label_return)
531 ReturnInst::Create(getGlobalContext(), label_return);
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)