1 //===- FunctionComparator.h - Function Comparator -------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the FunctionComparator and GlobalNumberState classes
10 // which are used by the MergeFunctions pass for comparing functions.
12 //===----------------------------------------------------------------------===//
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"
51 #define DEBUG_TYPE "functioncomparator"
53 int FunctionComparator::cmpNumbers(uint64_t L, uint64_t R) const {
61 int FunctionComparator::cmpOrderings(AtomicOrdering L, AtomicOrdering R) const {
69 int FunctionComparator::cmpAPInts(const APInt &L, const APInt &R) const {
70 if (int Res = cmpNumbers(L.getBitWidth(), R.getBitWidth()))
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)))
86 if (int Res = cmpNumbers(APFloat::semanticsMaxExponent(SL),
87 APFloat::semanticsMaxExponent(SR)))
89 if (int Res = cmpNumbers(APFloat::semanticsMinExponent(SL),
90 APFloat::semanticsMinExponent(SR)))
92 if (int Res = cmpNumbers(APFloat::semanticsSizeInBits(SL),
93 APFloat::semanticsSizeInBits(SR)))
95 return cmpAPInts(L.bitcastToAPInt(), R.bitcastToAPInt());
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()))
103 // Compare strings lexicographically only when it is necessary: only when
104 // strings are equal in size.
108 int FunctionComparator::cmpAttrs(const AttributeList L,
109 const AttributeList R) const {
110 if (int Res = cmpNumbers(L.getNumAttrSets(), R.getNumAttrSets()))
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) {
121 if (LA.isTypeAttribute() && RA.isTypeAttribute()) {
122 if (LA.getKindAsEnum() != RA.getKindAsEnum())
123 return cmpNumbers(LA.getKindAsEnum(), RA.getKindAsEnum());
125 Type *TyL = LA.getValueAsType();
126 Type *TyR = RA.getValueAsType();
128 return cmpTypes(TyL, TyR);
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);
147 int FunctionComparator::cmpRangeMetadata(const MDNode *L,
148 const MDNode *R) const {
155 // Range metadata is a sequence of numbers. Make sure they are the same
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()))
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()))
174 int FunctionComparator::cmpOperandBundlesSchema(const CallBase &LCS,
175 const CallBase &RCS) const {
176 assert(LCS.getOpcode() == RCS.getOpcode() && "Can't compare otherwise!");
179 cmpNumbers(LCS.getNumOperandBundles(), RCS.getNumOperandBundles()))
182 for (unsigned I = 0, E = LCS.getNumOperandBundles(); I != E; ++I) {
183 auto OBL = LCS.getOperandBundleAt(I);
184 auto OBR = RCS.getOperandBundleAt(I);
186 if (int Res = OBL.getTagName().compare(OBR.getTagName()))
189 if (int Res = cmpNumbers(OBL.Inputs.size(), OBR.Inputs.size()))
196 /// Constants comparison:
197 /// 1. Check whether type of L constant could be losslessly bitcasted to R
199 /// 2. Compare constant contents.
200 /// For more details see declaration comments.
201 int FunctionComparator::cmpConstants(const Constant *L,
202 const Constant *R) const {
203 Type *TyL = L->getType();
204 Type *TyR = R->getType();
206 // Check whether types are bitcastable. This part is just re-factored
207 // Type::canLosslesslyBitCastTo method, but instead of returning true/false,
208 // we also pack into result which type is "less" for us.
209 int TypesRes = cmpTypes(TyL, TyR);
211 // Types are different, but check whether we can bitcast them.
212 if (!TyL->isFirstClassType()) {
213 if (TyR->isFirstClassType())
215 // Neither TyL nor TyR are values of first class type. Return the result
216 // of comparing the types
219 if (!TyR->isFirstClassType()) {
220 if (TyL->isFirstClassType())
225 // Vector -> Vector conversions are always lossless if the two vector types
226 // have the same size, otherwise not.
227 unsigned TyLWidth = 0;
228 unsigned TyRWidth = 0;
230 if (auto *VecTyL = dyn_cast<VectorType>(TyL))
231 TyLWidth = VecTyL->getPrimitiveSizeInBits().getFixedSize();
232 if (auto *VecTyR = dyn_cast<VectorType>(TyR))
233 TyRWidth = VecTyR->getPrimitiveSizeInBits().getFixedSize();
235 if (TyLWidth != TyRWidth)
236 return cmpNumbers(TyLWidth, TyRWidth);
238 // Zero bit-width means neither TyL nor TyR are vectors.
240 PointerType *PTyL = dyn_cast<PointerType>(TyL);
241 PointerType *PTyR = dyn_cast<PointerType>(TyR);
243 unsigned AddrSpaceL = PTyL->getAddressSpace();
244 unsigned AddrSpaceR = PTyR->getAddressSpace();
245 if (int Res = cmpNumbers(AddrSpaceL, AddrSpaceR))
253 // TyL and TyR aren't vectors, nor pointers. We don't know how to
259 // OK, types are bitcastable, now check constant contents.
261 if (L->isNullValue() && R->isNullValue())
263 if (L->isNullValue() && !R->isNullValue())
265 if (!L->isNullValue() && R->isNullValue())
268 auto GlobalValueL = const_cast<GlobalValue *>(dyn_cast<GlobalValue>(L));
269 auto GlobalValueR = const_cast<GlobalValue *>(dyn_cast<GlobalValue>(R));
270 if (GlobalValueL && GlobalValueR) {
271 return cmpGlobalValues(GlobalValueL, GlobalValueR);
274 if (int Res = cmpNumbers(L->getValueID(), R->getValueID()))
277 if (const auto *SeqL = dyn_cast<ConstantDataSequential>(L)) {
278 const auto *SeqR = cast<ConstantDataSequential>(R);
279 // This handles ConstantDataArray and ConstantDataVector. Note that we
280 // compare the two raw data arrays, which might differ depending on the host
281 // endianness. This isn't a problem though, because the endiness of a module
282 // will affect the order of the constants, but this order is the same
283 // for a given input module and host platform.
284 return cmpMem(SeqL->getRawDataValues(), SeqR->getRawDataValues());
287 switch (L->getValueID()) {
288 case Value::UndefValueVal:
289 case Value::ConstantTokenNoneVal:
291 case Value::ConstantIntVal: {
292 const APInt &LInt = cast<ConstantInt>(L)->getValue();
293 const APInt &RInt = cast<ConstantInt>(R)->getValue();
294 return cmpAPInts(LInt, RInt);
296 case Value::ConstantFPVal: {
297 const APFloat &LAPF = cast<ConstantFP>(L)->getValueAPF();
298 const APFloat &RAPF = cast<ConstantFP>(R)->getValueAPF();
299 return cmpAPFloats(LAPF, RAPF);
301 case Value::ConstantArrayVal: {
302 const ConstantArray *LA = cast<ConstantArray>(L);
303 const ConstantArray *RA = cast<ConstantArray>(R);
304 uint64_t NumElementsL = cast<ArrayType>(TyL)->getNumElements();
305 uint64_t NumElementsR = cast<ArrayType>(TyR)->getNumElements();
306 if (int Res = cmpNumbers(NumElementsL, NumElementsR))
308 for (uint64_t i = 0; i < NumElementsL; ++i) {
309 if (int Res = cmpConstants(cast<Constant>(LA->getOperand(i)),
310 cast<Constant>(RA->getOperand(i))))
315 case Value::ConstantStructVal: {
316 const ConstantStruct *LS = cast<ConstantStruct>(L);
317 const ConstantStruct *RS = cast<ConstantStruct>(R);
318 unsigned NumElementsL = cast<StructType>(TyL)->getNumElements();
319 unsigned NumElementsR = cast<StructType>(TyR)->getNumElements();
320 if (int Res = cmpNumbers(NumElementsL, NumElementsR))
322 for (unsigned i = 0; i != NumElementsL; ++i) {
323 if (int Res = cmpConstants(cast<Constant>(LS->getOperand(i)),
324 cast<Constant>(RS->getOperand(i))))
329 case Value::ConstantVectorVal: {
330 const ConstantVector *LV = cast<ConstantVector>(L);
331 const ConstantVector *RV = cast<ConstantVector>(R);
332 unsigned NumElementsL = cast<FixedVectorType>(TyL)->getNumElements();
333 unsigned NumElementsR = cast<FixedVectorType>(TyR)->getNumElements();
334 if (int Res = cmpNumbers(NumElementsL, NumElementsR))
336 for (uint64_t i = 0; i < NumElementsL; ++i) {
337 if (int Res = cmpConstants(cast<Constant>(LV->getOperand(i)),
338 cast<Constant>(RV->getOperand(i))))
343 case Value::ConstantExprVal: {
344 const ConstantExpr *LE = cast<ConstantExpr>(L);
345 const ConstantExpr *RE = cast<ConstantExpr>(R);
346 unsigned NumOperandsL = LE->getNumOperands();
347 unsigned NumOperandsR = RE->getNumOperands();
348 if (int Res = cmpNumbers(NumOperandsL, NumOperandsR))
350 for (unsigned i = 0; i < NumOperandsL; ++i) {
351 if (int Res = cmpConstants(cast<Constant>(LE->getOperand(i)),
352 cast<Constant>(RE->getOperand(i))))
357 case Value::BlockAddressVal: {
358 const BlockAddress *LBA = cast<BlockAddress>(L);
359 const BlockAddress *RBA = cast<BlockAddress>(R);
360 if (int Res = cmpValues(LBA->getFunction(), RBA->getFunction()))
362 if (LBA->getFunction() == RBA->getFunction()) {
363 // They are BBs in the same function. Order by which comes first in the
364 // BB order of the function. This order is deterministic.
365 Function *F = LBA->getFunction();
366 BasicBlock *LBB = LBA->getBasicBlock();
367 BasicBlock *RBB = RBA->getBasicBlock();
370 for (BasicBlock &BB : F->getBasicBlockList()) {
378 llvm_unreachable("Basic Block Address does not point to a basic block in "
382 // cmpValues said the functions are the same. So because they aren't
383 // literally the same pointer, they must respectively be the left and
385 assert(LBA->getFunction() == FnL && RBA->getFunction() == FnR);
386 // cmpValues will tell us if these are equivalent BasicBlocks, in the
387 // context of their respective functions.
388 return cmpValues(LBA->getBasicBlock(), RBA->getBasicBlock());
391 default: // Unknown constant, abort.
392 LLVM_DEBUG(dbgs() << "Looking at valueID " << L->getValueID() << "\n");
393 llvm_unreachable("Constant ValueID not recognized.");
398 int FunctionComparator::cmpGlobalValues(GlobalValue *L, GlobalValue *R) const {
399 uint64_t LNumber = GlobalNumbers->getNumber(L);
400 uint64_t RNumber = GlobalNumbers->getNumber(R);
401 return cmpNumbers(LNumber, RNumber);
404 /// cmpType - compares two types,
405 /// defines total ordering among the types set.
406 /// See method declaration comments for more details.
407 int FunctionComparator::cmpTypes(Type *TyL, Type *TyR) const {
408 PointerType *PTyL = dyn_cast<PointerType>(TyL);
409 PointerType *PTyR = dyn_cast<PointerType>(TyR);
411 const DataLayout &DL = FnL->getParent()->getDataLayout();
412 if (PTyL && PTyL->getAddressSpace() == 0)
413 TyL = DL.getIntPtrType(TyL);
414 if (PTyR && PTyR->getAddressSpace() == 0)
415 TyR = DL.getIntPtrType(TyR);
420 if (int Res = cmpNumbers(TyL->getTypeID(), TyR->getTypeID()))
423 switch (TyL->getTypeID()) {
425 llvm_unreachable("Unknown type!");
426 case Type::IntegerTyID:
427 return cmpNumbers(cast<IntegerType>(TyL)->getBitWidth(),
428 cast<IntegerType>(TyR)->getBitWidth());
429 // TyL == TyR would have returned true earlier, because types are uniqued.
431 case Type::FloatTyID:
432 case Type::DoubleTyID:
433 case Type::X86_FP80TyID:
434 case Type::FP128TyID:
435 case Type::PPC_FP128TyID:
436 case Type::LabelTyID:
437 case Type::MetadataTyID:
438 case Type::TokenTyID:
441 case Type::PointerTyID:
442 assert(PTyL && PTyR && "Both types must be pointers here.");
443 return cmpNumbers(PTyL->getAddressSpace(), PTyR->getAddressSpace());
445 case Type::StructTyID: {
446 StructType *STyL = cast<StructType>(TyL);
447 StructType *STyR = cast<StructType>(TyR);
448 if (STyL->getNumElements() != STyR->getNumElements())
449 return cmpNumbers(STyL->getNumElements(), STyR->getNumElements());
451 if (STyL->isPacked() != STyR->isPacked())
452 return cmpNumbers(STyL->isPacked(), STyR->isPacked());
454 for (unsigned i = 0, e = STyL->getNumElements(); i != e; ++i) {
455 if (int Res = cmpTypes(STyL->getElementType(i), STyR->getElementType(i)))
461 case Type::FunctionTyID: {
462 FunctionType *FTyL = cast<FunctionType>(TyL);
463 FunctionType *FTyR = cast<FunctionType>(TyR);
464 if (FTyL->getNumParams() != FTyR->getNumParams())
465 return cmpNumbers(FTyL->getNumParams(), FTyR->getNumParams());
467 if (FTyL->isVarArg() != FTyR->isVarArg())
468 return cmpNumbers(FTyL->isVarArg(), FTyR->isVarArg());
470 if (int Res = cmpTypes(FTyL->getReturnType(), FTyR->getReturnType()))
473 for (unsigned i = 0, e = FTyL->getNumParams(); i != e; ++i) {
474 if (int Res = cmpTypes(FTyL->getParamType(i), FTyR->getParamType(i)))
480 case Type::ArrayTyID: {
481 auto *STyL = cast<ArrayType>(TyL);
482 auto *STyR = cast<ArrayType>(TyR);
483 if (STyL->getNumElements() != STyR->getNumElements())
484 return cmpNumbers(STyL->getNumElements(), STyR->getNumElements());
485 return cmpTypes(STyL->getElementType(), STyR->getElementType());
487 case Type::FixedVectorTyID:
488 case Type::ScalableVectorTyID: {
489 auto *STyL = cast<VectorType>(TyL);
490 auto *STyR = cast<VectorType>(TyR);
491 if (STyL->getElementCount().Scalable != STyR->getElementCount().Scalable)
492 return cmpNumbers(STyL->getElementCount().Scalable,
493 STyR->getElementCount().Scalable);
494 if (STyL->getElementCount().Min != STyR->getElementCount().Min)
495 return cmpNumbers(STyL->getElementCount().Min,
496 STyR->getElementCount().Min);
497 return cmpTypes(STyL->getElementType(), STyR->getElementType());
502 // Determine whether the two operations are the same except that pointer-to-A
503 // and pointer-to-B are equivalent. This should be kept in sync with
504 // Instruction::isSameOperationAs.
505 // Read method declaration comments for more details.
506 int FunctionComparator::cmpOperations(const Instruction *L,
507 const Instruction *R,
508 bool &needToCmpOperands) const {
509 needToCmpOperands = true;
510 if (int Res = cmpValues(L, R))
513 // Differences from Instruction::isSameOperationAs:
514 // * replace type comparison with calls to cmpTypes.
515 // * we test for I->getRawSubclassOptionalData (nuw/nsw/tail) at the top.
516 // * because of the above, we don't test for the tail bit on calls later on.
517 if (int Res = cmpNumbers(L->getOpcode(), R->getOpcode()))
520 if (const GetElementPtrInst *GEPL = dyn_cast<GetElementPtrInst>(L)) {
521 needToCmpOperands = false;
522 const GetElementPtrInst *GEPR = cast<GetElementPtrInst>(R);
524 cmpValues(GEPL->getPointerOperand(), GEPR->getPointerOperand()))
526 return cmpGEPs(GEPL, GEPR);
529 if (int Res = cmpNumbers(L->getNumOperands(), R->getNumOperands()))
532 if (int Res = cmpTypes(L->getType(), R->getType()))
535 if (int Res = cmpNumbers(L->getRawSubclassOptionalData(),
536 R->getRawSubclassOptionalData()))
539 // We have two instructions of identical opcode and #operands. Check to see
540 // if all operands are the same type
541 for (unsigned i = 0, e = L->getNumOperands(); i != e; ++i) {
543 cmpTypes(L->getOperand(i)->getType(), R->getOperand(i)->getType()))
547 // Check special state that is a part of some instructions.
548 if (const AllocaInst *AI = dyn_cast<AllocaInst>(L)) {
549 if (int Res = cmpTypes(AI->getAllocatedType(),
550 cast<AllocaInst>(R)->getAllocatedType()))
552 return cmpNumbers(AI->getAlignment(), cast<AllocaInst>(R)->getAlignment());
554 if (const LoadInst *LI = dyn_cast<LoadInst>(L)) {
555 if (int Res = cmpNumbers(LI->isVolatile(), cast<LoadInst>(R)->isVolatile()))
558 cmpNumbers(LI->getAlignment(), cast<LoadInst>(R)->getAlignment()))
561 cmpOrderings(LI->getOrdering(), cast<LoadInst>(R)->getOrdering()))
563 if (int Res = cmpNumbers(LI->getSyncScopeID(),
564 cast<LoadInst>(R)->getSyncScopeID()))
566 return cmpRangeMetadata(
567 LI->getMetadata(LLVMContext::MD_range),
568 cast<LoadInst>(R)->getMetadata(LLVMContext::MD_range));
570 if (const StoreInst *SI = dyn_cast<StoreInst>(L)) {
572 cmpNumbers(SI->isVolatile(), cast<StoreInst>(R)->isVolatile()))
575 cmpNumbers(SI->getAlignment(), cast<StoreInst>(R)->getAlignment()))
578 cmpOrderings(SI->getOrdering(), cast<StoreInst>(R)->getOrdering()))
580 return cmpNumbers(SI->getSyncScopeID(),
581 cast<StoreInst>(R)->getSyncScopeID());
583 if (const CmpInst *CI = dyn_cast<CmpInst>(L))
584 return cmpNumbers(CI->getPredicate(), cast<CmpInst>(R)->getPredicate());
585 if (auto *CBL = dyn_cast<CallBase>(L)) {
586 auto *CBR = cast<CallBase>(R);
587 if (int Res = cmpNumbers(CBL->getCallingConv(), CBR->getCallingConv()))
589 if (int Res = cmpAttrs(CBL->getAttributes(), CBR->getAttributes()))
591 if (int Res = cmpOperandBundlesSchema(*CBL, *CBR))
593 if (const CallInst *CI = dyn_cast<CallInst>(L))
594 if (int Res = cmpNumbers(CI->getTailCallKind(),
595 cast<CallInst>(R)->getTailCallKind()))
597 return cmpRangeMetadata(L->getMetadata(LLVMContext::MD_range),
598 R->getMetadata(LLVMContext::MD_range));
600 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(L)) {
601 ArrayRef<unsigned> LIndices = IVI->getIndices();
602 ArrayRef<unsigned> RIndices = cast<InsertValueInst>(R)->getIndices();
603 if (int Res = cmpNumbers(LIndices.size(), RIndices.size()))
605 for (size_t i = 0, e = LIndices.size(); i != e; ++i) {
606 if (int Res = cmpNumbers(LIndices[i], RIndices[i]))
611 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(L)) {
612 ArrayRef<unsigned> LIndices = EVI->getIndices();
613 ArrayRef<unsigned> RIndices = cast<ExtractValueInst>(R)->getIndices();
614 if (int Res = cmpNumbers(LIndices.size(), RIndices.size()))
616 for (size_t i = 0, e = LIndices.size(); i != e; ++i) {
617 if (int Res = cmpNumbers(LIndices[i], RIndices[i]))
621 if (const FenceInst *FI = dyn_cast<FenceInst>(L)) {
623 cmpOrderings(FI->getOrdering(), cast<FenceInst>(R)->getOrdering()))
625 return cmpNumbers(FI->getSyncScopeID(),
626 cast<FenceInst>(R)->getSyncScopeID());
628 if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(L)) {
629 if (int Res = cmpNumbers(CXI->isVolatile(),
630 cast<AtomicCmpXchgInst>(R)->isVolatile()))
633 cmpNumbers(CXI->isWeak(), cast<AtomicCmpXchgInst>(R)->isWeak()))
636 cmpOrderings(CXI->getSuccessOrdering(),
637 cast<AtomicCmpXchgInst>(R)->getSuccessOrdering()))
640 cmpOrderings(CXI->getFailureOrdering(),
641 cast<AtomicCmpXchgInst>(R)->getFailureOrdering()))
643 return cmpNumbers(CXI->getSyncScopeID(),
644 cast<AtomicCmpXchgInst>(R)->getSyncScopeID());
646 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(L)) {
647 if (int Res = cmpNumbers(RMWI->getOperation(),
648 cast<AtomicRMWInst>(R)->getOperation()))
650 if (int Res = cmpNumbers(RMWI->isVolatile(),
651 cast<AtomicRMWInst>(R)->isVolatile()))
653 if (int Res = cmpOrderings(RMWI->getOrdering(),
654 cast<AtomicRMWInst>(R)->getOrdering()))
656 return cmpNumbers(RMWI->getSyncScopeID(),
657 cast<AtomicRMWInst>(R)->getSyncScopeID());
659 if (const ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(L)) {
660 ArrayRef<int> LMask = SVI->getShuffleMask();
661 ArrayRef<int> RMask = cast<ShuffleVectorInst>(R)->getShuffleMask();
662 if (int Res = cmpNumbers(LMask.size(), RMask.size()))
664 for (size_t i = 0, e = LMask.size(); i != e; ++i) {
665 if (int Res = cmpNumbers(LMask[i], RMask[i]))
669 if (const PHINode *PNL = dyn_cast<PHINode>(L)) {
670 const PHINode *PNR = cast<PHINode>(R);
671 // Ensure that in addition to the incoming values being identical
672 // (checked by the caller of this function), the incoming blocks
673 // are also identical.
674 for (unsigned i = 0, e = PNL->getNumIncomingValues(); i != e; ++i) {
676 cmpValues(PNL->getIncomingBlock(i), PNR->getIncomingBlock(i)))
683 // Determine whether two GEP operations perform the same underlying arithmetic.
684 // Read method declaration comments for more details.
685 int FunctionComparator::cmpGEPs(const GEPOperator *GEPL,
686 const GEPOperator *GEPR) const {
687 unsigned int ASL = GEPL->getPointerAddressSpace();
688 unsigned int ASR = GEPR->getPointerAddressSpace();
690 if (int Res = cmpNumbers(ASL, ASR))
693 // When we have target data, we can reduce the GEP down to the value in bytes
694 // added to the address.
695 const DataLayout &DL = FnL->getParent()->getDataLayout();
696 unsigned BitWidth = DL.getPointerSizeInBits(ASL);
697 APInt OffsetL(BitWidth, 0), OffsetR(BitWidth, 0);
698 if (GEPL->accumulateConstantOffset(DL, OffsetL) &&
699 GEPR->accumulateConstantOffset(DL, OffsetR))
700 return cmpAPInts(OffsetL, OffsetR);
702 cmpTypes(GEPL->getSourceElementType(), GEPR->getSourceElementType()))
705 if (int Res = cmpNumbers(GEPL->getNumOperands(), GEPR->getNumOperands()))
708 for (unsigned i = 0, e = GEPL->getNumOperands(); i != e; ++i) {
709 if (int Res = cmpValues(GEPL->getOperand(i), GEPR->getOperand(i)))
716 int FunctionComparator::cmpInlineAsm(const InlineAsm *L,
717 const InlineAsm *R) const {
718 // InlineAsm's are uniqued. If they are the same pointer, obviously they are
719 // the same, otherwise compare the fields.
722 if (int Res = cmpTypes(L->getFunctionType(), R->getFunctionType()))
724 if (int Res = cmpMem(L->getAsmString(), R->getAsmString()))
726 if (int Res = cmpMem(L->getConstraintString(), R->getConstraintString()))
728 if (int Res = cmpNumbers(L->hasSideEffects(), R->hasSideEffects()))
730 if (int Res = cmpNumbers(L->isAlignStack(), R->isAlignStack()))
732 if (int Res = cmpNumbers(L->getDialect(), R->getDialect()))
734 assert(L->getFunctionType() != R->getFunctionType());
738 /// Compare two values used by the two functions under pair-wise comparison. If
739 /// this is the first time the values are seen, they're added to the mapping so
740 /// that we will detect mismatches on next use.
741 /// See comments in declaration for more details.
742 int FunctionComparator::cmpValues(const Value *L, const Value *R) const {
743 // Catch self-reference case.
755 const Constant *ConstL = dyn_cast<Constant>(L);
756 const Constant *ConstR = dyn_cast<Constant>(R);
757 if (ConstL && ConstR) {
760 return cmpConstants(ConstL, ConstR);
768 const InlineAsm *InlineAsmL = dyn_cast<InlineAsm>(L);
769 const InlineAsm *InlineAsmR = dyn_cast<InlineAsm>(R);
771 if (InlineAsmL && InlineAsmR)
772 return cmpInlineAsm(InlineAsmL, InlineAsmR);
778 auto LeftSN = sn_mapL.insert(std::make_pair(L, sn_mapL.size())),
779 RightSN = sn_mapR.insert(std::make_pair(R, sn_mapR.size()));
781 return cmpNumbers(LeftSN.first->second, RightSN.first->second);
784 // Test whether two basic blocks have equivalent behaviour.
785 int FunctionComparator::cmpBasicBlocks(const BasicBlock *BBL,
786 const BasicBlock *BBR) const {
787 BasicBlock::const_iterator InstL = BBL->begin(), InstLE = BBL->end();
788 BasicBlock::const_iterator InstR = BBR->begin(), InstRE = BBR->end();
791 bool needToCmpOperands = true;
792 if (int Res = cmpOperations(&*InstL, &*InstR, needToCmpOperands))
794 if (needToCmpOperands) {
795 assert(InstL->getNumOperands() == InstR->getNumOperands());
797 for (unsigned i = 0, e = InstL->getNumOperands(); i != e; ++i) {
798 Value *OpL = InstL->getOperand(i);
799 Value *OpR = InstR->getOperand(i);
800 if (int Res = cmpValues(OpL, OpR))
802 // cmpValues should ensure this is true.
803 assert(cmpTypes(OpL->getType(), OpR->getType()) == 0);
809 } while (InstL != InstLE && InstR != InstRE);
811 if (InstL != InstLE && InstR == InstRE)
813 if (InstL == InstLE && InstR != InstRE)
818 int FunctionComparator::compareSignature() const {
819 if (int Res = cmpAttrs(FnL->getAttributes(), FnR->getAttributes()))
822 if (int Res = cmpNumbers(FnL->hasGC(), FnR->hasGC()))
826 if (int Res = cmpMem(FnL->getGC(), FnR->getGC()))
830 if (int Res = cmpNumbers(FnL->hasSection(), FnR->hasSection()))
833 if (FnL->hasSection()) {
834 if (int Res = cmpMem(FnL->getSection(), FnR->getSection()))
838 if (int Res = cmpNumbers(FnL->isVarArg(), FnR->isVarArg()))
841 // TODO: if it's internal and only used in direct calls, we could handle this
843 if (int Res = cmpNumbers(FnL->getCallingConv(), FnR->getCallingConv()))
846 if (int Res = cmpTypes(FnL->getFunctionType(), FnR->getFunctionType()))
849 assert(FnL->arg_size() == FnR->arg_size() &&
850 "Identically typed functions have different numbers of args!");
852 // Visit the arguments so that they get enumerated in the order they're
854 for (Function::const_arg_iterator ArgLI = FnL->arg_begin(),
855 ArgRI = FnR->arg_begin(),
856 ArgLE = FnL->arg_end();
857 ArgLI != ArgLE; ++ArgLI, ++ArgRI) {
858 if (cmpValues(&*ArgLI, &*ArgRI) != 0)
859 llvm_unreachable("Arguments repeat!");
864 // Test whether the two functions have equivalent behaviour.
865 int FunctionComparator::compare() {
868 if (int Res = compareSignature())
871 // We do a CFG-ordered walk since the actual ordering of the blocks in the
872 // linked list is immaterial. Our walk starts at the entry block for both
873 // functions, then takes each block from each terminator in order. As an
874 // artifact, this also means that unreachable blocks are ignored.
875 SmallVector<const BasicBlock *, 8> FnLBBs, FnRBBs;
876 SmallPtrSet<const BasicBlock *, 32> VisitedBBs; // in terms of F1.
878 FnLBBs.push_back(&FnL->getEntryBlock());
879 FnRBBs.push_back(&FnR->getEntryBlock());
881 VisitedBBs.insert(FnLBBs[0]);
882 while (!FnLBBs.empty()) {
883 const BasicBlock *BBL = FnLBBs.pop_back_val();
884 const BasicBlock *BBR = FnRBBs.pop_back_val();
886 if (int Res = cmpValues(BBL, BBR))
889 if (int Res = cmpBasicBlocks(BBL, BBR))
892 const Instruction *TermL = BBL->getTerminator();
893 const Instruction *TermR = BBR->getTerminator();
895 assert(TermL->getNumSuccessors() == TermR->getNumSuccessors());
896 for (unsigned i = 0, e = TermL->getNumSuccessors(); i != e; ++i) {
897 if (!VisitedBBs.insert(TermL->getSuccessor(i)).second)
900 FnLBBs.push_back(TermL->getSuccessor(i));
901 FnRBBs.push_back(TermR->getSuccessor(i));
909 // Accumulate the hash of a sequence of 64-bit integers. This is similar to a
910 // hash of a sequence of 64bit ints, but the entire input does not need to be
911 // available at once. This interface is necessary for functionHash because it
912 // needs to accumulate the hash as the structure of the function is traversed
913 // without saving these values to an intermediate buffer. This form of hashing
914 // is not often needed, as usually the object to hash is just read from a
916 class HashAccumulator64 {
920 // Initialize to random constant, so the state isn't zero.
921 HashAccumulator64() { Hash = 0x6acaa36bef8325c5ULL; }
923 void add(uint64_t V) { Hash = hashing::detail::hash_16_bytes(Hash, V); }
925 // No finishing is required, because the entire hash value is used.
926 uint64_t getHash() { return Hash; }
929 } // end anonymous namespace
931 // A function hash is calculated by considering only the number of arguments and
932 // whether a function is varargs, the order of basic blocks (given by the
933 // successors of each basic block in depth first order), and the order of
934 // opcodes of each instruction within each of these basic blocks. This mirrors
935 // the strategy compare() uses to compare functions by walking the BBs in depth
936 // first order and comparing each instruction in sequence. Because this hash
937 // does not look at the operands, it is insensitive to things such as the
938 // target of calls and the constants used in the function, which makes it useful
939 // when possibly merging functions which are the same modulo constants and call
941 FunctionComparator::FunctionHash FunctionComparator::functionHash(Function &F) {
946 SmallVector<const BasicBlock *, 8> BBs;
947 SmallPtrSet<const BasicBlock *, 16> VisitedBBs;
949 // Walk the blocks in the same order as FunctionComparator::cmpBasicBlocks(),
950 // accumulating the hash of the function "structure." (BB and opcode sequence)
951 BBs.push_back(&F.getEntryBlock());
952 VisitedBBs.insert(BBs[0]);
953 while (!BBs.empty()) {
954 const BasicBlock *BB = BBs.pop_back_val();
955 // This random value acts as a block header, as otherwise the partition of
956 // opcodes into BBs wouldn't affect the hash, only the order of the opcodes
958 for (auto &Inst : *BB) {
959 H.add(Inst.getOpcode());
961 const Instruction *Term = BB->getTerminator();
962 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
963 if (!VisitedBBs.insert(Term->getSuccessor(i)).second)
965 BBs.push_back(Term->getSuccessor(i));