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"
38 // NM = no modifications
39 struct ModuleNDNM: public ModulePass {
43 ModuleNDNM() : ModulePass(&ID) {}
44 virtual bool runOnModule(Module &M) {
48 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52 char ModuleNDNM::ID=0;
53 char ModuleNDNM::run=0;
55 struct ModuleNDM : public ModulePass {
59 ModuleNDM() : ModulePass(&ID) {}
60 virtual bool runOnModule(Module &M) {
66 char ModuleNDM::run=0;
67 RegisterPass<ModuleNDM> X("mndm","mndm",false,false);
69 struct ModuleNDM2 : public ModulePass {
73 ModuleNDM2() : ModulePass(&ID) {}
74 virtual bool runOnModule(Module &M) {
79 char ModuleNDM2::ID=0;
80 char ModuleNDM2::run=0;
82 struct ModuleDNM : public ModulePass {
86 ModuleDNM() : ModulePass(&ID) {}
87 virtual bool runOnModule(Module &M) {
88 EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
92 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
93 AU.addRequired<ModuleNDM>();
98 char ModuleDNM::run=0;
101 struct PassTestBase : public P {
104 static bool initialized;
105 static bool finalized;
108 EXPECT_EQ(true, initialized);
109 EXPECT_EQ(false, finalized);
110 EXPECT_EQ(0, allocated);
116 static void finishedOK(int run) {
118 EXPECT_EQ(true, initialized);
119 EXPECT_EQ(true, finalized);
120 EXPECT_EQ(run, runc);
122 PassTestBase() : P(&ID), allocated(0) {
128 virtual void releaseMemory() {
130 EXPECT_GT(allocated, 0);
134 template<typename P> char PassTestBase<P>::ID;
135 template<typename P> int PassTestBase<P>::runc;
136 template<typename P> bool PassTestBase<P>::initialized;
137 template<typename P> bool PassTestBase<P>::finalized;
139 template<typename T, typename P>
140 struct PassTest : public PassTestBase<P> {
142 virtual bool doInitialization(T &t) {
143 EXPECT_EQ(false, PassTestBase<P>::initialized);
144 PassTestBase<P>::initialized = true;
147 virtual bool doFinalization(T &t) {
148 EXPECT_EQ(false, PassTestBase<P>::finalized);
149 PassTestBase<P>::finalized = true;
150 EXPECT_EQ(0, PassTestBase<P>::allocated);
155 struct CGPass : public PassTest<CallGraph, CallGraphSCCPass> {
157 virtual bool runOnSCC(const std::vector<CallGraphNode*> &SCMM) {
158 EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
163 RegisterPass<CGPass> X1("cgp","cgp");
165 struct FPass : public PassTest<Module, FunctionPass> {
167 virtual bool runOnFunction(Function &F) {
169 // EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
174 RegisterPass<FPass> X2("fp","fp");
176 struct LPass : public PassTestBase<LoopPass> {
178 static int initcount;
182 initcount = 0; fincount=0;
183 EXPECT_EQ(false, initialized);
185 static void finishedOK(int run, int finalized) {
186 PassTestBase<LoopPass>::finishedOK(run);
187 EXPECT_EQ(run, initcount);
188 EXPECT_EQ(finalized, fincount);
190 virtual bool doInitialization(Loop* L, LPPassManager &LPM) {
195 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
196 EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
200 virtual bool doFinalization() {
206 int LPass::initcount=0;
207 int LPass::fincount=0;
208 RegisterPass<LPass> X3("lp","lp");
210 struct BPass : public PassTestBase<BasicBlockPass> {
215 static void finishedOK(int run, int N) {
216 PassTestBase<BasicBlockPass>::finishedOK(run);
217 EXPECT_EQ(inited, N);
224 virtual bool doInitialization(Module &M) {
225 EXPECT_EQ(false, initialized);
229 virtual bool doInitialization(Function &F) {
233 virtual bool runOnBasicBlock(BasicBlock &BB) {
234 EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
238 virtual bool doFinalization(Function &F) {
242 virtual bool doFinalization(Module &M) {
243 EXPECT_EQ(false, finalized);
245 EXPECT_EQ(0, allocated);
251 RegisterPass<BPass> X4("bp","bp");
253 struct OnTheFlyTest: public ModulePass {
256 OnTheFlyTest() : ModulePass(&ID) {}
257 virtual bool runOnModule(Module &M) {
258 EXPECT_TRUE(getAnalysisIfAvailable<TargetData>());
259 for (Module::iterator I=M.begin(),E=M.end(); I != E; ++I) {
262 SCOPED_TRACE("Running on the fly function pass");
263 getAnalysis<FPass>(F);
268 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
269 AU.addRequired<FPass>();
272 char OnTheFlyTest::ID=0;
274 TEST(PassManager, RunOnce) {
275 Module M("test-once", *new LLVMContext());
276 struct ModuleNDNM *mNDNM = new ModuleNDNM();
277 struct ModuleDNM *mDNM = new ModuleDNM();
278 struct ModuleNDM *mNDM = new ModuleNDM();
279 struct ModuleNDM2 *mNDM2 = new ModuleNDM2();
281 mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
284 Passes.add(new TargetData(&M));
291 // each pass must be run exactly once, since nothing invalidates them
292 EXPECT_EQ(1, mNDM->run);
293 EXPECT_EQ(1, mNDNM->run);
294 EXPECT_EQ(1, mDNM->run);
295 EXPECT_EQ(1, mNDM2->run);
298 TEST(PassManager, ReRun) {
299 Module M("test-rerun", *new LLVMContext());
300 struct ModuleNDNM *mNDNM = new ModuleNDNM();
301 struct ModuleDNM *mDNM = new ModuleDNM();
302 struct ModuleNDM *mNDM = new ModuleNDM();
303 struct ModuleNDM2 *mNDM2 = new ModuleNDM2();
305 mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
308 Passes.add(new TargetData(&M));
311 Passes.add(mNDM2);// invalidates mNDM needed by mDNM
315 // Some passes must be rerun because a pass that modified the
316 // module/function was run inbetween
317 EXPECT_EQ(2, mNDM->run);
318 EXPECT_EQ(1, mNDNM->run);
319 EXPECT_EQ(1, mNDM2->run);
320 EXPECT_EQ(1, mDNM->run);
323 Module* makeLLVMModule();
326 void MemoryTestHelper(int run) {
327 Module *M = makeLLVMModule();
330 Passes.add(new TargetData(M));
337 void MemoryTestHelper(int run, int N) {
338 Module *M = makeLLVMModule();
341 Passes.add(new TargetData(M));
344 T::finishedOK(run, N);
348 TEST(PassManager, Memory) {
349 // SCC#1: test1->test2->test3->test1
351 // SCC#3: indirect call node
353 SCOPED_TRACE("Callgraph pass");
354 MemoryTestHelper<CGPass>(3);
358 SCOPED_TRACE("Function pass");
359 MemoryTestHelper<FPass>(4);// 4 functions
363 SCOPED_TRACE("Loop pass");
364 MemoryTestHelper<LPass>(2, 1); //2 loops, 1 function
367 SCOPED_TRACE("Basic block pass");
368 MemoryTestHelper<BPass>(7, 4); //9 basic blocks
373 TEST(PassManager, MemoryOnTheFly) {
374 Module *M = makeLLVMModule();
376 SCOPED_TRACE("Running OnTheFlyTest");
377 struct OnTheFlyTest *O = new OnTheFlyTest();
379 Passes.add(new TargetData(M));
383 FPass::finishedOK(4);
388 Module* makeLLVMModule() {
389 // Module Construction
390 Module* mod = new Module("test-mem", *new LLVMContext());
391 mod->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
392 "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
393 "a0:0:64-s0:64:64-f80:128:128");
394 mod->setTargetTriple("x86_64-unknown-linux-gnu");
397 std::vector<const Type*>FuncTy_0_args;
398 FunctionType* FuncTy_0 = FunctionType::get(
399 /*Result=*/IntegerType::get(32),
400 /*Params=*/FuncTy_0_args,
403 std::vector<const Type*>FuncTy_2_args;
404 FuncTy_2_args.push_back(IntegerType::get(1));
405 FunctionType* FuncTy_2 = FunctionType::get(
406 /*Result=*/Type::VoidTy,
407 /*Params=*/FuncTy_2_args,
411 // Function Declarations
413 Function* func_test1 = Function::Create(
415 /*Linkage=*/GlobalValue::ExternalLinkage,
416 /*Name=*/"test1", mod);
417 func_test1->setCallingConv(CallingConv::C);
418 AttrListPtr func_test1_PAL;
419 func_test1->setAttributes(func_test1_PAL);
421 Function* func_test2 = Function::Create(
423 /*Linkage=*/GlobalValue::ExternalLinkage,
424 /*Name=*/"test2", mod);
425 func_test2->setCallingConv(CallingConv::C);
426 AttrListPtr func_test2_PAL;
427 func_test2->setAttributes(func_test2_PAL);
429 Function* func_test3 = Function::Create(
431 /*Linkage=*/GlobalValue::ExternalLinkage,
432 /*Name=*/"test3", mod);
433 func_test3->setCallingConv(CallingConv::C);
434 AttrListPtr func_test3_PAL;
435 func_test3->setAttributes(func_test3_PAL);
437 Function* func_test4 = Function::Create(
439 /*Linkage=*/GlobalValue::ExternalLinkage,
440 /*Name=*/"test4", mod);
441 func_test4->setCallingConv(CallingConv::C);
442 AttrListPtr func_test4_PAL;
443 func_test4->setAttributes(func_test4_PAL);
445 // Global Variable Declarations
448 // Constant Definitions
450 // Global Variable Definitions
452 // Function Definitions
454 // Function: test1 (func_test1)
457 BasicBlock* label_entry = BasicBlock::Create("entry",func_test1,0);
459 // Block entry (label_entry)
460 CallInst* int32_3 = CallInst::Create(func_test2, "", label_entry);
461 int32_3->setCallingConv(CallingConv::C);
462 int32_3->setTailCall(false);AttrListPtr int32_3_PAL;
463 int32_3->setAttributes(int32_3_PAL);
465 ReturnInst::Create(int32_3, label_entry);
469 // Function: test2 (func_test2)
472 BasicBlock* label_entry_5 = BasicBlock::Create("entry",func_test2,0);
474 // Block entry (label_entry_5)
475 CallInst* int32_6 = CallInst::Create(func_test3, "", label_entry_5);
476 int32_6->setCallingConv(CallingConv::C);
477 int32_6->setTailCall(false);AttrListPtr int32_6_PAL;
478 int32_6->setAttributes(int32_6_PAL);
480 ReturnInst::Create(int32_6, label_entry_5);
484 // Function: test3 (func_test3)
487 BasicBlock* label_entry_8 = BasicBlock::Create("entry",func_test3,0);
489 // Block entry (label_entry_8)
490 CallInst* int32_9 = CallInst::Create(func_test1, "", label_entry_8);
491 int32_9->setCallingConv(CallingConv::C);
492 int32_9->setTailCall(false);AttrListPtr int32_9_PAL;
493 int32_9->setAttributes(int32_9_PAL);
495 ReturnInst::Create(int32_9, label_entry_8);
499 // Function: test4 (func_test4)
501 Function::arg_iterator args = func_test4->arg_begin();
502 Value* int1_f = args++;
503 int1_f->setName("f");
505 BasicBlock* label_entry_11 = BasicBlock::Create("entry",func_test4,0);
506 BasicBlock* label_bb = BasicBlock::Create("bb",func_test4,0);
507 BasicBlock* label_bb1 = BasicBlock::Create("bb1",func_test4,0);
508 BasicBlock* label_return = BasicBlock::Create("return",func_test4,0);
510 // Block entry (label_entry_11)
511 BranchInst::Create(label_bb, label_entry_11);
513 // Block bb (label_bb)
514 BranchInst::Create(label_bb, label_bb1, int1_f, label_bb);
516 // Block bb1 (label_bb1)
517 BranchInst::Create(label_bb1, label_return, int1_f, label_bb1);
519 // Block return (label_return)
520 ReturnInst::Create(label_return);