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