15 #include <boost/thread/thread.hpp>
16 #include <gtest/gtest.h>
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/PassManager.h"
22 #include "llvm/IR/CallingConv.h"
23 #include "llvm/Analysis/Verifier.h"
24 #include "llvm/Assembly/PrintModulePass.h"
25 #include "llvm/IR/IRBuilder.h"
48 Module* mod =
new Module(
"test", *context);
49 Constant* c = mod->getOrInsertFunction(
"mul_add", IntegerType::get(*context, 32),
50 IntegerType::get(*context, 32), IntegerType::get(*context, 32),
51 IntegerType::get(*context, 32), NULL);
52 Function* mul_add = cast<Function>(c);
53 mul_add->setCallingConv(CallingConv::C);
54 Function::arg_iterator args = mul_add->arg_begin();
61 BasicBlock* block = BasicBlock::Create(*context,
"entry", mul_add);
63 Value* tmp = builder.CreateBinOp(Instruction::Mul, x, y,
"tmp");
64 Value* tmp2 = builder.CreateBinOp(Instruction::Add, tmp, z,
"tmp2");
65 builder.CreateRet(tmp2);
72 instruction_counter->
visit(*MulAddModule);
74 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::TOTAL_FUNCTIONS), 1);
75 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::TOTAL_INSTS), 3);
76 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::TERMINATOR_INSTS), 1);
77 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::MEMORY_INSTS), 0);
81 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::TOTAL_FUNCTIONS), 0);
82 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::TOTAL_INSTS), 0);
83 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::MEMORY_INSTS), 0);
111 Module* mod =
new Module(
"gcd", *context);
112 Constant* c = mod->getOrInsertFunction(
"gcd", IntegerType::get(*context, 32),
113 IntegerType::get(*context, 32), IntegerType::get(*context, 32), NULL);
114 Function* gcd = cast<Function>(c);
115 Function::arg_iterator args = gcd->arg_begin();
120 BasicBlock* entry = BasicBlock::Create(*context,
"entry", gcd);
121 BasicBlock* ret = BasicBlock::Create(*context,
"return", gcd);
122 BasicBlock* cond_false = BasicBlock::Create(*context,
"cond_false", gcd);
123 BasicBlock* cond_true = BasicBlock::Create(*context,
"cond_true", gcd);
124 BasicBlock* cond_false_2 = BasicBlock::Create(*context,
"cond_false", gcd);
126 Value* xEqualsY = builder.CreateICmpEQ(x, y,
"tmp");
127 builder.CreateCondBr(xEqualsY, ret, cond_false); builder.SetInsertPoint(ret);
128 builder.CreateRet(x);
129 builder.SetInsertPoint(cond_false);
130 Value* xLessThanY = builder.CreateICmpULT(x, y,
"tmp");
131 builder.CreateCondBr(xLessThanY, cond_true, cond_false_2);
132 builder.SetInsertPoint(cond_true);
133 Value* yMinusX = builder.CreateSub(y, x,
"tmp");
134 Value* args1[2] = {x , yMinusX};
135 Value* recur_1 = builder.CreateCall(gcd, args1,
"tmp");
136 builder.CreateRet(recur_1);
137 builder.SetInsertPoint(cond_false_2);
138 Value* xMinusY = builder.CreateSub(x, y,
"tmp");
139 Value* args2[2] = {xMinusY, y};
140 Value* recur_2 = builder.CreateCall(gcd, args2,
"tmp");
141 builder.CreateRet(recur_2);
146 Module* GcdModule =
CodegenGcd(&getGlobalContext());
148 instruction_counter->
visit(*GcdModule);
150 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::TOTAL_FUNCTIONS), 1);
151 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::TOTAL_BLOCKS), 5);
152 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::TOTAL_INSTS), 11);
154 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::TERMINATOR_INSTS), 5);
155 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::MEMORY_INSTS), 0);
156 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::OTHER_INSTS), 4);
160 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::TOTAL_FUNCTIONS), 0);
161 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::TOTAL_BLOCKS), 0);
162 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::TOTAL_INSTS), 0);
163 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::TERMINATOR_INSTS), 0);
164 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::MEMORY_INSTS), 0);
165 EXPECT_EQ(instruction_counter->
GetCount(InstructionCounter::OTHER_INSTS), 0);
170 int main(
int argc,
char **argv) {
171 ::testing::InitGoogleTest(&argc, argv);
172 return RUN_ALL_TESTS();
BigIntVal Count(FunctionContext *context)
TEST_F(InstructionCounterTest, TestMemInstrCount)
int GetCount(const char *name)
Return count of counter described by name.
void ResetCount()
Set all counts to 0.
Module * CodegenMulAdd(LLVMContext *context)
std::string PrintCounters() const
Prints all counters.
uint64_t Test(T *ht, const ProbeTuple *input, uint64_t num_tuples)
void visit(const llvm::Module &M)
Visits each Function in Module M.
Module * CodegenGcd(LLVMContext *context)
int main(int argc, char **argv)