6 #include <gtest/gtest.h>
13 #include <llvm/IR/BasicBlock.h>
14 #include <llvm/IR/IRBuilder.h>
15 #include <llvm/IR/Module.h>
17 TEST(StoreTests, StoreConversion)
22 llvm::LLVMContext context;
23 std::unique_ptr<Module> llvmModule(
new Module(
"module", context));
25 auto int64Type = Type::getInt64Ty(context);
26 auto pointerType = llvm::PointerType::getUnqual(context);
27 auto voidType = Type::getVoidTy(context);
30 FunctionType::get(voidType, ArrayRef<Type *>({ int64Type, pointerType }),
false);
32 Function::Create(functionType, GlobalValue::ExternalLinkage,
"f", llvmModule.get());
33 auto valueArgument =
function->getArg(0);
34 auto addressArgument =
function->getArg(1);
36 auto basicBlock = BasicBlock::Create(context,
"BasicBlock",
function);
38 IRBuilder<> builder(basicBlock);
39 builder.CreateStore(valueArgument, addressArgument,
true);
40 builder.CreateStore(valueArgument, addressArgument,
false);
41 builder.CreateStore(valueArgument, addressArgument,
true);
42 builder.CreateRetVoid();
44 llvmModule->print(llvm::errs(),
nullptr);
48 print(*ipgModule, stdout);
54 auto controlFlowGraph =
55 dynamic_cast<const FunctionNode *
>(ipgModule->ipgraph().find(
"f"))->cfg();
59 size_t numStoreThreeAddressCodes = 0;
60 size_t numStoreVolatileThreeAddressCodes = 0;
61 for (
auto it = basicBlock->begin(); it != basicBlock->end(); it++)
63 if (is<StoreVolatileOperation>(*it))
65 numStoreVolatileThreeAddressCodes++;
66 auto ioStateAssignment = *std::next(it);
67 auto memoryStateAssignment = *std::next(it, 2);
69 EXPECT_TRUE(is<AssignmentOperation>(ioStateAssignment->operation()));
70 EXPECT_TRUE(is<IOStateType>(ioStateAssignment->operand(0)->type()));
72 EXPECT_TRUE(is<AssignmentOperation>(memoryStateAssignment->operation()));
73 EXPECT_TRUE(is<MemoryStateType>(memoryStateAssignment->operand(0)->type()));
75 else if (is<StoreNonVolatileOperation>(*it))
77 numStoreThreeAddressCodes++;
78 auto memoryStateAssignment = *std::next(it, 1);
80 EXPECT_TRUE(is<AssignmentOperation>(memoryStateAssignment->operation()));
81 EXPECT_TRUE(is<MemoryStateType>(memoryStateAssignment->operand(0)->type()));
85 EXPECT_EQ(numStoreThreeAddressCodes, 1u);
86 EXPECT_EQ(numStoreVolatileThreeAddressCodes, 2u);
TEST(StoreTests, StoreConversion)
Global memory state passed between functions.
void print(const AggregationNode &n, const AnnotationMap &dm, FILE *out)
std::unique_ptr< InterProceduralGraphModule > ConvertLlvmModule(::llvm::Module &llvmModule)