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