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 wrapper
12 // works for out-of-tree code that expects to include llvm/PassManager.h and
13 // subclass the core pass classes.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/PassManager.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Analysis/CallGraphSCCPass.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/Analysis/LoopPass.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/CallingConv.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/GlobalVariable.h"
29 #include "llvm/IR/IRPrintingPasses.h"
30 #include "llvm/IR/InlineAsm.h"
31 #include "llvm/IR/Instructions.h"
32 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/Verifier.h"
35 #include "llvm/Pass.h"
36 #include "llvm/Support/MathExtras.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "gtest/gtest.h"
43 void initializeModuleNDMPass(PassRegistry&);
44 void initializeFPassPass(PassRegistry&);
45 void initializeCGPassPass(PassRegistry&);
46 void initializeLPassPass(PassRegistry&);
47 void initializeBPassPass(PassRegistry&);
51 // NM = no modifications
52 struct ModuleNDNM: public ModulePass {
56 ModuleNDNM() : ModulePass(ID) { }
57 bool runOnModule(Module &M) override {
61 void getAnalysisUsage(AnalysisUsage &AU) const override {
65 char ModuleNDNM::ID=0;
66 char ModuleNDNM::run=0;
68 struct ModuleNDM : public ModulePass {
72 ModuleNDM() : ModulePass(ID) {}
73 bool runOnModule(Module &M) override {
79 char ModuleNDM::run=0;
81 struct ModuleNDM2 : public ModulePass {
85 ModuleNDM2() : ModulePass(ID) {}
86 bool runOnModule(Module &M) override {
91 char ModuleNDM2::ID=0;
92 char ModuleNDM2::run=0;
94 struct ModuleDNM : public ModulePass {
98 ModuleDNM() : ModulePass(ID) {
99 initializeModuleNDMPass(*PassRegistry::getPassRegistry());
101 bool runOnModule(Module &M) override {
102 EXPECT_TRUE(getAnalysisIfAvailable<DataLayoutPass>());
106 void getAnalysisUsage(AnalysisUsage &AU) const override {
107 AU.addRequired<ModuleNDM>();
108 AU.setPreservesAll();
111 char ModuleDNM::ID=0;
112 char ModuleDNM::run=0;
115 struct PassTestBase : public P {
118 static bool initialized;
119 static bool finalized;
122 EXPECT_TRUE(initialized);
123 EXPECT_FALSE(finalized);
124 EXPECT_EQ(0, allocated);
130 static void finishedOK(int run) {
132 EXPECT_TRUE(initialized);
133 EXPECT_TRUE(finalized);
134 EXPECT_EQ(run, runc);
136 PassTestBase() : P(ID), allocated(0) {
142 void releaseMemory() override {
144 EXPECT_GT(allocated, 0);
148 template<typename P> char PassTestBase<P>::ID;
149 template<typename P> int PassTestBase<P>::runc;
150 template<typename P> bool PassTestBase<P>::initialized;
151 template<typename P> bool PassTestBase<P>::finalized;
153 template<typename T, typename P>
154 struct PassTest : public PassTestBase<P> {
156 #ifndef _MSC_VER // MSVC complains that Pass is not base class.
157 using llvm::Pass::doInitialization;
158 using llvm::Pass::doFinalization;
160 bool doInitialization(T &t) override {
161 EXPECT_FALSE(PassTestBase<P>::initialized);
162 PassTestBase<P>::initialized = true;
165 bool doFinalization(T &t) override {
166 EXPECT_FALSE(PassTestBase<P>::finalized);
167 PassTestBase<P>::finalized = true;
168 EXPECT_EQ(0, PassTestBase<P>::allocated);
173 struct CGPass : public PassTest<CallGraph, CallGraphSCCPass> {
176 initializeCGPassPass(*PassRegistry::getPassRegistry());
178 bool runOnSCC(CallGraphSCC &SCMM) override {
179 EXPECT_TRUE(getAnalysisIfAvailable<DataLayoutPass>());
185 struct FPass : public PassTest<Module, FunctionPass> {
187 bool runOnFunction(Function &F) override {
189 // EXPECT_TRUE(getAnalysisIfAvailable<DataLayout>());
195 struct LPass : public PassTestBase<LoopPass> {
197 static int initcount;
201 initializeLPassPass(*PassRegistry::getPassRegistry());
202 initcount = 0; fincount=0;
203 EXPECT_FALSE(initialized);
205 static void finishedOK(int run, int finalized) {
206 PassTestBase<LoopPass>::finishedOK(run);
207 EXPECT_EQ(run, initcount);
208 EXPECT_EQ(finalized, fincount);
210 using llvm::Pass::doInitialization;
211 using llvm::Pass::doFinalization;
212 bool doInitialization(Loop* L, LPPassManager &LPM) override {
217 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
218 EXPECT_TRUE(getAnalysisIfAvailable<DataLayoutPass>());
222 bool doFinalization() override {
228 int LPass::initcount=0;
229 int LPass::fincount=0;
231 struct BPass : public PassTestBase<BasicBlockPass> {
236 static void finishedOK(int run, int N) {
237 PassTestBase<BasicBlockPass>::finishedOK(run);
238 EXPECT_EQ(inited, N);
245 bool doInitialization(Module &M) override {
246 EXPECT_FALSE(initialized);
250 bool doInitialization(Function &F) override {
254 bool runOnBasicBlock(BasicBlock &BB) override {
255 EXPECT_TRUE(getAnalysisIfAvailable<DataLayoutPass>());
259 bool doFinalization(Function &F) override {
263 bool doFinalization(Module &M) override {
264 EXPECT_FALSE(finalized);
266 EXPECT_EQ(0, allocated);
273 struct OnTheFlyTest: public ModulePass {
276 OnTheFlyTest() : ModulePass(ID) {
277 initializeFPassPass(*PassRegistry::getPassRegistry());
279 bool runOnModule(Module &M) override {
280 EXPECT_TRUE(getAnalysisIfAvailable<DataLayoutPass>());
281 for (Module::iterator I=M.begin(),E=M.end(); I != E; ++I) {
284 SCOPED_TRACE("Running on the fly function pass");
285 getAnalysis<FPass>(F);
290 void getAnalysisUsage(AnalysisUsage &AU) const override {
291 AU.addRequired<FPass>();
294 char OnTheFlyTest::ID=0;
296 TEST(PassManager, RunOnce) {
297 Module M("test-once", getGlobalContext());
298 struct ModuleNDNM *mNDNM = new ModuleNDNM();
299 struct ModuleDNM *mDNM = new ModuleDNM();
300 struct ModuleNDM *mNDM = new ModuleNDM();
301 struct ModuleNDM2 *mNDM2 = new ModuleNDM2();
303 mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
306 Passes.add(new DataLayoutPass());
313 // each pass must be run exactly once, since nothing invalidates them
314 EXPECT_EQ(1, mNDM->run);
315 EXPECT_EQ(1, mNDNM->run);
316 EXPECT_EQ(1, mDNM->run);
317 EXPECT_EQ(1, mNDM2->run);
320 TEST(PassManager, ReRun) {
321 Module M("test-rerun", getGlobalContext());
322 struct ModuleNDNM *mNDNM = new ModuleNDNM();
323 struct ModuleDNM *mDNM = new ModuleDNM();
324 struct ModuleNDM *mNDM = new ModuleNDM();
325 struct ModuleNDM2 *mNDM2 = new ModuleNDM2();
327 mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
330 Passes.add(new DataLayoutPass());
333 Passes.add(mNDM2);// invalidates mNDM needed by mDNM
337 // Some passes must be rerun because a pass that modified the
338 // module/function was run in between
339 EXPECT_EQ(2, mNDM->run);
340 EXPECT_EQ(1, mNDNM->run);
341 EXPECT_EQ(1, mNDM2->run);
342 EXPECT_EQ(1, mDNM->run);
345 Module* makeLLVMModule();
348 void MemoryTestHelper(int run) {
349 std::unique_ptr<Module> M(makeLLVMModule());
352 Passes.add(new DataLayoutPass());
359 void MemoryTestHelper(int run, int N) {
360 Module *M = makeLLVMModule();
363 Passes.add(new DataLayoutPass());
366 T::finishedOK(run, N);
370 TEST(PassManager, Memory) {
371 // SCC#1: test1->test2->test3->test1
373 // SCC#3: indirect call node
375 SCOPED_TRACE("Callgraph pass");
376 MemoryTestHelper<CGPass>(3);
380 SCOPED_TRACE("Function pass");
381 MemoryTestHelper<FPass>(4);// 4 functions
385 SCOPED_TRACE("Loop pass");
386 MemoryTestHelper<LPass>(2, 1); //2 loops, 1 function
389 SCOPED_TRACE("Basic block pass");
390 MemoryTestHelper<BPass>(7, 4); //9 basic blocks
395 TEST(PassManager, MemoryOnTheFly) {
396 Module *M = makeLLVMModule();
398 SCOPED_TRACE("Running OnTheFlyTest");
399 struct OnTheFlyTest *O = new OnTheFlyTest();
401 Passes.add(new DataLayoutPass());
405 FPass::finishedOK(4);
410 Module* makeLLVMModule() {
411 // Module Construction
412 Module* mod = new Module("test-mem", getGlobalContext());
413 mod->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
414 "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
415 "a:0:64-s:64:64-f80:128:128");
416 mod->setTargetTriple("x86_64-unknown-linux-gnu");
419 std::vector<Type*>FuncTy_0_args;
420 FunctionType* FuncTy_0 = FunctionType::get(
421 /*Result=*/IntegerType::get(getGlobalContext(), 32),
422 /*Params=*/FuncTy_0_args,
425 std::vector<Type*>FuncTy_2_args;
426 FuncTy_2_args.push_back(IntegerType::get(getGlobalContext(), 1));
427 FunctionType* FuncTy_2 = FunctionType::get(
428 /*Result=*/Type::getVoidTy(getGlobalContext()),
429 /*Params=*/FuncTy_2_args,
433 // Function Declarations
435 Function* func_test1 = Function::Create(
437 /*Linkage=*/GlobalValue::ExternalLinkage,
438 /*Name=*/"test1", mod);
439 func_test1->setCallingConv(CallingConv::C);
440 AttributeSet func_test1_PAL;
441 func_test1->setAttributes(func_test1_PAL);
443 Function* func_test2 = Function::Create(
445 /*Linkage=*/GlobalValue::ExternalLinkage,
446 /*Name=*/"test2", mod);
447 func_test2->setCallingConv(CallingConv::C);
448 AttributeSet func_test2_PAL;
449 func_test2->setAttributes(func_test2_PAL);
451 Function* func_test3 = Function::Create(
453 /*Linkage=*/GlobalValue::ExternalLinkage,
454 /*Name=*/"test3", mod);
455 func_test3->setCallingConv(CallingConv::C);
456 AttributeSet func_test3_PAL;
457 func_test3->setAttributes(func_test3_PAL);
459 Function* func_test4 = Function::Create(
461 /*Linkage=*/GlobalValue::ExternalLinkage,
462 /*Name=*/"test4", mod);
463 func_test4->setCallingConv(CallingConv::C);
464 AttributeSet func_test4_PAL;
465 func_test4->setAttributes(func_test4_PAL);
467 // Global Variable Declarations
470 // Constant Definitions
472 // Global Variable Definitions
474 // Function Definitions
476 // Function: test1 (func_test1)
479 BasicBlock* label_entry = BasicBlock::Create(getGlobalContext(), "entry",func_test1,nullptr);
481 // Block entry (label_entry)
482 CallInst* int32_3 = CallInst::Create(func_test2, "", label_entry);
483 int32_3->setCallingConv(CallingConv::C);
484 int32_3->setTailCall(false);AttributeSet int32_3_PAL;
485 int32_3->setAttributes(int32_3_PAL);
487 ReturnInst::Create(getGlobalContext(), int32_3, label_entry);
491 // Function: test2 (func_test2)
494 BasicBlock* label_entry_5 = BasicBlock::Create(getGlobalContext(), "entry",func_test2,nullptr);
496 // Block entry (label_entry_5)
497 CallInst* int32_6 = CallInst::Create(func_test3, "", label_entry_5);
498 int32_6->setCallingConv(CallingConv::C);
499 int32_6->setTailCall(false);AttributeSet int32_6_PAL;
500 int32_6->setAttributes(int32_6_PAL);
502 ReturnInst::Create(getGlobalContext(), int32_6, label_entry_5);
506 // Function: test3 (func_test3)
509 BasicBlock* label_entry_8 = BasicBlock::Create(getGlobalContext(), "entry",func_test3,nullptr);
511 // Block entry (label_entry_8)
512 CallInst* int32_9 = CallInst::Create(func_test1, "", label_entry_8);
513 int32_9->setCallingConv(CallingConv::C);
514 int32_9->setTailCall(false);AttributeSet int32_9_PAL;
515 int32_9->setAttributes(int32_9_PAL);
517 ReturnInst::Create(getGlobalContext(), int32_9, label_entry_8);
521 // Function: test4 (func_test4)
523 Function::arg_iterator args = func_test4->arg_begin();
524 Value* int1_f = args++;
525 int1_f->setName("f");
527 BasicBlock* label_entry_11 = BasicBlock::Create(getGlobalContext(), "entry",func_test4,nullptr);
528 BasicBlock* label_bb = BasicBlock::Create(getGlobalContext(), "bb",func_test4,nullptr);
529 BasicBlock* label_bb1 = BasicBlock::Create(getGlobalContext(), "bb1",func_test4,nullptr);
530 BasicBlock* label_return = BasicBlock::Create(getGlobalContext(), "return",func_test4,nullptr);
532 // Block entry (label_entry_11)
533 BranchInst::Create(label_bb, label_entry_11);
535 // Block bb (label_bb)
536 BranchInst::Create(label_bb, label_bb1, int1_f, label_bb);
538 // Block bb1 (label_bb1)
539 BranchInst::Create(label_bb1, label_return, int1_f, label_bb1);
541 // Block return (label_return)
542 ReturnInst::Create(getGlobalContext(), label_return);
551 INITIALIZE_PASS(ModuleNDM, "mndm", "mndm", false, false)
552 INITIALIZE_PASS_BEGIN(CGPass, "cgp","cgp", false, false)
553 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
554 INITIALIZE_PASS_END(CGPass, "cgp","cgp", false, false)
555 INITIALIZE_PASS(FPass, "fp","fp", false, false)
556 INITIALIZE_PASS_BEGIN(LPass, "lp","lp", false, false)
557 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
558 INITIALIZE_PASS_END(LPass, "lp","lp", false, false)
559 INITIALIZE_PASS(BPass, "bp","bp", false, false)