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