blob: 9dceb976c9375eb21d38511b8460b4004a5801de [file] [log] [blame]
Chandler Carruth7caea412013-11-09 12:26:54 +00001//===- llvm/unittest/IR/LegacyPassManager.cpp - Legacy PassManager tests --===//
Torok Edwin24c78352009-06-29 18:49:09 +00002//
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 Carruth7caea412013-11-09 12:26:54 +00009//
10// This unit test exercises the legacy pass manager infrastructure. We use the
Chandler Carruth30d69c22015-02-13 10:01:29 +000011// old names as well to ensure that the source-level compatibility is preserved
12// where possible.
Chandler Carruth7caea412013-11-09 12:26:54 +000013//
14//===----------------------------------------------------------------------===//
Torok Edwin24c78352009-06-29 18:49:09 +000015
Chandler Carruth30d69c22015-02-13 10:01:29 +000016#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth839a98e2013-01-07 15:26:48 +000017#include "llvm/Analysis/CallGraphSCCPass.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000018#include "llvm/Analysis/LoopInfo.h"
19#include "llvm/Analysis/LoopPass.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#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 Carruthb8ddc702014-01-12 11:10:32 +000027#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/InlineAsm.h"
29#include "llvm/IR/Instructions.h"
30#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Module.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000032#include "llvm/IR/Verifier.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000033#include "llvm/Pass.h"
34#include "llvm/Support/MathExtras.h"
35#include "llvm/Support/raw_ostream.h"
Torok Edwin24c78352009-06-29 18:49:09 +000036#include "gtest/gtest.h"
37
Owen Anderson6c18d1a2010-10-19 17:21:58 +000038using namespace llvm;
39
Torok Edwin24c78352009-06-29 18:49:09 +000040namespace llvm {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000041 void initializeModuleNDMPass(PassRegistry&);
42 void initializeFPassPass(PassRegistry&);
43 void initializeCGPassPass(PassRegistry&);
44 void initializeLPassPass(PassRegistry&);
45 void initializeBPassPass(PassRegistry&);
Duncan Sands6ae98632011-03-31 09:58:51 +000046
Torok Edwin24c78352009-06-29 18:49:09 +000047 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 Anderson6c18d1a2010-10-19 17:21:58 +000054 ModuleNDNM() : ModulePass(ID) { }
Rafael Espindolad8bd91c2014-09-10 15:50:08 +000055 bool runOnModule(Module &M) override {
Torok Edwin24c78352009-06-29 18:49:09 +000056 run++;
57 return false;
58 }
Rafael Espindolad8bd91c2014-09-10 15:50:08 +000059 void getAnalysisUsage(AnalysisUsage &AU) const override {
Torok Edwin24c78352009-06-29 18:49:09 +000060 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 Andersona7aed182010-08-06 18:33:48 +000070 ModuleNDM() : ModulePass(ID) {}
Rafael Espindolad8bd91c2014-09-10 15:50:08 +000071 bool runOnModule(Module &M) override {
Torok Edwin24c78352009-06-29 18:49:09 +000072 run++;
73 return true;
74 }
75 };
76 char ModuleNDM::ID=0;
77 char ModuleNDM::run=0;
Torok Edwin24c78352009-06-29 18:49:09 +000078
79 struct ModuleNDM2 : public ModulePass {
80 public:
81 static char run;
82 static char ID;
Owen Andersona7aed182010-08-06 18:33:48 +000083 ModuleNDM2() : ModulePass(ID) {}
Rafael Espindolad8bd91c2014-09-10 15:50:08 +000084 bool runOnModule(Module &M) override {
Torok Edwin24c78352009-06-29 18:49:09 +000085 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 Anderson6c18d1a2010-10-19 17:21:58 +000096 ModuleDNM() : ModulePass(ID) {
97 initializeModuleNDMPass(*PassRegistry::getPassRegistry());
98 }
Rafael Espindolad8bd91c2014-09-10 15:50:08 +000099 bool runOnModule(Module &M) override {
Torok Edwin24c78352009-06-29 18:49:09 +0000100 run++;
101 return false;
102 }
Rafael Espindolad8bd91c2014-09-10 15:50:08 +0000103 void getAnalysisUsage(AnalysisUsage &AU) const override {
Torok Edwin24c78352009-06-29 18:49:09 +0000104 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 Carruthbc3e8a72010-07-13 17:28:05 +0000119 EXPECT_TRUE(initialized);
120 EXPECT_FALSE(finalized);
Torok Edwin24c78352009-06-29 18:49:09 +0000121 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 Carruthbc3e8a72010-07-13 17:28:05 +0000129 EXPECT_TRUE(initialized);
130 EXPECT_TRUE(finalized);
Torok Edwin24c78352009-06-29 18:49:09 +0000131 EXPECT_EQ(run, runc);
132 }
Owen Andersona7aed182010-08-06 18:33:48 +0000133 PassTestBase() : P(ID), allocated(0) {
Torok Edwin24c78352009-06-29 18:49:09 +0000134 initialized = false;
135 finalized = false;
136 runc = 0;
137 }
138
Rafael Espindolad8bd91c2014-09-10 15:50:08 +0000139 void releaseMemory() override {
Torok Edwin24c78352009-06-29 18:49:09 +0000140 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 Takumi0e9acc92012-12-04 07:25:24 +0000153#ifndef _MSC_VER // MSVC complains that Pass is not base class.
Matt Beaumont-Gayabfc4462012-12-04 05:41:27 +0000154 using llvm::Pass::doInitialization;
155 using llvm::Pass::doFinalization;
NAKAMURA Takumi0e9acc92012-12-04 07:25:24 +0000156#endif
Rafael Espindolad8bd91c2014-09-10 15:50:08 +0000157 bool doInitialization(T &t) override {
Chandler Carruthbc3e8a72010-07-13 17:28:05 +0000158 EXPECT_FALSE(PassTestBase<P>::initialized);
Torok Edwin24c78352009-06-29 18:49:09 +0000159 PassTestBase<P>::initialized = true;
160 return false;
161 }
Rafael Espindolad8bd91c2014-09-10 15:50:08 +0000162 bool doFinalization(T &t) override {
Chandler Carruthbc3e8a72010-07-13 17:28:05 +0000163 EXPECT_FALSE(PassTestBase<P>::finalized);
Torok Edwin24c78352009-06-29 18:49:09 +0000164 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 Anderson6c18d1a2010-10-19 17:21:58 +0000172 CGPass() {
173 initializeCGPassPass(*PassRegistry::getPassRegistry());
174 }
Rafael Espindolad8bd91c2014-09-10 15:50:08 +0000175 bool runOnSCC(CallGraphSCC &SCMM) override {
Torok Edwin24c78352009-06-29 18:49:09 +0000176 run();
177 return false;
178 }
179 };
Torok Edwin24c78352009-06-29 18:49:09 +0000180
181 struct FPass : public PassTest<Module, FunctionPass> {
182 public:
Rafael Espindolad8bd91c2014-09-10 15:50:08 +0000183 bool runOnFunction(Function &F) override {
Torok Edwin24c78352009-06-29 18:49:09 +0000184 // FIXME: PR4112
Micah Villmow9cfc13d2012-10-08 16:39:34 +0000185 // EXPECT_TRUE(getAnalysisIfAvailable<DataLayout>());
Torok Edwin24c78352009-06-29 18:49:09 +0000186 run();
187 return false;
188 }
189 };
Torok Edwin24c78352009-06-29 18:49:09 +0000190
191 struct LPass : public PassTestBase<LoopPass> {
192 private:
193 static int initcount;
194 static int fincount;
195 public:
196 LPass() {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000197 initializeLPassPass(*PassRegistry::getPassRegistry());
Torok Edwin24c78352009-06-29 18:49:09 +0000198 initcount = 0; fincount=0;
Chandler Carruthbc3e8a72010-07-13 17:28:05 +0000199 EXPECT_FALSE(initialized);
Torok Edwin24c78352009-06-29 18:49:09 +0000200 }
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-Gayabfc4462012-12-04 05:41:27 +0000206 using llvm::Pass::doInitialization;
207 using llvm::Pass::doFinalization;
Rafael Espindolad8bd91c2014-09-10 15:50:08 +0000208 bool doInitialization(Loop* L, LPPassManager &LPM) override {
Torok Edwin24c78352009-06-29 18:49:09 +0000209 initialized = true;
210 initcount++;
211 return false;
212 }
Rafael Espindolad8bd91c2014-09-10 15:50:08 +0000213 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
Torok Edwin24c78352009-06-29 18:49:09 +0000214 run();
215 return false;
216 }
Rafael Espindolad8bd91c2014-09-10 15:50:08 +0000217 bool doFinalization() override {
Torok Edwin24c78352009-06-29 18:49:09 +0000218 fincount++;
219 finalized = true;
220 return false;
221 }
222 };
223 int LPass::initcount=0;
224 int LPass::fincount=0;
Torok Edwin24c78352009-06-29 18:49:09 +0000225
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 Espindolad8bd91c2014-09-10 15:50:08 +0000240 bool doInitialization(Module &M) override {
Chandler Carruthbc3e8a72010-07-13 17:28:05 +0000241 EXPECT_FALSE(initialized);
Torok Edwin24c78352009-06-29 18:49:09 +0000242 initialized = true;
243 return false;
244 }
Rafael Espindolad8bd91c2014-09-10 15:50:08 +0000245 bool doInitialization(Function &F) override {
Torok Edwin24c78352009-06-29 18:49:09 +0000246 inited++;
247 return false;
248 }
Rafael Espindolad8bd91c2014-09-10 15:50:08 +0000249 bool runOnBasicBlock(BasicBlock &BB) override {
Torok Edwin24c78352009-06-29 18:49:09 +0000250 run();
251 return false;
252 }
Rafael Espindolad8bd91c2014-09-10 15:50:08 +0000253 bool doFinalization(Function &F) override {
Torok Edwin24c78352009-06-29 18:49:09 +0000254 fin++;
255 return false;
256 }
Rafael Espindolad8bd91c2014-09-10 15:50:08 +0000257 bool doFinalization(Module &M) override {
Chandler Carruthbc3e8a72010-07-13 17:28:05 +0000258 EXPECT_FALSE(finalized);
Torok Edwin24c78352009-06-29 18:49:09 +0000259 finalized = true;
260 EXPECT_EQ(0, allocated);
261 return false;
262 }
263 };
264 int BPass::inited=0;
265 int BPass::fin=0;
Torok Edwin24c78352009-06-29 18:49:09 +0000266
267 struct OnTheFlyTest: public ModulePass {
268 public:
269 static char ID;
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000270 OnTheFlyTest() : ModulePass(ID) {
271 initializeFPassPass(*PassRegistry::getPassRegistry());
272 }
Rafael Espindolad8bd91c2014-09-10 15:50:08 +0000273 bool runOnModule(Module &M) override {
Torok Edwin24c78352009-06-29 18:49:09 +0000274 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 Espindolad8bd91c2014-09-10 15:50:08 +0000283 void getAnalysisUsage(AnalysisUsage &AU) const override {
Torok Edwin24c78352009-06-29 18:49:09 +0000284 AU.addRequired<FPass>();
285 }
286 };
287 char OnTheFlyTest::ID=0;
288
289 TEST(PassManager, RunOnce) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000290 LLVMContext Context;
291 Module M("test-once", Context);
Torok Edwin24c78352009-06-29 18:49:09 +0000292 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 Carruth30d69c22015-02-13 10:01:29 +0000299 legacy::PassManager Passes;
Torok Edwin24c78352009-06-29 18:49:09 +0000300 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 Amini03b42e42016-04-14 21:59:01 +0000314 LLVMContext Context;
315 Module M("test-rerun", Context);
Torok Edwin24c78352009-06-29 18:49:09 +0000316 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 Carruth30d69c22015-02-13 10:01:29 +0000323 legacy::PassManager Passes;
Torok Edwin24c78352009-06-29 18:49:09 +0000324 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 Kramerbde91762012-06-02 10:20:22 +0000331 // module/function was run in between
Torok Edwin24c78352009-06-29 18:49:09 +0000332 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 Amini03b42e42016-04-14 21:59:01 +0000338 Module *makeLLVMModule(LLVMContext &Context);
Torok Edwin24c78352009-06-29 18:49:09 +0000339
340 template<typename T>
341 void MemoryTestHelper(int run) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000342 LLVMContext Context;
343 std::unique_ptr<Module> M(makeLLVMModule(Context));
Torok Edwin24c78352009-06-29 18:49:09 +0000344 T *P = new T();
Chandler Carruth30d69c22015-02-13 10:01:29 +0000345 legacy::PassManager Passes;
Torok Edwin24c78352009-06-29 18:49:09 +0000346 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 Amini03b42e42016-04-14 21:59:01 +0000353 LLVMContext Context;
354 Module *M = makeLLVMModule(Context);
Torok Edwin24c78352009-06-29 18:49:09 +0000355 T *P = new T();
Chandler Carruth30d69c22015-02-13 10:01:29 +0000356 legacy::PassManager Passes;
Torok Edwin24c78352009-06-29 18:49:09 +0000357 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 Amini03b42e42016-04-14 21:59:01 +0000389 LLVMContext Context;
390 Module *M = makeLLVMModule(Context);
Torok Edwin24c78352009-06-29 18:49:09 +0000391 {
392 SCOPED_TRACE("Running OnTheFlyTest");
393 struct OnTheFlyTest *O = new OnTheFlyTest();
Chandler Carruth30d69c22015-02-13 10:01:29 +0000394 legacy::PassManager Passes;
Torok Edwin24c78352009-06-29 18:49:09 +0000395 Passes.add(O);
396 Passes.run(*M);
397
398 FPass::finishedOK(4);
399 }
400 delete M;
401 }
402
Mehdi Amini03b42e42016-04-14 21:59:01 +0000403 Module *makeLLVMModule(LLVMContext &Context) {
Torok Edwin24c78352009-06-29 18:49:09 +0000404 // Module Construction
Mehdi Amini03b42e42016-04-14 21:59:01 +0000405 Module *mod = new Module("test-mem", Context);
Torok Edwin24c78352009-06-29 18:49:09 +0000406 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 Espindolac6751622013-12-13 18:56:34 +0000408 "a:0:64-s:64:64-f80:128:128");
Torok Edwin24c78352009-06-29 18:49:09 +0000409 mod->setTargetTriple("x86_64-unknown-linux-gnu");
410
411 // Type Definitions
Jay Foadb804a2b2011-07-12 14:06:48 +0000412 std::vector<Type*>FuncTy_0_args;
Mehdi Amini03b42e42016-04-14 21:59:01 +0000413 FunctionType *FuncTy_0 = FunctionType::get(
414 /*Result=*/IntegerType::get(Context, 32),
415 /*Params=*/FuncTy_0_args,
416 /*isVarArg=*/false);
Torok Edwin24c78352009-06-29 18:49:09 +0000417
Jay Foadb804a2b2011-07-12 14:06:48 +0000418 std::vector<Type*>FuncTy_2_args;
Mehdi Amini03b42e42016-04-14 21:59:01 +0000419 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 Edwin24c78352009-06-29 18:49:09 +0000424
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 Wendlinge94d8432012-12-07 23:16:57 +0000432 AttributeSet func_test1_PAL;
Torok Edwin24c78352009-06-29 18:49:09 +0000433 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 Wendlinge94d8432012-12-07 23:16:57 +0000440 AttributeSet func_test2_PAL;
Torok Edwin24c78352009-06-29 18:49:09 +0000441 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 Wendlinge94d8432012-12-07 23:16:57 +0000448 AttributeSet func_test3_PAL;
Torok Edwin24c78352009-06-29 18:49:09 +0000449 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 Wendlinge94d8432012-12-07 23:16:57 +0000456 AttributeSet func_test4_PAL;
Torok Edwin24c78352009-06-29 18:49:09 +0000457 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 Amini03b42e42016-04-14 21:59:01 +0000471 BasicBlock *label_entry =
472 BasicBlock::Create(Context, "entry", func_test1, nullptr);
Torok Edwin24c78352009-06-29 18:49:09 +0000473
474 // Block entry (label_entry)
475 CallInst* int32_3 = CallInst::Create(func_test2, "", label_entry);
476 int32_3->setCallingConv(CallingConv::C);
Bill Wendlinge94d8432012-12-07 23:16:57 +0000477 int32_3->setTailCall(false);AttributeSet int32_3_PAL;
Torok Edwin24c78352009-06-29 18:49:09 +0000478 int32_3->setAttributes(int32_3_PAL);
479
Mehdi Amini03b42e42016-04-14 21:59:01 +0000480 ReturnInst::Create(Context, int32_3, label_entry);
Torok Edwin24c78352009-06-29 18:49:09 +0000481 }
482
483 // Function: test2 (func_test2)
484 {
485
Mehdi Amini03b42e42016-04-14 21:59:01 +0000486 BasicBlock *label_entry_5 =
487 BasicBlock::Create(Context, "entry", func_test2, nullptr);
Torok Edwin24c78352009-06-29 18:49:09 +0000488
489 // Block entry (label_entry_5)
490 CallInst* int32_6 = CallInst::Create(func_test3, "", label_entry_5);
491 int32_6->setCallingConv(CallingConv::C);
Bill Wendlinge94d8432012-12-07 23:16:57 +0000492 int32_6->setTailCall(false);AttributeSet int32_6_PAL;
Torok Edwin24c78352009-06-29 18:49:09 +0000493 int32_6->setAttributes(int32_6_PAL);
494
Mehdi Amini03b42e42016-04-14 21:59:01 +0000495 ReturnInst::Create(Context, int32_6, label_entry_5);
Torok Edwin24c78352009-06-29 18:49:09 +0000496 }
497
498 // Function: test3 (func_test3)
499 {
500
Mehdi Amini03b42e42016-04-14 21:59:01 +0000501 BasicBlock *label_entry_8 =
502 BasicBlock::Create(Context, "entry", func_test3, nullptr);
Torok Edwin24c78352009-06-29 18:49:09 +0000503
504 // Block entry (label_entry_8)
505 CallInst* int32_9 = CallInst::Create(func_test1, "", label_entry_8);
506 int32_9->setCallingConv(CallingConv::C);
Bill Wendlinge94d8432012-12-07 23:16:57 +0000507 int32_9->setTailCall(false);AttributeSet int32_9_PAL;
Torok Edwin24c78352009-06-29 18:49:09 +0000508 int32_9->setAttributes(int32_9_PAL);
509
Mehdi Amini03b42e42016-04-14 21:59:01 +0000510 ReturnInst::Create(Context, int32_9, label_entry_8);
Torok Edwin24c78352009-06-29 18:49:09 +0000511 }
512
513 // Function: test4 (func_test4)
514 {
515 Function::arg_iterator args = func_test4->arg_begin();
Duncan P. N. Exon Smithc8925b12015-10-20 18:30:20 +0000516 Value *int1_f = &*args++;
Torok Edwin24c78352009-06-29 18:49:09 +0000517 int1_f->setName("f");
518
Mehdi Amini03b42e42016-04-14 21:59:01 +0000519 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 Edwin24c78352009-06-29 18:49:09 +0000527
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 Amini03b42e42016-04-14 21:59:01 +0000538 ReturnInst::Create(Context, label_return);
Torok Edwin24c78352009-06-29 18:49:09 +0000539 }
540 return mod;
541 }
542
543 }
544}
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000545
546INITIALIZE_PASS(ModuleNDM, "mndm", "mndm", false, false)
547INITIALIZE_PASS_BEGIN(CGPass, "cgp","cgp", false, false)
Chandler Carruth6378cf52013-11-26 04:19:30 +0000548INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000549INITIALIZE_PASS_END(CGPass, "cgp","cgp", false, false)
550INITIALIZE_PASS(FPass, "fp","fp", false, false)
551INITIALIZE_PASS_BEGIN(LPass, "lp","lp", false, false)
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000552INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000553INITIALIZE_PASS_END(LPass, "lp","lp", false, false)
554INITIALIZE_PASS(BPass, "bp","bp", false, false)