1 //===- llvm/unittest/VMCore/PassManager.cpp - Constants unit 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 #include "llvm/PassManager.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/Analysis/LoopInfo.h"
13 #include "llvm/Analysis/LoopPass.h"
14 #include "llvm/Analysis/Verifier.h"
15 #include "llvm/Assembly/PrintModulePass.h"
16 #include "llvm/BasicBlock.h"
17 #include "llvm/CallGraphSCCPass.h"
18 #include "llvm/CallingConv.h"
19 #include "llvm/Constants.h"
20 #include "llvm/DataLayout.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Function.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/InlineAsm.h"
25 #include "llvm/Instructions.h"
26 #include "llvm/LLVMContext.h"
27 #include "llvm/Module.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Support/MathExtras.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "gtest/gtest.h"
37 void initializeModuleNDMPass(PassRegistry&);
38 void initializeFPassPass(PassRegistry&);
39 void initializeCGPassPass(PassRegistry&);
40 void initializeLPassPass(PassRegistry&);
41 void initializeBPassPass(PassRegistry&);
45 // NM = no modifications
46 struct ModuleNDNM: public ModulePass {
50 ModuleNDNM() : ModulePass(ID) { }
51 virtual bool runOnModule(Module &M) {
55 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59 char ModuleNDNM::ID=0;
60 char ModuleNDNM::run=0;
62 struct ModuleNDM : public ModulePass {
66 ModuleNDM() : ModulePass(ID) {}
67 virtual bool runOnModule(Module &M) {
73 char ModuleNDM::run=0;
75 struct ModuleNDM2 : public ModulePass {
79 ModuleNDM2() : ModulePass(ID) {}
80 virtual bool runOnModule(Module &M) {
85 char ModuleNDM2::ID=0;
86 char ModuleNDM2::run=0;
88 struct ModuleDNM : public ModulePass {
92 ModuleDNM() : ModulePass(ID) {
93 initializeModuleNDMPass(*PassRegistry::getPassRegistry());
95 virtual bool runOnModule(Module &M) {
96 EXPECT_TRUE(getAnalysisIfAvailable<DataLayout>());
100 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
101 AU.addRequired<ModuleNDM>();
102 AU.setPreservesAll();
105 char ModuleDNM::ID=0;
106 char ModuleDNM::run=0;
109 struct PassTestBase : public P {
112 static bool initialized;
113 static bool finalized;
116 EXPECT_TRUE(initialized);
117 EXPECT_FALSE(finalized);
118 EXPECT_EQ(0, allocated);
124 static void finishedOK(int run) {
126 EXPECT_TRUE(initialized);
127 EXPECT_TRUE(finalized);
128 EXPECT_EQ(run, runc);
130 PassTestBase() : P(ID), allocated(0) {
136 virtual void releaseMemory() {
138 EXPECT_GT(allocated, 0);
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;
147 template<typename T, typename P>
148 struct PassTest : public PassTestBase<P> {
150 #ifndef _MSC_VER // MSVC complains that Pass is not base class.
151 using llvm::Pass::doInitialization;
152 using llvm::Pass::doFinalization;
154 virtual bool doInitialization(T &t) {
155 EXPECT_FALSE(PassTestBase<P>::initialized);
156 PassTestBase<P>::initialized = true;
159 virtual bool doFinalization(T &t) {
160 EXPECT_FALSE(PassTestBase<P>::finalized);
161 PassTestBase<P>::finalized = true;
162 EXPECT_EQ(0, PassTestBase<P>::allocated);
167 struct CGPass : public PassTest<CallGraph, CallGraphSCCPass> {
170 initializeCGPassPass(*PassRegistry::getPassRegistry());
172 virtual bool runOnSCC(CallGraphSCC &SCMM) {
173 EXPECT_TRUE(getAnalysisIfAvailable<DataLayout>());
179 struct FPass : public PassTest<Module, FunctionPass> {
181 virtual bool runOnFunction(Function &F) {
183 // EXPECT_TRUE(getAnalysisIfAvailable<DataLayout>());
189 struct LPass : public PassTestBase<LoopPass> {
191 static int initcount;
195 initializeLPassPass(*PassRegistry::getPassRegistry());
196 initcount = 0; fincount=0;
197 EXPECT_FALSE(initialized);
199 static void finishedOK(int run, int finalized) {
200 PassTestBase<LoopPass>::finishedOK(run);
201 EXPECT_EQ(run, initcount);
202 EXPECT_EQ(finalized, fincount);
204 using llvm::Pass::doInitialization;
205 using llvm::Pass::doFinalization;
206 virtual bool doInitialization(Loop* L, LPPassManager &LPM) {
211 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
212 EXPECT_TRUE(getAnalysisIfAvailable<DataLayout>());
216 virtual bool doFinalization() {
222 int LPass::initcount=0;
223 int LPass::fincount=0;
225 struct BPass : public PassTestBase<BasicBlockPass> {
230 static void finishedOK(int run, int N) {
231 PassTestBase<BasicBlockPass>::finishedOK(run);
232 EXPECT_EQ(inited, N);
239 virtual bool doInitialization(Module &M) {
240 EXPECT_FALSE(initialized);
244 virtual bool doInitialization(Function &F) {
248 virtual bool runOnBasicBlock(BasicBlock &BB) {
249 EXPECT_TRUE(getAnalysisIfAvailable<DataLayout>());
253 virtual bool doFinalization(Function &F) {
257 virtual bool doFinalization(Module &M) {
258 EXPECT_FALSE(finalized);
260 EXPECT_EQ(0, allocated);
267 struct OnTheFlyTest: public ModulePass {
270 OnTheFlyTest() : ModulePass(ID) {
271 initializeFPassPass(*PassRegistry::getPassRegistry());
273 virtual bool runOnModule(Module &M) {
274 EXPECT_TRUE(getAnalysisIfAvailable<DataLayout>());
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 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
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;
300 Passes.add(new DataLayout(&M));
307 // each pass must be run exactly once, since nothing invalidates them
308 EXPECT_EQ(1, mNDM->run);
309 EXPECT_EQ(1, mNDNM->run);
310 EXPECT_EQ(1, mDNM->run);
311 EXPECT_EQ(1, mNDM2->run);
314 TEST(PassManager, ReRun) {
315 Module M("test-rerun", getGlobalContext());
316 struct ModuleNDNM *mNDNM = new ModuleNDNM();
317 struct ModuleDNM *mDNM = new ModuleDNM();
318 struct ModuleNDM *mNDM = new ModuleNDM();
319 struct ModuleNDM2 *mNDM2 = new ModuleNDM2();
321 mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
324 Passes.add(new DataLayout(&M));
327 Passes.add(mNDM2);// invalidates mNDM needed by mDNM
331 // Some passes must be rerun because a pass that modified the
332 // module/function was run in between
333 EXPECT_EQ(2, mNDM->run);
334 EXPECT_EQ(1, mNDNM->run);
335 EXPECT_EQ(1, mNDM2->run);
336 EXPECT_EQ(1, mDNM->run);
339 Module* makeLLVMModule();
342 void MemoryTestHelper(int run) {
343 OwningPtr<Module> M(makeLLVMModule());
346 Passes.add(new DataLayout(M.get()));
353 void MemoryTestHelper(int run, int N) {
354 Module *M = makeLLVMModule();
357 Passes.add(new DataLayout(M));
360 T::finishedOK(run, N);
364 TEST(PassManager, Memory) {
365 // SCC#1: test1->test2->test3->test1
367 // SCC#3: indirect call node
369 SCOPED_TRACE("Callgraph pass");
370 MemoryTestHelper<CGPass>(3);
374 SCOPED_TRACE("Function pass");
375 MemoryTestHelper<FPass>(4);// 4 functions
379 SCOPED_TRACE("Loop pass");
380 MemoryTestHelper<LPass>(2, 1); //2 loops, 1 function
383 SCOPED_TRACE("Basic block pass");
384 MemoryTestHelper<BPass>(7, 4); //9 basic blocks
389 TEST(PassManager, MemoryOnTheFly) {
390 Module *M = makeLLVMModule();
392 SCOPED_TRACE("Running OnTheFlyTest");
393 struct OnTheFlyTest *O = new OnTheFlyTest();
395 Passes.add(new DataLayout(M));
399 FPass::finishedOK(4);
404 Module* makeLLVMModule() {
405 // Module Construction
406 Module* mod = new Module("test-mem", getGlobalContext());
407 mod->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
408 "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
409 "a0:0:64-s0:64:64-f80:128:128");
410 mod->setTargetTriple("x86_64-unknown-linux-gnu");
413 std::vector<Type*>FuncTy_0_args;
414 FunctionType* FuncTy_0 = FunctionType::get(
415 /*Result=*/IntegerType::get(getGlobalContext(), 32),
416 /*Params=*/FuncTy_0_args,
419 std::vector<Type*>FuncTy_2_args;
420 FuncTy_2_args.push_back(IntegerType::get(getGlobalContext(), 1));
421 FunctionType* FuncTy_2 = FunctionType::get(
422 /*Result=*/Type::getVoidTy(getGlobalContext()),
423 /*Params=*/FuncTy_2_args,
427 // Function Declarations
429 Function* func_test1 = Function::Create(
431 /*Linkage=*/GlobalValue::ExternalLinkage,
432 /*Name=*/"test1", mod);
433 func_test1->setCallingConv(CallingConv::C);
434 AttrListPtr func_test1_PAL;
435 func_test1->setAttributes(func_test1_PAL);
437 Function* func_test2 = Function::Create(
439 /*Linkage=*/GlobalValue::ExternalLinkage,
440 /*Name=*/"test2", mod);
441 func_test2->setCallingConv(CallingConv::C);
442 AttrListPtr func_test2_PAL;
443 func_test2->setAttributes(func_test2_PAL);
445 Function* func_test3 = Function::Create(
447 /*Linkage=*/GlobalValue::ExternalLinkage,
448 /*Name=*/"test3", mod);
449 func_test3->setCallingConv(CallingConv::C);
450 AttrListPtr func_test3_PAL;
451 func_test3->setAttributes(func_test3_PAL);
453 Function* func_test4 = Function::Create(
455 /*Linkage=*/GlobalValue::ExternalLinkage,
456 /*Name=*/"test4", mod);
457 func_test4->setCallingConv(CallingConv::C);
458 AttrListPtr func_test4_PAL;
459 func_test4->setAttributes(func_test4_PAL);
461 // Global Variable Declarations
464 // Constant Definitions
466 // Global Variable Definitions
468 // Function Definitions
470 // Function: test1 (func_test1)
473 BasicBlock* label_entry = BasicBlock::Create(getGlobalContext(), "entry",func_test1,0);
475 // Block entry (label_entry)
476 CallInst* int32_3 = CallInst::Create(func_test2, "", label_entry);
477 int32_3->setCallingConv(CallingConv::C);
478 int32_3->setTailCall(false);AttrListPtr int32_3_PAL;
479 int32_3->setAttributes(int32_3_PAL);
481 ReturnInst::Create(getGlobalContext(), int32_3, label_entry);
485 // Function: test2 (func_test2)
488 BasicBlock* label_entry_5 = BasicBlock::Create(getGlobalContext(), "entry",func_test2,0);
490 // Block entry (label_entry_5)
491 CallInst* int32_6 = CallInst::Create(func_test3, "", label_entry_5);
492 int32_6->setCallingConv(CallingConv::C);
493 int32_6->setTailCall(false);AttrListPtr int32_6_PAL;
494 int32_6->setAttributes(int32_6_PAL);
496 ReturnInst::Create(getGlobalContext(), int32_6, label_entry_5);
500 // Function: test3 (func_test3)
503 BasicBlock* label_entry_8 = BasicBlock::Create(getGlobalContext(), "entry",func_test3,0);
505 // Block entry (label_entry_8)
506 CallInst* int32_9 = CallInst::Create(func_test1, "", label_entry_8);
507 int32_9->setCallingConv(CallingConv::C);
508 int32_9->setTailCall(false);AttrListPtr int32_9_PAL;
509 int32_9->setAttributes(int32_9_PAL);
511 ReturnInst::Create(getGlobalContext(), int32_9, label_entry_8);
515 // Function: test4 (func_test4)
517 Function::arg_iterator args = func_test4->arg_begin();
518 Value* int1_f = args++;
519 int1_f->setName("f");
521 BasicBlock* label_entry_11 = BasicBlock::Create(getGlobalContext(), "entry",func_test4,0);
522 BasicBlock* label_bb = BasicBlock::Create(getGlobalContext(), "bb",func_test4,0);
523 BasicBlock* label_bb1 = BasicBlock::Create(getGlobalContext(), "bb1",func_test4,0);
524 BasicBlock* label_return = BasicBlock::Create(getGlobalContext(), "return",func_test4,0);
526 // Block entry (label_entry_11)
527 BranchInst::Create(label_bb, label_entry_11);
529 // Block bb (label_bb)
530 BranchInst::Create(label_bb, label_bb1, int1_f, label_bb);
532 // Block bb1 (label_bb1)
533 BranchInst::Create(label_bb1, label_return, int1_f, label_bb1);
535 // Block return (label_return)
536 ReturnInst::Create(getGlobalContext(), label_return);
545 INITIALIZE_PASS(ModuleNDM, "mndm", "mndm", false, false)
546 INITIALIZE_PASS_BEGIN(CGPass, "cgp","cgp", false, false)
547 INITIALIZE_AG_DEPENDENCY(CallGraph)
548 INITIALIZE_PASS_END(CGPass, "cgp","cgp", false, false)
549 INITIALIZE_PASS(FPass, "fp","fp", false, false)
550 INITIALIZE_PASS_BEGIN(LPass, "lp","lp", false, false)
551 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
552 INITIALIZE_PASS_END(LPass, "lp","lp", false, false)
553 INITIALIZE_PASS(BPass, "bp","bp", false, false)