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::VectorTyID: {
492     auto *STyL = cast<VectorType>(TyL);
493     auto *STyR = cast<VectorType>(TyR);
494     if (STyL->getElementCount().Scalable != STyR->getElementCount().Scalable)
495       return cmpNumbers(STyL->getElementCount().Scalable,
496                         STyR->getElementCount().Scalable);
497     if (STyL->getElementCount().Min != STyR->getElementCount().Min)
498       return cmpNumbers(STyL->getElementCount().Min,
499                         STyR->getElementCount().Min);
500     return cmpTypes(STyL->getElementType(), STyR->getElementType());
501   }
502   }
503 }
504 
505 // Determine whether the two operations are the same except that pointer-to-A
506 // and pointer-to-B are equivalent. This should be kept in sync with
507 // Instruction::isSameOperationAs.
508 // Read method declaration comments for more details.
509 int FunctionComparator::cmpOperations(const Instruction *L,
510                                       const Instruction *R,
511                                       bool &needToCmpOperands) const {
512   needToCmpOperands = true;
513   if (int Res = cmpValues(L, R))
514     return Res;
515 
516   // Differences from Instruction::isSameOperationAs:
517   //  * replace type comparison with calls to cmpTypes.
518   //  * we test for I->getRawSubclassOptionalData (nuw/nsw/tail) at the top.
519   //  * because of the above, we don't test for the tail bit on calls later on.
520   if (int Res = cmpNumbers(L->getOpcode(), R->getOpcode()))
521     return Res;
522 
523   if (const GetElementPtrInst *GEPL = dyn_cast<GetElementPtrInst>(L)) {
524     needToCmpOperands = false;
525     const GetElementPtrInst *GEPR = cast<GetElementPtrInst>(R);
526     if (int Res =
527             cmpValues(GEPL->getPointerOperand(), GEPR->getPointerOperand()))
528       return Res;
529     return cmpGEPs(GEPL, GEPR);
530   }
531 
532   if (int Res = cmpNumbers(L->getNumOperands(), R->getNumOperands()))
533     return Res;
534 
535   if (int Res = cmpTypes(L->getType(), R->getType()))
536     return Res;
537 
538   if (int Res = cmpNumbers(L->getRawSubclassOptionalData(),
539                            R->getRawSubclassOptionalData()))
540     return Res;
541 
542   // We have two instructions of identical opcode and #operands.  Check to see
543   // if all operands are the same type
544   for (unsigned i = 0, e = L->getNumOperands(); i != e; ++i) {
545     if (int Res =
546             cmpTypes(L->getOperand(i)->getType(), R->getOperand(i)->getType()))
547       return Res;
548   }
549 
550   // Check special state that is a part of some instructions.
551   if (const AllocaInst *AI = dyn_cast<AllocaInst>(L)) {
552     if (int Res = cmpTypes(AI->getAllocatedType(),
553                            cast<AllocaInst>(R)->getAllocatedType()))
554       return Res;
555     return cmpNumbers(AI->getAlignment(), cast<AllocaInst>(R)->getAlignment());
556   }
557   if (const LoadInst *LI = dyn_cast<LoadInst>(L)) {
558     if (int Res = cmpNumbers(LI->isVolatile(), cast<LoadInst>(R)->isVolatile()))
559       return Res;
560     if (int Res =
561             cmpNumbers(LI->getAlignment(), cast<LoadInst>(R)->getAlignment()))
562       return Res;
563     if (int Res =
564             cmpOrderings(LI->getOrdering(), cast<LoadInst>(R)->getOrdering()))
565       return Res;
566     if (int Res = cmpNumbers(LI->getSyncScopeID(),
567                              cast<LoadInst>(R)->getSyncScopeID()))
568       return Res;
569     return cmpRangeMetadata(
570         LI->getMetadata(LLVMContext::MD_range),
571         cast<LoadInst>(R)->getMetadata(LLVMContext::MD_range));
572   }
573   if (const StoreInst *SI = dyn_cast<StoreInst>(L)) {
574     if (int Res =
575             cmpNumbers(SI->isVolatile(), cast<StoreInst>(R)->isVolatile()))
576       return Res;
577     if (int Res =
578             cmpNumbers(SI->getAlignment(), cast<StoreInst>(R)->getAlignment()))
579       return Res;
580     if (int Res =
581             cmpOrderings(SI->getOrdering(), cast<StoreInst>(R)->getOrdering()))
582       return Res;
583     return cmpNumbers(SI->getSyncScopeID(),
584                       cast<StoreInst>(R)->getSyncScopeID());
585   }
586   if (const CmpInst *CI = dyn_cast<CmpInst>(L))
587     return cmpNumbers(CI->getPredicate(), cast<CmpInst>(R)->getPredicate());
588   if (auto *CBL = dyn_cast<CallBase>(L)) {
589     auto *CBR = cast<CallBase>(R);
590     if (int Res = cmpNumbers(CBL->getCallingConv(), CBR->getCallingConv()))
591       return Res;
592     if (int Res = cmpAttrs(CBL->getAttributes(), CBR->getAttributes()))
593       return Res;
594     if (int Res = cmpOperandBundlesSchema(L, R))
595       return Res;
596     if (const CallInst *CI = dyn_cast<CallInst>(L))
597       if (int Res = cmpNumbers(CI->getTailCallKind(),
598                                cast<CallInst>(R)->getTailCallKind()))
599         return Res;
600     return cmpRangeMetadata(L->getMetadata(LLVMContext::MD_range),
601                             R->getMetadata(LLVMContext::MD_range));
602   }
603   if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(L)) {
604     ArrayRef<unsigned> LIndices = IVI->getIndices();
605     ArrayRef<unsigned> RIndices = cast<InsertValueInst>(R)->getIndices();
606     if (int Res = cmpNumbers(LIndices.size(), RIndices.size()))
607       return Res;
608     for (size_t i = 0, e = LIndices.size(); i != e; ++i) {
609       if (int Res = cmpNumbers(LIndices[i], RIndices[i]))
610         return Res;
611     }
612     return 0;
613   }
614   if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(L)) {
615     ArrayRef<unsigned> LIndices = EVI->getIndices();
616     ArrayRef<unsigned> RIndices = cast<ExtractValueInst>(R)->getIndices();
617     if (int Res = cmpNumbers(LIndices.size(), RIndices.size()))
618       return Res;
619     for (size_t i = 0, e = LIndices.size(); i != e; ++i) {
620       if (int Res = cmpNumbers(LIndices[i], RIndices[i]))
621         return Res;
622     }
623   }
624   if (const FenceInst *FI = dyn_cast<FenceInst>(L)) {
625     if (int Res =
626             cmpOrderings(FI->getOrdering(), cast<FenceInst>(R)->getOrdering()))
627       return Res;
628     return cmpNumbers(FI->getSyncScopeID(),
629                       cast<FenceInst>(R)->getSyncScopeID());
630   }
631   if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(L)) {
632     if (int Res = cmpNumbers(CXI->isVolatile(),
633                              cast<AtomicCmpXchgInst>(R)->isVolatile()))
634       return Res;
635     if (int Res =
636             cmpNumbers(CXI->isWeak(), cast<AtomicCmpXchgInst>(R)->isWeak()))
637       return Res;
638     if (int Res =
639             cmpOrderings(CXI->getSuccessOrdering(),
640                          cast<AtomicCmpXchgInst>(R)->getSuccessOrdering()))
641       return Res;
642     if (int Res =
643             cmpOrderings(CXI->getFailureOrdering(),
644                          cast<AtomicCmpXchgInst>(R)->getFailureOrdering()))
645       return Res;
646     return cmpNumbers(CXI->getSyncScopeID(),
647                       cast<AtomicCmpXchgInst>(R)->getSyncScopeID());
648   }
649   if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(L)) {
650     if (int Res = cmpNumbers(RMWI->getOperation(),
651                              cast<AtomicRMWInst>(R)->getOperation()))
652       return Res;
653     if (int Res = cmpNumbers(RMWI->isVolatile(),
654                              cast<AtomicRMWInst>(R)->isVolatile()))
655       return Res;
656     if (int Res = cmpOrderings(RMWI->getOrdering(),
657                                cast<AtomicRMWInst>(R)->getOrdering()))
658       return Res;
659     return cmpNumbers(RMWI->getSyncScopeID(),
660                       cast<AtomicRMWInst>(R)->getSyncScopeID());
661   }
662   if (const PHINode *PNL = dyn_cast<PHINode>(L)) {
663     const PHINode *PNR = cast<PHINode>(R);
664     // Ensure that in addition to the incoming values being identical
665     // (checked by the caller of this function), the incoming blocks
666     // are also identical.
667     for (unsigned i = 0, e = PNL->getNumIncomingValues(); i != e; ++i) {
668       if (int Res =
669               cmpValues(PNL->getIncomingBlock(i), PNR->getIncomingBlock(i)))
670         return Res;
671     }
672   }
673   return 0;
674 }
675 
676 // Determine whether two GEP operations perform the same underlying arithmetic.
677 // Read method declaration comments for more details.
678 int FunctionComparator::cmpGEPs(const GEPOperator *GEPL,
679                                 const GEPOperator *GEPR) const {
680   unsigned int ASL = GEPL->getPointerAddressSpace();
681   unsigned int ASR = GEPR->getPointerAddressSpace();
682 
683   if (int Res = cmpNumbers(ASL, ASR))
684     return Res;
685 
686   // When we have target data, we can reduce the GEP down to the value in bytes
687   // added to the address.
688   const DataLayout &DL = FnL->getParent()->getDataLayout();
689   unsigned BitWidth = DL.getPointerSizeInBits(ASL);
690   APInt OffsetL(BitWidth, 0), OffsetR(BitWidth, 0);
691   if (GEPL->accumulateConstantOffset(DL, OffsetL) &&
692       GEPR->accumulateConstantOffset(DL, OffsetR))
693     return cmpAPInts(OffsetL, OffsetR);
694   if (int Res =
695           cmpTypes(GEPL->getSourceElementType(), GEPR->getSourceElementType()))
696     return Res;
697 
698   if (int Res = cmpNumbers(GEPL->getNumOperands(), GEPR->getNumOperands()))
699     return Res;
700 
701   for (unsigned i = 0, e = GEPL->getNumOperands(); i != e; ++i) {
702     if (int Res = cmpValues(GEPL->getOperand(i), GEPR->getOperand(i)))
703       return Res;
704   }
705 
706   return 0;
707 }
708 
709 int FunctionComparator::cmpInlineAsm(const InlineAsm *L,
710                                      const InlineAsm *R) const {
711   // InlineAsm's are uniqued. If they are the same pointer, obviously they are
712   // the same, otherwise compare the fields.
713   if (L == R)
714     return 0;
715   if (int Res = cmpTypes(L->getFunctionType(), R->getFunctionType()))
716     return Res;
717   if (int Res = cmpMem(L->getAsmString(), R->getAsmString()))
718     return Res;
719   if (int Res = cmpMem(L->getConstraintString(), R->getConstraintString()))
720     return Res;
721   if (int Res = cmpNumbers(L->hasSideEffects(), R->hasSideEffects()))
722     return Res;
723   if (int Res = cmpNumbers(L->isAlignStack(), R->isAlignStack()))
724     return Res;
725   if (int Res = cmpNumbers(L->getDialect(), R->getDialect()))
726     return Res;
727   assert(L->getFunctionType() != R->getFunctionType());
728   return 0;
729 }
730 
731 /// Compare two values used by the two functions under pair-wise comparison. If
732 /// this is the first time the values are seen, they're added to the mapping so
733 /// that we will detect mismatches on next use.
734 /// See comments in declaration for more details.
735 int FunctionComparator::cmpValues(const Value *L, const Value *R) const {
736   // Catch self-reference case.
737   if (L == FnL) {
738     if (R == FnR)
739       return 0;
740     return -1;
741   }
742   if (R == FnR) {
743     if (L == FnL)
744       return 0;
745     return 1;
746   }
747 
748   const Constant *ConstL = dyn_cast<Constant>(L);
749   const Constant *ConstR = dyn_cast<Constant>(R);
750   if (ConstL && ConstR) {
751     if (L == R)
752       return 0;
753     return cmpConstants(ConstL, ConstR);
754   }
755 
756   if (ConstL)
757     return 1;
758   if (ConstR)
759     return -1;
760 
761   const InlineAsm *InlineAsmL = dyn_cast<InlineAsm>(L);
762   const InlineAsm *InlineAsmR = dyn_cast<InlineAsm>(R);
763 
764   if (InlineAsmL && InlineAsmR)
765     return cmpInlineAsm(InlineAsmL, InlineAsmR);
766   if (InlineAsmL)
767     return 1;
768   if (InlineAsmR)
769     return -1;
770 
771   auto LeftSN = sn_mapL.insert(std::make_pair(L, sn_mapL.size())),
772        RightSN = sn_mapR.insert(std::make_pair(R, sn_mapR.size()));
773 
774   return cmpNumbers(LeftSN.first->second, RightSN.first->second);
775 }
776 
777 // Test whether two basic blocks have equivalent behaviour.
778 int FunctionComparator::cmpBasicBlocks(const BasicBlock *BBL,
779                                        const BasicBlock *BBR) const {
780   BasicBlock::const_iterator InstL = BBL->begin(), InstLE = BBL->end();
781   BasicBlock::const_iterator InstR = BBR->begin(), InstRE = BBR->end();
782 
783   do {
784     bool needToCmpOperands = true;
785     if (int Res = cmpOperations(&*InstL, &*InstR, needToCmpOperands))
786       return Res;
787     if (needToCmpOperands) {
788       assert(InstL->getNumOperands() == InstR->getNumOperands());
789 
790       for (unsigned i = 0, e = InstL->getNumOperands(); i != e; ++i) {
791         Value *OpL = InstL->getOperand(i);
792         Value *OpR = InstR->getOperand(i);
793         if (int Res = cmpValues(OpL, OpR))
794           return Res;
795         // cmpValues should ensure this is true.
796         assert(cmpTypes(OpL->getType(), OpR->getType()) == 0);
797       }
798     }
799 
800     ++InstL;
801     ++InstR;
802   } while (InstL != InstLE && InstR != InstRE);
803 
804   if (InstL != InstLE && InstR == InstRE)
805     return 1;
806   if (InstL == InstLE && InstR != InstRE)
807     return -1;
808   return 0;
809 }
810 
811 int FunctionComparator::compareSignature() const {
812   if (int Res = cmpAttrs(FnL->getAttributes(), FnR->getAttributes()))
813     return Res;
814 
815   if (int Res = cmpNumbers(FnL->hasGC(), FnR->hasGC()))
816     return Res;
817 
818   if (FnL->hasGC()) {
819     if (int Res = cmpMem(FnL->getGC(), FnR->getGC()))
820       return Res;
821   }
822 
823   if (int Res = cmpNumbers(FnL->hasSection(), FnR->hasSection()))
824     return Res;
825 
826   if (FnL->hasSection()) {
827     if (int Res = cmpMem(FnL->getSection(), FnR->getSection()))
828       return Res;
829   }
830 
831   if (int Res = cmpNumbers(FnL->isVarArg(), FnR->isVarArg()))
832     return Res;
833 
834   // TODO: if it's internal and only used in direct calls, we could handle this
835   // case too.
836   if (int Res = cmpNumbers(FnL->getCallingConv(), FnR->getCallingConv()))
837     return Res;
838 
839   if (int Res = cmpTypes(FnL->getFunctionType(), FnR->getFunctionType()))
840     return Res;
841 
842   assert(FnL->arg_size() == FnR->arg_size() &&
843          "Identically typed functions have different numbers of args!");
844 
845   // Visit the arguments so that they get enumerated in the order they're
846   // passed in.
847   for (Function::const_arg_iterator ArgLI = FnL->arg_begin(),
848                                     ArgRI = FnR->arg_begin(),
849                                     ArgLE = FnL->arg_end();
850        ArgLI != ArgLE; ++ArgLI, ++ArgRI) {
851     if (cmpValues(&*ArgLI, &*ArgRI) != 0)
852       llvm_unreachable("Arguments repeat!");
853   }
854   return 0;
855 }
856 
857 // Test whether the two functions have equivalent behaviour.
858 int FunctionComparator::compare() {
859   beginCompare();
860 
861   if (int Res = compareSignature())
862     return Res;
863 
864   // We do a CFG-ordered walk since the actual ordering of the blocks in the
865   // linked list is immaterial. Our walk starts at the entry block for both
866   // functions, then takes each block from each terminator in order. As an
867   // artifact, this also means that unreachable blocks are ignored.
868   SmallVector<const BasicBlock *, 8> FnLBBs, FnRBBs;
869   SmallPtrSet<const BasicBlock *, 32> VisitedBBs; // in terms of F1.
870 
871   FnLBBs.push_back(&FnL->getEntryBlock());
872   FnRBBs.push_back(&FnR->getEntryBlock());
873 
874   VisitedBBs.insert(FnLBBs[0]);
875   while (!FnLBBs.empty()) {
876     const BasicBlock *BBL = FnLBBs.pop_back_val();
877     const BasicBlock *BBR = FnRBBs.pop_back_val();
878 
879     if (int Res = cmpValues(BBL, BBR))
880       return Res;
881 
882     if (int Res = cmpBasicBlocks(BBL, BBR))
883       return Res;
884 
885     const Instruction *TermL = BBL->getTerminator();
886     const Instruction *TermR = BBR->getTerminator();
887 
888     assert(TermL->getNumSuccessors() == TermR->getNumSuccessors());
889     for (unsigned i = 0, e = TermL->getNumSuccessors(); i != e; ++i) {
890       if (!VisitedBBs.insert(TermL->getSuccessor(i)).second)
891         continue;
892 
893       FnLBBs.push_back(TermL->getSuccessor(i));
894       FnRBBs.push_back(TermR->getSuccessor(i));
895     }
896   }
897   return 0;
898 }
899 
900 namespace {
901 
902 // Accumulate the hash of a sequence of 64-bit integers. This is similar to a
903 // hash of a sequence of 64bit ints, but the entire input does not need to be
904 // available at once. This interface is necessary for functionHash because it
905 // needs to accumulate the hash as the structure of the function is traversed
906 // without saving these values to an intermediate buffer. This form of hashing
907 // is not often needed, as usually the object to hash is just read from a
908 // buffer.
909 class HashAccumulator64 {
910   uint64_t Hash;
911 
912 public:
913   // Initialize to random constant, so the state isn't zero.
914   HashAccumulator64() { Hash = 0x6acaa36bef8325c5ULL; }
915 
916   void add(uint64_t V) { Hash = hashing::detail::hash_16_bytes(Hash, V); }
917 
918   // No finishing is required, because the entire hash value is used.
919   uint64_t getHash() { return Hash; }
920 };
921 
922 } // end anonymous namespace
923 
924 // A function hash is calculated by considering only the number of arguments and
925 // whether a function is varargs, the order of basic blocks (given by the
926 // successors of each basic block in depth first order), and the order of
927 // opcodes of each instruction within each of these basic blocks. This mirrors
928 // the strategy compare() uses to compare functions by walking the BBs in depth
929 // first order and comparing each instruction in sequence. Because this hash
930 // does not look at the operands, it is insensitive to things such as the
931 // target of calls and the constants used in the function, which makes it useful
932 // when possibly merging functions which are the same modulo constants and call
933 // targets.
934 FunctionComparator::FunctionHash FunctionComparator::functionHash(Function &F) {
935   HashAccumulator64 H;
936   H.add(F.isVarArg());
937   H.add(F.arg_size());
938 
939   SmallVector<const BasicBlock *, 8> BBs;
940   SmallPtrSet<const BasicBlock *, 16> VisitedBBs;
941 
942   // Walk the blocks in the same order as FunctionComparator::cmpBasicBlocks(),
943   // accumulating the hash of the function "structure." (BB and opcode sequence)
944   BBs.push_back(&F.getEntryBlock());
945   VisitedBBs.insert(BBs[0]);
946   while (!BBs.empty()) {
947     const BasicBlock *BB = BBs.pop_back_val();
948     // This random value acts as a block header, as otherwise the partition of
949     // opcodes into BBs wouldn't affect the hash, only the order of the opcodes
950     H.add(45798);
951     for (auto &Inst : *BB) {
952       H.add(Inst.getOpcode());
953     }
954     const Instruction *Term = BB->getTerminator();
955     for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
956       if (!VisitedBBs.insert(Term->getSuccessor(i)).second)
957         continue;
958       BBs.push_back(Term->getSuccessor(i));
959     }
960   }
961   return H.getHash();
962 }
963