FunctionComparatorTest.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //===- FunctionComparator.cpp - Unit tests for FunctionComparator ---------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Transforms/Utils/FunctionComparator.h"
  9. #include "llvm/IR/BasicBlock.h"
  10. #include "llvm/IR/IRBuilder.h"
  11. #include "llvm/IR/Instructions.h"
  12. #include "llvm/IR/LLVMContext.h"
  13. #include "llvm/IR/Module.h"
  14. #include "gtest/gtest.h"
  15. using namespace llvm;
  16. /// Generates a simple test function.
  17. struct TestFunction {
  18. Function *F;
  19. BasicBlock *BB;
  20. Constant *C;
  21. Instruction *I;
  22. Type *T;
  23. TestFunction(LLVMContext &Ctx, Module &M, int addVal) {
  24. IRBuilder<> B(Ctx);
  25. T = B.getInt8Ty();
  26. F = Function::Create(FunctionType::get(T, {B.getInt8PtrTy()}, false),
  27. GlobalValue::ExternalLinkage, "F", &M);
  28. BB = BasicBlock::Create(Ctx, "", F);
  29. B.SetInsertPoint(BB);
  30. Argument *PointerArg = &*F->arg_begin();
  31. LoadInst *LoadInst = B.CreateLoad(T, PointerArg);
  32. C = B.getInt8(addVal);
  33. I = cast<Instruction>(B.CreateAdd(LoadInst, C));
  34. B.CreateRet(I);
  35. }
  36. };
  37. /// A class for testing the FunctionComparator API.
  38. ///
  39. /// The main purpose is to test if the required protected functions are
  40. /// accessible from a derived class of FunctionComparator.
  41. class TestComparator : public FunctionComparator {
  42. public:
  43. TestComparator(const Function *F1, const Function *F2,
  44. GlobalNumberState *GN)
  45. : FunctionComparator(F1, F2, GN) {
  46. }
  47. bool testFunctionAccess(const Function *F1, const Function *F2) {
  48. // Test if FnL and FnR are accessible.
  49. return F1 == FnL && F2 == FnR;
  50. }
  51. int testCompare() {
  52. return compare();
  53. }
  54. int testCompareSignature() {
  55. beginCompare();
  56. return compareSignature();
  57. }
  58. int testCmpBasicBlocks(BasicBlock *BBL, BasicBlock *BBR) {
  59. beginCompare();
  60. return cmpBasicBlocks(BBL, BBR);
  61. }
  62. int testCmpConstants(const Constant *L, const Constant *R) {
  63. beginCompare();
  64. return cmpConstants(L, R);
  65. }
  66. int testCmpGlobalValues(GlobalValue *L, GlobalValue *R) {
  67. beginCompare();
  68. return cmpGlobalValues(L, R);
  69. }
  70. int testCmpValues(const Value *L, const Value *R) {
  71. beginCompare();
  72. return cmpValues(L, R);
  73. }
  74. int testCmpOperations(const Instruction *L, const Instruction *R,
  75. bool &needToCmpOperands) {
  76. beginCompare();
  77. return cmpOperations(L, R, needToCmpOperands);
  78. }
  79. int testCmpTypes(Type *TyL, Type *TyR) {
  80. beginCompare();
  81. return cmpTypes(TyL, TyR);
  82. }
  83. int testCmpPrimitives() {
  84. beginCompare();
  85. return
  86. cmpNumbers(2, 3) +
  87. cmpAPInts(APInt(32, 2), APInt(32, 3)) +
  88. cmpAPFloats(APFloat(2.0), APFloat(3.0)) +
  89. cmpMem("2", "3");
  90. }
  91. };
  92. /// A sanity check for the FunctionComparator API.
  93. TEST(FunctionComparatorTest, TestAPI) {
  94. LLVMContext C;
  95. Module M("test", C);
  96. TestFunction F1(C, M, 27);
  97. TestFunction F2(C, M, 28);
  98. GlobalNumberState GN;
  99. TestComparator Cmp(F1.F, F2.F, &GN);
  100. EXPECT_TRUE(Cmp.testFunctionAccess(F1.F, F2.F));
  101. EXPECT_EQ(Cmp.testCompare(), -1);
  102. EXPECT_EQ(Cmp.testCompareSignature(), 0);
  103. EXPECT_EQ(Cmp.testCmpBasicBlocks(F1.BB, F2.BB), -1);
  104. EXPECT_EQ(Cmp.testCmpConstants(F1.C, F2.C), -1);
  105. EXPECT_EQ(Cmp.testCmpGlobalValues(F1.F, F2.F), -1);
  106. EXPECT_EQ(Cmp.testCmpValues(F1.I, F2.I), 0);
  107. bool needToCmpOperands = false;
  108. EXPECT_EQ(Cmp.testCmpOperations(F1.I, F2.I, needToCmpOperands), 0);
  109. EXPECT_TRUE(needToCmpOperands);
  110. EXPECT_EQ(Cmp.testCmpTypes(F1.T, F2.T), 0);
  111. EXPECT_EQ(Cmp.testCmpPrimitives(), -4);
  112. }