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