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