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/Module.h"
11 #include "llvm/LLVMContext.h"
12 #include "llvm/PassManager.h"
13 #include "llvm/Analysis/LoopInfo.h"
14 #include "llvm/Pass.h"
15 #include "llvm/Analysis/LoopPass.h"
16 #include "llvm/CallGraphSCCPass.h"
17 #include "llvm/Target/TargetData.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Constants.h"
21 #include "llvm/GlobalVariable.h"
22 #include "llvm/Function.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/BasicBlock.h"
25 #include "llvm/Instructions.h"
26 #include "llvm/InlineAsm.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/PassManager.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/Analysis/Verifier.h"
32 #include "llvm/Assembly/PrintModulePass.h"
33 #include "gtest/gtest.h"
42 // NM = no modifications
43 struct ModuleNDNM: public ModulePass {
47 ModuleNDNM() : ModulePass(&ID) {}
48 virtual bool runOnModule(Module &M) {
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56 char ModuleNDNM::ID=0;
57 char ModuleNDNM::run=0;
59 struct ModuleNDM : public ModulePass {
63 ModuleNDM() : ModulePass(&ID) {}
64 virtual bool runOnModule(Module &M) {
70 char ModuleNDM::run=0;
71 RegisterPass<ModuleNDM> X("mndm","mndm",false,false);
73 struct ModuleNDM2 : public ModulePass {
77 ModuleNDM2() : ModulePass(&ID) {}
78 virtual bool runOnModule(Module &M) {
83 char ModuleNDM2::ID=0;
84 char ModuleNDM2::run=0;
86 struct ModuleDNM : public ModulePass {
90 ModuleDNM() : ModulePass(&ID) {}
91 virtual bool runOnModule(Module &M) {
92 EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
96 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
97 AU.addRequired<ModuleNDM>();
101 char ModuleDNM::ID=0;
102 char ModuleDNM::run=0;
105 struct PassTestBase : public P {
108 static bool initialized;
109 static bool finalized;
112 EXPECT_EQ(true, initialized);
113 EXPECT_EQ(false, finalized);
114 EXPECT_EQ(0, allocated);
120 static void finishedOK(int run) {
122 EXPECT_EQ(true, initialized);
123 EXPECT_EQ(true, finalized);
124 EXPECT_EQ(run, runc);
126 PassTestBase() : P(&ID), allocated(0) {
132 virtual void releaseMemory() {
134 EXPECT_GT(allocated, 0);
138 template<typename P> char PassTestBase<P>::ID;
139 template<typename P> int PassTestBase<P>::runc;
140 template<typename P> bool PassTestBase<P>::initialized;
141 template<typename P> bool PassTestBase<P>::finalized;
143 template<typename T, typename P>
144 struct PassTest : public PassTestBase<P> {
146 virtual bool doInitialization(T &t) {
147 EXPECT_EQ(false, PassTestBase<P>::initialized);
148 PassTestBase<P>::initialized = true;
151 virtual bool doFinalization(T &t) {
152 EXPECT_EQ(false, PassTestBase<P>::finalized);
153 PassTestBase<P>::finalized = true;
154 EXPECT_EQ(0, PassTestBase<P>::allocated);
159 struct CGPass : public PassTest<CallGraph, CallGraphSCCPass> {
161 virtual bool runOnSCC(std::vector<CallGraphNode*> &SCMM) {
162 EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
167 RegisterPass<CGPass> X1("cgp","cgp");
169 struct FPass : public PassTest<Module, FunctionPass> {
171 virtual bool runOnFunction(Function &F) {
173 // EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
178 RegisterPass<FPass> X2("fp","fp");
180 struct LPass : public PassTestBase<LoopPass> {
182 static int initcount;
186 initcount = 0; fincount=0;
187 EXPECT_EQ(false, initialized);
189 static void finishedOK(int run, int finalized) {
190 PassTestBase<LoopPass>::finishedOK(run);
191 EXPECT_EQ(run, initcount);
192 EXPECT_EQ(finalized, fincount);
194 virtual bool doInitialization(Loop* L, LPPassManager &LPM) {
199 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
200 EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
204 virtual bool doFinalization() {
210 int LPass::initcount=0;
211 int LPass::fincount=0;
212 RegisterPass<LPass> X3("lp","lp");
214 struct BPass : public PassTestBase<BasicBlockPass> {
219 static void finishedOK(int run, int N) {
220 PassTestBase<BasicBlockPass>::finishedOK(run);
221 EXPECT_EQ(inited, N);
228 virtual bool doInitialization(Module &M) {
229 EXPECT_EQ(false, initialized);
233 virtual bool doInitialization(Function &F) {
237 virtual bool runOnBasicBlock(BasicBlock &BB) {
238 EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
242 virtual bool doFinalization(Function &F) {
246 virtual bool doFinalization(Module &M) {
247 EXPECT_EQ(false, finalized);
249 EXPECT_EQ(0, allocated);
255 RegisterPass<BPass> X4("bp","bp");
257 struct OnTheFlyTest: public ModulePass {
260 OnTheFlyTest() : ModulePass(&ID) {}
261 virtual bool runOnModule(Module &M) {
262 EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
263 for (Module::iterator I=M.begin(),E=M.end(); I != E; ++I) {
266 SCOPED_TRACE("Running on the fly function pass");
267 getAnalysis<FPass>(F);
272 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
273 AU.addRequired<FPass>();
276 char OnTheFlyTest::ID=0;
278 TEST(PassManager, RunOnce) {
279 Module M("test-once", getGlobalContext());
280 struct ModuleNDNM *mNDNM = new ModuleNDNM();
281 struct ModuleDNM *mDNM = new ModuleDNM();
282 struct ModuleNDM *mNDM = new ModuleNDM();
283 struct ModuleNDM2 *mNDM2 = new ModuleNDM2();
285 mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
288 Passes.add(new TargetData(&M));
295 // each pass must be run exactly once, since nothing invalidates them
296 EXPECT_EQ(1, mNDM->run);
297 EXPECT_EQ(1, mNDNM->run);
298 EXPECT_EQ(1, mDNM->run);
299 EXPECT_EQ(1, mNDM2->run);
302 TEST(PassManager, ReRun) {
303 Module M("test-rerun", getGlobalContext());
304 struct ModuleNDNM *mNDNM = new ModuleNDNM();
305 struct ModuleDNM *mDNM = new ModuleDNM();
306 struct ModuleNDM *mNDM = new ModuleNDM();
307 struct ModuleNDM2 *mNDM2 = new ModuleNDM2();
309 mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
312 Passes.add(new TargetData(&M));
315 Passes.add(mNDM2);// invalidates mNDM needed by mDNM
319 // Some passes must be rerun because a pass that modified the
320 // module/function was run inbetween
321 EXPECT_EQ(2, mNDM->run);
322 EXPECT_EQ(1, mNDNM->run);
323 EXPECT_EQ(1, mNDM2->run);
324 EXPECT_EQ(1, mDNM->run);
327 Module* makeLLVMModule();
330 void MemoryTestHelper(int run) {
331 Module *M = makeLLVMModule();
334 Passes.add(new TargetData(M));
341 void MemoryTestHelper(int run, int N) {
342 Module *M = makeLLVMModule();
345 Passes.add(new TargetData(M));
348 T::finishedOK(run, N);
352 TEST(PassManager, Memory) {
353 // SCC#1: test1->test2->test3->test1
355 // SCC#3: indirect call node
357 SCOPED_TRACE("Callgraph pass");
358 MemoryTestHelper<CGPass>(3);
362 SCOPED_TRACE("Function pass");
363 MemoryTestHelper<FPass>(4);// 4 functions
367 SCOPED_TRACE("Loop pass");
368 MemoryTestHelper<LPass>(2, 1); //2 loops, 1 function
371 SCOPED_TRACE("Basic block pass");
372 MemoryTestHelper<BPass>(7, 4); //9 basic blocks
377 TEST(PassManager, MemoryOnTheFly) {
378 Module *M = makeLLVMModule();
380 SCOPED_TRACE("Running OnTheFlyTest");
381 struct OnTheFlyTest *O = new OnTheFlyTest();
383 Passes.add(new TargetData(M));
387 FPass::finishedOK(4);
392 Module* makeLLVMModule() {
393 // Module Construction
394 Module* mod = new Module("test-mem", getGlobalContext());
395 mod->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
396 "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
397 "a0:0:64-s0:64:64-f80:128:128");
398 mod->setTargetTriple("x86_64-unknown-linux-gnu");
401 std::vector<const Type*>FuncTy_0_args;
402 FunctionType* FuncTy_0 = FunctionType::get(
403 /*Result=*/IntegerType::get(getGlobalContext(), 32),
404 /*Params=*/FuncTy_0_args,
407 std::vector<const Type*>FuncTy_2_args;
408 FuncTy_2_args.push_back(IntegerType::get(getGlobalContext(), 1));
409 FunctionType* FuncTy_2 = FunctionType::get(
410 /*Result=*/Type::getVoidTy(getGlobalContext()),
411 /*Params=*/FuncTy_2_args,
415 // Function Declarations
417 Function* func_test1 = Function::Create(
419 /*Linkage=*/GlobalValue::ExternalLinkage,
420 /*Name=*/"test1", mod);
421 func_test1->setCallingConv(CallingConv::C);
422 AttrListPtr func_test1_PAL;
423 func_test1->setAttributes(func_test1_PAL);
425 Function* func_test2 = Function::Create(
427 /*Linkage=*/GlobalValue::ExternalLinkage,
428 /*Name=*/"test2", mod);
429 func_test2->setCallingConv(CallingConv::C);
430 AttrListPtr func_test2_PAL;
431 func_test2->setAttributes(func_test2_PAL);
433 Function* func_test3 = Function::Create(
435 /*Linkage=*/GlobalValue::ExternalLinkage,
436 /*Name=*/"test3", mod);
437 func_test3->setCallingConv(CallingConv::C);
438 AttrListPtr func_test3_PAL;
439 func_test3->setAttributes(func_test3_PAL);
441 Function* func_test4 = Function::Create(
443 /*Linkage=*/GlobalValue::ExternalLinkage,
444 /*Name=*/"test4", mod);
445 func_test4->setCallingConv(CallingConv::C);
446 AttrListPtr func_test4_PAL;
447 func_test4->setAttributes(func_test4_PAL);
449 // Global Variable Declarations
452 // Constant Definitions
454 // Global Variable Definitions
456 // Function Definitions
458 // Function: test1 (func_test1)
461 BasicBlock* label_entry = BasicBlock::Create(getGlobalContext(), "entry",func_test1,0);
463 // Block entry (label_entry)
464 CallInst* int32_3 = CallInst::Create(func_test2, "", label_entry);
465 int32_3->setCallingConv(CallingConv::C);
466 int32_3->setTailCall(false);AttrListPtr int32_3_PAL;
467 int32_3->setAttributes(int32_3_PAL);
469 ReturnInst::Create(getGlobalContext(), int32_3, label_entry);
473 // Function: test2 (func_test2)
476 BasicBlock* label_entry_5 = BasicBlock::Create(getGlobalContext(), "entry",func_test2,0);
478 // Block entry (label_entry_5)
479 CallInst* int32_6 = CallInst::Create(func_test3, "", label_entry_5);
480 int32_6->setCallingConv(CallingConv::C);
481 int32_6->setTailCall(false);AttrListPtr int32_6_PAL;
482 int32_6->setAttributes(int32_6_PAL);
484 ReturnInst::Create(getGlobalContext(), int32_6, label_entry_5);
488 // Function: test3 (func_test3)
491 BasicBlock* label_entry_8 = BasicBlock::Create(getGlobalContext(), "entry",func_test3,0);
493 // Block entry (label_entry_8)
494 CallInst* int32_9 = CallInst::Create(func_test1, "", label_entry_8);
495 int32_9->setCallingConv(CallingConv::C);
496 int32_9->setTailCall(false);AttrListPtr int32_9_PAL;
497 int32_9->setAttributes(int32_9_PAL);
499 ReturnInst::Create(getGlobalContext(), int32_9, label_entry_8);
503 // Function: test4 (func_test4)
505 Function::arg_iterator args = func_test4->arg_begin();
506 Value* int1_f = args++;
507 int1_f->setName("f");
509 BasicBlock* label_entry_11 = BasicBlock::Create(getGlobalContext(), "entry",func_test4,0);
510 BasicBlock* label_bb = BasicBlock::Create(getGlobalContext(), "bb",func_test4,0);
511 BasicBlock* label_bb1 = BasicBlock::Create(getGlobalContext(), "bb1",func_test4,0);
512 BasicBlock* label_return = BasicBlock::Create(getGlobalContext(), "return",func_test4,0);
514 // Block entry (label_entry_11)
515 BranchInst::Create(label_bb, label_entry_11);
517 // Block bb (label_bb)
518 BranchInst::Create(label_bb, label_bb1, int1_f, label_bb);
520 // Block bb1 (label_bb1)
521 BranchInst::Create(label_bb1, label_return, int1_f, label_bb1);
523 // Block return (label_return)
524 ReturnInst::Create(getGlobalContext(), label_return);