#include "test.h" #include "codon/cir/util/matching.h" using namespace codon::ir; TEST_F(CIRCoreTest, MatchingEquivalentVar) { auto *first = module->Nr(module->getIntType()); auto *second = module->Nr(module->getIntType()); auto *third = module->Nr(module->getFloatType()); ASSERT_TRUE(util::match(first, second)); ASSERT_FALSE(util::match(first, third)); } TEST_F(CIRCoreTest, MatchingNonEquivalentVar) { auto *first = module->Nr(module->getIntType()); auto *second = module->Nr(module->getFloatType()); ASSERT_FALSE(util::match(first, second)); } TEST_F(CIRCoreTest, MatchingEquivalentFunc) { { auto *first = module->Nr(); first->realize(module->unsafeGetDummyFuncType(), {}); auto *second = module->Nr(); second->realize(module->unsafeGetDummyFuncType(), {}); first->setJIT(); second->setJIT(); ASSERT_TRUE(util::match(first, second)); } { auto *first = module->Nr(); first->realize(module->unsafeGetDummyFuncType(), {}); auto *second = module->Nr(); second->realize(module->unsafeGetDummyFuncType(), {}); first->setUnmangledName("baz"); second->setUnmangledName("baz"); ASSERT_TRUE(util::match(first, second)); } { auto *first = module->Nr(); first->realize(module->unsafeGetDummyFuncType(), {}); auto *second = module->Nr(); second->realize(module->unsafeGetDummyFuncType(), {}); ASSERT_TRUE(util::match(first, second)); } } TEST_F(CIRCoreTest, MatchingNonEquivalentFunc) { { auto *first = module->Nr(); first->realize(module->unsafeGetDummyFuncType(), {}); auto *second = module->Nr(); second->realize(module->unsafeGetDummyFuncType(), {}); first->setJIT(); ASSERT_FALSE(util::match(first, second)); } { auto *first = module->Nr(); first->realize(module->unsafeGetDummyFuncType(), {}); auto *second = module->Nr(); second->realize(module->unsafeGetDummyFuncType(), {}); first->setUnmangledName("baz"); second->setUnmangledName("bar"); ASSERT_FALSE(util::match(first, second)); } { auto *first = module->Nr(); first->realize(module->unsafeGetDummyFuncType(), {}); auto *second = module->Nr(); second->realize(module->unsafeGetDummyFuncType(), {}); first->setLLVMLiterals({types::Generic(1)}); ASSERT_FALSE(util::match(first, second)); } } TEST_F(CIRCoreTest, MatchingAnyValue) { auto *first = module->Nr(module->Nr(module->getIntType())); ASSERT_TRUE(util::match(first, module->Nr())); } TEST_F(CIRCoreTest, MatchingVarValue) { auto *first = module->Nr(module->Nr(module->getIntType())); auto *second = module->Nr(module->Nr(module->getIntType())); ASSERT_TRUE(util::match(first, second)); first->setVar(module->Nr(module->getFloatType())); ASSERT_FALSE(util::match(first, second)); } TEST_F(CIRCoreTest, MatchingPointerValue) { auto *first = module->Nr(module->Nr(module->getIntType())); auto *second = module->Nr(module->Nr(module->getIntType())); ASSERT_TRUE(util::match(first, second)); first->setVar(module->Nr(module->getFloatType())); ASSERT_FALSE(util::match(first, second)); } TEST_F(CIRCoreTest, MatchingSeriesFlow) { auto *first = module->Nr(); auto *second = module->Nr(); first->push_back(module->Nr(1, module->getIntType())); second->push_back(module->Nr(1, module->getIntType())); ASSERT_TRUE(util::match(first, second)); second->push_back(module->Nr(1, module->getIntType())); ASSERT_FALSE(util::match(first, second)); } TEST_F(CIRCoreTest, MatchingIfFlow) { auto *cond = module->Nr(true, module->getBoolType()); auto *tVal = module->Nr(); auto *first = module->Nr(cond, tVal); auto *second = module->Nr(cv->clone(cond), cast(cv->clone(tVal))); ASSERT_TRUE(util::match(first, second)); second->setFalseBranch(cast(cv->clone(tVal))); ASSERT_FALSE(util::match(first, second)); } TEST_F(CIRCoreTest, MatchingForFlow) { auto *body = module->Nr(); auto *var = module->Nr(module->getIntType()); auto *iter = module->Nr("hello", module->getStringType()); auto *first = module->Nr(iter, body, var); auto *second = module->Nr(cv->clone(iter), cast(cv->clone(body)), cv->clone(var)); ASSERT_TRUE(util::match(first, second)); second->setIter(module->Nr("foo", module->getStringType())); ASSERT_FALSE(util::match(first, second)); } TEST_F(CIRCoreTest, MatchingIntConst) { auto *first = module->Nr(0, module->getIntType()); auto *second = module->Nr(0, module->getIntType()); ASSERT_TRUE(util::match(first, second)); first->setVal(2); ASSERT_FALSE(util::match(first, second)); } TEST_F(CIRCoreTest, MatchingFloatConst) { auto *first = module->Nr(0.0, module->getFloatType()); auto *second = module->Nr(0.0, module->getFloatType()); ASSERT_TRUE(util::match(first, second)); first->setVal(2.0); ASSERT_FALSE(util::match(first, second)); } TEST_F(CIRCoreTest, MatchingBoolConst) { auto *first = module->Nr(false, module->getBoolType()); auto *second = module->Nr(false, module->getBoolType()); ASSERT_TRUE(util::match(first, second)); first->setVal(true); ASSERT_FALSE(util::match(first, second)); } TEST_F(CIRCoreTest, MatchingStringConst) { auto *first = module->Nr("hi", module->getStringType()); auto *second = module->Nr("hi", module->getStringType()); ASSERT_TRUE(util::match(first, second)); first->setVal("bye"); ASSERT_FALSE(util::match(first, second)); } TEST_F(CIRCoreTest, MatchingAssignInstr) { auto *var = module->Nr(module->getIntType()); auto *val = module->Nr(1, module->getIntType()); auto *first = module->Nr(var, val); auto *second = module->Nr(cv->clone(var), cv->clone(val)); ASSERT_TRUE(util::match(first, second)); second->setRhs(module->Nr(5, module->getIntType())); ASSERT_FALSE(util::match(first, second)); } TEST_F(CIRCoreTest, MatchingExtractInstr) { auto FIELD = "foo"; auto *type = cast(module->unsafeGetMemberedType("**internal**")); type->realize({module->getIntType()}, {FIELD}); auto *var = module->Nr(type); auto *val = module->Nr(var); auto *first = module->Nr(val, FIELD); auto *second = module->Nr(cv->clone(val), FIELD); ASSERT_TRUE(util::match(first, second)); second->setField(""); ASSERT_FALSE(util::match(first, second)); } TEST_F(CIRCoreTest, MatchingInsertInstr) { auto FIELD = "foo"; auto *type = cast(module->unsafeGetMemberedType("**internal**")); type->realize({module->getIntType()}, {FIELD}); auto *var = module->Nr(type); auto *val = module->Nr(var); auto *toInsert = module->Nr(1, module->getIntType()); auto *first = module->Nr(val, FIELD, toInsert); auto *second = module->Nr(cv->clone(val), FIELD, cv->clone(toInsert)); ASSERT_TRUE(util::match(first, second)); second->setField(""); ASSERT_FALSE(util::match(first, second)); } TEST_F(CIRCoreTest, MatchingCallInstr) { auto *type = module->unsafeGetDummyFuncType(); auto *func = module->Nr(); func->realize(type, {}); auto *func2 = module->Nr(); func2->realize(module->unsafeGetFuncType("baz", module->getIntType(), {}), {}); auto *funcVal = module->Nr(func); auto *first = module->Nr(funcVal); auto *second = module->Nr(cv->clone(funcVal)); ASSERT_TRUE(util::match(first, second)); second->setCallee(module->Nr(func2)); ASSERT_FALSE(util::match(first, second)); } TEST_F(CIRCoreTest, MatchingTernaryInstr) { auto *trueValue = module->Nr(true, module->getBoolType()); auto *falseValue = module->Nr(false, module->getBoolType()); auto *cond = module->Nr(true, module->getBoolType()); auto *first = module->Nr(cond, trueValue, falseValue); auto *second = module->Nr(cv->clone(cond), cv->clone(trueValue), cv->clone(falseValue)); ASSERT_TRUE(util::match(first, second)); second->setFalseValue(module->Nr(true, module->getBoolType())); ASSERT_FALSE(util::match(first, second)); }