18267b333SNico Weber //===- FunctionComparator.cpp - Unit tests for FunctionComparator ---------===//
28267b333SNico Weber //
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
68267b333SNico Weber //
78267b333SNico Weber //===----------------------------------------------------------------------===//
88267b333SNico Weber #include "llvm/Transforms/Utils/FunctionComparator.h"
98267b333SNico Weber #include "llvm/IR/BasicBlock.h"
108267b333SNico Weber #include "llvm/IR/IRBuilder.h"
118267b333SNico Weber #include "llvm/IR/Instructions.h"
128267b333SNico Weber #include "llvm/IR/LLVMContext.h"
138267b333SNico Weber #include "llvm/IR/Module.h"
148267b333SNico Weber #include "gtest/gtest.h"
158267b333SNico Weber
168267b333SNico Weber using namespace llvm;
178267b333SNico Weber
188267b333SNico Weber /// Generates a simple test function.
198267b333SNico Weber struct TestFunction {
208267b333SNico Weber Function *F;
218267b333SNico Weber BasicBlock *BB;
228267b333SNico Weber Constant *C;
238267b333SNico Weber Instruction *I;
248267b333SNico Weber Type *T;
258267b333SNico Weber
TestFunctionTestFunction268267b333SNico Weber TestFunction(LLVMContext &Ctx, Module &M, int addVal) {
278267b333SNico Weber IRBuilder<> B(Ctx);
288267b333SNico Weber T = B.getInt8Ty();
29*dd3184c3SFangrui Song F = Function::Create(FunctionType::get(T, {B.getPtrTy()}, false),
308267b333SNico Weber GlobalValue::ExternalLinkage, "F", &M);
318267b333SNico Weber BB = BasicBlock::Create(Ctx, "", F);
328267b333SNico Weber B.SetInsertPoint(BB);
338267b333SNico Weber Argument *PointerArg = &*F->arg_begin();
3414359ef1SJames Y Knight LoadInst *LoadInst = B.CreateLoad(T, PointerArg);
358267b333SNico Weber C = B.getInt8(addVal);
368267b333SNico Weber I = cast<Instruction>(B.CreateAdd(LoadInst, C));
378267b333SNico Weber B.CreateRet(I);
388267b333SNico Weber }
398267b333SNico Weber };
408267b333SNico Weber
418267b333SNico Weber /// A class for testing the FunctionComparator API.
428267b333SNico Weber ///
438267b333SNico Weber /// The main purpose is to test if the required protected functions are
448267b333SNico Weber /// accessible from a derived class of FunctionComparator.
458267b333SNico Weber class TestComparator : public FunctionComparator {
468267b333SNico Weber public:
TestComparator(const Function * F1,const Function * F2,GlobalNumberState * GN)478267b333SNico Weber TestComparator(const Function *F1, const Function *F2,
488267b333SNico Weber GlobalNumberState *GN)
498267b333SNico Weber : FunctionComparator(F1, F2, GN) {
508267b333SNico Weber }
518267b333SNico Weber
testFunctionAccess(const Function * F1,const Function * F2)528267b333SNico Weber bool testFunctionAccess(const Function *F1, const Function *F2) {
538267b333SNico Weber // Test if FnL and FnR are accessible.
548267b333SNico Weber return F1 == FnL && F2 == FnR;
558267b333SNico Weber }
568267b333SNico Weber
testCompare()578267b333SNico Weber int testCompare() {
588267b333SNico Weber return compare();
598267b333SNico Weber }
608267b333SNico Weber
testCompareSignature()618267b333SNico Weber int testCompareSignature() {
628267b333SNico Weber beginCompare();
638267b333SNico Weber return compareSignature();
648267b333SNico Weber }
658267b333SNico Weber
testCmpBasicBlocks(BasicBlock * BBL,BasicBlock * BBR)668267b333SNico Weber int testCmpBasicBlocks(BasicBlock *BBL, BasicBlock *BBR) {
678267b333SNico Weber beginCompare();
688267b333SNico Weber return cmpBasicBlocks(BBL, BBR);
698267b333SNico Weber }
708267b333SNico Weber
testCmpConstants(const Constant * L,const Constant * R)718267b333SNico Weber int testCmpConstants(const Constant *L, const Constant *R) {
728267b333SNico Weber beginCompare();
738267b333SNico Weber return cmpConstants(L, R);
748267b333SNico Weber }
758267b333SNico Weber
testCmpGlobalValues(GlobalValue * L,GlobalValue * R)768267b333SNico Weber int testCmpGlobalValues(GlobalValue *L, GlobalValue *R) {
778267b333SNico Weber beginCompare();
788267b333SNico Weber return cmpGlobalValues(L, R);
798267b333SNico Weber }
808267b333SNico Weber
testCmpValues(const Value * L,const Value * R)818267b333SNico Weber int testCmpValues(const Value *L, const Value *R) {
828267b333SNico Weber beginCompare();
838267b333SNico Weber return cmpValues(L, R);
848267b333SNico Weber }
858267b333SNico Weber
testCmpOperations(const Instruction * L,const Instruction * R,bool & needToCmpOperands)868267b333SNico Weber int testCmpOperations(const Instruction *L, const Instruction *R,
878267b333SNico Weber bool &needToCmpOperands) {
888267b333SNico Weber beginCompare();
898267b333SNico Weber return cmpOperations(L, R, needToCmpOperands);
908267b333SNico Weber }
918267b333SNico Weber
testCmpTypes(Type * TyL,Type * TyR)928267b333SNico Weber int testCmpTypes(Type *TyL, Type *TyR) {
938267b333SNico Weber beginCompare();
948267b333SNico Weber return cmpTypes(TyL, TyR);
958267b333SNico Weber }
968267b333SNico Weber
testCmpPrimitives()978267b333SNico Weber int testCmpPrimitives() {
988267b333SNico Weber beginCompare();
998267b333SNico Weber return
1008267b333SNico Weber cmpNumbers(2, 3) +
1018267b333SNico Weber cmpAPInts(APInt(32, 2), APInt(32, 3)) +
1028267b333SNico Weber cmpAPFloats(APFloat(2.0), APFloat(3.0)) +
1038267b333SNico Weber cmpMem("2", "3");
1048267b333SNico Weber }
1058267b333SNico Weber };
1068267b333SNico Weber
1078267b333SNico Weber /// A sanity check for the FunctionComparator API.
TEST(FunctionComparatorTest,TestAPI)1088267b333SNico Weber TEST(FunctionComparatorTest, TestAPI) {
1098267b333SNico Weber LLVMContext C;
1108267b333SNico Weber Module M("test", C);
1118267b333SNico Weber TestFunction F1(C, M, 27);
1128267b333SNico Weber TestFunction F2(C, M, 28);
1138267b333SNico Weber
1148267b333SNico Weber GlobalNumberState GN;
1158267b333SNico Weber TestComparator Cmp(F1.F, F2.F, &GN);
1168267b333SNico Weber
1178267b333SNico Weber EXPECT_TRUE(Cmp.testFunctionAccess(F1.F, F2.F));
1188267b333SNico Weber EXPECT_EQ(Cmp.testCompare(), -1);
1198267b333SNico Weber EXPECT_EQ(Cmp.testCompareSignature(), 0);
1208267b333SNico Weber EXPECT_EQ(Cmp.testCmpBasicBlocks(F1.BB, F2.BB), -1);
1218267b333SNico Weber EXPECT_EQ(Cmp.testCmpConstants(F1.C, F2.C), -1);
1228267b333SNico Weber EXPECT_EQ(Cmp.testCmpGlobalValues(F1.F, F2.F), -1);
1238267b333SNico Weber EXPECT_EQ(Cmp.testCmpValues(F1.I, F2.I), 0);
1248267b333SNico Weber bool needToCmpOperands = false;
1258267b333SNico Weber EXPECT_EQ(Cmp.testCmpOperations(F1.I, F2.I, needToCmpOperands), 0);
1268267b333SNico Weber EXPECT_TRUE(needToCmpOperands);
1278267b333SNico Weber EXPECT_EQ(Cmp.testCmpTypes(F1.T, F2.T), 0);
1288267b333SNico Weber EXPECT_EQ(Cmp.testCmpPrimitives(), -4);
1298267b333SNico Weber }
130