17d449d31SJustin Bogner //===- OperationsTest.cpp - Tests for fuzzer operations -------------------===//
27d449d31SJustin Bogner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67d449d31SJustin Bogner //
77d449d31SJustin Bogner //===----------------------------------------------------------------------===//
87d449d31SJustin Bogner
97d449d31SJustin Bogner #include "llvm/FuzzMutate/Operations.h"
10e8a3475bSIgor Laevsky #include "llvm/AsmParser/Parser.h"
117d449d31SJustin Bogner #include "llvm/FuzzMutate/OpDescriptor.h"
127d449d31SJustin Bogner #include "llvm/IR/Constants.h"
137d449d31SJustin Bogner #include "llvm/IR/Instructions.h"
147d449d31SJustin Bogner #include "llvm/IR/Module.h"
157d449d31SJustin Bogner #include "llvm/IR/Verifier.h"
16e8a3475bSIgor Laevsky #include "llvm/Support/SourceMgr.h"
177d449d31SJustin Bogner #include "gmock/gmock.h"
187d449d31SJustin Bogner #include "gtest/gtest.h"
197d449d31SJustin Bogner #include <iostream>
207d449d31SJustin Bogner
217d449d31SJustin Bogner // Define some pretty printers to help with debugging failures.
227d449d31SJustin Bogner namespace llvm {
PrintTo(Type * T,::std::ostream * OS)237d449d31SJustin Bogner void PrintTo(Type *T, ::std::ostream *OS) {
247d449d31SJustin Bogner raw_os_ostream ROS(*OS);
257d449d31SJustin Bogner T->print(ROS);
267d449d31SJustin Bogner }
277d449d31SJustin Bogner
PrintTo(BasicBlock * BB,::std::ostream * OS)287d449d31SJustin Bogner void PrintTo(BasicBlock *BB, ::std::ostream *OS) {
297d449d31SJustin Bogner raw_os_ostream ROS(*OS);
307d449d31SJustin Bogner ROS << BB << " (" << BB->getName() << ")";
317d449d31SJustin Bogner }
327d449d31SJustin Bogner
PrintTo(Value * V,::std::ostream * OS)337d449d31SJustin Bogner void PrintTo(Value *V, ::std::ostream *OS) {
347d449d31SJustin Bogner raw_os_ostream ROS(*OS);
357d449d31SJustin Bogner ROS << V << " (";
367d449d31SJustin Bogner V->print(ROS);
377d449d31SJustin Bogner ROS << ")";
387d449d31SJustin Bogner }
PrintTo(Constant * C,::std::ostream * OS)397d449d31SJustin Bogner void PrintTo(Constant *C, ::std::ostream *OS) { PrintTo(cast<Value>(C), OS); }
407d449d31SJustin Bogner
417d449d31SJustin Bogner } // namespace llvm
427d449d31SJustin Bogner
437d449d31SJustin Bogner using namespace llvm;
447d449d31SJustin Bogner
457d449d31SJustin Bogner using testing::AllOf;
467d449d31SJustin Bogner using testing::AnyOf;
477d449d31SJustin Bogner using testing::ElementsAre;
487d449d31SJustin Bogner using testing::Eq;
497d449d31SJustin Bogner using testing::Ge;
507d449d31SJustin Bogner using testing::Each;
517d449d31SJustin Bogner using testing::Truly;
527d449d31SJustin Bogner using testing::NotNull;
537d449d31SJustin Bogner using testing::PrintToString;
547d449d31SJustin Bogner using testing::SizeIs;
557d449d31SJustin Bogner
56e8a3475bSIgor Laevsky namespace {
parseAssembly(const char * Assembly,LLVMContext & Context)57e8a3475bSIgor Laevsky std::unique_ptr<Module> parseAssembly(
58e8a3475bSIgor Laevsky const char *Assembly, LLVMContext &Context) {
59e8a3475bSIgor Laevsky
60e8a3475bSIgor Laevsky SMDiagnostic Error;
61e8a3475bSIgor Laevsky std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
62e8a3475bSIgor Laevsky
63e8a3475bSIgor Laevsky std::string ErrMsg;
64e8a3475bSIgor Laevsky raw_string_ostream OS(ErrMsg);
65e8a3475bSIgor Laevsky Error.print("", OS);
66e8a3475bSIgor Laevsky
67e8a3475bSIgor Laevsky assert(M && !verifyModule(*M, &errs()));
68e8a3475bSIgor Laevsky return M;
69e8a3475bSIgor Laevsky }
70e8a3475bSIgor Laevsky
717d449d31SJustin Bogner MATCHER_P(TypesMatch, V, "has type " + PrintToString(V->getType())) {
727d449d31SJustin Bogner return arg->getType() == V->getType();
737d449d31SJustin Bogner }
74e8a3475bSIgor Laevsky
757d449d31SJustin Bogner MATCHER_P(HasType, T, "") { return arg->getType() == T; }
767d449d31SJustin Bogner
TEST(OperationsTest,SourcePreds)777d449d31SJustin Bogner TEST(OperationsTest, SourcePreds) {
787d449d31SJustin Bogner using namespace llvm::fuzzerop;
797d449d31SJustin Bogner
807d449d31SJustin Bogner LLVMContext Ctx;
817d449d31SJustin Bogner
827d449d31SJustin Bogner Constant *i1 = ConstantInt::getFalse(Ctx);
837d449d31SJustin Bogner Constant *i8 = ConstantInt::get(Type::getInt8Ty(Ctx), 3);
847d449d31SJustin Bogner Constant *i16 = ConstantInt::get(Type::getInt16Ty(Ctx), 1 << 15);
857d449d31SJustin Bogner Constant *i32 = ConstantInt::get(Type::getInt32Ty(Ctx), 0);
867d449d31SJustin Bogner Constant *i64 = ConstantInt::get(Type::getInt64Ty(Ctx),
877d449d31SJustin Bogner std::numeric_limits<uint64_t>::max());
887d449d31SJustin Bogner Constant *f16 = ConstantFP::getInfinity(Type::getHalfTy(Ctx));
897d449d31SJustin Bogner Constant *f32 = ConstantFP::get(Type::getFloatTy(Ctx), 0.0);
907d449d31SJustin Bogner Constant *f64 = ConstantFP::get(Type::getDoubleTy(Ctx), 123.45);
917d449d31SJustin Bogner Constant *s =
927d449d31SJustin Bogner ConstantStruct::get(StructType::create(Ctx, "OpaqueStruct"));
937d449d31SJustin Bogner Constant *a =
947d449d31SJustin Bogner ConstantArray::get(ArrayType::get(i32->getType(), 2), {i32, i32});
95*a407ec9bSMehdi Amini Constant *v8i8 = ConstantVector::getSplat(ElementCount::getFixed(8), i8);
96*a407ec9bSMehdi Amini Constant *v4f16 = ConstantVector::getSplat(ElementCount::getFixed(4), f16);
977d449d31SJustin Bogner Constant *p0i32 =
987d449d31SJustin Bogner ConstantPointerNull::get(PointerType::get(i32->getType(), 0));
997d449d31SJustin Bogner
1007d449d31SJustin Bogner auto OnlyI32 = onlyType(i32->getType());
1017d449d31SJustin Bogner EXPECT_TRUE(OnlyI32.matches({}, i32));
1027d449d31SJustin Bogner EXPECT_FALSE(OnlyI32.matches({}, i64));
1037d449d31SJustin Bogner EXPECT_FALSE(OnlyI32.matches({}, p0i32));
1047d449d31SJustin Bogner EXPECT_FALSE(OnlyI32.matches({}, a));
1057d449d31SJustin Bogner
1067d449d31SJustin Bogner EXPECT_THAT(OnlyI32.generate({}, {}),
1077d449d31SJustin Bogner AllOf(SizeIs(Ge(1u)), Each(TypesMatch(i32))));
1087d449d31SJustin Bogner
1097d449d31SJustin Bogner auto AnyType = anyType();
1107d449d31SJustin Bogner EXPECT_TRUE(AnyType.matches({}, i1));
1117d449d31SJustin Bogner EXPECT_TRUE(AnyType.matches({}, f64));
1127d449d31SJustin Bogner EXPECT_TRUE(AnyType.matches({}, s));
1137d449d31SJustin Bogner EXPECT_TRUE(AnyType.matches({}, v8i8));
1147d449d31SJustin Bogner EXPECT_TRUE(AnyType.matches({}, p0i32));
1157d449d31SJustin Bogner
1167d449d31SJustin Bogner EXPECT_THAT(
1177d449d31SJustin Bogner AnyType.generate({}, {i32->getType(), f16->getType(), v8i8->getType()}),
1187d449d31SJustin Bogner Each(AnyOf(TypesMatch(i32), TypesMatch(f16), TypesMatch(v8i8))));
1197d449d31SJustin Bogner
1207d449d31SJustin Bogner auto AnyInt = anyIntType();
1217d449d31SJustin Bogner EXPECT_TRUE(AnyInt.matches({}, i1));
1227d449d31SJustin Bogner EXPECT_TRUE(AnyInt.matches({}, i64));
1237d449d31SJustin Bogner EXPECT_FALSE(AnyInt.matches({}, f32));
1247d449d31SJustin Bogner EXPECT_FALSE(AnyInt.matches({}, v4f16));
1257d449d31SJustin Bogner
1267d449d31SJustin Bogner EXPECT_THAT(
1277d449d31SJustin Bogner AnyInt.generate({}, {i32->getType(), f16->getType(), v8i8->getType()}),
1287d449d31SJustin Bogner AllOf(SizeIs(Ge(1u)), Each(TypesMatch(i32))));
1297d449d31SJustin Bogner
1307d449d31SJustin Bogner auto AnyFP = anyFloatType();
1317d449d31SJustin Bogner EXPECT_TRUE(AnyFP.matches({}, f16));
1327d449d31SJustin Bogner EXPECT_TRUE(AnyFP.matches({}, f32));
1337d449d31SJustin Bogner EXPECT_FALSE(AnyFP.matches({}, i16));
1347d449d31SJustin Bogner EXPECT_FALSE(AnyFP.matches({}, p0i32));
1357d449d31SJustin Bogner EXPECT_FALSE(AnyFP.matches({}, v4f16));
1367d449d31SJustin Bogner
1377d449d31SJustin Bogner EXPECT_THAT(
1387d449d31SJustin Bogner AnyFP.generate({}, {i32->getType(), f16->getType(), v8i8->getType()}),
1397d449d31SJustin Bogner AllOf(SizeIs(Ge(1u)), Each(TypesMatch(f16))));
1407d449d31SJustin Bogner
1417d449d31SJustin Bogner auto AnyPtr = anyPtrType();
1427d449d31SJustin Bogner EXPECT_TRUE(AnyPtr.matches({}, p0i32));
1437d449d31SJustin Bogner EXPECT_FALSE(AnyPtr.matches({}, i8));
1447d449d31SJustin Bogner EXPECT_FALSE(AnyPtr.matches({}, a));
1457d449d31SJustin Bogner EXPECT_FALSE(AnyPtr.matches({}, v8i8));
1467d449d31SJustin Bogner
1477d449d31SJustin Bogner auto isPointer = [](Value *V) { return V->getType()->isPointerTy(); };
1487d449d31SJustin Bogner EXPECT_THAT(
1497d449d31SJustin Bogner AnyPtr.generate({}, {i32->getType(), f16->getType(), v8i8->getType()}),
1507d449d31SJustin Bogner AllOf(SizeIs(Ge(3u)), Each(Truly(isPointer))));
1517d449d31SJustin Bogner
1527d449d31SJustin Bogner auto AnyVec = anyVectorType();
1537d449d31SJustin Bogner EXPECT_TRUE(AnyVec.matches({}, v8i8));
1547d449d31SJustin Bogner EXPECT_TRUE(AnyVec.matches({}, v4f16));
1557d449d31SJustin Bogner EXPECT_FALSE(AnyVec.matches({}, i8));
1567d449d31SJustin Bogner EXPECT_FALSE(AnyVec.matches({}, a));
1577d449d31SJustin Bogner EXPECT_FALSE(AnyVec.matches({}, s));
1587d449d31SJustin Bogner
1597d449d31SJustin Bogner EXPECT_THAT(AnyVec.generate({}, {v8i8->getType()}),
1607d449d31SJustin Bogner ElementsAre(TypesMatch(v8i8)));
1617d449d31SJustin Bogner
1627d449d31SJustin Bogner auto First = matchFirstType();
1637d449d31SJustin Bogner EXPECT_TRUE(First.matches({i8}, i8));
1647d449d31SJustin Bogner EXPECT_TRUE(First.matches({s, a}, s));
1657d449d31SJustin Bogner EXPECT_FALSE(First.matches({f16}, f32));
1667d449d31SJustin Bogner EXPECT_FALSE(First.matches({v4f16, f64}, f64));
1677d449d31SJustin Bogner
1687d449d31SJustin Bogner EXPECT_THAT(First.generate({i8}, {}), Each(TypesMatch(i8)));
1697d449d31SJustin Bogner EXPECT_THAT(First.generate({f16}, {i8->getType()}),
1707d449d31SJustin Bogner Each(TypesMatch(f16)));
1717d449d31SJustin Bogner EXPECT_THAT(First.generate({v8i8, i32}, {}), Each(TypesMatch(v8i8)));
1727d449d31SJustin Bogner }
1737d449d31SJustin Bogner
TEST(OperationsTest,SplitBlock)1747d449d31SJustin Bogner TEST(OperationsTest, SplitBlock) {
1757d449d31SJustin Bogner LLVMContext Ctx;
1767d449d31SJustin Bogner
1777d449d31SJustin Bogner Module M("M", Ctx);
1787d449d31SJustin Bogner Function *F = Function::Create(FunctionType::get(Type::getVoidTy(Ctx), {},
1797d449d31SJustin Bogner /*isVarArg=*/false),
1807d449d31SJustin Bogner GlobalValue::ExternalLinkage, "f", &M);
1817d449d31SJustin Bogner auto SBOp = fuzzerop::splitBlockDescriptor(1);
1827d449d31SJustin Bogner
1837d449d31SJustin Bogner // Create a block with only a return and split it on the return.
1847d449d31SJustin Bogner auto *BB = BasicBlock::Create(Ctx, "BB", F);
1857d449d31SJustin Bogner auto *RI = ReturnInst::Create(Ctx, BB);
1867d449d31SJustin Bogner SBOp.BuilderFunc({UndefValue::get(Type::getInt1Ty(Ctx))}, RI);
1877d449d31SJustin Bogner
1887d449d31SJustin Bogner // We should end up with an unconditional branch from BB to BB1, and the
1897d449d31SJustin Bogner // return ends up in BB1.
1907d449d31SJustin Bogner auto *UncondBr = cast<BranchInst>(BB->getTerminator());
1917d449d31SJustin Bogner ASSERT_TRUE(UncondBr->isUnconditional());
1927d449d31SJustin Bogner auto *BB1 = UncondBr->getSuccessor(0);
1937d449d31SJustin Bogner ASSERT_THAT(RI->getParent(), Eq(BB1));
1947d449d31SJustin Bogner
1957d449d31SJustin Bogner // Now add an instruction to BB1 and split on that.
1967d449d31SJustin Bogner auto *AI = new AllocaInst(Type::getInt8Ty(Ctx), 0, "a", RI);
1977d449d31SJustin Bogner Value *Cond = ConstantInt::getFalse(Ctx);
1987d449d31SJustin Bogner SBOp.BuilderFunc({Cond}, AI);
1997d449d31SJustin Bogner
2007d449d31SJustin Bogner // We should end up with a loop back on BB1 and the instruction we split on
2017d449d31SJustin Bogner // moves to BB2.
2027d449d31SJustin Bogner auto *CondBr = cast<BranchInst>(BB1->getTerminator());
2037d449d31SJustin Bogner EXPECT_THAT(CondBr->getCondition(), Eq(Cond));
2047d449d31SJustin Bogner ASSERT_THAT(CondBr->getNumSuccessors(), Eq(2u));
2057d449d31SJustin Bogner ASSERT_THAT(CondBr->getSuccessor(0), Eq(BB1));
2067d449d31SJustin Bogner auto *BB2 = CondBr->getSuccessor(1);
2077d449d31SJustin Bogner EXPECT_THAT(AI->getParent(), Eq(BB2));
2087d449d31SJustin Bogner EXPECT_THAT(RI->getParent(), Eq(BB2));
2097d449d31SJustin Bogner
2107d449d31SJustin Bogner EXPECT_FALSE(verifyModule(M, &errs()));
2117d449d31SJustin Bogner }
2127d449d31SJustin Bogner
TEST(OperationsTest,SplitEHBlock)213541f9707SIgor Laevsky TEST(OperationsTest, SplitEHBlock) {
214541f9707SIgor Laevsky // Check that we will not try to branch back to the landingpad block using
215541f9707SIgor Laevsky // regular branch instruction
216541f9707SIgor Laevsky
217541f9707SIgor Laevsky LLVMContext Ctx;
218541f9707SIgor Laevsky const char *SourceCode =
219541f9707SIgor Laevsky "declare i32* @f()"
220541f9707SIgor Laevsky "declare i32 @personality_function()"
221541f9707SIgor Laevsky "define i32* @test() personality i32 ()* @personality_function {\n"
222541f9707SIgor Laevsky "entry:\n"
223541f9707SIgor Laevsky " %val = invoke i32* @f()\n"
224541f9707SIgor Laevsky " to label %normal unwind label %exceptional\n"
225541f9707SIgor Laevsky "normal:\n"
226541f9707SIgor Laevsky " ret i32* %val\n"
227541f9707SIgor Laevsky "exceptional:\n"
228541f9707SIgor Laevsky " %landing_pad4 = landingpad token cleanup\n"
229541f9707SIgor Laevsky " ret i32* undef\n"
230541f9707SIgor Laevsky "}";
231541f9707SIgor Laevsky auto M = parseAssembly(SourceCode, Ctx);
232541f9707SIgor Laevsky
233541f9707SIgor Laevsky // Get the landingpad block
234541f9707SIgor Laevsky BasicBlock &BB = *std::next(M->getFunction("test")->begin(), 2);
235541f9707SIgor Laevsky
236541f9707SIgor Laevsky fuzzerop::OpDescriptor Descr = fuzzerop::splitBlockDescriptor(1);
237541f9707SIgor Laevsky
238541f9707SIgor Laevsky Descr.BuilderFunc({ConstantInt::getTrue(Ctx)},&*BB.getFirstInsertionPt());
239541f9707SIgor Laevsky ASSERT_TRUE(!verifyModule(*M, &errs()));
240541f9707SIgor Laevsky }
241541f9707SIgor Laevsky
TEST(OperationsTest,SplitBlockWithPhis)2427d449d31SJustin Bogner TEST(OperationsTest, SplitBlockWithPhis) {
2437d449d31SJustin Bogner LLVMContext Ctx;
2447d449d31SJustin Bogner
2457d449d31SJustin Bogner Type *Int8Ty = Type::getInt8Ty(Ctx);
2467d449d31SJustin Bogner
2477d449d31SJustin Bogner Module M("M", Ctx);
2487d449d31SJustin Bogner Function *F = Function::Create(FunctionType::get(Type::getVoidTy(Ctx), {},
2497d449d31SJustin Bogner /*isVarArg=*/false),
2507d449d31SJustin Bogner GlobalValue::ExternalLinkage, "f", &M);
2517d449d31SJustin Bogner auto SBOp = fuzzerop::splitBlockDescriptor(1);
2527d449d31SJustin Bogner
2537d449d31SJustin Bogner // Create 3 blocks with an if-then branch.
2547d449d31SJustin Bogner auto *BB1 = BasicBlock::Create(Ctx, "BB1", F);
2557d449d31SJustin Bogner auto *BB2 = BasicBlock::Create(Ctx, "BB2", F);
2567d449d31SJustin Bogner auto *BB3 = BasicBlock::Create(Ctx, "BB3", F);
2577d449d31SJustin Bogner BranchInst::Create(BB2, BB3, ConstantInt::getFalse(Ctx), BB1);
2587d449d31SJustin Bogner BranchInst::Create(BB3, BB2);
2597d449d31SJustin Bogner
2607d449d31SJustin Bogner // Set up phi nodes selecting values for the incoming edges.
2617d449d31SJustin Bogner auto *PHI1 = PHINode::Create(Int8Ty, /*NumReservedValues=*/2, "p1", BB3);
2627d449d31SJustin Bogner PHI1->addIncoming(ConstantInt::get(Int8Ty, 0), BB1);
2637d449d31SJustin Bogner PHI1->addIncoming(ConstantInt::get(Int8Ty, 1), BB2);
2647d449d31SJustin Bogner auto *PHI2 = PHINode::Create(Int8Ty, /*NumReservedValues=*/2, "p2", BB3);
2657d449d31SJustin Bogner PHI2->addIncoming(ConstantInt::get(Int8Ty, 1), BB1);
2667d449d31SJustin Bogner PHI2->addIncoming(ConstantInt::get(Int8Ty, 0), BB2);
2677d449d31SJustin Bogner auto *RI = ReturnInst::Create(Ctx, BB3);
2687d449d31SJustin Bogner
2697d449d31SJustin Bogner // Now we split the block with PHI nodes, making sure they're all updated.
2707d449d31SJustin Bogner Value *Cond = ConstantInt::getFalse(Ctx);
2717d449d31SJustin Bogner SBOp.BuilderFunc({Cond}, RI);
2727d449d31SJustin Bogner
2737d449d31SJustin Bogner // Make sure the PHIs are updated with a value for the third incoming edge.
2747d449d31SJustin Bogner EXPECT_THAT(PHI1->getNumIncomingValues(), Eq(3u));
2757d449d31SJustin Bogner EXPECT_THAT(PHI2->getNumIncomingValues(), Eq(3u));
2767d449d31SJustin Bogner EXPECT_FALSE(verifyModule(M, &errs()));
2777d449d31SJustin Bogner }
2787d449d31SJustin Bogner
TEST(OperationsTest,GEP)2797d449d31SJustin Bogner TEST(OperationsTest, GEP) {
2807d449d31SJustin Bogner LLVMContext Ctx;
2817d449d31SJustin Bogner
2827d449d31SJustin Bogner Type *Int8PtrTy = Type::getInt8PtrTy(Ctx);
2837d449d31SJustin Bogner Type *Int32Ty = Type::getInt32Ty(Ctx);
2847d449d31SJustin Bogner
2857d449d31SJustin Bogner Module M("M", Ctx);
2867d449d31SJustin Bogner Function *F = Function::Create(FunctionType::get(Type::getVoidTy(Ctx), {},
2877d449d31SJustin Bogner /*isVarArg=*/false),
2887d449d31SJustin Bogner GlobalValue::ExternalLinkage, "f", &M);
2897d449d31SJustin Bogner auto *BB = BasicBlock::Create(Ctx, "BB", F);
2907d449d31SJustin Bogner auto *RI = ReturnInst::Create(Ctx, BB);
2917d449d31SJustin Bogner
2927d449d31SJustin Bogner auto GEPOp = fuzzerop::gepDescriptor(1);
2937d449d31SJustin Bogner EXPECT_TRUE(GEPOp.SourcePreds[0].matches({}, UndefValue::get(Int8PtrTy)));
2947d449d31SJustin Bogner EXPECT_TRUE(GEPOp.SourcePreds[1].matches({UndefValue::get(Int8PtrTy)},
2957d449d31SJustin Bogner ConstantInt::get(Int32Ty, 0)));
2967d449d31SJustin Bogner
2977d449d31SJustin Bogner GEPOp.BuilderFunc({UndefValue::get(Int8PtrTy), ConstantInt::get(Int32Ty, 0)},
2987d449d31SJustin Bogner RI);
2997d449d31SJustin Bogner EXPECT_FALSE(verifyModule(M, &errs()));
3007d449d31SJustin Bogner }
3017d449d31SJustin Bogner
302e8a3475bSIgor Laevsky
TEST(OperationsTest,GEPPointerOperand)303e8a3475bSIgor Laevsky TEST(OperationsTest, GEPPointerOperand) {
304e8a3475bSIgor Laevsky // Check that we only pick sized pointers for the GEP instructions
305e8a3475bSIgor Laevsky
306e8a3475bSIgor Laevsky LLVMContext Ctx;
307e8a3475bSIgor Laevsky const char *SourceCode =
308e8a3475bSIgor Laevsky "declare void @f()\n"
309e8a3475bSIgor Laevsky "define void @test() {\n"
310e8a3475bSIgor Laevsky " %v = bitcast void ()* @f to i64 (i8 addrspace(4)*)*\n"
311e8a3475bSIgor Laevsky " %a = alloca i64, i32 10\n"
312e8a3475bSIgor Laevsky " ret void\n"
313e8a3475bSIgor Laevsky "}";
314e8a3475bSIgor Laevsky auto M = parseAssembly(SourceCode, Ctx);
315e8a3475bSIgor Laevsky
316e8a3475bSIgor Laevsky fuzzerop::OpDescriptor Descr = fuzzerop::gepDescriptor(1);
317e8a3475bSIgor Laevsky
318e8a3475bSIgor Laevsky // Get first basic block of the test function
319e8a3475bSIgor Laevsky Function &F = *M->getFunction("test");
320e8a3475bSIgor Laevsky BasicBlock &BB = *F.begin();
321e8a3475bSIgor Laevsky
322e8a3475bSIgor Laevsky // Don't match %v
323e8a3475bSIgor Laevsky ASSERT_FALSE(Descr.SourcePreds[0].matches({}, &*BB.begin()));
324e8a3475bSIgor Laevsky
325e8a3475bSIgor Laevsky // Match %a
326e8a3475bSIgor Laevsky ASSERT_TRUE(Descr.SourcePreds[0].matches({}, &*std::next(BB.begin())));
327e8a3475bSIgor Laevsky }
328e8a3475bSIgor Laevsky
TEST(OperationsTest,ExtractAndInsertValue)3297d449d31SJustin Bogner TEST(OperationsTest, ExtractAndInsertValue) {
3307d449d31SJustin Bogner LLVMContext Ctx;
3317d449d31SJustin Bogner
3327d449d31SJustin Bogner Type *Int8PtrTy = Type::getInt8PtrTy(Ctx);
3337d449d31SJustin Bogner Type *Int32Ty = Type::getInt32Ty(Ctx);
3347d449d31SJustin Bogner Type *Int64Ty = Type::getInt64Ty(Ctx);
3357d449d31SJustin Bogner
3367d449d31SJustin Bogner Type *StructTy = StructType::create(Ctx, {Int8PtrTy, Int32Ty});
3377d449d31SJustin Bogner Type *OpaqueTy = StructType::create(Ctx, "OpaqueStruct");
338f39a2926SIgor Laevsky Type *ZeroSizedArrayTy = ArrayType::get(Int64Ty, 0);
3397d449d31SJustin Bogner Type *ArrayTy = ArrayType::get(Int64Ty, 4);
340eb81c85aSChristopher Tetreault Type *VectorTy = FixedVectorType::get(Int32Ty, 2);
3417d449d31SJustin Bogner
3427d449d31SJustin Bogner auto EVOp = fuzzerop::extractValueDescriptor(1);
3437d449d31SJustin Bogner auto IVOp = fuzzerop::insertValueDescriptor(1);
3447d449d31SJustin Bogner
3457d449d31SJustin Bogner // Sanity check the source preds.
3467d449d31SJustin Bogner Constant *SVal = UndefValue::get(StructTy);
3477d449d31SJustin Bogner Constant *OVal = UndefValue::get(OpaqueTy);
3487d449d31SJustin Bogner Constant *AVal = UndefValue::get(ArrayTy);
349f39a2926SIgor Laevsky Constant *ZAVal = UndefValue::get(ZeroSizedArrayTy);
3507d449d31SJustin Bogner Constant *VVal = UndefValue::get(VectorTy);
3517d449d31SJustin Bogner
3527d449d31SJustin Bogner EXPECT_TRUE(EVOp.SourcePreds[0].matches({}, SVal));
353f39a2926SIgor Laevsky EXPECT_FALSE(EVOp.SourcePreds[0].matches({}, OVal));
3547d449d31SJustin Bogner EXPECT_TRUE(EVOp.SourcePreds[0].matches({}, AVal));
3557d449d31SJustin Bogner EXPECT_FALSE(EVOp.SourcePreds[0].matches({}, VVal));
3567d449d31SJustin Bogner EXPECT_TRUE(IVOp.SourcePreds[0].matches({}, SVal));
357f39a2926SIgor Laevsky EXPECT_FALSE(IVOp.SourcePreds[0].matches({}, OVal));
3587d449d31SJustin Bogner EXPECT_TRUE(IVOp.SourcePreds[0].matches({}, AVal));
3597d449d31SJustin Bogner EXPECT_FALSE(IVOp.SourcePreds[0].matches({}, VVal));
3607d449d31SJustin Bogner
361f39a2926SIgor Laevsky // Don't consider zero sized arrays as viable sources
362f39a2926SIgor Laevsky EXPECT_FALSE(EVOp.SourcePreds[0].matches({}, ZAVal));
363f39a2926SIgor Laevsky EXPECT_FALSE(IVOp.SourcePreds[0].matches({}, ZAVal));
364f39a2926SIgor Laevsky
3657d449d31SJustin Bogner // Make sure we're range checking appropriately.
3667d449d31SJustin Bogner EXPECT_TRUE(
3677d449d31SJustin Bogner EVOp.SourcePreds[1].matches({SVal}, ConstantInt::get(Int32Ty, 0)));
3687d449d31SJustin Bogner EXPECT_TRUE(
3697d449d31SJustin Bogner EVOp.SourcePreds[1].matches({SVal}, ConstantInt::get(Int32Ty, 1)));
3707d449d31SJustin Bogner EXPECT_FALSE(
3717d449d31SJustin Bogner EVOp.SourcePreds[1].matches({SVal}, ConstantInt::get(Int32Ty, 2)));
3727d449d31SJustin Bogner EXPECT_FALSE(
3737d449d31SJustin Bogner EVOp.SourcePreds[1].matches({OVal}, ConstantInt::get(Int32Ty, 0)));
3747d449d31SJustin Bogner EXPECT_FALSE(
3757d449d31SJustin Bogner EVOp.SourcePreds[1].matches({OVal}, ConstantInt::get(Int32Ty, 65536)));
3767d449d31SJustin Bogner EXPECT_TRUE(
3777d449d31SJustin Bogner EVOp.SourcePreds[1].matches({AVal}, ConstantInt::get(Int32Ty, 0)));
3787d449d31SJustin Bogner EXPECT_TRUE(
3797d449d31SJustin Bogner EVOp.SourcePreds[1].matches({AVal}, ConstantInt::get(Int32Ty, 3)));
3807d449d31SJustin Bogner EXPECT_FALSE(
3817d449d31SJustin Bogner EVOp.SourcePreds[1].matches({AVal}, ConstantInt::get(Int32Ty, 4)));
3827d449d31SJustin Bogner
3837d449d31SJustin Bogner EXPECT_THAT(
3847d449d31SJustin Bogner EVOp.SourcePreds[1].generate({SVal}, {}),
3857d449d31SJustin Bogner ElementsAre(ConstantInt::get(Int32Ty, 0), ConstantInt::get(Int32Ty, 1)));
3867d449d31SJustin Bogner
3877d449d31SJustin Bogner // InsertValue should accept any type in the struct, but only in positions
3887d449d31SJustin Bogner // where it makes sense.
3897d449d31SJustin Bogner EXPECT_TRUE(IVOp.SourcePreds[1].matches({SVal}, UndefValue::get(Int8PtrTy)));
3907d449d31SJustin Bogner EXPECT_TRUE(IVOp.SourcePreds[1].matches({SVal}, UndefValue::get(Int32Ty)));
3917d449d31SJustin Bogner EXPECT_FALSE(IVOp.SourcePreds[1].matches({SVal}, UndefValue::get(Int64Ty)));
3927d449d31SJustin Bogner EXPECT_FALSE(IVOp.SourcePreds[2].matches({SVal, UndefValue::get(Int32Ty)},
3937d449d31SJustin Bogner ConstantInt::get(Int32Ty, 0)));
3947d449d31SJustin Bogner EXPECT_TRUE(IVOp.SourcePreds[2].matches({SVal, UndefValue::get(Int32Ty)},
3957d449d31SJustin Bogner ConstantInt::get(Int32Ty, 1)));
3967d449d31SJustin Bogner
3977d449d31SJustin Bogner EXPECT_THAT(IVOp.SourcePreds[1].generate({SVal}, {}),
3987d449d31SJustin Bogner Each(AnyOf(HasType(Int32Ty), HasType(Int8PtrTy))));
3997d449d31SJustin Bogner EXPECT_THAT(
4007d449d31SJustin Bogner IVOp.SourcePreds[2].generate({SVal, ConstantInt::get(Int32Ty, 0)}, {}),
4017d449d31SJustin Bogner ElementsAre(ConstantInt::get(Int32Ty, 1)));
4027d449d31SJustin Bogner }
403e8a3475bSIgor Laevsky
404e8a3475bSIgor Laevsky }
405