blob: 9f5f431a55850ff710e7e080212b3545e43f4c4b [file] [log] [blame]
Chandler Carruth49837ef2013-11-09 12:26:54 +00001//===- llvm/unittest/IR/LegacyPassManager.cpp - Legacy PassManager tests --===//
Torok Edwin1970a892009-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 Carruth49837ef2013-11-09 12:26:54 +00009//
10// This unit test exercises the legacy pass manager infrastructure. We use the
Chandler Carruth417c5c12015-02-13 10:01:29 +000011// old names as well to ensure that the source-level compatibility is preserved
12// where possible.
Chandler Carruth49837ef2013-11-09 12:26:54 +000013//
14//===----------------------------------------------------------------------===//
Torok Edwin1970a892009-06-29 18:49:09 +000015
Chandler Carruth417c5c12015-02-13 10:01:29 +000016#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth3251e812013-01-07 15:26:48 +000017#include "llvm/Analysis/CallGraphSCCPass.h"
Chandler Carruth5a88dda2012-12-04 10:23:08 +000018#include "llvm/Analysis/LoopInfo.h"
19#include "llvm/Analysis/LoopPass.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/BasicBlock.h"
21#include "llvm/IR/CallingConv.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/Function.h"
25#include "llvm/IR/GlobalVariable.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000026#include "llvm/IR/Instructions.h"
27#include "llvm/IR/LLVMContext.h"
28#include "llvm/IR/Module.h"
Fedor Sergeev46d5e2b2018-04-05 10:29:37 +000029#include "llvm/IR/OptBisect.h"
Chandler Carruth5a88dda2012-12-04 10:23:08 +000030#include "llvm/Pass.h"
31#include "llvm/Support/MathExtras.h"
32#include "llvm/Support/raw_ostream.h"
Torok Edwin1970a892009-06-29 18:49:09 +000033#include "gtest/gtest.h"
34
Owen Anderson081c34b2010-10-19 17:21:58 +000035using namespace llvm;
36
Torok Edwin1970a892009-06-29 18:49:09 +000037namespace llvm {
Owen Anderson081c34b2010-10-19 17:21:58 +000038 void initializeModuleNDMPass(PassRegistry&);
39 void initializeFPassPass(PassRegistry&);
40 void initializeCGPassPass(PassRegistry&);
41 void initializeLPassPass(PassRegistry&);
42 void initializeBPassPass(PassRegistry&);
Duncan Sandsf202c432011-03-31 09:58:51 +000043
Torok Edwin1970a892009-06-29 18:49:09 +000044 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 Anderson081c34b2010-10-19 17:21:58 +000051 ModuleNDNM() : ModulePass(ID) { }
Rafael Espindola5ab6b152014-09-10 15:50:08 +000052 bool runOnModule(Module &M) override {
Torok Edwin1970a892009-06-29 18:49:09 +000053 run++;
54 return false;
55 }
Rafael Espindola5ab6b152014-09-10 15:50:08 +000056 void getAnalysisUsage(AnalysisUsage &AU) const override {
Torok Edwin1970a892009-06-29 18:49:09 +000057 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 Anderson90c579d2010-08-06 18:33:48 +000067 ModuleNDM() : ModulePass(ID) {}
Rafael Espindola5ab6b152014-09-10 15:50:08 +000068 bool runOnModule(Module &M) override {
Torok Edwin1970a892009-06-29 18:49:09 +000069 run++;
70 return true;
71 }
72 };
73 char ModuleNDM::ID=0;
74 char ModuleNDM::run=0;
Torok Edwin1970a892009-06-29 18:49:09 +000075
76 struct ModuleNDM2 : public ModulePass {
77 public:
78 static char run;
79 static char ID;
Owen Anderson90c579d2010-08-06 18:33:48 +000080 ModuleNDM2() : ModulePass(ID) {}
Rafael Espindola5ab6b152014-09-10 15:50:08 +000081 bool runOnModule(Module &M) override {
Torok Edwin1970a892009-06-29 18:49:09 +000082 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 Anderson081c34b2010-10-19 17:21:58 +000093 ModuleDNM() : ModulePass(ID) {
94 initializeModuleNDMPass(*PassRegistry::getPassRegistry());
95 }
Rafael Espindola5ab6b152014-09-10 15:50:08 +000096 bool runOnModule(Module &M) override {
Torok Edwin1970a892009-06-29 18:49:09 +000097 run++;
98 return false;
99 }
Rafael Espindola5ab6b152014-09-10 15:50:08 +0000100 void getAnalysisUsage(AnalysisUsage &AU) const override {
Torok Edwin1970a892009-06-29 18:49:09 +0000101 AU.addRequired<ModuleNDM>();
102 AU.setPreservesAll();
103 }
104 };
105 char ModuleDNM::ID=0;
106 char ModuleDNM::run=0;
107
108 template<typename P>
109 struct PassTestBase : public P {
110 protected:
111 static int runc;
112 static bool initialized;
113 static bool finalized;
114 int allocated;
115 void run() {
Chandler Carruth48b17fa2010-07-13 17:28:05 +0000116 EXPECT_TRUE(initialized);
117 EXPECT_FALSE(finalized);
Torok Edwin1970a892009-06-29 18:49:09 +0000118 EXPECT_EQ(0, allocated);
119 allocated++;
120 runc++;
121 }
122 public:
123 static char ID;
124 static void finishedOK(int run) {
125 EXPECT_GT(runc, 0);
Chandler Carruth48b17fa2010-07-13 17:28:05 +0000126 EXPECT_TRUE(initialized);
127 EXPECT_TRUE(finalized);
Torok Edwin1970a892009-06-29 18:49:09 +0000128 EXPECT_EQ(run, runc);
129 }
Owen Anderson90c579d2010-08-06 18:33:48 +0000130 PassTestBase() : P(ID), allocated(0) {
Torok Edwin1970a892009-06-29 18:49:09 +0000131 initialized = false;
132 finalized = false;
133 runc = 0;
134 }
135
Rafael Espindola5ab6b152014-09-10 15:50:08 +0000136 void releaseMemory() override {
Torok Edwin1970a892009-06-29 18:49:09 +0000137 EXPECT_GT(runc, 0);
138 EXPECT_GT(allocated, 0);
139 allocated--;
140 }
141 };
142 template<typename P> char PassTestBase<P>::ID;
143 template<typename P> int PassTestBase<P>::runc;
144 template<typename P> bool PassTestBase<P>::initialized;
145 template<typename P> bool PassTestBase<P>::finalized;
146
147 template<typename T, typename P>
148 struct PassTest : public PassTestBase<P> {
149 public:
NAKAMURA Takumi4cd0a822012-12-04 07:25:24 +0000150#ifndef _MSC_VER // MSVC complains that Pass is not base class.
Matt Beaumont-Gayee721152012-12-04 05:41:27 +0000151 using llvm::Pass::doInitialization;
152 using llvm::Pass::doFinalization;
NAKAMURA Takumi4cd0a822012-12-04 07:25:24 +0000153#endif
Rafael Espindola5ab6b152014-09-10 15:50:08 +0000154 bool doInitialization(T &t) override {
Chandler Carruth48b17fa2010-07-13 17:28:05 +0000155 EXPECT_FALSE(PassTestBase<P>::initialized);
Torok Edwin1970a892009-06-29 18:49:09 +0000156 PassTestBase<P>::initialized = true;
157 return false;
158 }
Rafael Espindola5ab6b152014-09-10 15:50:08 +0000159 bool doFinalization(T &t) override {
Chandler Carruth48b17fa2010-07-13 17:28:05 +0000160 EXPECT_FALSE(PassTestBase<P>::finalized);
Torok Edwin1970a892009-06-29 18:49:09 +0000161 PassTestBase<P>::finalized = true;
162 EXPECT_EQ(0, PassTestBase<P>::allocated);
163 return false;
164 }
165 };
166
167 struct CGPass : public PassTest<CallGraph, CallGraphSCCPass> {
168 public:
Owen Anderson081c34b2010-10-19 17:21:58 +0000169 CGPass() {
170 initializeCGPassPass(*PassRegistry::getPassRegistry());
171 }
Rafael Espindola5ab6b152014-09-10 15:50:08 +0000172 bool runOnSCC(CallGraphSCC &SCMM) override {
Torok Edwin1970a892009-06-29 18:49:09 +0000173 run();
174 return false;
175 }
176 };
Torok Edwin1970a892009-06-29 18:49:09 +0000177
178 struct FPass : public PassTest<Module, FunctionPass> {
179 public:
Rafael Espindola5ab6b152014-09-10 15:50:08 +0000180 bool runOnFunction(Function &F) override {
Torok Edwin1970a892009-06-29 18:49:09 +0000181 // FIXME: PR4112
Micah Villmow791cfc22012-10-08 16:39:34 +0000182 // EXPECT_TRUE(getAnalysisIfAvailable<DataLayout>());
Torok Edwin1970a892009-06-29 18:49:09 +0000183 run();
184 return false;
185 }
186 };
Torok Edwin1970a892009-06-29 18:49:09 +0000187
188 struct LPass : public PassTestBase<LoopPass> {
189 private:
190 static int initcount;
191 static int fincount;
192 public:
193 LPass() {
Owen Anderson081c34b2010-10-19 17:21:58 +0000194 initializeLPassPass(*PassRegistry::getPassRegistry());
Torok Edwin1970a892009-06-29 18:49:09 +0000195 initcount = 0; fincount=0;
Chandler Carruth48b17fa2010-07-13 17:28:05 +0000196 EXPECT_FALSE(initialized);
Torok Edwin1970a892009-06-29 18:49:09 +0000197 }
198 static void finishedOK(int run, int finalized) {
199 PassTestBase<LoopPass>::finishedOK(run);
200 EXPECT_EQ(run, initcount);
201 EXPECT_EQ(finalized, fincount);
202 }
Matt Beaumont-Gayee721152012-12-04 05:41:27 +0000203 using llvm::Pass::doInitialization;
204 using llvm::Pass::doFinalization;
Rafael Espindola5ab6b152014-09-10 15:50:08 +0000205 bool doInitialization(Loop* L, LPPassManager &LPM) override {
Torok Edwin1970a892009-06-29 18:49:09 +0000206 initialized = true;
207 initcount++;
208 return false;
209 }
Rafael Espindola5ab6b152014-09-10 15:50:08 +0000210 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
Torok Edwin1970a892009-06-29 18:49:09 +0000211 run();
212 return false;
213 }
Rafael Espindola5ab6b152014-09-10 15:50:08 +0000214 bool doFinalization() override {
Torok Edwin1970a892009-06-29 18:49:09 +0000215 fincount++;
216 finalized = true;
217 return false;
218 }
219 };
220 int LPass::initcount=0;
221 int LPass::fincount=0;
Torok Edwin1970a892009-06-29 18:49:09 +0000222
223 struct BPass : public PassTestBase<BasicBlockPass> {
224 private:
225 static int inited;
226 static int fin;
227 public:
228 static void finishedOK(int run, int N) {
229 PassTestBase<BasicBlockPass>::finishedOK(run);
230 EXPECT_EQ(inited, N);
231 EXPECT_EQ(fin, N);
232 }
233 BPass() {
234 inited = 0;
235 fin = 0;
236 }
Rafael Espindola5ab6b152014-09-10 15:50:08 +0000237 bool doInitialization(Module &M) override {
Chandler Carruth48b17fa2010-07-13 17:28:05 +0000238 EXPECT_FALSE(initialized);
Torok Edwin1970a892009-06-29 18:49:09 +0000239 initialized = true;
240 return false;
241 }
Rafael Espindola5ab6b152014-09-10 15:50:08 +0000242 bool doInitialization(Function &F) override {
Torok Edwin1970a892009-06-29 18:49:09 +0000243 inited++;
244 return false;
245 }
Rafael Espindola5ab6b152014-09-10 15:50:08 +0000246 bool runOnBasicBlock(BasicBlock &BB) override {
Torok Edwin1970a892009-06-29 18:49:09 +0000247 run();
248 return false;
249 }
Rafael Espindola5ab6b152014-09-10 15:50:08 +0000250 bool doFinalization(Function &F) override {
Torok Edwin1970a892009-06-29 18:49:09 +0000251 fin++;
252 return false;
253 }
Rafael Espindola5ab6b152014-09-10 15:50:08 +0000254 bool doFinalization(Module &M) override {
Chandler Carruth48b17fa2010-07-13 17:28:05 +0000255 EXPECT_FALSE(finalized);
Torok Edwin1970a892009-06-29 18:49:09 +0000256 finalized = true;
257 EXPECT_EQ(0, allocated);
258 return false;
259 }
260 };
261 int BPass::inited=0;
262 int BPass::fin=0;
Torok Edwin1970a892009-06-29 18:49:09 +0000263
264 struct OnTheFlyTest: public ModulePass {
265 public:
266 static char ID;
Owen Anderson081c34b2010-10-19 17:21:58 +0000267 OnTheFlyTest() : ModulePass(ID) {
268 initializeFPassPass(*PassRegistry::getPassRegistry());
269 }
Rafael Espindola5ab6b152014-09-10 15:50:08 +0000270 bool runOnModule(Module &M) override {
Torok Edwin1970a892009-06-29 18:49:09 +0000271 for (Module::iterator I=M.begin(),E=M.end(); I != E; ++I) {
272 Function &F = *I;
273 {
274 SCOPED_TRACE("Running on the fly function pass");
275 getAnalysis<FPass>(F);
276 }
277 }
278 return false;
279 }
Rafael Espindola5ab6b152014-09-10 15:50:08 +0000280 void getAnalysisUsage(AnalysisUsage &AU) const override {
Torok Edwin1970a892009-06-29 18:49:09 +0000281 AU.addRequired<FPass>();
282 }
283 };
284 char OnTheFlyTest::ID=0;
285
286 TEST(PassManager, RunOnce) {
Mehdi Amini8be77072016-04-14 21:59:01 +0000287 LLVMContext Context;
288 Module M("test-once", Context);
Torok Edwin1970a892009-06-29 18:49:09 +0000289 struct ModuleNDNM *mNDNM = new ModuleNDNM();
290 struct ModuleDNM *mDNM = new ModuleDNM();
291 struct ModuleNDM *mNDM = new ModuleNDM();
292 struct ModuleNDM2 *mNDM2 = new ModuleNDM2();
293
294 mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
295
Chandler Carruth417c5c12015-02-13 10:01:29 +0000296 legacy::PassManager Passes;
Torok Edwin1970a892009-06-29 18:49:09 +0000297 Passes.add(mNDM2);
298 Passes.add(mNDM);
299 Passes.add(mNDNM);
300 Passes.add(mDNM);
301
302 Passes.run(M);
303 // each pass must be run exactly once, since nothing invalidates them
304 EXPECT_EQ(1, mNDM->run);
305 EXPECT_EQ(1, mNDNM->run);
306 EXPECT_EQ(1, mDNM->run);
307 EXPECT_EQ(1, mNDM2->run);
308 }
309
310 TEST(PassManager, ReRun) {
Mehdi Amini8be77072016-04-14 21:59:01 +0000311 LLVMContext Context;
312 Module M("test-rerun", Context);
Torok Edwin1970a892009-06-29 18:49:09 +0000313 struct ModuleNDNM *mNDNM = new ModuleNDNM();
314 struct ModuleDNM *mDNM = new ModuleDNM();
315 struct ModuleNDM *mNDM = new ModuleNDM();
316 struct ModuleNDM2 *mNDM2 = new ModuleNDM2();
317
318 mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
319
Chandler Carruth417c5c12015-02-13 10:01:29 +0000320 legacy::PassManager Passes;
Torok Edwin1970a892009-06-29 18:49:09 +0000321 Passes.add(mNDM);
322 Passes.add(mNDNM);
323 Passes.add(mNDM2);// invalidates mNDM needed by mDNM
324 Passes.add(mDNM);
325
326 Passes.run(M);
327 // Some passes must be rerun because a pass that modified the
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000328 // module/function was run in between
Torok Edwin1970a892009-06-29 18:49:09 +0000329 EXPECT_EQ(2, mNDM->run);
330 EXPECT_EQ(1, mNDNM->run);
331 EXPECT_EQ(1, mNDM2->run);
332 EXPECT_EQ(1, mDNM->run);
333 }
334
Mehdi Amini8be77072016-04-14 21:59:01 +0000335 Module *makeLLVMModule(LLVMContext &Context);
Torok Edwin1970a892009-06-29 18:49:09 +0000336
337 template<typename T>
338 void MemoryTestHelper(int run) {
Mehdi Amini8be77072016-04-14 21:59:01 +0000339 LLVMContext Context;
340 std::unique_ptr<Module> M(makeLLVMModule(Context));
Torok Edwin1970a892009-06-29 18:49:09 +0000341 T *P = new T();
Chandler Carruth417c5c12015-02-13 10:01:29 +0000342 legacy::PassManager Passes;
Torok Edwin1970a892009-06-29 18:49:09 +0000343 Passes.add(P);
344 Passes.run(*M);
345 T::finishedOK(run);
346 }
347
348 template<typename T>
349 void MemoryTestHelper(int run, int N) {
Mehdi Amini8be77072016-04-14 21:59:01 +0000350 LLVMContext Context;
351 Module *M = makeLLVMModule(Context);
Torok Edwin1970a892009-06-29 18:49:09 +0000352 T *P = new T();
Chandler Carruth417c5c12015-02-13 10:01:29 +0000353 legacy::PassManager Passes;
Torok Edwin1970a892009-06-29 18:49:09 +0000354 Passes.add(P);
355 Passes.run(*M);
356 T::finishedOK(run, N);
357 delete M;
358 }
359
360 TEST(PassManager, Memory) {
361 // SCC#1: test1->test2->test3->test1
362 // SCC#2: test4
363 // SCC#3: indirect call node
364 {
365 SCOPED_TRACE("Callgraph pass");
366 MemoryTestHelper<CGPass>(3);
367 }
368
369 {
370 SCOPED_TRACE("Function pass");
371 MemoryTestHelper<FPass>(4);// 4 functions
372 }
373
374 {
375 SCOPED_TRACE("Loop pass");
376 MemoryTestHelper<LPass>(2, 1); //2 loops, 1 function
377 }
378 {
379 SCOPED_TRACE("Basic block pass");
380 MemoryTestHelper<BPass>(7, 4); //9 basic blocks
381 }
382
383 }
384
385 TEST(PassManager, MemoryOnTheFly) {
Mehdi Amini8be77072016-04-14 21:59:01 +0000386 LLVMContext Context;
387 Module *M = makeLLVMModule(Context);
Torok Edwin1970a892009-06-29 18:49:09 +0000388 {
389 SCOPED_TRACE("Running OnTheFlyTest");
390 struct OnTheFlyTest *O = new OnTheFlyTest();
Chandler Carruth417c5c12015-02-13 10:01:29 +0000391 legacy::PassManager Passes;
Torok Edwin1970a892009-06-29 18:49:09 +0000392 Passes.add(O);
393 Passes.run(*M);
394
395 FPass::finishedOK(4);
396 }
397 delete M;
398 }
399
Fedor Sergeev46d5e2b2018-04-05 10:29:37 +0000400 // Skips or runs optional passes.
401 struct CustomOptPassGate : public OptPassGate {
402 bool Skip;
403 CustomOptPassGate(bool Skip) : Skip(Skip) { }
404 bool shouldRunPass(const Pass *P, const Module &U) { return !Skip; }
405 };
406
407 // Optional module pass.
408 struct ModuleOpt: public ModulePass {
409 char run = 0;
410 static char ID;
411 ModuleOpt() : ModulePass(ID) { }
412 bool runOnModule(Module &M) override {
413 if (!skipModule(M))
414 run++;
415 return false;
416 }
417 };
418 char ModuleOpt::ID=0;
419
420 TEST(PassManager, CustomOptPassGate) {
421 LLVMContext Context0;
422 LLVMContext Context1;
423 LLVMContext Context2;
424 CustomOptPassGate SkipOptionalPasses(true);
425 CustomOptPassGate RunOptionalPasses(false);
426
427 Module M0("custom-opt-bisect", Context0);
428 Module M1("custom-opt-bisect", Context1);
429 Module M2("custom-opt-bisect2", Context2);
430 struct ModuleOpt *mOpt0 = new ModuleOpt();
431 struct ModuleOpt *mOpt1 = new ModuleOpt();
432 struct ModuleOpt *mOpt2 = new ModuleOpt();
433
434 mOpt0->run = mOpt1->run = mOpt2->run = 0;
435
436 legacy::PassManager Passes0;
437 legacy::PassManager Passes1;
438 legacy::PassManager Passes2;
439
440 Passes0.add(mOpt0);
441 Passes1.add(mOpt1);
442 Passes2.add(mOpt2);
443
444 Context1.setOptPassGate(SkipOptionalPasses);
445 Context2.setOptPassGate(RunOptionalPasses);
446
447 Passes0.run(M0);
448 Passes1.run(M1);
449 Passes2.run(M2);
450
451 // By default optional passes are run.
452 EXPECT_EQ(1, mOpt0->run);
453
454 // The first context skips optional passes.
455 EXPECT_EQ(0, mOpt1->run);
456
457 // The second context runs optional passes.
458 EXPECT_EQ(1, mOpt2->run);
459 }
460
Mehdi Amini8be77072016-04-14 21:59:01 +0000461 Module *makeLLVMModule(LLVMContext &Context) {
Torok Edwin1970a892009-06-29 18:49:09 +0000462 // Module Construction
Mehdi Amini8be77072016-04-14 21:59:01 +0000463 Module *mod = new Module("test-mem", Context);
Torok Edwin1970a892009-06-29 18:49:09 +0000464 mod->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
465 "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
Rafael Espindolaf343bc92013-12-13 18:56:34 +0000466 "a:0:64-s:64:64-f80:128:128");
Torok Edwin1970a892009-06-29 18:49:09 +0000467 mod->setTargetTriple("x86_64-unknown-linux-gnu");
468
469 // Type Definitions
Jay Foad5fdd6c82011-07-12 14:06:48 +0000470 std::vector<Type*>FuncTy_0_args;
Mehdi Amini8be77072016-04-14 21:59:01 +0000471 FunctionType *FuncTy_0 = FunctionType::get(
472 /*Result=*/IntegerType::get(Context, 32),
473 /*Params=*/FuncTy_0_args,
474 /*isVarArg=*/false);
Torok Edwin1970a892009-06-29 18:49:09 +0000475
Jay Foad5fdd6c82011-07-12 14:06:48 +0000476 std::vector<Type*>FuncTy_2_args;
Mehdi Amini8be77072016-04-14 21:59:01 +0000477 FuncTy_2_args.push_back(IntegerType::get(Context, 1));
478 FunctionType *FuncTy_2 = FunctionType::get(
479 /*Result=*/Type::getVoidTy(Context),
480 /*Params=*/FuncTy_2_args,
481 /*isVarArg=*/false);
Torok Edwin1970a892009-06-29 18:49:09 +0000482
483 // Function Declarations
484
485 Function* func_test1 = Function::Create(
486 /*Type=*/FuncTy_0,
487 /*Linkage=*/GlobalValue::ExternalLinkage,
488 /*Name=*/"test1", mod);
489 func_test1->setCallingConv(CallingConv::C);
Reid Kleckner67077702017-03-21 16:57:19 +0000490 AttributeList func_test1_PAL;
Torok Edwin1970a892009-06-29 18:49:09 +0000491 func_test1->setAttributes(func_test1_PAL);
492
493 Function* func_test2 = Function::Create(
494 /*Type=*/FuncTy_0,
495 /*Linkage=*/GlobalValue::ExternalLinkage,
496 /*Name=*/"test2", mod);
497 func_test2->setCallingConv(CallingConv::C);
Reid Kleckner67077702017-03-21 16:57:19 +0000498 AttributeList func_test2_PAL;
Torok Edwin1970a892009-06-29 18:49:09 +0000499 func_test2->setAttributes(func_test2_PAL);
500
501 Function* func_test3 = Function::Create(
502 /*Type=*/FuncTy_0,
503 /*Linkage=*/GlobalValue::ExternalLinkage,
504 /*Name=*/"test3", mod);
505 func_test3->setCallingConv(CallingConv::C);
Reid Kleckner67077702017-03-21 16:57:19 +0000506 AttributeList func_test3_PAL;
Torok Edwin1970a892009-06-29 18:49:09 +0000507 func_test3->setAttributes(func_test3_PAL);
508
509 Function* func_test4 = Function::Create(
510 /*Type=*/FuncTy_2,
511 /*Linkage=*/GlobalValue::ExternalLinkage,
512 /*Name=*/"test4", mod);
513 func_test4->setCallingConv(CallingConv::C);
Reid Kleckner67077702017-03-21 16:57:19 +0000514 AttributeList func_test4_PAL;
Torok Edwin1970a892009-06-29 18:49:09 +0000515 func_test4->setAttributes(func_test4_PAL);
516
517 // Global Variable Declarations
518
519
520 // Constant Definitions
521
522 // Global Variable Definitions
523
524 // Function Definitions
525
526 // Function: test1 (func_test1)
527 {
528
Mehdi Amini8be77072016-04-14 21:59:01 +0000529 BasicBlock *label_entry =
530 BasicBlock::Create(Context, "entry", func_test1, nullptr);
Torok Edwin1970a892009-06-29 18:49:09 +0000531
532 // Block entry (label_entry)
533 CallInst* int32_3 = CallInst::Create(func_test2, "", label_entry);
534 int32_3->setCallingConv(CallingConv::C);
Reid Kleckner67077702017-03-21 16:57:19 +0000535 int32_3->setTailCall(false);
536 AttributeList int32_3_PAL;
Torok Edwin1970a892009-06-29 18:49:09 +0000537 int32_3->setAttributes(int32_3_PAL);
538
Mehdi Amini8be77072016-04-14 21:59:01 +0000539 ReturnInst::Create(Context, int32_3, label_entry);
Torok Edwin1970a892009-06-29 18:49:09 +0000540 }
541
542 // Function: test2 (func_test2)
543 {
544
Mehdi Amini8be77072016-04-14 21:59:01 +0000545 BasicBlock *label_entry_5 =
546 BasicBlock::Create(Context, "entry", func_test2, nullptr);
Torok Edwin1970a892009-06-29 18:49:09 +0000547
548 // Block entry (label_entry_5)
549 CallInst* int32_6 = CallInst::Create(func_test3, "", label_entry_5);
550 int32_6->setCallingConv(CallingConv::C);
Reid Kleckner67077702017-03-21 16:57:19 +0000551 int32_6->setTailCall(false);
552 AttributeList int32_6_PAL;
Torok Edwin1970a892009-06-29 18:49:09 +0000553 int32_6->setAttributes(int32_6_PAL);
554
Mehdi Amini8be77072016-04-14 21:59:01 +0000555 ReturnInst::Create(Context, int32_6, label_entry_5);
Torok Edwin1970a892009-06-29 18:49:09 +0000556 }
557
558 // Function: test3 (func_test3)
559 {
560
Mehdi Amini8be77072016-04-14 21:59:01 +0000561 BasicBlock *label_entry_8 =
562 BasicBlock::Create(Context, "entry", func_test3, nullptr);
Torok Edwin1970a892009-06-29 18:49:09 +0000563
564 // Block entry (label_entry_8)
565 CallInst* int32_9 = CallInst::Create(func_test1, "", label_entry_8);
566 int32_9->setCallingConv(CallingConv::C);
Reid Kleckner67077702017-03-21 16:57:19 +0000567 int32_9->setTailCall(false);
568 AttributeList int32_9_PAL;
Torok Edwin1970a892009-06-29 18:49:09 +0000569 int32_9->setAttributes(int32_9_PAL);
570
Mehdi Amini8be77072016-04-14 21:59:01 +0000571 ReturnInst::Create(Context, int32_9, label_entry_8);
Torok Edwin1970a892009-06-29 18:49:09 +0000572 }
573
574 // Function: test4 (func_test4)
575 {
576 Function::arg_iterator args = func_test4->arg_begin();
Duncan P. N. Exon Smith70f8c202015-10-20 18:30:20 +0000577 Value *int1_f = &*args++;
Torok Edwin1970a892009-06-29 18:49:09 +0000578 int1_f->setName("f");
579
Mehdi Amini8be77072016-04-14 21:59:01 +0000580 BasicBlock *label_entry_11 =
581 BasicBlock::Create(Context, "entry", func_test4, nullptr);
582 BasicBlock *label_bb =
583 BasicBlock::Create(Context, "bb", func_test4, nullptr);
584 BasicBlock *label_bb1 =
585 BasicBlock::Create(Context, "bb1", func_test4, nullptr);
586 BasicBlock *label_return =
587 BasicBlock::Create(Context, "return", func_test4, nullptr);
Torok Edwin1970a892009-06-29 18:49:09 +0000588
589 // Block entry (label_entry_11)
590 BranchInst::Create(label_bb, label_entry_11);
591
592 // Block bb (label_bb)
593 BranchInst::Create(label_bb, label_bb1, int1_f, label_bb);
594
595 // Block bb1 (label_bb1)
596 BranchInst::Create(label_bb1, label_return, int1_f, label_bb1);
597
598 // Block return (label_return)
Mehdi Amini8be77072016-04-14 21:59:01 +0000599 ReturnInst::Create(Context, label_return);
Torok Edwin1970a892009-06-29 18:49:09 +0000600 }
601 return mod;
602 }
603
604 }
605}
Owen Anderson081c34b2010-10-19 17:21:58 +0000606
607INITIALIZE_PASS(ModuleNDM, "mndm", "mndm", false, false)
608INITIALIZE_PASS_BEGIN(CGPass, "cgp","cgp", false, false)
Chandler Carruth54fec072013-11-26 04:19:30 +0000609INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Owen Anderson081c34b2010-10-19 17:21:58 +0000610INITIALIZE_PASS_END(CGPass, "cgp","cgp", false, false)
611INITIALIZE_PASS(FPass, "fp","fp", false, false)
612INITIALIZE_PASS_BEGIN(LPass, "lp","lp", false, false)
Chandler Carruthde5df292015-01-17 14:16:18 +0000613INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Owen Anderson081c34b2010-10-19 17:21:58 +0000614INITIALIZE_PASS_END(LPass, "lp","lp", false, false)
615INITIALIZE_PASS(BPass, "bp","bp", false, false)