14d6fb72aSErik Eckstein //===- FunctionComparator.h - Function Comparator -------------------------===//
24d6fb72aSErik Eckstein //
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
64d6fb72aSErik Eckstein //
74d6fb72aSErik Eckstein //===----------------------------------------------------------------------===//
84d6fb72aSErik Eckstein //
94d6fb72aSErik Eckstein // This file implements the FunctionComparator and GlobalNumberState classes
104d6fb72aSErik Eckstein // which are used by the MergeFunctions pass for comparing functions.
114d6fb72aSErik Eckstein //
124d6fb72aSErik Eckstein //===----------------------------------------------------------------------===//
134d6fb72aSErik Eckstein 
144d6fb72aSErik Eckstein #include "llvm/Transforms/Utils/FunctionComparator.h"
15286d5897SEugene Zelenko #include "llvm/ADT/APFloat.h"
16286d5897SEugene Zelenko #include "llvm/ADT/APInt.h"
17286d5897SEugene Zelenko #include "llvm/ADT/ArrayRef.h"
18286d5897SEugene Zelenko #include "llvm/ADT/Hashing.h"
19286d5897SEugene Zelenko #include "llvm/ADT/SmallPtrSet.h"
20286d5897SEugene Zelenko #include "llvm/ADT/SmallVector.h"
21286d5897SEugene Zelenko #include "llvm/IR/Attributes.h"
22286d5897SEugene Zelenko #include "llvm/IR/BasicBlock.h"
23286d5897SEugene Zelenko #include "llvm/IR/Constant.h"
24286d5897SEugene Zelenko #include "llvm/IR/Constants.h"
25286d5897SEugene Zelenko #include "llvm/IR/DataLayout.h"
26286d5897SEugene Zelenko #include "llvm/IR/DerivedTypes.h"
27286d5897SEugene Zelenko #include "llvm/IR/Function.h"
28286d5897SEugene Zelenko #include "llvm/IR/GlobalValue.h"
294d6fb72aSErik Eckstein #include "llvm/IR/InlineAsm.h"
30286d5897SEugene Zelenko #include "llvm/IR/InstrTypes.h"
31286d5897SEugene Zelenko #include "llvm/IR/Instruction.h"
326bda14b3SChandler Carruth #include "llvm/IR/Instructions.h"
33286d5897SEugene Zelenko #include "llvm/IR/LLVMContext.h"
34286d5897SEugene Zelenko #include "llvm/IR/Metadata.h"
354d6fb72aSErik Eckstein #include "llvm/IR/Module.h"
36286d5897SEugene Zelenko #include "llvm/IR/Operator.h"
37286d5897SEugene Zelenko #include "llvm/IR/Type.h"
38286d5897SEugene Zelenko #include "llvm/IR/Value.h"
39286d5897SEugene Zelenko #include "llvm/Support/Casting.h"
40286d5897SEugene Zelenko #include "llvm/Support/Compiler.h"
414d6fb72aSErik Eckstein #include "llvm/Support/Debug.h"
42286d5897SEugene Zelenko #include "llvm/Support/ErrorHandling.h"
434d6fb72aSErik Eckstein #include "llvm/Support/raw_ostream.h"
44286d5897SEugene Zelenko #include <cassert>
45286d5897SEugene Zelenko #include <cstddef>
46286d5897SEugene Zelenko #include <cstdint>
47286d5897SEugene Zelenko #include <utility>
484d6fb72aSErik Eckstein 
494d6fb72aSErik Eckstein using namespace llvm;
504d6fb72aSErik Eckstein 
514d6fb72aSErik Eckstein #define DEBUG_TYPE "functioncomparator"
524d6fb72aSErik Eckstein 
cmpNumbers(uint64_t L,uint64_t R) const534d6fb72aSErik Eckstein int FunctionComparator::cmpNumbers(uint64_t L, uint64_t R) const {
544213bc76SMircea Trofin   if (L < R)
554213bc76SMircea Trofin     return -1;
564213bc76SMircea Trofin   if (L > R)
574213bc76SMircea Trofin     return 1;
584d6fb72aSErik Eckstein   return 0;
594d6fb72aSErik Eckstein }
604d6fb72aSErik Eckstein 
cmpAligns(Align L,Align R) const61*d5583366SArthur Eubanks int FunctionComparator::cmpAligns(Align L, Align R) const {
62*d5583366SArthur Eubanks   if (L.value() < R.value())
63*d5583366SArthur Eubanks     return -1;
64*d5583366SArthur Eubanks   if (L.value() > R.value())
65*d5583366SArthur Eubanks     return 1;
66*d5583366SArthur Eubanks   return 0;
67*d5583366SArthur Eubanks }
68*d5583366SArthur Eubanks 
cmpOrderings(AtomicOrdering L,AtomicOrdering R) const694d6fb72aSErik Eckstein int FunctionComparator::cmpOrderings(AtomicOrdering L, AtomicOrdering R) const {
704213bc76SMircea Trofin   if ((int)L < (int)R)
714213bc76SMircea Trofin     return -1;
724213bc76SMircea Trofin   if ((int)L > (int)R)
734213bc76SMircea Trofin     return 1;
744d6fb72aSErik Eckstein   return 0;
754d6fb72aSErik Eckstein }
764d6fb72aSErik Eckstein 
cmpAPInts(const APInt & L,const APInt & R) const774d6fb72aSErik Eckstein int FunctionComparator::cmpAPInts(const APInt &L, const APInt &R) const {
784d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L.getBitWidth(), R.getBitWidth()))
794d6fb72aSErik Eckstein     return Res;
804213bc76SMircea Trofin   if (L.ugt(R))
814213bc76SMircea Trofin     return 1;
824213bc76SMircea Trofin   if (R.ugt(L))
834213bc76SMircea Trofin     return -1;
844d6fb72aSErik Eckstein   return 0;
854d6fb72aSErik Eckstein }
864d6fb72aSErik Eckstein 
cmpAPFloats(const APFloat & L,const APFloat & R) const874d6fb72aSErik Eckstein int FunctionComparator::cmpAPFloats(const APFloat &L, const APFloat &R) const {
884d6fb72aSErik Eckstein   // Floats are ordered first by semantics (i.e. float, double, half, etc.),
894d6fb72aSErik Eckstein   // then by value interpreted as a bitstring (aka APInt).
904d6fb72aSErik Eckstein   const fltSemantics &SL = L.getSemantics(), &SR = R.getSemantics();
914d6fb72aSErik Eckstein   if (int Res = cmpNumbers(APFloat::semanticsPrecision(SL),
924d6fb72aSErik Eckstein                            APFloat::semanticsPrecision(SR)))
934d6fb72aSErik Eckstein     return Res;
944d6fb72aSErik Eckstein   if (int Res = cmpNumbers(APFloat::semanticsMaxExponent(SL),
954d6fb72aSErik Eckstein                            APFloat::semanticsMaxExponent(SR)))
964d6fb72aSErik Eckstein     return Res;
974d6fb72aSErik Eckstein   if (int Res = cmpNumbers(APFloat::semanticsMinExponent(SL),
984d6fb72aSErik Eckstein                            APFloat::semanticsMinExponent(SR)))
994d6fb72aSErik Eckstein     return Res;
1004d6fb72aSErik Eckstein   if (int Res = cmpNumbers(APFloat::semanticsSizeInBits(SL),
1014d6fb72aSErik Eckstein                            APFloat::semanticsSizeInBits(SR)))
1024d6fb72aSErik Eckstein     return Res;
1034d6fb72aSErik Eckstein   return cmpAPInts(L.bitcastToAPInt(), R.bitcastToAPInt());
1044d6fb72aSErik Eckstein }
1054d6fb72aSErik Eckstein 
cmpMem(StringRef L,StringRef R) const1064d6fb72aSErik Eckstein int FunctionComparator::cmpMem(StringRef L, StringRef R) const {
1074d6fb72aSErik Eckstein   // Prevent heavy comparison, compare sizes first.
1084d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L.size(), R.size()))
1094d6fb72aSErik Eckstein     return Res;
1104d6fb72aSErik Eckstein 
1114d6fb72aSErik Eckstein   // Compare strings lexicographically only when it is necessary: only when
1124d6fb72aSErik Eckstein   // strings are equal in size.
1134d6fb72aSErik Eckstein   return L.compare(R);
1144d6fb72aSErik Eckstein }
1154d6fb72aSErik Eckstein 
cmpAttrs(const AttributeList L,const AttributeList R) const116b518054bSReid Kleckner int FunctionComparator::cmpAttrs(const AttributeList L,
117b518054bSReid Kleckner                                  const AttributeList R) const {
1188bf67fe9SReid Kleckner   if (int Res = cmpNumbers(L.getNumAttrSets(), R.getNumAttrSets()))
1194d6fb72aSErik Eckstein     return Res;
1204d6fb72aSErik Eckstein 
121a7b4ce9cSArthur Eubanks   for (unsigned i : L.indexes()) {
1228bf67fe9SReid Kleckner     AttributeSet LAS = L.getAttributes(i);
1238bf67fe9SReid Kleckner     AttributeSet RAS = R.getAttributes(i);
1248bf67fe9SReid Kleckner     AttributeSet::iterator LI = LAS.begin(), LE = LAS.end();
1258bf67fe9SReid Kleckner     AttributeSet::iterator RI = RAS.begin(), RE = RAS.end();
1264d6fb72aSErik Eckstein     for (; LI != LE && RI != RE; ++LI, ++RI) {
1274d6fb72aSErik Eckstein       Attribute LA = *LI;
1284d6fb72aSErik Eckstein       Attribute RA = *RI;
12922c96a96STim Northover       if (LA.isTypeAttribute() && RA.isTypeAttribute()) {
13022c96a96STim Northover         if (LA.getKindAsEnum() != RA.getKindAsEnum())
13122c96a96STim Northover           return cmpNumbers(LA.getKindAsEnum(), RA.getKindAsEnum());
13222c96a96STim Northover 
13322c96a96STim Northover         Type *TyL = LA.getValueAsType();
13422c96a96STim Northover         Type *TyR = RA.getValueAsType();
135f7fe7ea2STim Northover         if (TyL && TyR) {
136f7fe7ea2STim Northover           if (int Res = cmpTypes(TyL, TyR))
137f7fe7ea2STim Northover             return Res;
138f7fe7ea2STim Northover           continue;
139f7fe7ea2STim Northover         }
14022c96a96STim Northover 
14122c96a96STim Northover         // Two pointers, at least one null, so the comparison result is
14222c96a96STim Northover         // independent of the value of a real pointer.
143f7fe7ea2STim Northover         if (int Res = cmpNumbers((uint64_t)TyL, (uint64_t)TyR))
144f7fe7ea2STim Northover           return Res;
145f7fe7ea2STim Northover         continue;
14622c96a96STim Northover       }
1474d6fb72aSErik Eckstein       if (LA < RA)
1484d6fb72aSErik Eckstein         return -1;
1494d6fb72aSErik Eckstein       if (RA < LA)
1504d6fb72aSErik Eckstein         return 1;
1514d6fb72aSErik Eckstein     }
1524d6fb72aSErik Eckstein     if (LI != LE)
1534d6fb72aSErik Eckstein       return 1;
1544d6fb72aSErik Eckstein     if (RI != RE)
1554d6fb72aSErik Eckstein       return -1;
1564d6fb72aSErik Eckstein   }
1574d6fb72aSErik Eckstein   return 0;
1584d6fb72aSErik Eckstein }
1594d6fb72aSErik Eckstein 
cmpRangeMetadata(const MDNode * L,const MDNode * R) const1604d6fb72aSErik Eckstein int FunctionComparator::cmpRangeMetadata(const MDNode *L,
1614d6fb72aSErik Eckstein                                          const MDNode *R) const {
1624d6fb72aSErik Eckstein   if (L == R)
1634d6fb72aSErik Eckstein     return 0;
1644d6fb72aSErik Eckstein   if (!L)
1654d6fb72aSErik Eckstein     return -1;
1664d6fb72aSErik Eckstein   if (!R)
1674d6fb72aSErik Eckstein     return 1;
1684d6fb72aSErik Eckstein   // Range metadata is a sequence of numbers. Make sure they are the same
1694d6fb72aSErik Eckstein   // sequence.
1704d6fb72aSErik Eckstein   // TODO: Note that as this is metadata, it is possible to drop and/or merge
1714d6fb72aSErik Eckstein   // this data when considering functions to merge. Thus this comparison would
1724d6fb72aSErik Eckstein   // return 0 (i.e. equivalent), but merging would become more complicated
1734d6fb72aSErik Eckstein   // because the ranges would need to be unioned. It is not likely that
1744d6fb72aSErik Eckstein   // functions differ ONLY in this metadata if they are actually the same
1754d6fb72aSErik Eckstein   // function semantically.
1764d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L->getNumOperands(), R->getNumOperands()))
1774d6fb72aSErik Eckstein     return Res;
1784d6fb72aSErik Eckstein   for (size_t I = 0; I < L->getNumOperands(); ++I) {
1794d6fb72aSErik Eckstein     ConstantInt *LLow = mdconst::extract<ConstantInt>(L->getOperand(I));
1804d6fb72aSErik Eckstein     ConstantInt *RLow = mdconst::extract<ConstantInt>(R->getOperand(I));
1814d6fb72aSErik Eckstein     if (int Res = cmpAPInts(LLow->getValue(), RLow->getValue()))
1824d6fb72aSErik Eckstein       return Res;
1834d6fb72aSErik Eckstein   }
1844d6fb72aSErik Eckstein   return 0;
1854d6fb72aSErik Eckstein }
1864d6fb72aSErik Eckstein 
cmpOperandBundlesSchema(const CallBase & LCS,const CallBase & RCS) const1873ab319b2SMircea Trofin int FunctionComparator::cmpOperandBundlesSchema(const CallBase &LCS,
1883ab319b2SMircea Trofin                                                 const CallBase &RCS) const {
1893ab319b2SMircea Trofin   assert(LCS.getOpcode() == RCS.getOpcode() && "Can't compare otherwise!");
1904d6fb72aSErik Eckstein 
1914d6fb72aSErik Eckstein   if (int Res =
1923ab319b2SMircea Trofin           cmpNumbers(LCS.getNumOperandBundles(), RCS.getNumOperandBundles()))
1934d6fb72aSErik Eckstein     return Res;
1944d6fb72aSErik Eckstein 
1953ab319b2SMircea Trofin   for (unsigned I = 0, E = LCS.getNumOperandBundles(); I != E; ++I) {
1963ab319b2SMircea Trofin     auto OBL = LCS.getOperandBundleAt(I);
1973ab319b2SMircea Trofin     auto OBR = RCS.getOperandBundleAt(I);
1984d6fb72aSErik Eckstein 
1994d6fb72aSErik Eckstein     if (int Res = OBL.getTagName().compare(OBR.getTagName()))
2004d6fb72aSErik Eckstein       return Res;
2014d6fb72aSErik Eckstein 
2024d6fb72aSErik Eckstein     if (int Res = cmpNumbers(OBL.Inputs.size(), OBR.Inputs.size()))
2034d6fb72aSErik Eckstein       return Res;
2044d6fb72aSErik Eckstein   }
2054d6fb72aSErik Eckstein 
2064d6fb72aSErik Eckstein   return 0;
2074d6fb72aSErik Eckstein }
2084d6fb72aSErik Eckstein 
2094d6fb72aSErik Eckstein /// Constants comparison:
2104d6fb72aSErik Eckstein /// 1. Check whether type of L constant could be losslessly bitcasted to R
2114d6fb72aSErik Eckstein /// type.
2124d6fb72aSErik Eckstein /// 2. Compare constant contents.
2134d6fb72aSErik Eckstein /// For more details see declaration comments.
cmpConstants(const Constant * L,const Constant * R) const2144d6fb72aSErik Eckstein int FunctionComparator::cmpConstants(const Constant *L,
2154d6fb72aSErik Eckstein                                      const Constant *R) const {
2164d6fb72aSErik Eckstein   Type *TyL = L->getType();
2174d6fb72aSErik Eckstein   Type *TyR = R->getType();
2184d6fb72aSErik Eckstein 
2194d6fb72aSErik Eckstein   // Check whether types are bitcastable. This part is just re-factored
2204d6fb72aSErik Eckstein   // Type::canLosslesslyBitCastTo method, but instead of returning true/false,
2214d6fb72aSErik Eckstein   // we also pack into result which type is "less" for us.
2224d6fb72aSErik Eckstein   int TypesRes = cmpTypes(TyL, TyR);
2234d6fb72aSErik Eckstein   if (TypesRes != 0) {
2244d6fb72aSErik Eckstein     // Types are different, but check whether we can bitcast them.
2254d6fb72aSErik Eckstein     if (!TyL->isFirstClassType()) {
2264d6fb72aSErik Eckstein       if (TyR->isFirstClassType())
2274d6fb72aSErik Eckstein         return -1;
2284d6fb72aSErik Eckstein       // Neither TyL nor TyR are values of first class type. Return the result
2294d6fb72aSErik Eckstein       // of comparing the types
2304d6fb72aSErik Eckstein       return TypesRes;
2314d6fb72aSErik Eckstein     }
2324d6fb72aSErik Eckstein     if (!TyR->isFirstClassType()) {
2334d6fb72aSErik Eckstein       if (TyL->isFirstClassType())
2344d6fb72aSErik Eckstein         return 1;
2354d6fb72aSErik Eckstein       return TypesRes;
2364d6fb72aSErik Eckstein     }
2374d6fb72aSErik Eckstein 
2384d6fb72aSErik Eckstein     // Vector -> Vector conversions are always lossless if the two vector types
2394d6fb72aSErik Eckstein     // have the same size, otherwise not.
2404d6fb72aSErik Eckstein     unsigned TyLWidth = 0;
2414d6fb72aSErik Eckstein     unsigned TyRWidth = 0;
2424d6fb72aSErik Eckstein 
2434d6fb72aSErik Eckstein     if (auto *VecTyL = dyn_cast<VectorType>(TyL))
2448226d599SChristopher Tetreault       TyLWidth = VecTyL->getPrimitiveSizeInBits().getFixedSize();
2454d6fb72aSErik Eckstein     if (auto *VecTyR = dyn_cast<VectorType>(TyR))
2468226d599SChristopher Tetreault       TyRWidth = VecTyR->getPrimitiveSizeInBits().getFixedSize();
2474d6fb72aSErik Eckstein 
2484d6fb72aSErik Eckstein     if (TyLWidth != TyRWidth)
2494d6fb72aSErik Eckstein       return cmpNumbers(TyLWidth, TyRWidth);
2504d6fb72aSErik Eckstein 
2514d6fb72aSErik Eckstein     // Zero bit-width means neither TyL nor TyR are vectors.
2524d6fb72aSErik Eckstein     if (!TyLWidth) {
2534d6fb72aSErik Eckstein       PointerType *PTyL = dyn_cast<PointerType>(TyL);
2544d6fb72aSErik Eckstein       PointerType *PTyR = dyn_cast<PointerType>(TyR);
2554d6fb72aSErik Eckstein       if (PTyL && PTyR) {
2564d6fb72aSErik Eckstein         unsigned AddrSpaceL = PTyL->getAddressSpace();
2574d6fb72aSErik Eckstein         unsigned AddrSpaceR = PTyR->getAddressSpace();
2584d6fb72aSErik Eckstein         if (int Res = cmpNumbers(AddrSpaceL, AddrSpaceR))
2594d6fb72aSErik Eckstein           return Res;
2604d6fb72aSErik Eckstein       }
2614d6fb72aSErik Eckstein       if (PTyL)
2624d6fb72aSErik Eckstein         return 1;
2634d6fb72aSErik Eckstein       if (PTyR)
2644d6fb72aSErik Eckstein         return -1;
2654d6fb72aSErik Eckstein 
2664d6fb72aSErik Eckstein       // TyL and TyR aren't vectors, nor pointers. We don't know how to
2674d6fb72aSErik Eckstein       // bitcast them.
2684d6fb72aSErik Eckstein       return TypesRes;
2694d6fb72aSErik Eckstein     }
2704d6fb72aSErik Eckstein   }
2714d6fb72aSErik Eckstein 
2724d6fb72aSErik Eckstein   // OK, types are bitcastable, now check constant contents.
2734d6fb72aSErik Eckstein 
2744d6fb72aSErik Eckstein   if (L->isNullValue() && R->isNullValue())
2754d6fb72aSErik Eckstein     return TypesRes;
2764d6fb72aSErik Eckstein   if (L->isNullValue() && !R->isNullValue())
2774d6fb72aSErik Eckstein     return 1;
2784d6fb72aSErik Eckstein   if (!L->isNullValue() && R->isNullValue())
2794d6fb72aSErik Eckstein     return -1;
2804d6fb72aSErik Eckstein 
2814d6fb72aSErik Eckstein   auto GlobalValueL = const_cast<GlobalValue *>(dyn_cast<GlobalValue>(L));
2824d6fb72aSErik Eckstein   auto GlobalValueR = const_cast<GlobalValue *>(dyn_cast<GlobalValue>(R));
2834d6fb72aSErik Eckstein   if (GlobalValueL && GlobalValueR) {
2844d6fb72aSErik Eckstein     return cmpGlobalValues(GlobalValueL, GlobalValueR);
2854d6fb72aSErik Eckstein   }
2864d6fb72aSErik Eckstein 
2874d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L->getValueID(), R->getValueID()))
2884d6fb72aSErik Eckstein     return Res;
2894d6fb72aSErik Eckstein 
2904d6fb72aSErik Eckstein   if (const auto *SeqL = dyn_cast<ConstantDataSequential>(L)) {
2914d6fb72aSErik Eckstein     const auto *SeqR = cast<ConstantDataSequential>(R);
2924d6fb72aSErik Eckstein     // This handles ConstantDataArray and ConstantDataVector. Note that we
2934d6fb72aSErik Eckstein     // compare the two raw data arrays, which might differ depending on the host
2944d6fb72aSErik Eckstein     // endianness. This isn't a problem though, because the endiness of a module
2954d6fb72aSErik Eckstein     // will affect the order of the constants, but this order is the same
2964d6fb72aSErik Eckstein     // for a given input module and host platform.
2974d6fb72aSErik Eckstein     return cmpMem(SeqL->getRawDataValues(), SeqR->getRawDataValues());
2984d6fb72aSErik Eckstein   }
2994d6fb72aSErik Eckstein 
3004d6fb72aSErik Eckstein   switch (L->getValueID()) {
3014d6fb72aSErik Eckstein   case Value::UndefValueVal:
302345fcccbSZhengyang Liu   case Value::PoisonValueVal:
3034d6fb72aSErik Eckstein   case Value::ConstantTokenNoneVal:
3044d6fb72aSErik Eckstein     return TypesRes;
3054d6fb72aSErik Eckstein   case Value::ConstantIntVal: {
3064d6fb72aSErik Eckstein     const APInt &LInt = cast<ConstantInt>(L)->getValue();
3074d6fb72aSErik Eckstein     const APInt &RInt = cast<ConstantInt>(R)->getValue();
3084d6fb72aSErik Eckstein     return cmpAPInts(LInt, RInt);
3094d6fb72aSErik Eckstein   }
3104d6fb72aSErik Eckstein   case Value::ConstantFPVal: {
3114d6fb72aSErik Eckstein     const APFloat &LAPF = cast<ConstantFP>(L)->getValueAPF();
3124d6fb72aSErik Eckstein     const APFloat &RAPF = cast<ConstantFP>(R)->getValueAPF();
3134d6fb72aSErik Eckstein     return cmpAPFloats(LAPF, RAPF);
3144d6fb72aSErik Eckstein   }
3154d6fb72aSErik Eckstein   case Value::ConstantArrayVal: {
3164d6fb72aSErik Eckstein     const ConstantArray *LA = cast<ConstantArray>(L);
3174d6fb72aSErik Eckstein     const ConstantArray *RA = cast<ConstantArray>(R);
3184d6fb72aSErik Eckstein     uint64_t NumElementsL = cast<ArrayType>(TyL)->getNumElements();
3194d6fb72aSErik Eckstein     uint64_t NumElementsR = cast<ArrayType>(TyR)->getNumElements();
3204d6fb72aSErik Eckstein     if (int Res = cmpNumbers(NumElementsL, NumElementsR))
3214d6fb72aSErik Eckstein       return Res;
3224d6fb72aSErik Eckstein     for (uint64_t i = 0; i < NumElementsL; ++i) {
3234d6fb72aSErik Eckstein       if (int Res = cmpConstants(cast<Constant>(LA->getOperand(i)),
3244d6fb72aSErik Eckstein                                  cast<Constant>(RA->getOperand(i))))
3254d6fb72aSErik Eckstein         return Res;
3264d6fb72aSErik Eckstein     }
3274d6fb72aSErik Eckstein     return 0;
3284d6fb72aSErik Eckstein   }
3294d6fb72aSErik Eckstein   case Value::ConstantStructVal: {
3304d6fb72aSErik Eckstein     const ConstantStruct *LS = cast<ConstantStruct>(L);
3314d6fb72aSErik Eckstein     const ConstantStruct *RS = cast<ConstantStruct>(R);
3324d6fb72aSErik Eckstein     unsigned NumElementsL = cast<StructType>(TyL)->getNumElements();
3334d6fb72aSErik Eckstein     unsigned NumElementsR = cast<StructType>(TyR)->getNumElements();
3344d6fb72aSErik Eckstein     if (int Res = cmpNumbers(NumElementsL, NumElementsR))
3354d6fb72aSErik Eckstein       return Res;
3364d6fb72aSErik Eckstein     for (unsigned i = 0; i != NumElementsL; ++i) {
3374d6fb72aSErik Eckstein       if (int Res = cmpConstants(cast<Constant>(LS->getOperand(i)),
3384d6fb72aSErik Eckstein                                  cast<Constant>(RS->getOperand(i))))
3394d6fb72aSErik Eckstein         return Res;
3404d6fb72aSErik Eckstein     }
3414d6fb72aSErik Eckstein     return 0;
3424d6fb72aSErik Eckstein   }
3434d6fb72aSErik Eckstein   case Value::ConstantVectorVal: {
3444d6fb72aSErik Eckstein     const ConstantVector *LV = cast<ConstantVector>(L);
3454d6fb72aSErik Eckstein     const ConstantVector *RV = cast<ConstantVector>(R);
3468d11ec66SChristopher Tetreault     unsigned NumElementsL = cast<FixedVectorType>(TyL)->getNumElements();
3478d11ec66SChristopher Tetreault     unsigned NumElementsR = cast<FixedVectorType>(TyR)->getNumElements();
3484d6fb72aSErik Eckstein     if (int Res = cmpNumbers(NumElementsL, NumElementsR))
3494d6fb72aSErik Eckstein       return Res;
3504d6fb72aSErik Eckstein     for (uint64_t i = 0; i < NumElementsL; ++i) {
3514d6fb72aSErik Eckstein       if (int Res = cmpConstants(cast<Constant>(LV->getOperand(i)),
3524d6fb72aSErik Eckstein                                  cast<Constant>(RV->getOperand(i))))
3534d6fb72aSErik Eckstein         return Res;
3544d6fb72aSErik Eckstein     }
3554d6fb72aSErik Eckstein     return 0;
3564d6fb72aSErik Eckstein   }
3574d6fb72aSErik Eckstein   case Value::ConstantExprVal: {
3584d6fb72aSErik Eckstein     const ConstantExpr *LE = cast<ConstantExpr>(L);
3594d6fb72aSErik Eckstein     const ConstantExpr *RE = cast<ConstantExpr>(R);
3604d6fb72aSErik Eckstein     unsigned NumOperandsL = LE->getNumOperands();
3614d6fb72aSErik Eckstein     unsigned NumOperandsR = RE->getNumOperands();
3624d6fb72aSErik Eckstein     if (int Res = cmpNumbers(NumOperandsL, NumOperandsR))
3634d6fb72aSErik Eckstein       return Res;
3644d6fb72aSErik Eckstein     for (unsigned i = 0; i < NumOperandsL; ++i) {
3654d6fb72aSErik Eckstein       if (int Res = cmpConstants(cast<Constant>(LE->getOperand(i)),
3664d6fb72aSErik Eckstein                                  cast<Constant>(RE->getOperand(i))))
3674d6fb72aSErik Eckstein         return Res;
3684d6fb72aSErik Eckstein     }
3694d6fb72aSErik Eckstein     return 0;
3704d6fb72aSErik Eckstein   }
3714d6fb72aSErik Eckstein   case Value::BlockAddressVal: {
3724d6fb72aSErik Eckstein     const BlockAddress *LBA = cast<BlockAddress>(L);
3734d6fb72aSErik Eckstein     const BlockAddress *RBA = cast<BlockAddress>(R);
3744d6fb72aSErik Eckstein     if (int Res = cmpValues(LBA->getFunction(), RBA->getFunction()))
3754d6fb72aSErik Eckstein       return Res;
3764d6fb72aSErik Eckstein     if (LBA->getFunction() == RBA->getFunction()) {
3774d6fb72aSErik Eckstein       // They are BBs in the same function. Order by which comes first in the
3784d6fb72aSErik Eckstein       // BB order of the function. This order is deterministic.
3794d6fb72aSErik Eckstein       Function *F = LBA->getFunction();
3804d6fb72aSErik Eckstein       BasicBlock *LBB = LBA->getBasicBlock();
3814d6fb72aSErik Eckstein       BasicBlock *RBB = RBA->getBasicBlock();
3824d6fb72aSErik Eckstein       if (LBB == RBB)
3834d6fb72aSErik Eckstein         return 0;
3844d6fb72aSErik Eckstein       for (BasicBlock &BB : F->getBasicBlockList()) {
3854d6fb72aSErik Eckstein         if (&BB == LBB) {
3864d6fb72aSErik Eckstein           assert(&BB != RBB);
3874d6fb72aSErik Eckstein           return -1;
3884d6fb72aSErik Eckstein         }
3894d6fb72aSErik Eckstein         if (&BB == RBB)
3904d6fb72aSErik Eckstein           return 1;
3914d6fb72aSErik Eckstein       }
3924d6fb72aSErik Eckstein       llvm_unreachable("Basic Block Address does not point to a basic block in "
3934d6fb72aSErik Eckstein                        "its function.");
3944d6fb72aSErik Eckstein       return -1;
3954d6fb72aSErik Eckstein     } else {
3964d6fb72aSErik Eckstein       // cmpValues said the functions are the same. So because they aren't
3974d6fb72aSErik Eckstein       // literally the same pointer, they must respectively be the left and
3984d6fb72aSErik Eckstein       // right functions.
3994d6fb72aSErik Eckstein       assert(LBA->getFunction() == FnL && RBA->getFunction() == FnR);
4004d6fb72aSErik Eckstein       // cmpValues will tell us if these are equivalent BasicBlocks, in the
4014d6fb72aSErik Eckstein       // context of their respective functions.
4024d6fb72aSErik Eckstein       return cmpValues(LBA->getBasicBlock(), RBA->getBasicBlock());
4034d6fb72aSErik Eckstein     }
4044d6fb72aSErik Eckstein   }
4054d6fb72aSErik Eckstein   default: // Unknown constant, abort.
406d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Looking at valueID " << L->getValueID() << "\n");
4074d6fb72aSErik Eckstein     llvm_unreachable("Constant ValueID not recognized.");
4084d6fb72aSErik Eckstein     return -1;
4094d6fb72aSErik Eckstein   }
4104d6fb72aSErik Eckstein }
4114d6fb72aSErik Eckstein 
cmpGlobalValues(GlobalValue * L,GlobalValue * R) const4124d6fb72aSErik Eckstein int FunctionComparator::cmpGlobalValues(GlobalValue *L, GlobalValue *R) const {
413c1d52e5cSErik Eckstein   uint64_t LNumber = GlobalNumbers->getNumber(L);
414c1d52e5cSErik Eckstein   uint64_t RNumber = GlobalNumbers->getNumber(R);
415c1d52e5cSErik Eckstein   return cmpNumbers(LNumber, RNumber);
4164d6fb72aSErik Eckstein }
4174d6fb72aSErik Eckstein 
4184d6fb72aSErik Eckstein /// cmpType - compares two types,
4194d6fb72aSErik Eckstein /// defines total ordering among the types set.
4204d6fb72aSErik Eckstein /// See method declaration comments for more details.
cmpTypes(Type * TyL,Type * TyR) const4214d6fb72aSErik Eckstein int FunctionComparator::cmpTypes(Type *TyL, Type *TyR) const {
4224d6fb72aSErik Eckstein   PointerType *PTyL = dyn_cast<PointerType>(TyL);
4234d6fb72aSErik Eckstein   PointerType *PTyR = dyn_cast<PointerType>(TyR);
4244d6fb72aSErik Eckstein 
4254d6fb72aSErik Eckstein   const DataLayout &DL = FnL->getParent()->getDataLayout();
4264d6fb72aSErik Eckstein   if (PTyL && PTyL->getAddressSpace() == 0)
4274d6fb72aSErik Eckstein     TyL = DL.getIntPtrType(TyL);
4284d6fb72aSErik Eckstein   if (PTyR && PTyR->getAddressSpace() == 0)
4294d6fb72aSErik Eckstein     TyR = DL.getIntPtrType(TyR);
4304d6fb72aSErik Eckstein 
4314d6fb72aSErik Eckstein   if (TyL == TyR)
4324d6fb72aSErik Eckstein     return 0;
4334d6fb72aSErik Eckstein 
4344d6fb72aSErik Eckstein   if (int Res = cmpNumbers(TyL->getTypeID(), TyR->getTypeID()))
4354d6fb72aSErik Eckstein     return Res;
4364d6fb72aSErik Eckstein 
4374d6fb72aSErik Eckstein   switch (TyL->getTypeID()) {
4384d6fb72aSErik Eckstein   default:
4394d6fb72aSErik Eckstein     llvm_unreachable("Unknown type!");
4404d6fb72aSErik Eckstein   case Type::IntegerTyID:
4414d6fb72aSErik Eckstein     return cmpNumbers(cast<IntegerType>(TyL)->getBitWidth(),
4424d6fb72aSErik Eckstein                       cast<IntegerType>(TyR)->getBitWidth());
4434d6fb72aSErik Eckstein   // TyL == TyR would have returned true earlier, because types are uniqued.
4444d6fb72aSErik Eckstein   case Type::VoidTyID:
4454d6fb72aSErik Eckstein   case Type::FloatTyID:
4464d6fb72aSErik Eckstein   case Type::DoubleTyID:
4474d6fb72aSErik Eckstein   case Type::X86_FP80TyID:
4484d6fb72aSErik Eckstein   case Type::FP128TyID:
4494d6fb72aSErik Eckstein   case Type::PPC_FP128TyID:
4504d6fb72aSErik Eckstein   case Type::LabelTyID:
4514d6fb72aSErik Eckstein   case Type::MetadataTyID:
4524d6fb72aSErik Eckstein   case Type::TokenTyID:
4534d6fb72aSErik Eckstein     return 0;
4544d6fb72aSErik Eckstein 
455286d5897SEugene Zelenko   case Type::PointerTyID:
4564d6fb72aSErik Eckstein     assert(PTyL && PTyR && "Both types must be pointers here.");
4574d6fb72aSErik Eckstein     return cmpNumbers(PTyL->getAddressSpace(), PTyR->getAddressSpace());
4584d6fb72aSErik Eckstein 
4594d6fb72aSErik Eckstein   case Type::StructTyID: {
4604d6fb72aSErik Eckstein     StructType *STyL = cast<StructType>(TyL);
4614d6fb72aSErik Eckstein     StructType *STyR = cast<StructType>(TyR);
4624d6fb72aSErik Eckstein     if (STyL->getNumElements() != STyR->getNumElements())
4634d6fb72aSErik Eckstein       return cmpNumbers(STyL->getNumElements(), STyR->getNumElements());
4644d6fb72aSErik Eckstein 
4654d6fb72aSErik Eckstein     if (STyL->isPacked() != STyR->isPacked())
4664d6fb72aSErik Eckstein       return cmpNumbers(STyL->isPacked(), STyR->isPacked());
4674d6fb72aSErik Eckstein 
4684d6fb72aSErik Eckstein     for (unsigned i = 0, e = STyL->getNumElements(); i != e; ++i) {
4694d6fb72aSErik Eckstein       if (int Res = cmpTypes(STyL->getElementType(i), STyR->getElementType(i)))
4704d6fb72aSErik Eckstein         return Res;
4714d6fb72aSErik Eckstein     }
4724d6fb72aSErik Eckstein     return 0;
4734d6fb72aSErik Eckstein   }
4744d6fb72aSErik Eckstein 
4754d6fb72aSErik Eckstein   case Type::FunctionTyID: {
4764d6fb72aSErik Eckstein     FunctionType *FTyL = cast<FunctionType>(TyL);
4774d6fb72aSErik Eckstein     FunctionType *FTyR = cast<FunctionType>(TyR);
4784d6fb72aSErik Eckstein     if (FTyL->getNumParams() != FTyR->getNumParams())
4794d6fb72aSErik Eckstein       return cmpNumbers(FTyL->getNumParams(), FTyR->getNumParams());
4804d6fb72aSErik Eckstein 
4814d6fb72aSErik Eckstein     if (FTyL->isVarArg() != FTyR->isVarArg())
4824d6fb72aSErik Eckstein       return cmpNumbers(FTyL->isVarArg(), FTyR->isVarArg());
4834d6fb72aSErik Eckstein 
4844d6fb72aSErik Eckstein     if (int Res = cmpTypes(FTyL->getReturnType(), FTyR->getReturnType()))
4854d6fb72aSErik Eckstein       return Res;
4864d6fb72aSErik Eckstein 
4874d6fb72aSErik Eckstein     for (unsigned i = 0, e = FTyL->getNumParams(); i != e; ++i) {
4884d6fb72aSErik Eckstein       if (int Res = cmpTypes(FTyL->getParamType(i), FTyR->getParamType(i)))
4894d6fb72aSErik Eckstein         return Res;
4904d6fb72aSErik Eckstein     }
4914d6fb72aSErik Eckstein     return 0;
4924d6fb72aSErik Eckstein   }
4934d6fb72aSErik Eckstein 
49468b03aeeSEli Friedman   case Type::ArrayTyID: {
49568b03aeeSEli Friedman     auto *STyL = cast<ArrayType>(TyL);
49668b03aeeSEli Friedman     auto *STyR = cast<ArrayType>(TyR);
497bc070524SPeter Collingbourne     if (STyL->getNumElements() != STyR->getNumElements())
498bc070524SPeter Collingbourne       return cmpNumbers(STyL->getNumElements(), STyR->getNumElements());
499bc070524SPeter Collingbourne     return cmpTypes(STyL->getElementType(), STyR->getElementType());
5004d6fb72aSErik Eckstein   }
5012dea3f12SChristopher Tetreault   case Type::FixedVectorTyID:
5022dea3f12SChristopher Tetreault   case Type::ScalableVectorTyID: {
50368b03aeeSEli Friedman     auto *STyL = cast<VectorType>(TyL);
50468b03aeeSEli Friedman     auto *STyR = cast<VectorType>(TyR);
505f4257c58SDavid Sherwood     if (STyL->getElementCount().isScalable() !=
506f4257c58SDavid Sherwood         STyR->getElementCount().isScalable())
507f4257c58SDavid Sherwood       return cmpNumbers(STyL->getElementCount().isScalable(),
508f4257c58SDavid Sherwood                         STyR->getElementCount().isScalable());
509f4257c58SDavid Sherwood     if (STyL->getElementCount() != STyR->getElementCount())
510f4257c58SDavid Sherwood       return cmpNumbers(STyL->getElementCount().getKnownMinValue(),
511f4257c58SDavid Sherwood                         STyR->getElementCount().getKnownMinValue());
51268b03aeeSEli Friedman     return cmpTypes(STyL->getElementType(), STyR->getElementType());
51368b03aeeSEli Friedman   }
5144d6fb72aSErik Eckstein   }
5154d6fb72aSErik Eckstein }
5164d6fb72aSErik Eckstein 
5174d6fb72aSErik Eckstein // Determine whether the two operations are the same except that pointer-to-A
5184d6fb72aSErik Eckstein // and pointer-to-B are equivalent. This should be kept in sync with
5194d6fb72aSErik Eckstein // Instruction::isSameOperationAs.
5204d6fb72aSErik Eckstein // Read method declaration comments for more details.
cmpOperations(const Instruction * L,const Instruction * R,bool & needToCmpOperands) const5214d6fb72aSErik Eckstein int FunctionComparator::cmpOperations(const Instruction *L,
5224d6fb72aSErik Eckstein                                       const Instruction *R,
5234d6fb72aSErik Eckstein                                       bool &needToCmpOperands) const {
5244d6fb72aSErik Eckstein   needToCmpOperands = true;
5254d6fb72aSErik Eckstein   if (int Res = cmpValues(L, R))
5264d6fb72aSErik Eckstein     return Res;
5274d6fb72aSErik Eckstein 
5284d6fb72aSErik Eckstein   // Differences from Instruction::isSameOperationAs:
5294d6fb72aSErik Eckstein   //  * replace type comparison with calls to cmpTypes.
5304d6fb72aSErik Eckstein   //  * we test for I->getRawSubclassOptionalData (nuw/nsw/tail) at the top.
5314d6fb72aSErik Eckstein   //  * because of the above, we don't test for the tail bit on calls later on.
5324d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L->getOpcode(), R->getOpcode()))
5334d6fb72aSErik Eckstein     return Res;
5344d6fb72aSErik Eckstein 
5354d6fb72aSErik Eckstein   if (const GetElementPtrInst *GEPL = dyn_cast<GetElementPtrInst>(L)) {
5364d6fb72aSErik Eckstein     needToCmpOperands = false;
5374d6fb72aSErik Eckstein     const GetElementPtrInst *GEPR = cast<GetElementPtrInst>(R);
5384d6fb72aSErik Eckstein     if (int Res =
5394d6fb72aSErik Eckstein             cmpValues(GEPL->getPointerOperand(), GEPR->getPointerOperand()))
5404d6fb72aSErik Eckstein       return Res;
5414d6fb72aSErik Eckstein     return cmpGEPs(GEPL, GEPR);
5424d6fb72aSErik Eckstein   }
5434d6fb72aSErik Eckstein 
5444d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L->getNumOperands(), R->getNumOperands()))
5454d6fb72aSErik Eckstein     return Res;
5464d6fb72aSErik Eckstein 
5474d6fb72aSErik Eckstein   if (int Res = cmpTypes(L->getType(), R->getType()))
5484d6fb72aSErik Eckstein     return Res;
5494d6fb72aSErik Eckstein 
5504d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L->getRawSubclassOptionalData(),
5514d6fb72aSErik Eckstein                            R->getRawSubclassOptionalData()))
5524d6fb72aSErik Eckstein     return Res;
5534d6fb72aSErik Eckstein 
5544d6fb72aSErik Eckstein   // We have two instructions of identical opcode and #operands.  Check to see
5554d6fb72aSErik Eckstein   // if all operands are the same type
5564d6fb72aSErik Eckstein   for (unsigned i = 0, e = L->getNumOperands(); i != e; ++i) {
5574d6fb72aSErik Eckstein     if (int Res =
5584d6fb72aSErik Eckstein             cmpTypes(L->getOperand(i)->getType(), R->getOperand(i)->getType()))
5594d6fb72aSErik Eckstein       return Res;
5604d6fb72aSErik Eckstein   }
5614d6fb72aSErik Eckstein 
5624d6fb72aSErik Eckstein   // Check special state that is a part of some instructions.
5634d6fb72aSErik Eckstein   if (const AllocaInst *AI = dyn_cast<AllocaInst>(L)) {
5644d6fb72aSErik Eckstein     if (int Res = cmpTypes(AI->getAllocatedType(),
5654d6fb72aSErik Eckstein                            cast<AllocaInst>(R)->getAllocatedType()))
5664d6fb72aSErik Eckstein       return Res;
567*d5583366SArthur Eubanks     return cmpAligns(AI->getAlign(), cast<AllocaInst>(R)->getAlign());
5684d6fb72aSErik Eckstein   }
5694d6fb72aSErik Eckstein   if (const LoadInst *LI = dyn_cast<LoadInst>(L)) {
5704d6fb72aSErik Eckstein     if (int Res = cmpNumbers(LI->isVolatile(), cast<LoadInst>(R)->isVolatile()))
5714d6fb72aSErik Eckstein       return Res;
572*d5583366SArthur Eubanks     if (int Res = cmpAligns(LI->getAlign(), cast<LoadInst>(R)->getAlign()))
5734d6fb72aSErik Eckstein       return Res;
5744d6fb72aSErik Eckstein     if (int Res =
5754d6fb72aSErik Eckstein             cmpOrderings(LI->getOrdering(), cast<LoadInst>(R)->getOrdering()))
5764d6fb72aSErik Eckstein       return Res;
577bb80d3e1SKonstantin Zhuravlyov     if (int Res = cmpNumbers(LI->getSyncScopeID(),
578bb80d3e1SKonstantin Zhuravlyov                              cast<LoadInst>(R)->getSyncScopeID()))
5794d6fb72aSErik Eckstein       return Res;
5804213bc76SMircea Trofin     return cmpRangeMetadata(
5814213bc76SMircea Trofin         LI->getMetadata(LLVMContext::MD_range),
5824d6fb72aSErik Eckstein         cast<LoadInst>(R)->getMetadata(LLVMContext::MD_range));
5834d6fb72aSErik Eckstein   }
5844d6fb72aSErik Eckstein   if (const StoreInst *SI = dyn_cast<StoreInst>(L)) {
5854d6fb72aSErik Eckstein     if (int Res =
5864d6fb72aSErik Eckstein             cmpNumbers(SI->isVolatile(), cast<StoreInst>(R)->isVolatile()))
5874d6fb72aSErik Eckstein       return Res;
588*d5583366SArthur Eubanks     if (int Res = cmpAligns(SI->getAlign(), cast<StoreInst>(R)->getAlign()))
5894d6fb72aSErik Eckstein       return Res;
5904d6fb72aSErik Eckstein     if (int Res =
5914d6fb72aSErik Eckstein             cmpOrderings(SI->getOrdering(), cast<StoreInst>(R)->getOrdering()))
5924d6fb72aSErik Eckstein       return Res;
593bb80d3e1SKonstantin Zhuravlyov     return cmpNumbers(SI->getSyncScopeID(),
594bb80d3e1SKonstantin Zhuravlyov                       cast<StoreInst>(R)->getSyncScopeID());
5954d6fb72aSErik Eckstein   }
5964d6fb72aSErik Eckstein   if (const CmpInst *CI = dyn_cast<CmpInst>(L))
5974d6fb72aSErik Eckstein     return cmpNumbers(CI->getPredicate(), cast<CmpInst>(R)->getPredicate());
5984213bc76SMircea Trofin   if (auto *CBL = dyn_cast<CallBase>(L)) {
5994213bc76SMircea Trofin     auto *CBR = cast<CallBase>(R);
6004213bc76SMircea Trofin     if (int Res = cmpNumbers(CBL->getCallingConv(), CBR->getCallingConv()))
6014d6fb72aSErik Eckstein       return Res;
6024213bc76SMircea Trofin     if (int Res = cmpAttrs(CBL->getAttributes(), CBR->getAttributes()))
6034d6fb72aSErik Eckstein       return Res;
6043ab319b2SMircea Trofin     if (int Res = cmpOperandBundlesSchema(*CBL, *CBR))
6054d6fb72aSErik Eckstein       return Res;
606e21ab221SVedant Kumar     if (const CallInst *CI = dyn_cast<CallInst>(L))
607e21ab221SVedant Kumar       if (int Res = cmpNumbers(CI->getTailCallKind(),
608e21ab221SVedant Kumar                                cast<CallInst>(R)->getTailCallKind()))
6094d6fb72aSErik Eckstein         return Res;
610e21ab221SVedant Kumar     return cmpRangeMetadata(L->getMetadata(LLVMContext::MD_range),
611e21ab221SVedant Kumar                             R->getMetadata(LLVMContext::MD_range));
6124d6fb72aSErik Eckstein   }
6134d6fb72aSErik Eckstein   if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(L)) {
6144d6fb72aSErik Eckstein     ArrayRef<unsigned> LIndices = IVI->getIndices();
6154d6fb72aSErik Eckstein     ArrayRef<unsigned> RIndices = cast<InsertValueInst>(R)->getIndices();
6164d6fb72aSErik Eckstein     if (int Res = cmpNumbers(LIndices.size(), RIndices.size()))
6174d6fb72aSErik Eckstein       return Res;
6184d6fb72aSErik Eckstein     for (size_t i = 0, e = LIndices.size(); i != e; ++i) {
6194d6fb72aSErik Eckstein       if (int Res = cmpNumbers(LIndices[i], RIndices[i]))
6204d6fb72aSErik Eckstein         return Res;
6214d6fb72aSErik Eckstein     }
6224d6fb72aSErik Eckstein     return 0;
6234d6fb72aSErik Eckstein   }
6244d6fb72aSErik Eckstein   if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(L)) {
6254d6fb72aSErik Eckstein     ArrayRef<unsigned> LIndices = EVI->getIndices();
6264d6fb72aSErik Eckstein     ArrayRef<unsigned> RIndices = cast<ExtractValueInst>(R)->getIndices();
6274d6fb72aSErik Eckstein     if (int Res = cmpNumbers(LIndices.size(), RIndices.size()))
6284d6fb72aSErik Eckstein       return Res;
6294d6fb72aSErik Eckstein     for (size_t i = 0, e = LIndices.size(); i != e; ++i) {
6304d6fb72aSErik Eckstein       if (int Res = cmpNumbers(LIndices[i], RIndices[i]))
6314d6fb72aSErik Eckstein         return Res;
6324d6fb72aSErik Eckstein     }
6334d6fb72aSErik Eckstein   }
6344d6fb72aSErik Eckstein   if (const FenceInst *FI = dyn_cast<FenceInst>(L)) {
6354d6fb72aSErik Eckstein     if (int Res =
6364d6fb72aSErik Eckstein             cmpOrderings(FI->getOrdering(), cast<FenceInst>(R)->getOrdering()))
6374d6fb72aSErik Eckstein       return Res;
638bb80d3e1SKonstantin Zhuravlyov     return cmpNumbers(FI->getSyncScopeID(),
639bb80d3e1SKonstantin Zhuravlyov                       cast<FenceInst>(R)->getSyncScopeID());
6404d6fb72aSErik Eckstein   }
6414d6fb72aSErik Eckstein   if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(L)) {
6424d6fb72aSErik Eckstein     if (int Res = cmpNumbers(CXI->isVolatile(),
6434d6fb72aSErik Eckstein                              cast<AtomicCmpXchgInst>(R)->isVolatile()))
6444d6fb72aSErik Eckstein       return Res;
6454213bc76SMircea Trofin     if (int Res =
6464213bc76SMircea Trofin             cmpNumbers(CXI->isWeak(), cast<AtomicCmpXchgInst>(R)->isWeak()))
6474d6fb72aSErik Eckstein       return Res;
6484d6fb72aSErik Eckstein     if (int Res =
6494d6fb72aSErik Eckstein             cmpOrderings(CXI->getSuccessOrdering(),
6504d6fb72aSErik Eckstein                          cast<AtomicCmpXchgInst>(R)->getSuccessOrdering()))
6514d6fb72aSErik Eckstein       return Res;
6524d6fb72aSErik Eckstein     if (int Res =
6534d6fb72aSErik Eckstein             cmpOrderings(CXI->getFailureOrdering(),
6544d6fb72aSErik Eckstein                          cast<AtomicCmpXchgInst>(R)->getFailureOrdering()))
6554d6fb72aSErik Eckstein       return Res;
656bb80d3e1SKonstantin Zhuravlyov     return cmpNumbers(CXI->getSyncScopeID(),
657bb80d3e1SKonstantin Zhuravlyov                       cast<AtomicCmpXchgInst>(R)->getSyncScopeID());
6584d6fb72aSErik Eckstein   }
6594d6fb72aSErik Eckstein   if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(L)) {
6604d6fb72aSErik Eckstein     if (int Res = cmpNumbers(RMWI->getOperation(),
6614d6fb72aSErik Eckstein                              cast<AtomicRMWInst>(R)->getOperation()))
6624d6fb72aSErik Eckstein       return Res;
6634d6fb72aSErik Eckstein     if (int Res = cmpNumbers(RMWI->isVolatile(),
6644d6fb72aSErik Eckstein                              cast<AtomicRMWInst>(R)->isVolatile()))
6654d6fb72aSErik Eckstein       return Res;
6664d6fb72aSErik Eckstein     if (int Res = cmpOrderings(RMWI->getOrdering(),
6674d6fb72aSErik Eckstein                                cast<AtomicRMWInst>(R)->getOrdering()))
6684d6fb72aSErik Eckstein       return Res;
669bb80d3e1SKonstantin Zhuravlyov     return cmpNumbers(RMWI->getSyncScopeID(),
670bb80d3e1SKonstantin Zhuravlyov                       cast<AtomicRMWInst>(R)->getSyncScopeID());
6714d6fb72aSErik Eckstein   }
67260e9ee16SNikita Popov   if (const ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(L)) {
67360e9ee16SNikita Popov     ArrayRef<int> LMask = SVI->getShuffleMask();
67460e9ee16SNikita Popov     ArrayRef<int> RMask = cast<ShuffleVectorInst>(R)->getShuffleMask();
67560e9ee16SNikita Popov     if (int Res = cmpNumbers(LMask.size(), RMask.size()))
67660e9ee16SNikita Popov       return Res;
67760e9ee16SNikita Popov     for (size_t i = 0, e = LMask.size(); i != e; ++i) {
67860e9ee16SNikita Popov       if (int Res = cmpNumbers(LMask[i], RMask[i]))
67960e9ee16SNikita Popov         return Res;
68060e9ee16SNikita Popov     }
68160e9ee16SNikita Popov   }
6824d6fb72aSErik Eckstein   if (const PHINode *PNL = dyn_cast<PHINode>(L)) {
6834d6fb72aSErik Eckstein     const PHINode *PNR = cast<PHINode>(R);
6844d6fb72aSErik Eckstein     // Ensure that in addition to the incoming values being identical
6854d6fb72aSErik Eckstein     // (checked by the caller of this function), the incoming blocks
6864d6fb72aSErik Eckstein     // are also identical.
6874d6fb72aSErik Eckstein     for (unsigned i = 0, e = PNL->getNumIncomingValues(); i != e; ++i) {
6884d6fb72aSErik Eckstein       if (int Res =
6894d6fb72aSErik Eckstein               cmpValues(PNL->getIncomingBlock(i), PNR->getIncomingBlock(i)))
6904d6fb72aSErik Eckstein         return Res;
6914d6fb72aSErik Eckstein     }
6924d6fb72aSErik Eckstein   }
6934d6fb72aSErik Eckstein   return 0;
6944d6fb72aSErik Eckstein }
6954d6fb72aSErik Eckstein 
6964d6fb72aSErik Eckstein // Determine whether two GEP operations perform the same underlying arithmetic.
6974d6fb72aSErik Eckstein // Read method declaration comments for more details.
cmpGEPs(const GEPOperator * GEPL,const GEPOperator * GEPR) const6984d6fb72aSErik Eckstein int FunctionComparator::cmpGEPs(const GEPOperator *GEPL,
6994d6fb72aSErik Eckstein                                 const GEPOperator *GEPR) const {
7004d6fb72aSErik Eckstein   unsigned int ASL = GEPL->getPointerAddressSpace();
7014d6fb72aSErik Eckstein   unsigned int ASR = GEPR->getPointerAddressSpace();
7024d6fb72aSErik Eckstein 
7034d6fb72aSErik Eckstein   if (int Res = cmpNumbers(ASL, ASR))
7044d6fb72aSErik Eckstein     return Res;
7054d6fb72aSErik Eckstein 
7064d6fb72aSErik Eckstein   // When we have target data, we can reduce the GEP down to the value in bytes
7074d6fb72aSErik Eckstein   // added to the address.
7084d6fb72aSErik Eckstein   const DataLayout &DL = FnL->getParent()->getDataLayout();
7094d6fb72aSErik Eckstein   unsigned BitWidth = DL.getPointerSizeInBits(ASL);
7104d6fb72aSErik Eckstein   APInt OffsetL(BitWidth, 0), OffsetR(BitWidth, 0);
7114d6fb72aSErik Eckstein   if (GEPL->accumulateConstantOffset(DL, OffsetL) &&
7124d6fb72aSErik Eckstein       GEPR->accumulateConstantOffset(DL, OffsetR))
7134d6fb72aSErik Eckstein     return cmpAPInts(OffsetL, OffsetR);
7144213bc76SMircea Trofin   if (int Res =
7154213bc76SMircea Trofin           cmpTypes(GEPL->getSourceElementType(), GEPR->getSourceElementType()))
7164d6fb72aSErik Eckstein     return Res;
7174d6fb72aSErik Eckstein 
7184d6fb72aSErik Eckstein   if (int Res = cmpNumbers(GEPL->getNumOperands(), GEPR->getNumOperands()))
7194d6fb72aSErik Eckstein     return Res;
7204d6fb72aSErik Eckstein 
7214d6fb72aSErik Eckstein   for (unsigned i = 0, e = GEPL->getNumOperands(); i != e; ++i) {
7224d6fb72aSErik Eckstein     if (int Res = cmpValues(GEPL->getOperand(i), GEPR->getOperand(i)))
7234d6fb72aSErik Eckstein       return Res;
7244d6fb72aSErik Eckstein   }
7254d6fb72aSErik Eckstein 
7264d6fb72aSErik Eckstein   return 0;
7274d6fb72aSErik Eckstein }
7284d6fb72aSErik Eckstein 
cmpInlineAsm(const InlineAsm * L,const InlineAsm * R) const7294d6fb72aSErik Eckstein int FunctionComparator::cmpInlineAsm(const InlineAsm *L,
7304d6fb72aSErik Eckstein                                      const InlineAsm *R) const {
7314d6fb72aSErik Eckstein   // InlineAsm's are uniqued. If they are the same pointer, obviously they are
7324d6fb72aSErik Eckstein   // the same, otherwise compare the fields.
7334d6fb72aSErik Eckstein   if (L == R)
7344d6fb72aSErik Eckstein     return 0;
7354d6fb72aSErik Eckstein   if (int Res = cmpTypes(L->getFunctionType(), R->getFunctionType()))
7364d6fb72aSErik Eckstein     return Res;
7374d6fb72aSErik Eckstein   if (int Res = cmpMem(L->getAsmString(), R->getAsmString()))
7384d6fb72aSErik Eckstein     return Res;
7394d6fb72aSErik Eckstein   if (int Res = cmpMem(L->getConstraintString(), R->getConstraintString()))
7404d6fb72aSErik Eckstein     return Res;
7414d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L->hasSideEffects(), R->hasSideEffects()))
7424d6fb72aSErik Eckstein     return Res;
7434d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L->isAlignStack(), R->isAlignStack()))
7444d6fb72aSErik Eckstein     return Res;
7454d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L->getDialect(), R->getDialect()))
7464d6fb72aSErik Eckstein     return Res;
74768403564Swhitequark   assert(L->getFunctionType() != R->getFunctionType());
7484d6fb72aSErik Eckstein   return 0;
7494d6fb72aSErik Eckstein }
7504d6fb72aSErik Eckstein 
7514d6fb72aSErik Eckstein /// Compare two values used by the two functions under pair-wise comparison. If
7524d6fb72aSErik Eckstein /// this is the first time the values are seen, they're added to the mapping so
7534d6fb72aSErik Eckstein /// that we will detect mismatches on next use.
7544d6fb72aSErik Eckstein /// See comments in declaration for more details.
cmpValues(const Value * L,const Value * R) const7554d6fb72aSErik Eckstein int FunctionComparator::cmpValues(const Value *L, const Value *R) const {
7564d6fb72aSErik Eckstein   // Catch self-reference case.
7574d6fb72aSErik Eckstein   if (L == FnL) {
7584d6fb72aSErik Eckstein     if (R == FnR)
7594d6fb72aSErik Eckstein       return 0;
7604d6fb72aSErik Eckstein     return -1;
7614d6fb72aSErik Eckstein   }
7624d6fb72aSErik Eckstein   if (R == FnR) {
7634d6fb72aSErik Eckstein     if (L == FnL)
7644d6fb72aSErik Eckstein       return 0;
7654d6fb72aSErik Eckstein     return 1;
7664d6fb72aSErik Eckstein   }
7674d6fb72aSErik Eckstein 
7684d6fb72aSErik Eckstein   const Constant *ConstL = dyn_cast<Constant>(L);
7694d6fb72aSErik Eckstein   const Constant *ConstR = dyn_cast<Constant>(R);
7704d6fb72aSErik Eckstein   if (ConstL && ConstR) {
7714d6fb72aSErik Eckstein     if (L == R)
7724d6fb72aSErik Eckstein       return 0;
7734d6fb72aSErik Eckstein     return cmpConstants(ConstL, ConstR);
7744d6fb72aSErik Eckstein   }
7754d6fb72aSErik Eckstein 
7764d6fb72aSErik Eckstein   if (ConstL)
7774d6fb72aSErik Eckstein     return 1;
7784d6fb72aSErik Eckstein   if (ConstR)
7794d6fb72aSErik Eckstein     return -1;
7804d6fb72aSErik Eckstein 
7814d6fb72aSErik Eckstein   const InlineAsm *InlineAsmL = dyn_cast<InlineAsm>(L);
7824d6fb72aSErik Eckstein   const InlineAsm *InlineAsmR = dyn_cast<InlineAsm>(R);
7834d6fb72aSErik Eckstein 
7844d6fb72aSErik Eckstein   if (InlineAsmL && InlineAsmR)
7854d6fb72aSErik Eckstein     return cmpInlineAsm(InlineAsmL, InlineAsmR);
7864d6fb72aSErik Eckstein   if (InlineAsmL)
7874d6fb72aSErik Eckstein     return 1;
7884d6fb72aSErik Eckstein   if (InlineAsmR)
7894d6fb72aSErik Eckstein     return -1;
7904d6fb72aSErik Eckstein 
7914d6fb72aSErik Eckstein   auto LeftSN = sn_mapL.insert(std::make_pair(L, sn_mapL.size())),
7924d6fb72aSErik Eckstein        RightSN = sn_mapR.insert(std::make_pair(R, sn_mapR.size()));
7934d6fb72aSErik Eckstein 
7944d6fb72aSErik Eckstein   return cmpNumbers(LeftSN.first->second, RightSN.first->second);
7954d6fb72aSErik Eckstein }
7964d6fb72aSErik Eckstein 
7974d6fb72aSErik Eckstein // Test whether two basic blocks have equivalent behaviour.
cmpBasicBlocks(const BasicBlock * BBL,const BasicBlock * BBR) const7984d6fb72aSErik Eckstein int FunctionComparator::cmpBasicBlocks(const BasicBlock *BBL,
7994d6fb72aSErik Eckstein                                        const BasicBlock *BBR) const {
8004d6fb72aSErik Eckstein   BasicBlock::const_iterator InstL = BBL->begin(), InstLE = BBL->end();
8014d6fb72aSErik Eckstein   BasicBlock::const_iterator InstR = BBR->begin(), InstRE = BBR->end();
8024d6fb72aSErik Eckstein 
8034d6fb72aSErik Eckstein   do {
8044d6fb72aSErik Eckstein     bool needToCmpOperands = true;
8054d6fb72aSErik Eckstein     if (int Res = cmpOperations(&*InstL, &*InstR, needToCmpOperands))
8064d6fb72aSErik Eckstein       return Res;
8074d6fb72aSErik Eckstein     if (needToCmpOperands) {
8084d6fb72aSErik Eckstein       assert(InstL->getNumOperands() == InstR->getNumOperands());
8094d6fb72aSErik Eckstein 
8104d6fb72aSErik Eckstein       for (unsigned i = 0, e = InstL->getNumOperands(); i != e; ++i) {
8114d6fb72aSErik Eckstein         Value *OpL = InstL->getOperand(i);
8124d6fb72aSErik Eckstein         Value *OpR = InstR->getOperand(i);
8134d6fb72aSErik Eckstein         if (int Res = cmpValues(OpL, OpR))
8144d6fb72aSErik Eckstein           return Res;
8154d6fb72aSErik Eckstein         // cmpValues should ensure this is true.
8164d6fb72aSErik Eckstein         assert(cmpTypes(OpL->getType(), OpR->getType()) == 0);
8174d6fb72aSErik Eckstein       }
8184d6fb72aSErik Eckstein     }
8194d6fb72aSErik Eckstein 
8204d6fb72aSErik Eckstein     ++InstL;
8214d6fb72aSErik Eckstein     ++InstR;
8224d6fb72aSErik Eckstein   } while (InstL != InstLE && InstR != InstRE);
8234d6fb72aSErik Eckstein 
8244d6fb72aSErik Eckstein   if (InstL != InstLE && InstR == InstRE)
8254d6fb72aSErik Eckstein     return 1;
8264d6fb72aSErik Eckstein   if (InstL == InstLE && InstR != InstRE)
8274d6fb72aSErik Eckstein     return -1;
8284d6fb72aSErik Eckstein   return 0;
8294d6fb72aSErik Eckstein }
8304d6fb72aSErik Eckstein 
compareSignature() const8314d6fb72aSErik Eckstein int FunctionComparator::compareSignature() const {
8324d6fb72aSErik Eckstein   if (int Res = cmpAttrs(FnL->getAttributes(), FnR->getAttributes()))
8334d6fb72aSErik Eckstein     return Res;
8344d6fb72aSErik Eckstein 
8354d6fb72aSErik Eckstein   if (int Res = cmpNumbers(FnL->hasGC(), FnR->hasGC()))
8364d6fb72aSErik Eckstein     return Res;
8374d6fb72aSErik Eckstein 
8384d6fb72aSErik Eckstein   if (FnL->hasGC()) {
8394d6fb72aSErik Eckstein     if (int Res = cmpMem(FnL->getGC(), FnR->getGC()))
8404d6fb72aSErik Eckstein       return Res;
8414d6fb72aSErik Eckstein   }
8424d6fb72aSErik Eckstein 
8434d6fb72aSErik Eckstein   if (int Res = cmpNumbers(FnL->hasSection(), FnR->hasSection()))
8444d6fb72aSErik Eckstein     return Res;
8454d6fb72aSErik Eckstein 
8464d6fb72aSErik Eckstein   if (FnL->hasSection()) {
8474d6fb72aSErik Eckstein     if (int Res = cmpMem(FnL->getSection(), FnR->getSection()))
8484d6fb72aSErik Eckstein       return Res;
8494d6fb72aSErik Eckstein   }
8504d6fb72aSErik Eckstein 
8514d6fb72aSErik Eckstein   if (int Res = cmpNumbers(FnL->isVarArg(), FnR->isVarArg()))
8524d6fb72aSErik Eckstein     return Res;
8534d6fb72aSErik Eckstein 
8544d6fb72aSErik Eckstein   // TODO: if it's internal and only used in direct calls, we could handle this
8554d6fb72aSErik Eckstein   // case too.
8564d6fb72aSErik Eckstein   if (int Res = cmpNumbers(FnL->getCallingConv(), FnR->getCallingConv()))
8574d6fb72aSErik Eckstein     return Res;
8584d6fb72aSErik Eckstein 
8594d6fb72aSErik Eckstein   if (int Res = cmpTypes(FnL->getFunctionType(), FnR->getFunctionType()))
8604d6fb72aSErik Eckstein     return Res;
8614d6fb72aSErik Eckstein 
8624d6fb72aSErik Eckstein   assert(FnL->arg_size() == FnR->arg_size() &&
8634d6fb72aSErik Eckstein          "Identically typed functions have different numbers of args!");
8644d6fb72aSErik Eckstein 
8654d6fb72aSErik Eckstein   // Visit the arguments so that they get enumerated in the order they're
8664d6fb72aSErik Eckstein   // passed in.
8674d6fb72aSErik Eckstein   for (Function::const_arg_iterator ArgLI = FnL->arg_begin(),
8684d6fb72aSErik Eckstein                                     ArgRI = FnR->arg_begin(),
8694d6fb72aSErik Eckstein                                     ArgLE = FnL->arg_end();
8704d6fb72aSErik Eckstein        ArgLI != ArgLE; ++ArgLI, ++ArgRI) {
8714d6fb72aSErik Eckstein     if (cmpValues(&*ArgLI, &*ArgRI) != 0)
8724d6fb72aSErik Eckstein       llvm_unreachable("Arguments repeat!");
8734d6fb72aSErik Eckstein   }
8744d6fb72aSErik Eckstein   return 0;
8754d6fb72aSErik Eckstein }
8764d6fb72aSErik Eckstein 
8774d6fb72aSErik Eckstein // Test whether the two functions have equivalent behaviour.
compare()8784d6fb72aSErik Eckstein int FunctionComparator::compare() {
8794d6fb72aSErik Eckstein   beginCompare();
8804d6fb72aSErik Eckstein 
8814d6fb72aSErik Eckstein   if (int Res = compareSignature())
8824d6fb72aSErik Eckstein     return Res;
8834d6fb72aSErik Eckstein 
8844d6fb72aSErik Eckstein   // We do a CFG-ordered walk since the actual ordering of the blocks in the
8854d6fb72aSErik Eckstein   // linked list is immaterial. Our walk starts at the entry block for both
8864d6fb72aSErik Eckstein   // functions, then takes each block from each terminator in order. As an
8874d6fb72aSErik Eckstein   // artifact, this also means that unreachable blocks are ignored.
8884d6fb72aSErik Eckstein   SmallVector<const BasicBlock *, 8> FnLBBs, FnRBBs;
8894d6fb72aSErik Eckstein   SmallPtrSet<const BasicBlock *, 32> VisitedBBs; // in terms of F1.
8904d6fb72aSErik Eckstein 
8914d6fb72aSErik Eckstein   FnLBBs.push_back(&FnL->getEntryBlock());
8924d6fb72aSErik Eckstein   FnRBBs.push_back(&FnR->getEntryBlock());
8934d6fb72aSErik Eckstein 
8944d6fb72aSErik Eckstein   VisitedBBs.insert(FnLBBs[0]);
8954d6fb72aSErik Eckstein   while (!FnLBBs.empty()) {
8964d6fb72aSErik Eckstein     const BasicBlock *BBL = FnLBBs.pop_back_val();
8974d6fb72aSErik Eckstein     const BasicBlock *BBR = FnRBBs.pop_back_val();
8984d6fb72aSErik Eckstein 
8994d6fb72aSErik Eckstein     if (int Res = cmpValues(BBL, BBR))
9004d6fb72aSErik Eckstein       return Res;
9014d6fb72aSErik Eckstein 
9024d6fb72aSErik Eckstein     if (int Res = cmpBasicBlocks(BBL, BBR))
9034d6fb72aSErik Eckstein       return Res;
9044d6fb72aSErik Eckstein 
905edb12a83SChandler Carruth     const Instruction *TermL = BBL->getTerminator();
906edb12a83SChandler Carruth     const Instruction *TermR = BBR->getTerminator();
9074d6fb72aSErik Eckstein 
9084d6fb72aSErik Eckstein     assert(TermL->getNumSuccessors() == TermR->getNumSuccessors());
9094d6fb72aSErik Eckstein     for (unsigned i = 0, e = TermL->getNumSuccessors(); i != e; ++i) {
9104d6fb72aSErik Eckstein       if (!VisitedBBs.insert(TermL->getSuccessor(i)).second)
9114d6fb72aSErik Eckstein         continue;
9124d6fb72aSErik Eckstein 
9134d6fb72aSErik Eckstein       FnLBBs.push_back(TermL->getSuccessor(i));
9144d6fb72aSErik Eckstein       FnRBBs.push_back(TermR->getSuccessor(i));
9154d6fb72aSErik Eckstein     }
9164d6fb72aSErik Eckstein   }
9174d6fb72aSErik Eckstein   return 0;
9184d6fb72aSErik Eckstein }
9194d6fb72aSErik Eckstein 
9204d6fb72aSErik Eckstein namespace {
9214d6fb72aSErik Eckstein 
9224d6fb72aSErik Eckstein // Accumulate the hash of a sequence of 64-bit integers. This is similar to a
9234d6fb72aSErik Eckstein // hash of a sequence of 64bit ints, but the entire input does not need to be
9244d6fb72aSErik Eckstein // available at once. This interface is necessary for functionHash because it
9254d6fb72aSErik Eckstein // needs to accumulate the hash as the structure of the function is traversed
9264d6fb72aSErik Eckstein // without saving these values to an intermediate buffer. This form of hashing
9274d6fb72aSErik Eckstein // is not often needed, as usually the object to hash is just read from a
9284d6fb72aSErik Eckstein // buffer.
9294d6fb72aSErik Eckstein class HashAccumulator64 {
9304d6fb72aSErik Eckstein   uint64_t Hash;
931286d5897SEugene Zelenko 
9324d6fb72aSErik Eckstein public:
9334d6fb72aSErik Eckstein   // Initialize to random constant, so the state isn't zero.
HashAccumulator64()9344d6fb72aSErik Eckstein   HashAccumulator64() { Hash = 0x6acaa36bef8325c5ULL; }
935286d5897SEugene Zelenko 
add(uint64_t V)9364213bc76SMircea Trofin   void add(uint64_t V) { Hash = hashing::detail::hash_16_bytes(Hash, V); }
937286d5897SEugene Zelenko 
9384d6fb72aSErik Eckstein   // No finishing is required, because the entire hash value is used.
getHash()9394d6fb72aSErik Eckstein   uint64_t getHash() { return Hash; }
9404d6fb72aSErik Eckstein };
941286d5897SEugene Zelenko 
9424d6fb72aSErik Eckstein } // end anonymous namespace
9434d6fb72aSErik Eckstein 
9444d6fb72aSErik Eckstein // A function hash is calculated by considering only the number of arguments and
9454d6fb72aSErik Eckstein // whether a function is varargs, the order of basic blocks (given by the
9464d6fb72aSErik Eckstein // successors of each basic block in depth first order), and the order of
9474d6fb72aSErik Eckstein // opcodes of each instruction within each of these basic blocks. This mirrors
9484d6fb72aSErik Eckstein // the strategy compare() uses to compare functions by walking the BBs in depth
9494d6fb72aSErik Eckstein // first order and comparing each instruction in sequence. Because this hash
9504d6fb72aSErik Eckstein // does not look at the operands, it is insensitive to things such as the
9514d6fb72aSErik Eckstein // target of calls and the constants used in the function, which makes it useful
9524d6fb72aSErik Eckstein // when possibly merging functions which are the same modulo constants and call
9534d6fb72aSErik Eckstein // targets.
functionHash(Function & F)9544d6fb72aSErik Eckstein FunctionComparator::FunctionHash FunctionComparator::functionHash(Function &F) {
9554d6fb72aSErik Eckstein   HashAccumulator64 H;
9564d6fb72aSErik Eckstein   H.add(F.isVarArg());
9574d6fb72aSErik Eckstein   H.add(F.arg_size());
9584d6fb72aSErik Eckstein 
9594d6fb72aSErik Eckstein   SmallVector<const BasicBlock *, 8> BBs;
960a1cc8483SFlorian Hahn   SmallPtrSet<const BasicBlock *, 16> VisitedBBs;
9614d6fb72aSErik Eckstein 
9624d6fb72aSErik Eckstein   // Walk the blocks in the same order as FunctionComparator::cmpBasicBlocks(),
9634d6fb72aSErik Eckstein   // accumulating the hash of the function "structure." (BB and opcode sequence)
9644d6fb72aSErik Eckstein   BBs.push_back(&F.getEntryBlock());
9654d6fb72aSErik Eckstein   VisitedBBs.insert(BBs[0]);
9664d6fb72aSErik Eckstein   while (!BBs.empty()) {
9674d6fb72aSErik Eckstein     const BasicBlock *BB = BBs.pop_back_val();
9684d6fb72aSErik Eckstein     // This random value acts as a block header, as otherwise the partition of
9694d6fb72aSErik Eckstein     // opcodes into BBs wouldn't affect the hash, only the order of the opcodes
9704d6fb72aSErik Eckstein     H.add(45798);
9714d6fb72aSErik Eckstein     for (auto &Inst : *BB) {
9724d6fb72aSErik Eckstein       H.add(Inst.getOpcode());
9734d6fb72aSErik Eckstein     }
974edb12a83SChandler Carruth     const Instruction *Term = BB->getTerminator();
9754d6fb72aSErik Eckstein     for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
9764d6fb72aSErik Eckstein       if (!VisitedBBs.insert(Term->getSuccessor(i)).second)
9774d6fb72aSErik Eckstein         continue;
9784d6fb72aSErik Eckstein       BBs.push_back(Term->getSuccessor(i));
9794d6fb72aSErik Eckstein     }
9804d6fb72aSErik Eckstein   }
9814d6fb72aSErik Eckstein   return H.getHash();
9824d6fb72aSErik Eckstein }
983