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