6 #include <gtest/gtest.h>
16 #include <llvm/IR/BasicBlock.h>
17 #include <llvm/IR/DerivedTypes.h>
18 #include <llvm/IR/IRBuilder.h>
19 #include <llvm/IR/Module.h>
48 int vectorization = GetParam();
51 llvm::LLVMContext context;
52 llvm::Module llvmModule(
"module", context);
54 llvm::Type * int64Type = llvm::Type::getInt64Ty(context);
55 llvm::Type * int32Type = llvm::Type::getInt32Ty(context);
56 llvm::Type * pointerType = llvm::PointerType::getUnqual(context);
59 if (vectorization != 0)
61 auto ec = llvm::ElementCount::getFixed(vectorization);
62 int64Type = llvm::VectorType::get(int64Type, ec);
63 int32Type = llvm::VectorType::get(int32Type, ec);
64 pointerType = llvm::VectorType::get(pointerType, ec);
68 size_t sizeofX = 8 * std::max(1, vectorization);
69 auto ec = llvm::ElementCount::getFixed(sizeofX);
70 llvm::Type * byteVectorType = llvm::VectorType::get(llvm::Type::getInt8Ty(context), ec);
73 llvm::FunctionType::get(byteVectorType, llvm::ArrayRef<llvm::Type *>({ int32Type }),
false);
75 llvm::Function::Create(functionType, llvm::GlobalValue::ExternalLinkage,
"f", &llvmModule);
77 auto basicBlock = llvm::BasicBlock::Create(context,
"bb0",
function);
79 llvm::IRBuilder<> builder(basicBlock);
80 auto zext = builder.CreateZExt(function->arg_begin(), int64Type);
81 auto trunc = builder.CreateTrunc(zext, int32Type);
82 auto sext = builder.CreateSExt(trunc, int64Type);
83 auto inttoptr = builder.CreateIntToPtr(sext, pointerType);
84 auto ptrtoint = builder.CreatePtrToInt(inttoptr, int64Type);
85 auto bitcast = builder.CreateBitCast(ptrtoint, byteVectorType);
86 builder.CreateRet(bitcast);
88 llvmModule.print(llvm::errs(),
nullptr);
92 print(*ipgModule, stdout);
104 auto controlFlowGraph =
105 dynamic_cast<const FunctionNode *
>(ipgModule->ipgraph().find(
"f"))->cfg();
109 size_t numUnaryVector = 0;
113 size_t numInttoptr = 0;
114 size_t numPtrtoint = 0;
115 size_t numBitcasts = 0;
116 for (
auto it = basicBlock->begin(); it != basicBlock->end(); it++)
118 auto op = &(*it)->operation();
124 op = &vecOp->operation();
127 std::cout << op->debug_string() << std::endl;
141 EXPECT_EQ(*op.argument(0), *jlmBits32);
142 EXPECT_EQ(*op.result(0), *jlmBits64);
147 EXPECT_EQ(*op.argument(0), *jlmBits64);
148 EXPECT_EQ(*op.result(0), *jlmBits32);
153 EXPECT_EQ(*op.argument(0), *jlmBits32);
154 EXPECT_EQ(*op.result(0), *jlmBits64);
159 EXPECT_EQ(*op.argument(0), *jlmBits64);
160 EXPECT_EQ(*op.result(0), *jlmPointerType);
165 EXPECT_EQ(*op.argument(0), *jlmPointerType);
166 EXPECT_EQ(*op.result(0), *jlmBits64);
176 const auto jlmBits64Vector =
178 EXPECT_EQ(*op.argument(0), *jlmBits64Vector);
183 EXPECT_EQ(*op.argument(0), *jlmBits64);
185 EXPECT_EQ(*op.result(0), *jlmByteVectorType);
189 EXPECT_EQ(numUnaryVector, vectorization ? 5 : 0u);
190 EXPECT_EQ(numZext, 1u);
191 EXPECT_EQ(numTrunc, 1u);
192 EXPECT_EQ(numSext, 1u);
193 EXPECT_EQ(numInttoptr, 1u);
194 EXPECT_EQ(numPtrtoint, 1u);
195 EXPECT_EQ(numBitcasts, 1u);
200 LlvmFrontendCastingTests,
202 testing::Values(0, 1, 2, 4, 8));
TEST_P(LlvmBackendCastingFixture, AllIntegerCasts)
INSTANTIATE_TEST_SUITE_P(LlvmBackendCastingTests, LlvmBackendCastingFixture, testing::Values(0, 1, 2, 4, 8))
static std::shared_ptr< const FixedVectorType > Create(std::shared_ptr< const rvsdg::Type > type, size_t size)
static std::shared_ptr< const PointerType > Create()
UndefValueOperation class.
static std::shared_ptr< const BitType > Create(std::size_t nbits)
Creates bit type of specified width.
Global memory state passed between functions.
void print(const AggregationNode &n, const AnnotationMap &dm, FILE *out)
std::unique_ptr< InterProceduralGraphModule > ConvertLlvmModule(::llvm::Module &llvmModule)
void MatchTypeOrFail(T &obj, const Fns &... fns)
Pattern match over subclass type of given object.