LegacyPassManagerTest.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. //===- llvm/unittest/IR/LegacyPassManager.cpp - Legacy PassManager tests --===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This unit test exercises the legacy pass manager infrastructure. We use the
  10. // old names as well to ensure that the source-level compatibility is preserved
  11. // where possible.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/IR/LegacyPassManager.h"
  15. #include "llvm/Analysis/CallGraphSCCPass.h"
  16. #include "llvm/Analysis/LoopInfo.h"
  17. #include "llvm/Analysis/LoopPass.h"
  18. #include "llvm/IR/BasicBlock.h"
  19. #include "llvm/IR/CallingConv.h"
  20. #include "llvm/IR/DataLayout.h"
  21. #include "llvm/IR/DerivedTypes.h"
  22. #include "llvm/IR/Function.h"
  23. #include "llvm/IR/GlobalVariable.h"
  24. #include "llvm/IR/Instructions.h"
  25. #include "llvm/IR/LLVMContext.h"
  26. #include "llvm/IR/Module.h"
  27. #include "llvm/IR/OptBisect.h"
  28. #include "llvm/Pass.h"
  29. #include "llvm/Support/MathExtras.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. #include "gtest/gtest.h"
  32. using namespace llvm;
  33. namespace llvm {
  34. void initializeModuleNDMPass(PassRegistry&);
  35. void initializeFPassPass(PassRegistry&);
  36. void initializeCGPassPass(PassRegistry&);
  37. void initializeLPassPass(PassRegistry&);
  38. void initializeBPassPass(PassRegistry&);
  39. namespace {
  40. // ND = no deps
  41. // NM = no modifications
  42. struct ModuleNDNM: public ModulePass {
  43. public:
  44. static char run;
  45. static char ID;
  46. ModuleNDNM() : ModulePass(ID) { }
  47. bool runOnModule(Module &M) override {
  48. run++;
  49. return false;
  50. }
  51. void getAnalysisUsage(AnalysisUsage &AU) const override {
  52. AU.setPreservesAll();
  53. }
  54. };
  55. char ModuleNDNM::ID=0;
  56. char ModuleNDNM::run=0;
  57. struct ModuleNDM : public ModulePass {
  58. public:
  59. static char run;
  60. static char ID;
  61. ModuleNDM() : ModulePass(ID) {}
  62. bool runOnModule(Module &M) override {
  63. run++;
  64. return true;
  65. }
  66. };
  67. char ModuleNDM::ID=0;
  68. char ModuleNDM::run=0;
  69. struct ModuleNDM2 : public ModulePass {
  70. public:
  71. static char run;
  72. static char ID;
  73. ModuleNDM2() : ModulePass(ID) {}
  74. bool runOnModule(Module &M) override {
  75. run++;
  76. return true;
  77. }
  78. };
  79. char ModuleNDM2::ID=0;
  80. char ModuleNDM2::run=0;
  81. struct ModuleDNM : public ModulePass {
  82. public:
  83. static char run;
  84. static char ID;
  85. ModuleDNM() : ModulePass(ID) {
  86. initializeModuleNDMPass(*PassRegistry::getPassRegistry());
  87. }
  88. bool runOnModule(Module &M) override {
  89. run++;
  90. return false;
  91. }
  92. void getAnalysisUsage(AnalysisUsage &AU) const override {
  93. AU.addRequired<ModuleNDM>();
  94. AU.setPreservesAll();
  95. }
  96. };
  97. char ModuleDNM::ID=0;
  98. char ModuleDNM::run=0;
  99. template<typename P>
  100. struct PassTestBase : public P {
  101. protected:
  102. static int runc;
  103. static bool initialized;
  104. static bool finalized;
  105. int allocated;
  106. void run() {
  107. EXPECT_TRUE(initialized);
  108. EXPECT_FALSE(finalized);
  109. EXPECT_EQ(0, allocated);
  110. allocated++;
  111. runc++;
  112. }
  113. public:
  114. static char ID;
  115. static void finishedOK(int run) {
  116. EXPECT_GT(runc, 0);
  117. EXPECT_TRUE(initialized);
  118. EXPECT_TRUE(finalized);
  119. EXPECT_EQ(run, runc);
  120. }
  121. PassTestBase() : P(ID), allocated(0) {
  122. initialized = false;
  123. finalized = false;
  124. runc = 0;
  125. }
  126. void releaseMemory() override {
  127. EXPECT_GT(runc, 0);
  128. EXPECT_GT(allocated, 0);
  129. allocated--;
  130. }
  131. };
  132. template<typename P> char PassTestBase<P>::ID;
  133. template<typename P> int PassTestBase<P>::runc;
  134. template<typename P> bool PassTestBase<P>::initialized;
  135. template<typename P> bool PassTestBase<P>::finalized;
  136. template<typename T, typename P>
  137. struct PassTest : public PassTestBase<P> {
  138. public:
  139. #ifndef _MSC_VER // MSVC complains that Pass is not base class.
  140. using llvm::Pass::doInitialization;
  141. using llvm::Pass::doFinalization;
  142. #endif
  143. bool doInitialization(T &t) override {
  144. EXPECT_FALSE(PassTestBase<P>::initialized);
  145. PassTestBase<P>::initialized = true;
  146. return false;
  147. }
  148. bool doFinalization(T &t) override {
  149. EXPECT_FALSE(PassTestBase<P>::finalized);
  150. PassTestBase<P>::finalized = true;
  151. EXPECT_EQ(0, PassTestBase<P>::allocated);
  152. return false;
  153. }
  154. };
  155. struct CGPass : public PassTest<CallGraph, CallGraphSCCPass> {
  156. public:
  157. CGPass() {
  158. initializeCGPassPass(*PassRegistry::getPassRegistry());
  159. }
  160. bool runOnSCC(CallGraphSCC &SCMM) override {
  161. run();
  162. return false;
  163. }
  164. };
  165. struct FPass : public PassTest<Module, FunctionPass> {
  166. public:
  167. bool runOnFunction(Function &F) override {
  168. // FIXME: PR4112
  169. // EXPECT_TRUE(getAnalysisIfAvailable<DataLayout>());
  170. run();
  171. return false;
  172. }
  173. };
  174. struct LPass : public PassTestBase<LoopPass> {
  175. private:
  176. static int initcount;
  177. static int fincount;
  178. public:
  179. LPass() {
  180. initializeLPassPass(*PassRegistry::getPassRegistry());
  181. initcount = 0; fincount=0;
  182. EXPECT_FALSE(initialized);
  183. }
  184. static void finishedOK(int run, int finalized) {
  185. PassTestBase<LoopPass>::finishedOK(run);
  186. EXPECT_EQ(run, initcount);
  187. EXPECT_EQ(finalized, fincount);
  188. }
  189. using llvm::Pass::doInitialization;
  190. using llvm::Pass::doFinalization;
  191. bool doInitialization(Loop* L, LPPassManager &LPM) override {
  192. initialized = true;
  193. initcount++;
  194. return false;
  195. }
  196. bool runOnLoop(Loop *L, LPPassManager &LPM) override {
  197. run();
  198. return false;
  199. }
  200. bool doFinalization() override {
  201. fincount++;
  202. finalized = true;
  203. return false;
  204. }
  205. };
  206. int LPass::initcount=0;
  207. int LPass::fincount=0;
  208. struct BPass : public PassTestBase<BasicBlockPass> {
  209. private:
  210. static int inited;
  211. static int fin;
  212. public:
  213. static void finishedOK(int run, int N) {
  214. PassTestBase<BasicBlockPass>::finishedOK(run);
  215. EXPECT_EQ(inited, N);
  216. EXPECT_EQ(fin, N);
  217. }
  218. BPass() {
  219. inited = 0;
  220. fin = 0;
  221. }
  222. bool doInitialization(Module &M) override {
  223. EXPECT_FALSE(initialized);
  224. initialized = true;
  225. return false;
  226. }
  227. bool doInitialization(Function &F) override {
  228. inited++;
  229. return false;
  230. }
  231. bool runOnBasicBlock(BasicBlock &BB) override {
  232. run();
  233. return false;
  234. }
  235. bool doFinalization(Function &F) override {
  236. fin++;
  237. return false;
  238. }
  239. bool doFinalization(Module &M) override {
  240. EXPECT_FALSE(finalized);
  241. finalized = true;
  242. EXPECT_EQ(0, allocated);
  243. return false;
  244. }
  245. };
  246. int BPass::inited=0;
  247. int BPass::fin=0;
  248. struct OnTheFlyTest: public ModulePass {
  249. public:
  250. static char ID;
  251. OnTheFlyTest() : ModulePass(ID) {
  252. initializeFPassPass(*PassRegistry::getPassRegistry());
  253. }
  254. bool runOnModule(Module &M) override {
  255. for (Module::iterator I=M.begin(),E=M.end(); I != E; ++I) {
  256. Function &F = *I;
  257. {
  258. SCOPED_TRACE("Running on the fly function pass");
  259. getAnalysis<FPass>(F);
  260. }
  261. }
  262. return false;
  263. }
  264. void getAnalysisUsage(AnalysisUsage &AU) const override {
  265. AU.addRequired<FPass>();
  266. }
  267. };
  268. char OnTheFlyTest::ID=0;
  269. TEST(PassManager, RunOnce) {
  270. LLVMContext Context;
  271. Module M("test-once", Context);
  272. struct ModuleNDNM *mNDNM = new ModuleNDNM();
  273. struct ModuleDNM *mDNM = new ModuleDNM();
  274. struct ModuleNDM *mNDM = new ModuleNDM();
  275. struct ModuleNDM2 *mNDM2 = new ModuleNDM2();
  276. mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
  277. legacy::PassManager Passes;
  278. Passes.add(mNDM2);
  279. Passes.add(mNDM);
  280. Passes.add(mNDNM);
  281. Passes.add(mDNM);
  282. Passes.run(M);
  283. // each pass must be run exactly once, since nothing invalidates them
  284. EXPECT_EQ(1, mNDM->run);
  285. EXPECT_EQ(1, mNDNM->run);
  286. EXPECT_EQ(1, mDNM->run);
  287. EXPECT_EQ(1, mNDM2->run);
  288. }
  289. TEST(PassManager, ReRun) {
  290. LLVMContext Context;
  291. Module M("test-rerun", Context);
  292. struct ModuleNDNM *mNDNM = new ModuleNDNM();
  293. struct ModuleDNM *mDNM = new ModuleDNM();
  294. struct ModuleNDM *mNDM = new ModuleNDM();
  295. struct ModuleNDM2 *mNDM2 = new ModuleNDM2();
  296. mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
  297. legacy::PassManager Passes;
  298. Passes.add(mNDM);
  299. Passes.add(mNDNM);
  300. Passes.add(mNDM2);// invalidates mNDM needed by mDNM
  301. Passes.add(mDNM);
  302. Passes.run(M);
  303. // Some passes must be rerun because a pass that modified the
  304. // module/function was run in between
  305. EXPECT_EQ(2, mNDM->run);
  306. EXPECT_EQ(1, mNDNM->run);
  307. EXPECT_EQ(1, mNDM2->run);
  308. EXPECT_EQ(1, mDNM->run);
  309. }
  310. Module *makeLLVMModule(LLVMContext &Context);
  311. template<typename T>
  312. void MemoryTestHelper(int run) {
  313. LLVMContext Context;
  314. std::unique_ptr<Module> M(makeLLVMModule(Context));
  315. T *P = new T();
  316. legacy::PassManager Passes;
  317. Passes.add(P);
  318. Passes.run(*M);
  319. T::finishedOK(run);
  320. }
  321. template<typename T>
  322. void MemoryTestHelper(int run, int N) {
  323. LLVMContext Context;
  324. Module *M = makeLLVMModule(Context);
  325. T *P = new T();
  326. legacy::PassManager Passes;
  327. Passes.add(P);
  328. Passes.run(*M);
  329. T::finishedOK(run, N);
  330. delete M;
  331. }
  332. TEST(PassManager, Memory) {
  333. // SCC#1: test1->test2->test3->test1
  334. // SCC#2: test4
  335. // SCC#3: indirect call node
  336. {
  337. SCOPED_TRACE("Callgraph pass");
  338. MemoryTestHelper<CGPass>(3);
  339. }
  340. {
  341. SCOPED_TRACE("Function pass");
  342. MemoryTestHelper<FPass>(4);// 4 functions
  343. }
  344. {
  345. SCOPED_TRACE("Loop pass");
  346. MemoryTestHelper<LPass>(2, 1); //2 loops, 1 function
  347. }
  348. {
  349. SCOPED_TRACE("Basic block pass");
  350. MemoryTestHelper<BPass>(7, 4); //9 basic blocks
  351. }
  352. }
  353. TEST(PassManager, MemoryOnTheFly) {
  354. LLVMContext Context;
  355. Module *M = makeLLVMModule(Context);
  356. {
  357. SCOPED_TRACE("Running OnTheFlyTest");
  358. struct OnTheFlyTest *O = new OnTheFlyTest();
  359. legacy::PassManager Passes;
  360. Passes.add(O);
  361. Passes.run(*M);
  362. FPass::finishedOK(4);
  363. }
  364. delete M;
  365. }
  366. // Skips or runs optional passes.
  367. struct CustomOptPassGate : public OptPassGate {
  368. bool Skip;
  369. CustomOptPassGate(bool Skip) : Skip(Skip) { }
  370. bool shouldRunPass(const Pass *P, StringRef IRDescription) {
  371. if (P->getPassKind() == PT_Module)
  372. return !Skip;
  373. return OptPassGate::shouldRunPass(P, IRDescription);
  374. }
  375. bool isEnabled() const { return true; }
  376. };
  377. // Optional module pass.
  378. struct ModuleOpt: public ModulePass {
  379. char run = 0;
  380. static char ID;
  381. ModuleOpt() : ModulePass(ID) { }
  382. bool runOnModule(Module &M) override {
  383. if (!skipModule(M))
  384. run++;
  385. return false;
  386. }
  387. };
  388. char ModuleOpt::ID=0;
  389. TEST(PassManager, CustomOptPassGate) {
  390. LLVMContext Context0;
  391. LLVMContext Context1;
  392. LLVMContext Context2;
  393. CustomOptPassGate SkipOptionalPasses(true);
  394. CustomOptPassGate RunOptionalPasses(false);
  395. Module M0("custom-opt-bisect", Context0);
  396. Module M1("custom-opt-bisect", Context1);
  397. Module M2("custom-opt-bisect2", Context2);
  398. struct ModuleOpt *mOpt0 = new ModuleOpt();
  399. struct ModuleOpt *mOpt1 = new ModuleOpt();
  400. struct ModuleOpt *mOpt2 = new ModuleOpt();
  401. mOpt0->run = mOpt1->run = mOpt2->run = 0;
  402. legacy::PassManager Passes0;
  403. legacy::PassManager Passes1;
  404. legacy::PassManager Passes2;
  405. Passes0.add(mOpt0);
  406. Passes1.add(mOpt1);
  407. Passes2.add(mOpt2);
  408. Context1.setOptPassGate(SkipOptionalPasses);
  409. Context2.setOptPassGate(RunOptionalPasses);
  410. Passes0.run(M0);
  411. Passes1.run(M1);
  412. Passes2.run(M2);
  413. // By default optional passes are run.
  414. EXPECT_EQ(1, mOpt0->run);
  415. // The first context skips optional passes.
  416. EXPECT_EQ(0, mOpt1->run);
  417. // The second context runs optional passes.
  418. EXPECT_EQ(1, mOpt2->run);
  419. }
  420. Module *makeLLVMModule(LLVMContext &Context) {
  421. // Module Construction
  422. Module *mod = new Module("test-mem", Context);
  423. mod->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
  424. "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
  425. "a:0:64-s:64:64-f80:128:128");
  426. mod->setTargetTriple("x86_64-unknown-linux-gnu");
  427. // Type Definitions
  428. std::vector<Type*>FuncTy_0_args;
  429. FunctionType *FuncTy_0 = FunctionType::get(
  430. /*Result=*/IntegerType::get(Context, 32),
  431. /*Params=*/FuncTy_0_args,
  432. /*isVarArg=*/false);
  433. std::vector<Type*>FuncTy_2_args;
  434. FuncTy_2_args.push_back(IntegerType::get(Context, 1));
  435. FunctionType *FuncTy_2 = FunctionType::get(
  436. /*Result=*/Type::getVoidTy(Context),
  437. /*Params=*/FuncTy_2_args,
  438. /*isVarArg=*/false);
  439. // Function Declarations
  440. Function* func_test1 = Function::Create(
  441. /*Type=*/FuncTy_0,
  442. /*Linkage=*/GlobalValue::ExternalLinkage,
  443. /*Name=*/"test1", mod);
  444. func_test1->setCallingConv(CallingConv::C);
  445. AttributeList func_test1_PAL;
  446. func_test1->setAttributes(func_test1_PAL);
  447. Function* func_test2 = Function::Create(
  448. /*Type=*/FuncTy_0,
  449. /*Linkage=*/GlobalValue::ExternalLinkage,
  450. /*Name=*/"test2", mod);
  451. func_test2->setCallingConv(CallingConv::C);
  452. AttributeList func_test2_PAL;
  453. func_test2->setAttributes(func_test2_PAL);
  454. Function* func_test3 = Function::Create(
  455. /*Type=*/FuncTy_0,
  456. /*Linkage=*/GlobalValue::ExternalLinkage,
  457. /*Name=*/"test3", mod);
  458. func_test3->setCallingConv(CallingConv::C);
  459. AttributeList func_test3_PAL;
  460. func_test3->setAttributes(func_test3_PAL);
  461. Function* func_test4 = Function::Create(
  462. /*Type=*/FuncTy_2,
  463. /*Linkage=*/GlobalValue::ExternalLinkage,
  464. /*Name=*/"test4", mod);
  465. func_test4->setCallingConv(CallingConv::C);
  466. AttributeList func_test4_PAL;
  467. func_test4->setAttributes(func_test4_PAL);
  468. // Global Variable Declarations
  469. // Constant Definitions
  470. // Global Variable Definitions
  471. // Function Definitions
  472. // Function: test1 (func_test1)
  473. {
  474. BasicBlock *label_entry =
  475. BasicBlock::Create(Context, "entry", func_test1, nullptr);
  476. // Block entry (label_entry)
  477. CallInst* int32_3 = CallInst::Create(func_test2, "", label_entry);
  478. int32_3->setCallingConv(CallingConv::C);
  479. int32_3->setTailCall(false);
  480. AttributeList int32_3_PAL;
  481. int32_3->setAttributes(int32_3_PAL);
  482. ReturnInst::Create(Context, int32_3, label_entry);
  483. }
  484. // Function: test2 (func_test2)
  485. {
  486. BasicBlock *label_entry_5 =
  487. BasicBlock::Create(Context, "entry", func_test2, nullptr);
  488. // Block entry (label_entry_5)
  489. CallInst* int32_6 = CallInst::Create(func_test3, "", label_entry_5);
  490. int32_6->setCallingConv(CallingConv::C);
  491. int32_6->setTailCall(false);
  492. AttributeList int32_6_PAL;
  493. int32_6->setAttributes(int32_6_PAL);
  494. ReturnInst::Create(Context, int32_6, label_entry_5);
  495. }
  496. // Function: test3 (func_test3)
  497. {
  498. BasicBlock *label_entry_8 =
  499. BasicBlock::Create(Context, "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);
  504. AttributeList int32_9_PAL;
  505. int32_9->setAttributes(int32_9_PAL);
  506. ReturnInst::Create(Context, int32_9, label_entry_8);
  507. }
  508. // Function: test4 (func_test4)
  509. {
  510. Function::arg_iterator args = func_test4->arg_begin();
  511. Value *int1_f = &*args++;
  512. int1_f->setName("f");
  513. BasicBlock *label_entry_11 =
  514. BasicBlock::Create(Context, "entry", func_test4, nullptr);
  515. BasicBlock *label_bb =
  516. BasicBlock::Create(Context, "bb", func_test4, nullptr);
  517. BasicBlock *label_bb1 =
  518. BasicBlock::Create(Context, "bb1", func_test4, nullptr);
  519. BasicBlock *label_return =
  520. BasicBlock::Create(Context, "return", func_test4, nullptr);
  521. // Block entry (label_entry_11)
  522. BranchInst::Create(label_bb, label_entry_11);
  523. // Block bb (label_bb)
  524. BranchInst::Create(label_bb, label_bb1, int1_f, label_bb);
  525. // Block bb1 (label_bb1)
  526. BranchInst::Create(label_bb1, label_return, int1_f, label_bb1);
  527. // Block return (label_return)
  528. ReturnInst::Create(Context, label_return);
  529. }
  530. return mod;
  531. }
  532. }
  533. }
  534. INITIALIZE_PASS(ModuleNDM, "mndm", "mndm", false, false)
  535. INITIALIZE_PASS_BEGIN(CGPass, "cgp","cgp", false, false)
  536. INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
  537. INITIALIZE_PASS_END(CGPass, "cgp","cgp", false, false)
  538. INITIALIZE_PASS(FPass, "fp","fp", false, false)
  539. INITIALIZE_PASS_BEGIN(LPass, "lp","lp", false, false)
  540. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
  541. INITIALIZE_PASS_END(LPass, "lp","lp", false, false)
  542. INITIALIZE_PASS(BPass, "bp","bp", false, false)