47
47
using namespace llvm ;
48
48
49
49
static Function* createAdd1 (Module *M) {
50
+ LLVMContext &Context = M->getContext ();
50
51
// Create the add1 function entry and insert this entry into module M. The
51
52
// function will have a return type of "int" and take an argument of "int".
52
53
Function *Add1F =
@@ -56,10 +57,10 @@ static Function* createAdd1(Module *M) {
56
57
57
58
// Add a basic block to the function. As before, it automatically inserts
58
59
// because of the last argument.
59
- BasicBlock *BB = BasicBlock::Create (M-> getContext () , " EntryBlock" , Add1F);
60
+ BasicBlock *BB = BasicBlock::Create (Context , " EntryBlock" , Add1F);
60
61
61
62
// Get pointers to the constant `1'.
62
- Value *One = ConstantInt::get (Type::getInt32Ty (M-> getContext () ), 1 );
63
+ Value *One = ConstantInt::get (Type::getInt32Ty (Context ), 1 );
63
64
64
65
// Get pointers to the integer argument of the add1 function...
65
66
assert (Add1F->arg_begin () != Add1F->arg_end ()); // Make sure there's an arg
@@ -70,13 +71,14 @@ static Function* createAdd1(Module *M) {
70
71
Instruction *Add = BinaryOperator::CreateAdd (One, ArgX, " addresult" , BB);
71
72
72
73
// Create the return instruction and add it to the basic block
73
- ReturnInst::Create (M-> getContext () , Add, BB);
74
+ ReturnInst::Create (Context , Add, BB);
74
75
75
76
// Now, function add1 is ready.
76
77
return Add1F;
77
78
}
78
79
79
80
static Function *CreateFibFunction (Module *M) {
81
+ LLVMContext &Context = M->getContext ();
80
82
// Create the fib function and insert it into module M. This function is said
81
83
// to return an int and take an int parameter.
82
84
FunctionType *FibFTy = FunctionType::get (Type::getInt32Ty (Context),
@@ -85,27 +87,27 @@ static Function *CreateFibFunction(Module *M) {
85
87
Function::Create (FibFTy, Function::ExternalLinkage, " fib" , M);
86
88
87
89
// Add a basic block to the function.
88
- BasicBlock *BB = BasicBlock::Create (M-> getContext () , " EntryBlock" , FibF);
90
+ BasicBlock *BB = BasicBlock::Create (Context , " EntryBlock" , FibF);
89
91
90
92
// Get pointers to the constants.
91
- Value *One = ConstantInt::get (Type::getInt32Ty (M-> getContext () ), 1 );
92
- Value *Two = ConstantInt::get (Type::getInt32Ty (M-> getContext () ), 2 );
93
+ Value *One = ConstantInt::get (Type::getInt32Ty (Context ), 1 );
94
+ Value *Two = ConstantInt::get (Type::getInt32Ty (Context ), 2 );
93
95
94
96
// Get pointer to the integer argument of the add1 function...
95
97
Argument *ArgX = &*FibF->arg_begin (); // Get the arg.
96
98
ArgX->setName (" AnArg" ); // Give it a nice symbolic name for fun.
97
99
98
100
// Create the true_block.
99
- BasicBlock *RetBB = BasicBlock::Create (M-> getContext () , " return" , FibF);
101
+ BasicBlock *RetBB = BasicBlock::Create (Context , " return" , FibF);
100
102
// Create an exit block.
101
- BasicBlock* RecurseBB = BasicBlock::Create (M-> getContext () , " recurse" , FibF);
103
+ BasicBlock * RecurseBB = BasicBlock::Create (Context , " recurse" , FibF);
102
104
103
105
// Create the "if (arg < 2) goto exitbb"
104
106
Value *CondInst = new ICmpInst (*BB, ICmpInst::ICMP_SLE, ArgX, Two, " cond" );
105
107
BranchInst::Create (RetBB, RecurseBB, CondInst, BB);
106
108
107
109
// Create: ret int 1
108
- ReturnInst::Create (M-> getContext () , One, RetBB);
110
+ ReturnInst::Create (Context , One, RetBB);
109
111
110
112
// create fib(x-1)
111
113
Value *Sub = BinaryOperator::CreateSub (ArgX, One, " arg" , RecurseBB);
@@ -120,7 +122,7 @@ static Function *CreateFibFunction(Module *M) {
120
122
BinaryOperator::CreateAdd (CallFibX1, CallFibX2, " addresult" , RecurseBB);
121
123
122
124
// Create the return instruction and add it to the basic block
123
- ReturnInst::Create (M-> getContext () , Sum, RecurseBB);
125
+ ReturnInst::Create (Context , Sum, RecurseBB);
124
126
125
127
return FibF;
126
128
}
0 commit comments