1*4d6fb72aSErik Eckstein //===- FunctionComparator.h - Function Comparator -------------------------===//
2*4d6fb72aSErik Eckstein //
3*4d6fb72aSErik Eckstein //                     The LLVM Compiler Infrastructure
4*4d6fb72aSErik Eckstein //
5*4d6fb72aSErik Eckstein // This file is distributed under the University of Illinois Open Source
6*4d6fb72aSErik Eckstein // License. See LICENSE.TXT for details.
7*4d6fb72aSErik Eckstein //
8*4d6fb72aSErik Eckstein //===----------------------------------------------------------------------===//
9*4d6fb72aSErik Eckstein //
10*4d6fb72aSErik Eckstein // This file implements the FunctionComparator and GlobalNumberState classes
11*4d6fb72aSErik Eckstein // which are used by the MergeFunctions pass for comparing functions.
12*4d6fb72aSErik Eckstein //
13*4d6fb72aSErik Eckstein //===----------------------------------------------------------------------===//
14*4d6fb72aSErik Eckstein 
15*4d6fb72aSErik Eckstein #include "llvm/Transforms/Utils/FunctionComparator.h"
16*4d6fb72aSErik Eckstein #include "llvm/ADT/SmallSet.h"
17*4d6fb72aSErik Eckstein #include "llvm/IR/CallSite.h"
18*4d6fb72aSErik Eckstein #include "llvm/IR/Instructions.h"
19*4d6fb72aSErik Eckstein #include "llvm/IR/InlineAsm.h"
20*4d6fb72aSErik Eckstein #include "llvm/IR/Module.h"
21*4d6fb72aSErik Eckstein #include "llvm/Support/Debug.h"
22*4d6fb72aSErik Eckstein #include "llvm/Support/raw_ostream.h"
23*4d6fb72aSErik Eckstein 
24*4d6fb72aSErik Eckstein using namespace llvm;
25*4d6fb72aSErik Eckstein 
26*4d6fb72aSErik Eckstein #define DEBUG_TYPE "functioncomparator"
27*4d6fb72aSErik Eckstein 
28*4d6fb72aSErik Eckstein int FunctionComparator::cmpNumbers(uint64_t L, uint64_t R) const {
29*4d6fb72aSErik Eckstein   if (L < R) return -1;
30*4d6fb72aSErik Eckstein   if (L > R) return 1;
31*4d6fb72aSErik Eckstein   return 0;
32*4d6fb72aSErik Eckstein }
33*4d6fb72aSErik Eckstein 
34*4d6fb72aSErik Eckstein int FunctionComparator::cmpOrderings(AtomicOrdering L, AtomicOrdering R) const {
35*4d6fb72aSErik Eckstein   if ((int)L < (int)R) return -1;
36*4d6fb72aSErik Eckstein   if ((int)L > (int)R) return 1;
37*4d6fb72aSErik Eckstein   return 0;
38*4d6fb72aSErik Eckstein }
39*4d6fb72aSErik Eckstein 
40*4d6fb72aSErik Eckstein int FunctionComparator::cmpAPInts(const APInt &L, const APInt &R) const {
41*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L.getBitWidth(), R.getBitWidth()))
42*4d6fb72aSErik Eckstein     return Res;
43*4d6fb72aSErik Eckstein   if (L.ugt(R)) return 1;
44*4d6fb72aSErik Eckstein   if (R.ugt(L)) return -1;
45*4d6fb72aSErik Eckstein   return 0;
46*4d6fb72aSErik Eckstein }
47*4d6fb72aSErik Eckstein 
48*4d6fb72aSErik Eckstein int FunctionComparator::cmpAPFloats(const APFloat &L, const APFloat &R) const {
49*4d6fb72aSErik Eckstein   // Floats are ordered first by semantics (i.e. float, double, half, etc.),
50*4d6fb72aSErik Eckstein   // then by value interpreted as a bitstring (aka APInt).
51*4d6fb72aSErik Eckstein   const fltSemantics &SL = L.getSemantics(), &SR = R.getSemantics();
52*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(APFloat::semanticsPrecision(SL),
53*4d6fb72aSErik Eckstein                            APFloat::semanticsPrecision(SR)))
54*4d6fb72aSErik Eckstein     return Res;
55*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(APFloat::semanticsMaxExponent(SL),
56*4d6fb72aSErik Eckstein                            APFloat::semanticsMaxExponent(SR)))
57*4d6fb72aSErik Eckstein     return Res;
58*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(APFloat::semanticsMinExponent(SL),
59*4d6fb72aSErik Eckstein                            APFloat::semanticsMinExponent(SR)))
60*4d6fb72aSErik Eckstein     return Res;
61*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(APFloat::semanticsSizeInBits(SL),
62*4d6fb72aSErik Eckstein                            APFloat::semanticsSizeInBits(SR)))
63*4d6fb72aSErik Eckstein     return Res;
64*4d6fb72aSErik Eckstein   return cmpAPInts(L.bitcastToAPInt(), R.bitcastToAPInt());
65*4d6fb72aSErik Eckstein }
66*4d6fb72aSErik Eckstein 
67*4d6fb72aSErik Eckstein int FunctionComparator::cmpMem(StringRef L, StringRef R) const {
68*4d6fb72aSErik Eckstein   // Prevent heavy comparison, compare sizes first.
69*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L.size(), R.size()))
70*4d6fb72aSErik Eckstein     return Res;
71*4d6fb72aSErik Eckstein 
72*4d6fb72aSErik Eckstein   // Compare strings lexicographically only when it is necessary: only when
73*4d6fb72aSErik Eckstein   // strings are equal in size.
74*4d6fb72aSErik Eckstein   return L.compare(R);
75*4d6fb72aSErik Eckstein }
76*4d6fb72aSErik Eckstein 
77*4d6fb72aSErik Eckstein int FunctionComparator::cmpAttrs(const AttributeSet L,
78*4d6fb72aSErik Eckstein                                  const AttributeSet R) const {
79*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L.getNumSlots(), R.getNumSlots()))
80*4d6fb72aSErik Eckstein     return Res;
81*4d6fb72aSErik Eckstein 
82*4d6fb72aSErik Eckstein   for (unsigned i = 0, e = L.getNumSlots(); i != e; ++i) {
83*4d6fb72aSErik Eckstein     AttributeSet::iterator LI = L.begin(i), LE = L.end(i), RI = R.begin(i),
84*4d6fb72aSErik Eckstein                            RE = R.end(i);
85*4d6fb72aSErik Eckstein     for (; LI != LE && RI != RE; ++LI, ++RI) {
86*4d6fb72aSErik Eckstein       Attribute LA = *LI;
87*4d6fb72aSErik Eckstein       Attribute RA = *RI;
88*4d6fb72aSErik Eckstein       if (LA < RA)
89*4d6fb72aSErik Eckstein         return -1;
90*4d6fb72aSErik Eckstein       if (RA < LA)
91*4d6fb72aSErik Eckstein         return 1;
92*4d6fb72aSErik Eckstein     }
93*4d6fb72aSErik Eckstein     if (LI != LE)
94*4d6fb72aSErik Eckstein       return 1;
95*4d6fb72aSErik Eckstein     if (RI != RE)
96*4d6fb72aSErik Eckstein       return -1;
97*4d6fb72aSErik Eckstein   }
98*4d6fb72aSErik Eckstein   return 0;
99*4d6fb72aSErik Eckstein }
100*4d6fb72aSErik Eckstein 
101*4d6fb72aSErik Eckstein int FunctionComparator::cmpRangeMetadata(const MDNode *L,
102*4d6fb72aSErik Eckstein                                          const MDNode *R) const {
103*4d6fb72aSErik Eckstein   if (L == R)
104*4d6fb72aSErik Eckstein     return 0;
105*4d6fb72aSErik Eckstein   if (!L)
106*4d6fb72aSErik Eckstein     return -1;
107*4d6fb72aSErik Eckstein   if (!R)
108*4d6fb72aSErik Eckstein     return 1;
109*4d6fb72aSErik Eckstein   // Range metadata is a sequence of numbers. Make sure they are the same
110*4d6fb72aSErik Eckstein   // sequence.
111*4d6fb72aSErik Eckstein   // TODO: Note that as this is metadata, it is possible to drop and/or merge
112*4d6fb72aSErik Eckstein   // this data when considering functions to merge. Thus this comparison would
113*4d6fb72aSErik Eckstein   // return 0 (i.e. equivalent), but merging would become more complicated
114*4d6fb72aSErik Eckstein   // because the ranges would need to be unioned. It is not likely that
115*4d6fb72aSErik Eckstein   // functions differ ONLY in this metadata if they are actually the same
116*4d6fb72aSErik Eckstein   // function semantically.
117*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L->getNumOperands(), R->getNumOperands()))
118*4d6fb72aSErik Eckstein     return Res;
119*4d6fb72aSErik Eckstein   for (size_t I = 0; I < L->getNumOperands(); ++I) {
120*4d6fb72aSErik Eckstein     ConstantInt *LLow = mdconst::extract<ConstantInt>(L->getOperand(I));
121*4d6fb72aSErik Eckstein     ConstantInt *RLow = mdconst::extract<ConstantInt>(R->getOperand(I));
122*4d6fb72aSErik Eckstein     if (int Res = cmpAPInts(LLow->getValue(), RLow->getValue()))
123*4d6fb72aSErik Eckstein       return Res;
124*4d6fb72aSErik Eckstein   }
125*4d6fb72aSErik Eckstein   return 0;
126*4d6fb72aSErik Eckstein }
127*4d6fb72aSErik Eckstein 
128*4d6fb72aSErik Eckstein int FunctionComparator::cmpOperandBundlesSchema(const Instruction *L,
129*4d6fb72aSErik Eckstein                                                 const Instruction *R) const {
130*4d6fb72aSErik Eckstein   ImmutableCallSite LCS(L);
131*4d6fb72aSErik Eckstein   ImmutableCallSite RCS(R);
132*4d6fb72aSErik Eckstein 
133*4d6fb72aSErik Eckstein   assert(LCS && RCS && "Must be calls or invokes!");
134*4d6fb72aSErik Eckstein   assert(LCS.isCall() == RCS.isCall() && "Can't compare otherwise!");
135*4d6fb72aSErik Eckstein 
136*4d6fb72aSErik Eckstein   if (int Res =
137*4d6fb72aSErik Eckstein           cmpNumbers(LCS.getNumOperandBundles(), RCS.getNumOperandBundles()))
138*4d6fb72aSErik Eckstein     return Res;
139*4d6fb72aSErik Eckstein 
140*4d6fb72aSErik Eckstein   for (unsigned i = 0, e = LCS.getNumOperandBundles(); i != e; ++i) {
141*4d6fb72aSErik Eckstein     auto OBL = LCS.getOperandBundleAt(i);
142*4d6fb72aSErik Eckstein     auto OBR = RCS.getOperandBundleAt(i);
143*4d6fb72aSErik Eckstein 
144*4d6fb72aSErik Eckstein     if (int Res = OBL.getTagName().compare(OBR.getTagName()))
145*4d6fb72aSErik Eckstein       return Res;
146*4d6fb72aSErik Eckstein 
147*4d6fb72aSErik Eckstein     if (int Res = cmpNumbers(OBL.Inputs.size(), OBR.Inputs.size()))
148*4d6fb72aSErik Eckstein       return Res;
149*4d6fb72aSErik Eckstein   }
150*4d6fb72aSErik Eckstein 
151*4d6fb72aSErik Eckstein   return 0;
152*4d6fb72aSErik Eckstein }
153*4d6fb72aSErik Eckstein 
154*4d6fb72aSErik Eckstein /// Constants comparison:
155*4d6fb72aSErik Eckstein /// 1. Check whether type of L constant could be losslessly bitcasted to R
156*4d6fb72aSErik Eckstein /// type.
157*4d6fb72aSErik Eckstein /// 2. Compare constant contents.
158*4d6fb72aSErik Eckstein /// For more details see declaration comments.
159*4d6fb72aSErik Eckstein int FunctionComparator::cmpConstants(const Constant *L,
160*4d6fb72aSErik Eckstein                                      const Constant *R) const {
161*4d6fb72aSErik Eckstein 
162*4d6fb72aSErik Eckstein   Type *TyL = L->getType();
163*4d6fb72aSErik Eckstein   Type *TyR = R->getType();
164*4d6fb72aSErik Eckstein 
165*4d6fb72aSErik Eckstein   // Check whether types are bitcastable. This part is just re-factored
166*4d6fb72aSErik Eckstein   // Type::canLosslesslyBitCastTo method, but instead of returning true/false,
167*4d6fb72aSErik Eckstein   // we also pack into result which type is "less" for us.
168*4d6fb72aSErik Eckstein   int TypesRes = cmpTypes(TyL, TyR);
169*4d6fb72aSErik Eckstein   if (TypesRes != 0) {
170*4d6fb72aSErik Eckstein     // Types are different, but check whether we can bitcast them.
171*4d6fb72aSErik Eckstein     if (!TyL->isFirstClassType()) {
172*4d6fb72aSErik Eckstein       if (TyR->isFirstClassType())
173*4d6fb72aSErik Eckstein         return -1;
174*4d6fb72aSErik Eckstein       // Neither TyL nor TyR are values of first class type. Return the result
175*4d6fb72aSErik Eckstein       // of comparing the types
176*4d6fb72aSErik Eckstein       return TypesRes;
177*4d6fb72aSErik Eckstein     }
178*4d6fb72aSErik Eckstein     if (!TyR->isFirstClassType()) {
179*4d6fb72aSErik Eckstein       if (TyL->isFirstClassType())
180*4d6fb72aSErik Eckstein         return 1;
181*4d6fb72aSErik Eckstein       return TypesRes;
182*4d6fb72aSErik Eckstein     }
183*4d6fb72aSErik Eckstein 
184*4d6fb72aSErik Eckstein     // Vector -> Vector conversions are always lossless if the two vector types
185*4d6fb72aSErik Eckstein     // have the same size, otherwise not.
186*4d6fb72aSErik Eckstein     unsigned TyLWidth = 0;
187*4d6fb72aSErik Eckstein     unsigned TyRWidth = 0;
188*4d6fb72aSErik Eckstein 
189*4d6fb72aSErik Eckstein     if (auto *VecTyL = dyn_cast<VectorType>(TyL))
190*4d6fb72aSErik Eckstein       TyLWidth = VecTyL->getBitWidth();
191*4d6fb72aSErik Eckstein     if (auto *VecTyR = dyn_cast<VectorType>(TyR))
192*4d6fb72aSErik Eckstein       TyRWidth = VecTyR->getBitWidth();
193*4d6fb72aSErik Eckstein 
194*4d6fb72aSErik Eckstein     if (TyLWidth != TyRWidth)
195*4d6fb72aSErik Eckstein       return cmpNumbers(TyLWidth, TyRWidth);
196*4d6fb72aSErik Eckstein 
197*4d6fb72aSErik Eckstein     // Zero bit-width means neither TyL nor TyR are vectors.
198*4d6fb72aSErik Eckstein     if (!TyLWidth) {
199*4d6fb72aSErik Eckstein       PointerType *PTyL = dyn_cast<PointerType>(TyL);
200*4d6fb72aSErik Eckstein       PointerType *PTyR = dyn_cast<PointerType>(TyR);
201*4d6fb72aSErik Eckstein       if (PTyL && PTyR) {
202*4d6fb72aSErik Eckstein         unsigned AddrSpaceL = PTyL->getAddressSpace();
203*4d6fb72aSErik Eckstein         unsigned AddrSpaceR = PTyR->getAddressSpace();
204*4d6fb72aSErik Eckstein         if (int Res = cmpNumbers(AddrSpaceL, AddrSpaceR))
205*4d6fb72aSErik Eckstein           return Res;
206*4d6fb72aSErik Eckstein       }
207*4d6fb72aSErik Eckstein       if (PTyL)
208*4d6fb72aSErik Eckstein         return 1;
209*4d6fb72aSErik Eckstein       if (PTyR)
210*4d6fb72aSErik Eckstein         return -1;
211*4d6fb72aSErik Eckstein 
212*4d6fb72aSErik Eckstein       // TyL and TyR aren't vectors, nor pointers. We don't know how to
213*4d6fb72aSErik Eckstein       // bitcast them.
214*4d6fb72aSErik Eckstein       return TypesRes;
215*4d6fb72aSErik Eckstein     }
216*4d6fb72aSErik Eckstein   }
217*4d6fb72aSErik Eckstein 
218*4d6fb72aSErik Eckstein   // OK, types are bitcastable, now check constant contents.
219*4d6fb72aSErik Eckstein 
220*4d6fb72aSErik Eckstein   if (L->isNullValue() && R->isNullValue())
221*4d6fb72aSErik Eckstein     return TypesRes;
222*4d6fb72aSErik Eckstein   if (L->isNullValue() && !R->isNullValue())
223*4d6fb72aSErik Eckstein     return 1;
224*4d6fb72aSErik Eckstein   if (!L->isNullValue() && R->isNullValue())
225*4d6fb72aSErik Eckstein     return -1;
226*4d6fb72aSErik Eckstein 
227*4d6fb72aSErik Eckstein   auto GlobalValueL = const_cast<GlobalValue*>(dyn_cast<GlobalValue>(L));
228*4d6fb72aSErik Eckstein   auto GlobalValueR = const_cast<GlobalValue*>(dyn_cast<GlobalValue>(R));
229*4d6fb72aSErik Eckstein   if (GlobalValueL && GlobalValueR) {
230*4d6fb72aSErik Eckstein     return cmpGlobalValues(GlobalValueL, GlobalValueR);
231*4d6fb72aSErik Eckstein   }
232*4d6fb72aSErik Eckstein 
233*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L->getValueID(), R->getValueID()))
234*4d6fb72aSErik Eckstein     return Res;
235*4d6fb72aSErik Eckstein 
236*4d6fb72aSErik Eckstein   if (const auto *SeqL = dyn_cast<ConstantDataSequential>(L)) {
237*4d6fb72aSErik Eckstein     const auto *SeqR = cast<ConstantDataSequential>(R);
238*4d6fb72aSErik Eckstein     // This handles ConstantDataArray and ConstantDataVector. Note that we
239*4d6fb72aSErik Eckstein     // compare the two raw data arrays, which might differ depending on the host
240*4d6fb72aSErik Eckstein     // endianness. This isn't a problem though, because the endiness of a module
241*4d6fb72aSErik Eckstein     // will affect the order of the constants, but this order is the same
242*4d6fb72aSErik Eckstein     // for a given input module and host platform.
243*4d6fb72aSErik Eckstein     return cmpMem(SeqL->getRawDataValues(), SeqR->getRawDataValues());
244*4d6fb72aSErik Eckstein   }
245*4d6fb72aSErik Eckstein 
246*4d6fb72aSErik Eckstein   switch (L->getValueID()) {
247*4d6fb72aSErik Eckstein   case Value::UndefValueVal:
248*4d6fb72aSErik Eckstein   case Value::ConstantTokenNoneVal:
249*4d6fb72aSErik Eckstein     return TypesRes;
250*4d6fb72aSErik Eckstein   case Value::ConstantIntVal: {
251*4d6fb72aSErik Eckstein     const APInt &LInt = cast<ConstantInt>(L)->getValue();
252*4d6fb72aSErik Eckstein     const APInt &RInt = cast<ConstantInt>(R)->getValue();
253*4d6fb72aSErik Eckstein     return cmpAPInts(LInt, RInt);
254*4d6fb72aSErik Eckstein   }
255*4d6fb72aSErik Eckstein   case Value::ConstantFPVal: {
256*4d6fb72aSErik Eckstein     const APFloat &LAPF = cast<ConstantFP>(L)->getValueAPF();
257*4d6fb72aSErik Eckstein     const APFloat &RAPF = cast<ConstantFP>(R)->getValueAPF();
258*4d6fb72aSErik Eckstein     return cmpAPFloats(LAPF, RAPF);
259*4d6fb72aSErik Eckstein   }
260*4d6fb72aSErik Eckstein   case Value::ConstantArrayVal: {
261*4d6fb72aSErik Eckstein     const ConstantArray *LA = cast<ConstantArray>(L);
262*4d6fb72aSErik Eckstein     const ConstantArray *RA = cast<ConstantArray>(R);
263*4d6fb72aSErik Eckstein     uint64_t NumElementsL = cast<ArrayType>(TyL)->getNumElements();
264*4d6fb72aSErik Eckstein     uint64_t NumElementsR = cast<ArrayType>(TyR)->getNumElements();
265*4d6fb72aSErik Eckstein     if (int Res = cmpNumbers(NumElementsL, NumElementsR))
266*4d6fb72aSErik Eckstein       return Res;
267*4d6fb72aSErik Eckstein     for (uint64_t i = 0; i < NumElementsL; ++i) {
268*4d6fb72aSErik Eckstein       if (int Res = cmpConstants(cast<Constant>(LA->getOperand(i)),
269*4d6fb72aSErik Eckstein                                  cast<Constant>(RA->getOperand(i))))
270*4d6fb72aSErik Eckstein         return Res;
271*4d6fb72aSErik Eckstein     }
272*4d6fb72aSErik Eckstein     return 0;
273*4d6fb72aSErik Eckstein   }
274*4d6fb72aSErik Eckstein   case Value::ConstantStructVal: {
275*4d6fb72aSErik Eckstein     const ConstantStruct *LS = cast<ConstantStruct>(L);
276*4d6fb72aSErik Eckstein     const ConstantStruct *RS = cast<ConstantStruct>(R);
277*4d6fb72aSErik Eckstein     unsigned NumElementsL = cast<StructType>(TyL)->getNumElements();
278*4d6fb72aSErik Eckstein     unsigned NumElementsR = cast<StructType>(TyR)->getNumElements();
279*4d6fb72aSErik Eckstein     if (int Res = cmpNumbers(NumElementsL, NumElementsR))
280*4d6fb72aSErik Eckstein       return Res;
281*4d6fb72aSErik Eckstein     for (unsigned i = 0; i != NumElementsL; ++i) {
282*4d6fb72aSErik Eckstein       if (int Res = cmpConstants(cast<Constant>(LS->getOperand(i)),
283*4d6fb72aSErik Eckstein                                  cast<Constant>(RS->getOperand(i))))
284*4d6fb72aSErik Eckstein         return Res;
285*4d6fb72aSErik Eckstein     }
286*4d6fb72aSErik Eckstein     return 0;
287*4d6fb72aSErik Eckstein   }
288*4d6fb72aSErik Eckstein   case Value::ConstantVectorVal: {
289*4d6fb72aSErik Eckstein     const ConstantVector *LV = cast<ConstantVector>(L);
290*4d6fb72aSErik Eckstein     const ConstantVector *RV = cast<ConstantVector>(R);
291*4d6fb72aSErik Eckstein     unsigned NumElementsL = cast<VectorType>(TyL)->getNumElements();
292*4d6fb72aSErik Eckstein     unsigned NumElementsR = cast<VectorType>(TyR)->getNumElements();
293*4d6fb72aSErik Eckstein     if (int Res = cmpNumbers(NumElementsL, NumElementsR))
294*4d6fb72aSErik Eckstein       return Res;
295*4d6fb72aSErik Eckstein     for (uint64_t i = 0; i < NumElementsL; ++i) {
296*4d6fb72aSErik Eckstein       if (int Res = cmpConstants(cast<Constant>(LV->getOperand(i)),
297*4d6fb72aSErik Eckstein                                  cast<Constant>(RV->getOperand(i))))
298*4d6fb72aSErik Eckstein         return Res;
299*4d6fb72aSErik Eckstein     }
300*4d6fb72aSErik Eckstein     return 0;
301*4d6fb72aSErik Eckstein   }
302*4d6fb72aSErik Eckstein   case Value::ConstantExprVal: {
303*4d6fb72aSErik Eckstein     const ConstantExpr *LE = cast<ConstantExpr>(L);
304*4d6fb72aSErik Eckstein     const ConstantExpr *RE = cast<ConstantExpr>(R);
305*4d6fb72aSErik Eckstein     unsigned NumOperandsL = LE->getNumOperands();
306*4d6fb72aSErik Eckstein     unsigned NumOperandsR = RE->getNumOperands();
307*4d6fb72aSErik Eckstein     if (int Res = cmpNumbers(NumOperandsL, NumOperandsR))
308*4d6fb72aSErik Eckstein       return Res;
309*4d6fb72aSErik Eckstein     for (unsigned i = 0; i < NumOperandsL; ++i) {
310*4d6fb72aSErik Eckstein       if (int Res = cmpConstants(cast<Constant>(LE->getOperand(i)),
311*4d6fb72aSErik Eckstein                                  cast<Constant>(RE->getOperand(i))))
312*4d6fb72aSErik Eckstein         return Res;
313*4d6fb72aSErik Eckstein     }
314*4d6fb72aSErik Eckstein     return 0;
315*4d6fb72aSErik Eckstein   }
316*4d6fb72aSErik Eckstein   case Value::BlockAddressVal: {
317*4d6fb72aSErik Eckstein     const BlockAddress *LBA = cast<BlockAddress>(L);
318*4d6fb72aSErik Eckstein     const BlockAddress *RBA = cast<BlockAddress>(R);
319*4d6fb72aSErik Eckstein     if (int Res = cmpValues(LBA->getFunction(), RBA->getFunction()))
320*4d6fb72aSErik Eckstein       return Res;
321*4d6fb72aSErik Eckstein     if (LBA->getFunction() == RBA->getFunction()) {
322*4d6fb72aSErik Eckstein       // They are BBs in the same function. Order by which comes first in the
323*4d6fb72aSErik Eckstein       // BB order of the function. This order is deterministic.
324*4d6fb72aSErik Eckstein       Function* F = LBA->getFunction();
325*4d6fb72aSErik Eckstein       BasicBlock *LBB = LBA->getBasicBlock();
326*4d6fb72aSErik Eckstein       BasicBlock *RBB = RBA->getBasicBlock();
327*4d6fb72aSErik Eckstein       if (LBB == RBB)
328*4d6fb72aSErik Eckstein         return 0;
329*4d6fb72aSErik Eckstein       for(BasicBlock &BB : F->getBasicBlockList()) {
330*4d6fb72aSErik Eckstein         if (&BB == LBB) {
331*4d6fb72aSErik Eckstein           assert(&BB != RBB);
332*4d6fb72aSErik Eckstein           return -1;
333*4d6fb72aSErik Eckstein         }
334*4d6fb72aSErik Eckstein         if (&BB == RBB)
335*4d6fb72aSErik Eckstein           return 1;
336*4d6fb72aSErik Eckstein       }
337*4d6fb72aSErik Eckstein       llvm_unreachable("Basic Block Address does not point to a basic block in "
338*4d6fb72aSErik Eckstein                        "its function.");
339*4d6fb72aSErik Eckstein       return -1;
340*4d6fb72aSErik Eckstein     } else {
341*4d6fb72aSErik Eckstein       // cmpValues said the functions are the same. So because they aren't
342*4d6fb72aSErik Eckstein       // literally the same pointer, they must respectively be the left and
343*4d6fb72aSErik Eckstein       // right functions.
344*4d6fb72aSErik Eckstein       assert(LBA->getFunction() == FnL && RBA->getFunction() == FnR);
345*4d6fb72aSErik Eckstein       // cmpValues will tell us if these are equivalent BasicBlocks, in the
346*4d6fb72aSErik Eckstein       // context of their respective functions.
347*4d6fb72aSErik Eckstein       return cmpValues(LBA->getBasicBlock(), RBA->getBasicBlock());
348*4d6fb72aSErik Eckstein     }
349*4d6fb72aSErik Eckstein   }
350*4d6fb72aSErik Eckstein   default: // Unknown constant, abort.
351*4d6fb72aSErik Eckstein     DEBUG(dbgs() << "Looking at valueID " << L->getValueID() << "\n");
352*4d6fb72aSErik Eckstein     llvm_unreachable("Constant ValueID not recognized.");
353*4d6fb72aSErik Eckstein     return -1;
354*4d6fb72aSErik Eckstein   }
355*4d6fb72aSErik Eckstein }
356*4d6fb72aSErik Eckstein 
357*4d6fb72aSErik Eckstein int FunctionComparator::cmpGlobalValues(GlobalValue *L, GlobalValue *R) const {
358*4d6fb72aSErik Eckstein   return cmpNumbers(GlobalNumbers->getNumber(L), GlobalNumbers->getNumber(R));
359*4d6fb72aSErik Eckstein }
360*4d6fb72aSErik Eckstein 
361*4d6fb72aSErik Eckstein /// cmpType - compares two types,
362*4d6fb72aSErik Eckstein /// defines total ordering among the types set.
363*4d6fb72aSErik Eckstein /// See method declaration comments for more details.
364*4d6fb72aSErik Eckstein int FunctionComparator::cmpTypes(Type *TyL, Type *TyR) const {
365*4d6fb72aSErik Eckstein   PointerType *PTyL = dyn_cast<PointerType>(TyL);
366*4d6fb72aSErik Eckstein   PointerType *PTyR = dyn_cast<PointerType>(TyR);
367*4d6fb72aSErik Eckstein 
368*4d6fb72aSErik Eckstein   const DataLayout &DL = FnL->getParent()->getDataLayout();
369*4d6fb72aSErik Eckstein   if (PTyL && PTyL->getAddressSpace() == 0)
370*4d6fb72aSErik Eckstein     TyL = DL.getIntPtrType(TyL);
371*4d6fb72aSErik Eckstein   if (PTyR && PTyR->getAddressSpace() == 0)
372*4d6fb72aSErik Eckstein     TyR = DL.getIntPtrType(TyR);
373*4d6fb72aSErik Eckstein 
374*4d6fb72aSErik Eckstein   if (TyL == TyR)
375*4d6fb72aSErik Eckstein     return 0;
376*4d6fb72aSErik Eckstein 
377*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(TyL->getTypeID(), TyR->getTypeID()))
378*4d6fb72aSErik Eckstein     return Res;
379*4d6fb72aSErik Eckstein 
380*4d6fb72aSErik Eckstein   switch (TyL->getTypeID()) {
381*4d6fb72aSErik Eckstein   default:
382*4d6fb72aSErik Eckstein     llvm_unreachable("Unknown type!");
383*4d6fb72aSErik Eckstein     // Fall through in Release mode.
384*4d6fb72aSErik Eckstein     LLVM_FALLTHROUGH;
385*4d6fb72aSErik Eckstein   case Type::IntegerTyID:
386*4d6fb72aSErik Eckstein     return cmpNumbers(cast<IntegerType>(TyL)->getBitWidth(),
387*4d6fb72aSErik Eckstein                       cast<IntegerType>(TyR)->getBitWidth());
388*4d6fb72aSErik Eckstein   case Type::VectorTyID: {
389*4d6fb72aSErik Eckstein     VectorType *VTyL = cast<VectorType>(TyL), *VTyR = cast<VectorType>(TyR);
390*4d6fb72aSErik Eckstein     if (int Res = cmpNumbers(VTyL->getNumElements(), VTyR->getNumElements()))
391*4d6fb72aSErik Eckstein       return Res;
392*4d6fb72aSErik Eckstein     return cmpTypes(VTyL->getElementType(), VTyR->getElementType());
393*4d6fb72aSErik Eckstein   }
394*4d6fb72aSErik Eckstein   // TyL == TyR would have returned true earlier, because types are uniqued.
395*4d6fb72aSErik Eckstein   case Type::VoidTyID:
396*4d6fb72aSErik Eckstein   case Type::FloatTyID:
397*4d6fb72aSErik Eckstein   case Type::DoubleTyID:
398*4d6fb72aSErik Eckstein   case Type::X86_FP80TyID:
399*4d6fb72aSErik Eckstein   case Type::FP128TyID:
400*4d6fb72aSErik Eckstein   case Type::PPC_FP128TyID:
401*4d6fb72aSErik Eckstein   case Type::LabelTyID:
402*4d6fb72aSErik Eckstein   case Type::MetadataTyID:
403*4d6fb72aSErik Eckstein   case Type::TokenTyID:
404*4d6fb72aSErik Eckstein     return 0;
405*4d6fb72aSErik Eckstein 
406*4d6fb72aSErik Eckstein   case Type::PointerTyID: {
407*4d6fb72aSErik Eckstein     assert(PTyL && PTyR && "Both types must be pointers here.");
408*4d6fb72aSErik Eckstein     return cmpNumbers(PTyL->getAddressSpace(), PTyR->getAddressSpace());
409*4d6fb72aSErik Eckstein   }
410*4d6fb72aSErik Eckstein 
411*4d6fb72aSErik Eckstein   case Type::StructTyID: {
412*4d6fb72aSErik Eckstein     StructType *STyL = cast<StructType>(TyL);
413*4d6fb72aSErik Eckstein     StructType *STyR = cast<StructType>(TyR);
414*4d6fb72aSErik Eckstein     if (STyL->getNumElements() != STyR->getNumElements())
415*4d6fb72aSErik Eckstein       return cmpNumbers(STyL->getNumElements(), STyR->getNumElements());
416*4d6fb72aSErik Eckstein 
417*4d6fb72aSErik Eckstein     if (STyL->isPacked() != STyR->isPacked())
418*4d6fb72aSErik Eckstein       return cmpNumbers(STyL->isPacked(), STyR->isPacked());
419*4d6fb72aSErik Eckstein 
420*4d6fb72aSErik Eckstein     for (unsigned i = 0, e = STyL->getNumElements(); i != e; ++i) {
421*4d6fb72aSErik Eckstein       if (int Res = cmpTypes(STyL->getElementType(i), STyR->getElementType(i)))
422*4d6fb72aSErik Eckstein         return Res;
423*4d6fb72aSErik Eckstein     }
424*4d6fb72aSErik Eckstein     return 0;
425*4d6fb72aSErik Eckstein   }
426*4d6fb72aSErik Eckstein 
427*4d6fb72aSErik Eckstein   case Type::FunctionTyID: {
428*4d6fb72aSErik Eckstein     FunctionType *FTyL = cast<FunctionType>(TyL);
429*4d6fb72aSErik Eckstein     FunctionType *FTyR = cast<FunctionType>(TyR);
430*4d6fb72aSErik Eckstein     if (FTyL->getNumParams() != FTyR->getNumParams())
431*4d6fb72aSErik Eckstein       return cmpNumbers(FTyL->getNumParams(), FTyR->getNumParams());
432*4d6fb72aSErik Eckstein 
433*4d6fb72aSErik Eckstein     if (FTyL->isVarArg() != FTyR->isVarArg())
434*4d6fb72aSErik Eckstein       return cmpNumbers(FTyL->isVarArg(), FTyR->isVarArg());
435*4d6fb72aSErik Eckstein 
436*4d6fb72aSErik Eckstein     if (int Res = cmpTypes(FTyL->getReturnType(), FTyR->getReturnType()))
437*4d6fb72aSErik Eckstein       return Res;
438*4d6fb72aSErik Eckstein 
439*4d6fb72aSErik Eckstein     for (unsigned i = 0, e = FTyL->getNumParams(); i != e; ++i) {
440*4d6fb72aSErik Eckstein       if (int Res = cmpTypes(FTyL->getParamType(i), FTyR->getParamType(i)))
441*4d6fb72aSErik Eckstein         return Res;
442*4d6fb72aSErik Eckstein     }
443*4d6fb72aSErik Eckstein     return 0;
444*4d6fb72aSErik Eckstein   }
445*4d6fb72aSErik Eckstein 
446*4d6fb72aSErik Eckstein   case Type::ArrayTyID: {
447*4d6fb72aSErik Eckstein     ArrayType *ATyL = cast<ArrayType>(TyL);
448*4d6fb72aSErik Eckstein     ArrayType *ATyR = cast<ArrayType>(TyR);
449*4d6fb72aSErik Eckstein     if (ATyL->getNumElements() != ATyR->getNumElements())
450*4d6fb72aSErik Eckstein       return cmpNumbers(ATyL->getNumElements(), ATyR->getNumElements());
451*4d6fb72aSErik Eckstein     return cmpTypes(ATyL->getElementType(), ATyR->getElementType());
452*4d6fb72aSErik Eckstein   }
453*4d6fb72aSErik Eckstein   }
454*4d6fb72aSErik Eckstein }
455*4d6fb72aSErik Eckstein 
456*4d6fb72aSErik Eckstein // Determine whether the two operations are the same except that pointer-to-A
457*4d6fb72aSErik Eckstein // and pointer-to-B are equivalent. This should be kept in sync with
458*4d6fb72aSErik Eckstein // Instruction::isSameOperationAs.
459*4d6fb72aSErik Eckstein // Read method declaration comments for more details.
460*4d6fb72aSErik Eckstein int FunctionComparator::cmpOperations(const Instruction *L,
461*4d6fb72aSErik Eckstein                                       const Instruction *R,
462*4d6fb72aSErik Eckstein                                       bool &needToCmpOperands) const {
463*4d6fb72aSErik Eckstein   needToCmpOperands = true;
464*4d6fb72aSErik Eckstein   if (int Res = cmpValues(L, R))
465*4d6fb72aSErik Eckstein     return Res;
466*4d6fb72aSErik Eckstein 
467*4d6fb72aSErik Eckstein   // Differences from Instruction::isSameOperationAs:
468*4d6fb72aSErik Eckstein   //  * replace type comparison with calls to cmpTypes.
469*4d6fb72aSErik Eckstein   //  * we test for I->getRawSubclassOptionalData (nuw/nsw/tail) at the top.
470*4d6fb72aSErik Eckstein   //  * because of the above, we don't test for the tail bit on calls later on.
471*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L->getOpcode(), R->getOpcode()))
472*4d6fb72aSErik Eckstein     return Res;
473*4d6fb72aSErik Eckstein 
474*4d6fb72aSErik Eckstein   if (const GetElementPtrInst *GEPL = dyn_cast<GetElementPtrInst>(L)) {
475*4d6fb72aSErik Eckstein     needToCmpOperands = false;
476*4d6fb72aSErik Eckstein     const GetElementPtrInst *GEPR = cast<GetElementPtrInst>(R);
477*4d6fb72aSErik Eckstein     if (int Res =
478*4d6fb72aSErik Eckstein             cmpValues(GEPL->getPointerOperand(), GEPR->getPointerOperand()))
479*4d6fb72aSErik Eckstein       return Res;
480*4d6fb72aSErik Eckstein     return cmpGEPs(GEPL, GEPR);
481*4d6fb72aSErik Eckstein   }
482*4d6fb72aSErik Eckstein 
483*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L->getNumOperands(), R->getNumOperands()))
484*4d6fb72aSErik Eckstein     return Res;
485*4d6fb72aSErik Eckstein 
486*4d6fb72aSErik Eckstein   if (int Res = cmpTypes(L->getType(), R->getType()))
487*4d6fb72aSErik Eckstein     return Res;
488*4d6fb72aSErik Eckstein 
489*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L->getRawSubclassOptionalData(),
490*4d6fb72aSErik Eckstein                            R->getRawSubclassOptionalData()))
491*4d6fb72aSErik Eckstein     return Res;
492*4d6fb72aSErik Eckstein 
493*4d6fb72aSErik Eckstein   // We have two instructions of identical opcode and #operands.  Check to see
494*4d6fb72aSErik Eckstein   // if all operands are the same type
495*4d6fb72aSErik Eckstein   for (unsigned i = 0, e = L->getNumOperands(); i != e; ++i) {
496*4d6fb72aSErik Eckstein     if (int Res =
497*4d6fb72aSErik Eckstein             cmpTypes(L->getOperand(i)->getType(), R->getOperand(i)->getType()))
498*4d6fb72aSErik Eckstein       return Res;
499*4d6fb72aSErik Eckstein   }
500*4d6fb72aSErik Eckstein 
501*4d6fb72aSErik Eckstein   // Check special state that is a part of some instructions.
502*4d6fb72aSErik Eckstein   if (const AllocaInst *AI = dyn_cast<AllocaInst>(L)) {
503*4d6fb72aSErik Eckstein     if (int Res = cmpTypes(AI->getAllocatedType(),
504*4d6fb72aSErik Eckstein                            cast<AllocaInst>(R)->getAllocatedType()))
505*4d6fb72aSErik Eckstein       return Res;
506*4d6fb72aSErik Eckstein     return cmpNumbers(AI->getAlignment(), cast<AllocaInst>(R)->getAlignment());
507*4d6fb72aSErik Eckstein   }
508*4d6fb72aSErik Eckstein   if (const LoadInst *LI = dyn_cast<LoadInst>(L)) {
509*4d6fb72aSErik Eckstein     if (int Res = cmpNumbers(LI->isVolatile(), cast<LoadInst>(R)->isVolatile()))
510*4d6fb72aSErik Eckstein       return Res;
511*4d6fb72aSErik Eckstein     if (int Res =
512*4d6fb72aSErik Eckstein             cmpNumbers(LI->getAlignment(), cast<LoadInst>(R)->getAlignment()))
513*4d6fb72aSErik Eckstein       return Res;
514*4d6fb72aSErik Eckstein     if (int Res =
515*4d6fb72aSErik Eckstein             cmpOrderings(LI->getOrdering(), cast<LoadInst>(R)->getOrdering()))
516*4d6fb72aSErik Eckstein       return Res;
517*4d6fb72aSErik Eckstein     if (int Res =
518*4d6fb72aSErik Eckstein             cmpNumbers(LI->getSynchScope(), cast<LoadInst>(R)->getSynchScope()))
519*4d6fb72aSErik Eckstein       return Res;
520*4d6fb72aSErik Eckstein     return cmpRangeMetadata(LI->getMetadata(LLVMContext::MD_range),
521*4d6fb72aSErik Eckstein         cast<LoadInst>(R)->getMetadata(LLVMContext::MD_range));
522*4d6fb72aSErik Eckstein   }
523*4d6fb72aSErik Eckstein   if (const StoreInst *SI = dyn_cast<StoreInst>(L)) {
524*4d6fb72aSErik Eckstein     if (int Res =
525*4d6fb72aSErik Eckstein             cmpNumbers(SI->isVolatile(), cast<StoreInst>(R)->isVolatile()))
526*4d6fb72aSErik Eckstein       return Res;
527*4d6fb72aSErik Eckstein     if (int Res =
528*4d6fb72aSErik Eckstein             cmpNumbers(SI->getAlignment(), cast<StoreInst>(R)->getAlignment()))
529*4d6fb72aSErik Eckstein       return Res;
530*4d6fb72aSErik Eckstein     if (int Res =
531*4d6fb72aSErik Eckstein             cmpOrderings(SI->getOrdering(), cast<StoreInst>(R)->getOrdering()))
532*4d6fb72aSErik Eckstein       return Res;
533*4d6fb72aSErik Eckstein     return cmpNumbers(SI->getSynchScope(), cast<StoreInst>(R)->getSynchScope());
534*4d6fb72aSErik Eckstein   }
535*4d6fb72aSErik Eckstein   if (const CmpInst *CI = dyn_cast<CmpInst>(L))
536*4d6fb72aSErik Eckstein     return cmpNumbers(CI->getPredicate(), cast<CmpInst>(R)->getPredicate());
537*4d6fb72aSErik Eckstein   if (const CallInst *CI = dyn_cast<CallInst>(L)) {
538*4d6fb72aSErik Eckstein     if (int Res = cmpNumbers(CI->getCallingConv(),
539*4d6fb72aSErik Eckstein                              cast<CallInst>(R)->getCallingConv()))
540*4d6fb72aSErik Eckstein       return Res;
541*4d6fb72aSErik Eckstein     if (int Res =
542*4d6fb72aSErik Eckstein             cmpAttrs(CI->getAttributes(), cast<CallInst>(R)->getAttributes()))
543*4d6fb72aSErik Eckstein       return Res;
544*4d6fb72aSErik Eckstein     if (int Res = cmpOperandBundlesSchema(CI, R))
545*4d6fb72aSErik Eckstein       return Res;
546*4d6fb72aSErik Eckstein     return cmpRangeMetadata(
547*4d6fb72aSErik Eckstein         CI->getMetadata(LLVMContext::MD_range),
548*4d6fb72aSErik Eckstein         cast<CallInst>(R)->getMetadata(LLVMContext::MD_range));
549*4d6fb72aSErik Eckstein   }
550*4d6fb72aSErik Eckstein   if (const InvokeInst *II = dyn_cast<InvokeInst>(L)) {
551*4d6fb72aSErik Eckstein     if (int Res = cmpNumbers(II->getCallingConv(),
552*4d6fb72aSErik Eckstein                              cast<InvokeInst>(R)->getCallingConv()))
553*4d6fb72aSErik Eckstein       return Res;
554*4d6fb72aSErik Eckstein     if (int Res =
555*4d6fb72aSErik Eckstein             cmpAttrs(II->getAttributes(), cast<InvokeInst>(R)->getAttributes()))
556*4d6fb72aSErik Eckstein       return Res;
557*4d6fb72aSErik Eckstein     if (int Res = cmpOperandBundlesSchema(II, R))
558*4d6fb72aSErik Eckstein       return Res;
559*4d6fb72aSErik Eckstein     return cmpRangeMetadata(
560*4d6fb72aSErik Eckstein         II->getMetadata(LLVMContext::MD_range),
561*4d6fb72aSErik Eckstein         cast<InvokeInst>(R)->getMetadata(LLVMContext::MD_range));
562*4d6fb72aSErik Eckstein   }
563*4d6fb72aSErik Eckstein   if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(L)) {
564*4d6fb72aSErik Eckstein     ArrayRef<unsigned> LIndices = IVI->getIndices();
565*4d6fb72aSErik Eckstein     ArrayRef<unsigned> RIndices = cast<InsertValueInst>(R)->getIndices();
566*4d6fb72aSErik Eckstein     if (int Res = cmpNumbers(LIndices.size(), RIndices.size()))
567*4d6fb72aSErik Eckstein       return Res;
568*4d6fb72aSErik Eckstein     for (size_t i = 0, e = LIndices.size(); i != e; ++i) {
569*4d6fb72aSErik Eckstein       if (int Res = cmpNumbers(LIndices[i], RIndices[i]))
570*4d6fb72aSErik Eckstein         return Res;
571*4d6fb72aSErik Eckstein     }
572*4d6fb72aSErik Eckstein     return 0;
573*4d6fb72aSErik Eckstein   }
574*4d6fb72aSErik Eckstein   if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(L)) {
575*4d6fb72aSErik Eckstein     ArrayRef<unsigned> LIndices = EVI->getIndices();
576*4d6fb72aSErik Eckstein     ArrayRef<unsigned> RIndices = cast<ExtractValueInst>(R)->getIndices();
577*4d6fb72aSErik Eckstein     if (int Res = cmpNumbers(LIndices.size(), RIndices.size()))
578*4d6fb72aSErik Eckstein       return Res;
579*4d6fb72aSErik Eckstein     for (size_t i = 0, e = LIndices.size(); i != e; ++i) {
580*4d6fb72aSErik Eckstein       if (int Res = cmpNumbers(LIndices[i], RIndices[i]))
581*4d6fb72aSErik Eckstein         return Res;
582*4d6fb72aSErik Eckstein     }
583*4d6fb72aSErik Eckstein   }
584*4d6fb72aSErik Eckstein   if (const FenceInst *FI = dyn_cast<FenceInst>(L)) {
585*4d6fb72aSErik Eckstein     if (int Res =
586*4d6fb72aSErik Eckstein             cmpOrderings(FI->getOrdering(), cast<FenceInst>(R)->getOrdering()))
587*4d6fb72aSErik Eckstein       return Res;
588*4d6fb72aSErik Eckstein     return cmpNumbers(FI->getSynchScope(), cast<FenceInst>(R)->getSynchScope());
589*4d6fb72aSErik Eckstein   }
590*4d6fb72aSErik Eckstein   if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(L)) {
591*4d6fb72aSErik Eckstein     if (int Res = cmpNumbers(CXI->isVolatile(),
592*4d6fb72aSErik Eckstein                              cast<AtomicCmpXchgInst>(R)->isVolatile()))
593*4d6fb72aSErik Eckstein       return Res;
594*4d6fb72aSErik Eckstein     if (int Res = cmpNumbers(CXI->isWeak(),
595*4d6fb72aSErik Eckstein                              cast<AtomicCmpXchgInst>(R)->isWeak()))
596*4d6fb72aSErik Eckstein       return Res;
597*4d6fb72aSErik Eckstein     if (int Res =
598*4d6fb72aSErik Eckstein             cmpOrderings(CXI->getSuccessOrdering(),
599*4d6fb72aSErik Eckstein                          cast<AtomicCmpXchgInst>(R)->getSuccessOrdering()))
600*4d6fb72aSErik Eckstein       return Res;
601*4d6fb72aSErik Eckstein     if (int Res =
602*4d6fb72aSErik Eckstein             cmpOrderings(CXI->getFailureOrdering(),
603*4d6fb72aSErik Eckstein                          cast<AtomicCmpXchgInst>(R)->getFailureOrdering()))
604*4d6fb72aSErik Eckstein       return Res;
605*4d6fb72aSErik Eckstein     return cmpNumbers(CXI->getSynchScope(),
606*4d6fb72aSErik Eckstein                       cast<AtomicCmpXchgInst>(R)->getSynchScope());
607*4d6fb72aSErik Eckstein   }
608*4d6fb72aSErik Eckstein   if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(L)) {
609*4d6fb72aSErik Eckstein     if (int Res = cmpNumbers(RMWI->getOperation(),
610*4d6fb72aSErik Eckstein                              cast<AtomicRMWInst>(R)->getOperation()))
611*4d6fb72aSErik Eckstein       return Res;
612*4d6fb72aSErik Eckstein     if (int Res = cmpNumbers(RMWI->isVolatile(),
613*4d6fb72aSErik Eckstein                              cast<AtomicRMWInst>(R)->isVolatile()))
614*4d6fb72aSErik Eckstein       return Res;
615*4d6fb72aSErik Eckstein     if (int Res = cmpOrderings(RMWI->getOrdering(),
616*4d6fb72aSErik Eckstein                              cast<AtomicRMWInst>(R)->getOrdering()))
617*4d6fb72aSErik Eckstein       return Res;
618*4d6fb72aSErik Eckstein     return cmpNumbers(RMWI->getSynchScope(),
619*4d6fb72aSErik Eckstein                       cast<AtomicRMWInst>(R)->getSynchScope());
620*4d6fb72aSErik Eckstein   }
621*4d6fb72aSErik Eckstein   if (const PHINode *PNL = dyn_cast<PHINode>(L)) {
622*4d6fb72aSErik Eckstein     const PHINode *PNR = cast<PHINode>(R);
623*4d6fb72aSErik Eckstein     // Ensure that in addition to the incoming values being identical
624*4d6fb72aSErik Eckstein     // (checked by the caller of this function), the incoming blocks
625*4d6fb72aSErik Eckstein     // are also identical.
626*4d6fb72aSErik Eckstein     for (unsigned i = 0, e = PNL->getNumIncomingValues(); i != e; ++i) {
627*4d6fb72aSErik Eckstein       if (int Res =
628*4d6fb72aSErik Eckstein               cmpValues(PNL->getIncomingBlock(i), PNR->getIncomingBlock(i)))
629*4d6fb72aSErik Eckstein         return Res;
630*4d6fb72aSErik Eckstein     }
631*4d6fb72aSErik Eckstein   }
632*4d6fb72aSErik Eckstein   return 0;
633*4d6fb72aSErik Eckstein }
634*4d6fb72aSErik Eckstein 
635*4d6fb72aSErik Eckstein // Determine whether two GEP operations perform the same underlying arithmetic.
636*4d6fb72aSErik Eckstein // Read method declaration comments for more details.
637*4d6fb72aSErik Eckstein int FunctionComparator::cmpGEPs(const GEPOperator *GEPL,
638*4d6fb72aSErik Eckstein                                 const GEPOperator *GEPR) const {
639*4d6fb72aSErik Eckstein 
640*4d6fb72aSErik Eckstein   unsigned int ASL = GEPL->getPointerAddressSpace();
641*4d6fb72aSErik Eckstein   unsigned int ASR = GEPR->getPointerAddressSpace();
642*4d6fb72aSErik Eckstein 
643*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(ASL, ASR))
644*4d6fb72aSErik Eckstein     return Res;
645*4d6fb72aSErik Eckstein 
646*4d6fb72aSErik Eckstein   // When we have target data, we can reduce the GEP down to the value in bytes
647*4d6fb72aSErik Eckstein   // added to the address.
648*4d6fb72aSErik Eckstein   const DataLayout &DL = FnL->getParent()->getDataLayout();
649*4d6fb72aSErik Eckstein   unsigned BitWidth = DL.getPointerSizeInBits(ASL);
650*4d6fb72aSErik Eckstein   APInt OffsetL(BitWidth, 0), OffsetR(BitWidth, 0);
651*4d6fb72aSErik Eckstein   if (GEPL->accumulateConstantOffset(DL, OffsetL) &&
652*4d6fb72aSErik Eckstein       GEPR->accumulateConstantOffset(DL, OffsetR))
653*4d6fb72aSErik Eckstein     return cmpAPInts(OffsetL, OffsetR);
654*4d6fb72aSErik Eckstein   if (int Res = cmpTypes(GEPL->getSourceElementType(),
655*4d6fb72aSErik Eckstein                          GEPR->getSourceElementType()))
656*4d6fb72aSErik Eckstein     return Res;
657*4d6fb72aSErik Eckstein 
658*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(GEPL->getNumOperands(), GEPR->getNumOperands()))
659*4d6fb72aSErik Eckstein     return Res;
660*4d6fb72aSErik Eckstein 
661*4d6fb72aSErik Eckstein   for (unsigned i = 0, e = GEPL->getNumOperands(); i != e; ++i) {
662*4d6fb72aSErik Eckstein     if (int Res = cmpValues(GEPL->getOperand(i), GEPR->getOperand(i)))
663*4d6fb72aSErik Eckstein       return Res;
664*4d6fb72aSErik Eckstein   }
665*4d6fb72aSErik Eckstein 
666*4d6fb72aSErik Eckstein   return 0;
667*4d6fb72aSErik Eckstein }
668*4d6fb72aSErik Eckstein 
669*4d6fb72aSErik Eckstein int FunctionComparator::cmpInlineAsm(const InlineAsm *L,
670*4d6fb72aSErik Eckstein                                      const InlineAsm *R) const {
671*4d6fb72aSErik Eckstein   // InlineAsm's are uniqued. If they are the same pointer, obviously they are
672*4d6fb72aSErik Eckstein   // the same, otherwise compare the fields.
673*4d6fb72aSErik Eckstein   if (L == R)
674*4d6fb72aSErik Eckstein     return 0;
675*4d6fb72aSErik Eckstein   if (int Res = cmpTypes(L->getFunctionType(), R->getFunctionType()))
676*4d6fb72aSErik Eckstein     return Res;
677*4d6fb72aSErik Eckstein   if (int Res = cmpMem(L->getAsmString(), R->getAsmString()))
678*4d6fb72aSErik Eckstein     return Res;
679*4d6fb72aSErik Eckstein   if (int Res = cmpMem(L->getConstraintString(), R->getConstraintString()))
680*4d6fb72aSErik Eckstein     return Res;
681*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L->hasSideEffects(), R->hasSideEffects()))
682*4d6fb72aSErik Eckstein     return Res;
683*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L->isAlignStack(), R->isAlignStack()))
684*4d6fb72aSErik Eckstein     return Res;
685*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(L->getDialect(), R->getDialect()))
686*4d6fb72aSErik Eckstein     return Res;
687*4d6fb72aSErik Eckstein   llvm_unreachable("InlineAsm blocks were not uniqued.");
688*4d6fb72aSErik Eckstein   return 0;
689*4d6fb72aSErik Eckstein }
690*4d6fb72aSErik Eckstein 
691*4d6fb72aSErik Eckstein /// Compare two values used by the two functions under pair-wise comparison. If
692*4d6fb72aSErik Eckstein /// this is the first time the values are seen, they're added to the mapping so
693*4d6fb72aSErik Eckstein /// that we will detect mismatches on next use.
694*4d6fb72aSErik Eckstein /// See comments in declaration for more details.
695*4d6fb72aSErik Eckstein int FunctionComparator::cmpValues(const Value *L, const Value *R) const {
696*4d6fb72aSErik Eckstein   // Catch self-reference case.
697*4d6fb72aSErik Eckstein   if (L == FnL) {
698*4d6fb72aSErik Eckstein     if (R == FnR)
699*4d6fb72aSErik Eckstein       return 0;
700*4d6fb72aSErik Eckstein     return -1;
701*4d6fb72aSErik Eckstein   }
702*4d6fb72aSErik Eckstein   if (R == FnR) {
703*4d6fb72aSErik Eckstein     if (L == FnL)
704*4d6fb72aSErik Eckstein       return 0;
705*4d6fb72aSErik Eckstein     return 1;
706*4d6fb72aSErik Eckstein   }
707*4d6fb72aSErik Eckstein 
708*4d6fb72aSErik Eckstein   const Constant *ConstL = dyn_cast<Constant>(L);
709*4d6fb72aSErik Eckstein   const Constant *ConstR = dyn_cast<Constant>(R);
710*4d6fb72aSErik Eckstein   if (ConstL && ConstR) {
711*4d6fb72aSErik Eckstein     if (L == R)
712*4d6fb72aSErik Eckstein       return 0;
713*4d6fb72aSErik Eckstein     return cmpConstants(ConstL, ConstR);
714*4d6fb72aSErik Eckstein   }
715*4d6fb72aSErik Eckstein 
716*4d6fb72aSErik Eckstein   if (ConstL)
717*4d6fb72aSErik Eckstein     return 1;
718*4d6fb72aSErik Eckstein   if (ConstR)
719*4d6fb72aSErik Eckstein     return -1;
720*4d6fb72aSErik Eckstein 
721*4d6fb72aSErik Eckstein   const InlineAsm *InlineAsmL = dyn_cast<InlineAsm>(L);
722*4d6fb72aSErik Eckstein   const InlineAsm *InlineAsmR = dyn_cast<InlineAsm>(R);
723*4d6fb72aSErik Eckstein 
724*4d6fb72aSErik Eckstein   if (InlineAsmL && InlineAsmR)
725*4d6fb72aSErik Eckstein     return cmpInlineAsm(InlineAsmL, InlineAsmR);
726*4d6fb72aSErik Eckstein   if (InlineAsmL)
727*4d6fb72aSErik Eckstein     return 1;
728*4d6fb72aSErik Eckstein   if (InlineAsmR)
729*4d6fb72aSErik Eckstein     return -1;
730*4d6fb72aSErik Eckstein 
731*4d6fb72aSErik Eckstein   auto LeftSN = sn_mapL.insert(std::make_pair(L, sn_mapL.size())),
732*4d6fb72aSErik Eckstein        RightSN = sn_mapR.insert(std::make_pair(R, sn_mapR.size()));
733*4d6fb72aSErik Eckstein 
734*4d6fb72aSErik Eckstein   return cmpNumbers(LeftSN.first->second, RightSN.first->second);
735*4d6fb72aSErik Eckstein }
736*4d6fb72aSErik Eckstein 
737*4d6fb72aSErik Eckstein // Test whether two basic blocks have equivalent behaviour.
738*4d6fb72aSErik Eckstein int FunctionComparator::cmpBasicBlocks(const BasicBlock *BBL,
739*4d6fb72aSErik Eckstein                                        const BasicBlock *BBR) const {
740*4d6fb72aSErik Eckstein   BasicBlock::const_iterator InstL = BBL->begin(), InstLE = BBL->end();
741*4d6fb72aSErik Eckstein   BasicBlock::const_iterator InstR = BBR->begin(), InstRE = BBR->end();
742*4d6fb72aSErik Eckstein 
743*4d6fb72aSErik Eckstein   do {
744*4d6fb72aSErik Eckstein     bool needToCmpOperands = true;
745*4d6fb72aSErik Eckstein     if (int Res = cmpOperations(&*InstL, &*InstR, needToCmpOperands))
746*4d6fb72aSErik Eckstein       return Res;
747*4d6fb72aSErik Eckstein     if (needToCmpOperands) {
748*4d6fb72aSErik Eckstein       assert(InstL->getNumOperands() == InstR->getNumOperands());
749*4d6fb72aSErik Eckstein 
750*4d6fb72aSErik Eckstein       for (unsigned i = 0, e = InstL->getNumOperands(); i != e; ++i) {
751*4d6fb72aSErik Eckstein         Value *OpL = InstL->getOperand(i);
752*4d6fb72aSErik Eckstein         Value *OpR = InstR->getOperand(i);
753*4d6fb72aSErik Eckstein         if (int Res = cmpValues(OpL, OpR))
754*4d6fb72aSErik Eckstein           return Res;
755*4d6fb72aSErik Eckstein         // cmpValues should ensure this is true.
756*4d6fb72aSErik Eckstein         assert(cmpTypes(OpL->getType(), OpR->getType()) == 0);
757*4d6fb72aSErik Eckstein       }
758*4d6fb72aSErik Eckstein     }
759*4d6fb72aSErik Eckstein 
760*4d6fb72aSErik Eckstein     ++InstL;
761*4d6fb72aSErik Eckstein     ++InstR;
762*4d6fb72aSErik Eckstein   } while (InstL != InstLE && InstR != InstRE);
763*4d6fb72aSErik Eckstein 
764*4d6fb72aSErik Eckstein   if (InstL != InstLE && InstR == InstRE)
765*4d6fb72aSErik Eckstein     return 1;
766*4d6fb72aSErik Eckstein   if (InstL == InstLE && InstR != InstRE)
767*4d6fb72aSErik Eckstein     return -1;
768*4d6fb72aSErik Eckstein   return 0;
769*4d6fb72aSErik Eckstein }
770*4d6fb72aSErik Eckstein 
771*4d6fb72aSErik Eckstein int FunctionComparator::compareSignature() const {
772*4d6fb72aSErik Eckstein   if (int Res = cmpAttrs(FnL->getAttributes(), FnR->getAttributes()))
773*4d6fb72aSErik Eckstein     return Res;
774*4d6fb72aSErik Eckstein 
775*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(FnL->hasGC(), FnR->hasGC()))
776*4d6fb72aSErik Eckstein     return Res;
777*4d6fb72aSErik Eckstein 
778*4d6fb72aSErik Eckstein   if (FnL->hasGC()) {
779*4d6fb72aSErik Eckstein     if (int Res = cmpMem(FnL->getGC(), FnR->getGC()))
780*4d6fb72aSErik Eckstein       return Res;
781*4d6fb72aSErik Eckstein   }
782*4d6fb72aSErik Eckstein 
783*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(FnL->hasSection(), FnR->hasSection()))
784*4d6fb72aSErik Eckstein     return Res;
785*4d6fb72aSErik Eckstein 
786*4d6fb72aSErik Eckstein   if (FnL->hasSection()) {
787*4d6fb72aSErik Eckstein     if (int Res = cmpMem(FnL->getSection(), FnR->getSection()))
788*4d6fb72aSErik Eckstein       return Res;
789*4d6fb72aSErik Eckstein   }
790*4d6fb72aSErik Eckstein 
791*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(FnL->isVarArg(), FnR->isVarArg()))
792*4d6fb72aSErik Eckstein     return Res;
793*4d6fb72aSErik Eckstein 
794*4d6fb72aSErik Eckstein   // TODO: if it's internal and only used in direct calls, we could handle this
795*4d6fb72aSErik Eckstein   // case too.
796*4d6fb72aSErik Eckstein   if (int Res = cmpNumbers(FnL->getCallingConv(), FnR->getCallingConv()))
797*4d6fb72aSErik Eckstein     return Res;
798*4d6fb72aSErik Eckstein 
799*4d6fb72aSErik Eckstein   if (int Res = cmpTypes(FnL->getFunctionType(), FnR->getFunctionType()))
800*4d6fb72aSErik Eckstein     return Res;
801*4d6fb72aSErik Eckstein 
802*4d6fb72aSErik Eckstein   assert(FnL->arg_size() == FnR->arg_size() &&
803*4d6fb72aSErik Eckstein          "Identically typed functions have different numbers of args!");
804*4d6fb72aSErik Eckstein 
805*4d6fb72aSErik Eckstein   // Visit the arguments so that they get enumerated in the order they're
806*4d6fb72aSErik Eckstein   // passed in.
807*4d6fb72aSErik Eckstein   for (Function::const_arg_iterator ArgLI = FnL->arg_begin(),
808*4d6fb72aSErik Eckstein        ArgRI = FnR->arg_begin(),
809*4d6fb72aSErik Eckstein        ArgLE = FnL->arg_end();
810*4d6fb72aSErik Eckstein        ArgLI != ArgLE; ++ArgLI, ++ArgRI) {
811*4d6fb72aSErik Eckstein     if (cmpValues(&*ArgLI, &*ArgRI) != 0)
812*4d6fb72aSErik Eckstein       llvm_unreachable("Arguments repeat!");
813*4d6fb72aSErik Eckstein   }
814*4d6fb72aSErik Eckstein   return 0;
815*4d6fb72aSErik Eckstein }
816*4d6fb72aSErik Eckstein 
817*4d6fb72aSErik Eckstein // Test whether the two functions have equivalent behaviour.
818*4d6fb72aSErik Eckstein int FunctionComparator::compare() {
819*4d6fb72aSErik Eckstein   beginCompare();
820*4d6fb72aSErik Eckstein 
821*4d6fb72aSErik Eckstein   if (int Res = compareSignature())
822*4d6fb72aSErik Eckstein     return Res;
823*4d6fb72aSErik Eckstein 
824*4d6fb72aSErik Eckstein   // We do a CFG-ordered walk since the actual ordering of the blocks in the
825*4d6fb72aSErik Eckstein   // linked list is immaterial. Our walk starts at the entry block for both
826*4d6fb72aSErik Eckstein   // functions, then takes each block from each terminator in order. As an
827*4d6fb72aSErik Eckstein   // artifact, this also means that unreachable blocks are ignored.
828*4d6fb72aSErik Eckstein   SmallVector<const BasicBlock *, 8> FnLBBs, FnRBBs;
829*4d6fb72aSErik Eckstein   SmallPtrSet<const BasicBlock *, 32> VisitedBBs; // in terms of F1.
830*4d6fb72aSErik Eckstein 
831*4d6fb72aSErik Eckstein   FnLBBs.push_back(&FnL->getEntryBlock());
832*4d6fb72aSErik Eckstein   FnRBBs.push_back(&FnR->getEntryBlock());
833*4d6fb72aSErik Eckstein 
834*4d6fb72aSErik Eckstein   VisitedBBs.insert(FnLBBs[0]);
835*4d6fb72aSErik Eckstein   while (!FnLBBs.empty()) {
836*4d6fb72aSErik Eckstein     const BasicBlock *BBL = FnLBBs.pop_back_val();
837*4d6fb72aSErik Eckstein     const BasicBlock *BBR = FnRBBs.pop_back_val();
838*4d6fb72aSErik Eckstein 
839*4d6fb72aSErik Eckstein     if (int Res = cmpValues(BBL, BBR))
840*4d6fb72aSErik Eckstein       return Res;
841*4d6fb72aSErik Eckstein 
842*4d6fb72aSErik Eckstein     if (int Res = cmpBasicBlocks(BBL, BBR))
843*4d6fb72aSErik Eckstein       return Res;
844*4d6fb72aSErik Eckstein 
845*4d6fb72aSErik Eckstein     const TerminatorInst *TermL = BBL->getTerminator();
846*4d6fb72aSErik Eckstein     const TerminatorInst *TermR = BBR->getTerminator();
847*4d6fb72aSErik Eckstein 
848*4d6fb72aSErik Eckstein     assert(TermL->getNumSuccessors() == TermR->getNumSuccessors());
849*4d6fb72aSErik Eckstein     for (unsigned i = 0, e = TermL->getNumSuccessors(); i != e; ++i) {
850*4d6fb72aSErik Eckstein       if (!VisitedBBs.insert(TermL->getSuccessor(i)).second)
851*4d6fb72aSErik Eckstein         continue;
852*4d6fb72aSErik Eckstein 
853*4d6fb72aSErik Eckstein       FnLBBs.push_back(TermL->getSuccessor(i));
854*4d6fb72aSErik Eckstein       FnRBBs.push_back(TermR->getSuccessor(i));
855*4d6fb72aSErik Eckstein     }
856*4d6fb72aSErik Eckstein   }
857*4d6fb72aSErik Eckstein   return 0;
858*4d6fb72aSErik Eckstein }
859*4d6fb72aSErik Eckstein 
860*4d6fb72aSErik Eckstein namespace {
861*4d6fb72aSErik Eckstein 
862*4d6fb72aSErik Eckstein // Accumulate the hash of a sequence of 64-bit integers. This is similar to a
863*4d6fb72aSErik Eckstein // hash of a sequence of 64bit ints, but the entire input does not need to be
864*4d6fb72aSErik Eckstein // available at once. This interface is necessary for functionHash because it
865*4d6fb72aSErik Eckstein // needs to accumulate the hash as the structure of the function is traversed
866*4d6fb72aSErik Eckstein // without saving these values to an intermediate buffer. This form of hashing
867*4d6fb72aSErik Eckstein // is not often needed, as usually the object to hash is just read from a
868*4d6fb72aSErik Eckstein // buffer.
869*4d6fb72aSErik Eckstein class HashAccumulator64 {
870*4d6fb72aSErik Eckstein   uint64_t Hash;
871*4d6fb72aSErik Eckstein public:
872*4d6fb72aSErik Eckstein   // Initialize to random constant, so the state isn't zero.
873*4d6fb72aSErik Eckstein   HashAccumulator64() { Hash = 0x6acaa36bef8325c5ULL; }
874*4d6fb72aSErik Eckstein   void add(uint64_t V) {
875*4d6fb72aSErik Eckstein      Hash = llvm::hashing::detail::hash_16_bytes(Hash, V);
876*4d6fb72aSErik Eckstein   }
877*4d6fb72aSErik Eckstein   // No finishing is required, because the entire hash value is used.
878*4d6fb72aSErik Eckstein   uint64_t getHash() { return Hash; }
879*4d6fb72aSErik Eckstein };
880*4d6fb72aSErik Eckstein } // end anonymous namespace
881*4d6fb72aSErik Eckstein 
882*4d6fb72aSErik Eckstein // A function hash is calculated by considering only the number of arguments and
883*4d6fb72aSErik Eckstein // whether a function is varargs, the order of basic blocks (given by the
884*4d6fb72aSErik Eckstein // successors of each basic block in depth first order), and the order of
885*4d6fb72aSErik Eckstein // opcodes of each instruction within each of these basic blocks. This mirrors
886*4d6fb72aSErik Eckstein // the strategy compare() uses to compare functions by walking the BBs in depth
887*4d6fb72aSErik Eckstein // first order and comparing each instruction in sequence. Because this hash
888*4d6fb72aSErik Eckstein // does not look at the operands, it is insensitive to things such as the
889*4d6fb72aSErik Eckstein // target of calls and the constants used in the function, which makes it useful
890*4d6fb72aSErik Eckstein // when possibly merging functions which are the same modulo constants and call
891*4d6fb72aSErik Eckstein // targets.
892*4d6fb72aSErik Eckstein FunctionComparator::FunctionHash FunctionComparator::functionHash(Function &F) {
893*4d6fb72aSErik Eckstein   HashAccumulator64 H;
894*4d6fb72aSErik Eckstein   H.add(F.isVarArg());
895*4d6fb72aSErik Eckstein   H.add(F.arg_size());
896*4d6fb72aSErik Eckstein 
897*4d6fb72aSErik Eckstein   SmallVector<const BasicBlock *, 8> BBs;
898*4d6fb72aSErik Eckstein   SmallSet<const BasicBlock *, 16> VisitedBBs;
899*4d6fb72aSErik Eckstein 
900*4d6fb72aSErik Eckstein   // Walk the blocks in the same order as FunctionComparator::cmpBasicBlocks(),
901*4d6fb72aSErik Eckstein   // accumulating the hash of the function "structure." (BB and opcode sequence)
902*4d6fb72aSErik Eckstein   BBs.push_back(&F.getEntryBlock());
903*4d6fb72aSErik Eckstein   VisitedBBs.insert(BBs[0]);
904*4d6fb72aSErik Eckstein   while (!BBs.empty()) {
905*4d6fb72aSErik Eckstein     const BasicBlock *BB = BBs.pop_back_val();
906*4d6fb72aSErik Eckstein     // This random value acts as a block header, as otherwise the partition of
907*4d6fb72aSErik Eckstein     // opcodes into BBs wouldn't affect the hash, only the order of the opcodes
908*4d6fb72aSErik Eckstein     H.add(45798);
909*4d6fb72aSErik Eckstein     for (auto &Inst : *BB) {
910*4d6fb72aSErik Eckstein       H.add(Inst.getOpcode());
911*4d6fb72aSErik Eckstein     }
912*4d6fb72aSErik Eckstein     const TerminatorInst *Term = BB->getTerminator();
913*4d6fb72aSErik Eckstein     for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
914*4d6fb72aSErik Eckstein       if (!VisitedBBs.insert(Term->getSuccessor(i)).second)
915*4d6fb72aSErik Eckstein         continue;
916*4d6fb72aSErik Eckstein       BBs.push_back(Term->getSuccessor(i));
917*4d6fb72aSErik Eckstein     }
918*4d6fb72aSErik Eckstein   }
919*4d6fb72aSErik Eckstein   return H.getHash();
920*4d6fb72aSErik Eckstein }
921*4d6fb72aSErik Eckstein 
922*4d6fb72aSErik Eckstein 
923