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 int FunctionComparator::cmpOperandBundlesSchema(const CallBase &LCS, 175 const CallBase &RCS) const { 176 assert(LCS.getOpcode() == RCS.getOpcode() && "Can't compare otherwise!"); 177 178 if (int Res = 179 cmpNumbers(LCS.getNumOperandBundles(), RCS.getNumOperandBundles())) 180 return Res; 181 182 for (unsigned I = 0, E = LCS.getNumOperandBundles(); I != E; ++I) { 183 auto OBL = LCS.getOperandBundleAt(I); 184 auto OBR = RCS.getOperandBundleAt(I); 185 186 if (int Res = OBL.getTagName().compare(OBR.getTagName())) 187 return Res; 188 189 if (int Res = cmpNumbers(OBL.Inputs.size(), OBR.Inputs.size())) 190 return Res; 191 } 192 193 return 0; 194 } 195 196 /// Constants comparison: 197 /// 1. Check whether type of L constant could be losslessly bitcasted to R 198 /// type. 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(); 205 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); 210 if (TypesRes != 0) { 211 // Types are different, but check whether we can bitcast them. 212 if (!TyL->isFirstClassType()) { 213 if (TyR->isFirstClassType()) 214 return -1; 215 // Neither TyL nor TyR are values of first class type. Return the result 216 // of comparing the types 217 return TypesRes; 218 } 219 if (!TyR->isFirstClassType()) { 220 if (TyL->isFirstClassType()) 221 return 1; 222 return TypesRes; 223 } 224 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; 229 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(); 234 235 if (TyLWidth != TyRWidth) 236 return cmpNumbers(TyLWidth, TyRWidth); 237 238 // Zero bit-width means neither TyL nor TyR are vectors. 239 if (!TyLWidth) { 240 PointerType *PTyL = dyn_cast<PointerType>(TyL); 241 PointerType *PTyR = dyn_cast<PointerType>(TyR); 242 if (PTyL && PTyR) { 243 unsigned AddrSpaceL = PTyL->getAddressSpace(); 244 unsigned AddrSpaceR = PTyR->getAddressSpace(); 245 if (int Res = cmpNumbers(AddrSpaceL, AddrSpaceR)) 246 return Res; 247 } 248 if (PTyL) 249 return 1; 250 if (PTyR) 251 return -1; 252 253 // TyL and TyR aren't vectors, nor pointers. We don't know how to 254 // bitcast them. 255 return TypesRes; 256 } 257 } 258 259 // OK, types are bitcastable, now check constant contents. 260 261 if (L->isNullValue() && R->isNullValue()) 262 return TypesRes; 263 if (L->isNullValue() && !R->isNullValue()) 264 return 1; 265 if (!L->isNullValue() && R->isNullValue()) 266 return -1; 267 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); 272 } 273 274 if (int Res = cmpNumbers(L->getValueID(), R->getValueID())) 275 return Res; 276 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()); 285 } 286 287 switch (L->getValueID()) { 288 case Value::UndefValueVal: 289 case Value::ConstantTokenNoneVal: 290 return TypesRes; 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); 295 } 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); 300 } 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)) 307 return Res; 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)))) 311 return Res; 312 } 313 return 0; 314 } 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)) 321 return Res; 322 for (unsigned i = 0; i != NumElementsL; ++i) { 323 if (int Res = cmpConstants(cast<Constant>(LS->getOperand(i)), 324 cast<Constant>(RS->getOperand(i)))) 325 return Res; 326 } 327 return 0; 328 } 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)) 335 return Res; 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)))) 339 return Res; 340 } 341 return 0; 342 } 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)) 349 return Res; 350 for (unsigned i = 0; i < NumOperandsL; ++i) { 351 if (int Res = cmpConstants(cast<Constant>(LE->getOperand(i)), 352 cast<Constant>(RE->getOperand(i)))) 353 return Res; 354 } 355 return 0; 356 } 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())) 361 return Res; 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(); 368 if (LBB == RBB) 369 return 0; 370 for (BasicBlock &BB : F->getBasicBlockList()) { 371 if (&BB == LBB) { 372 assert(&BB != RBB); 373 return -1; 374 } 375 if (&BB == RBB) 376 return 1; 377 } 378 llvm_unreachable("Basic Block Address does not point to a basic block in " 379 "its function."); 380 return -1; 381 } else { 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 384 // right functions. 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()); 389 } 390 } 391 default: // Unknown constant, abort. 392 LLVM_DEBUG(dbgs() << "Looking at valueID " << L->getValueID() << "\n"); 393 llvm_unreachable("Constant ValueID not recognized."); 394 return -1; 395 } 396 } 397 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); 402 } 403 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); 410 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); 416 417 if (TyL == TyR) 418 return 0; 419 420 if (int Res = cmpNumbers(TyL->getTypeID(), TyR->getTypeID())) 421 return Res; 422 423 switch (TyL->getTypeID()) { 424 default: 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. 430 case Type::VoidTyID: 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: 439 return 0; 440 441 case Type::PointerTyID: 442 assert(PTyL && PTyR && "Both types must be pointers here."); 443 return cmpNumbers(PTyL->getAddressSpace(), PTyR->getAddressSpace()); 444 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()); 450 451 if (STyL->isPacked() != STyR->isPacked()) 452 return cmpNumbers(STyL->isPacked(), STyR->isPacked()); 453 454 for (unsigned i = 0, e = STyL->getNumElements(); i != e; ++i) { 455 if (int Res = cmpTypes(STyL->getElementType(i), STyR->getElementType(i))) 456 return Res; 457 } 458 return 0; 459 } 460 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()); 466 467 if (FTyL->isVarArg() != FTyR->isVarArg()) 468 return cmpNumbers(FTyL->isVarArg(), FTyR->isVarArg()); 469 470 if (int Res = cmpTypes(FTyL->getReturnType(), FTyR->getReturnType())) 471 return Res; 472 473 for (unsigned i = 0, e = FTyL->getNumParams(); i != e; ++i) { 474 if (int Res = cmpTypes(FTyL->getParamType(i), FTyR->getParamType(i))) 475 return Res; 476 } 477 return 0; 478 } 479 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()); 486 } 487 case Type::FixedVectorTyID: 488 case Type::ScalableVectorTyID: { 489 auto *STyL = cast<VectorType>(TyL); 490 auto *STyR = cast<VectorType>(TyR); 491 if (STyL->getElementCount().isScalable() != 492 STyR->getElementCount().isScalable()) 493 return cmpNumbers(STyL->getElementCount().isScalable(), 494 STyR->getElementCount().isScalable()); 495 if (STyL->getElementCount() != STyR->getElementCount()) 496 return cmpNumbers(STyL->getElementCount().getKnownMinValue(), 497 STyR->getElementCount().getKnownMinValue()); 498 return cmpTypes(STyL->getElementType(), STyR->getElementType()); 499 } 500 } 501 } 502 503 // Determine whether the two operations are the same except that pointer-to-A 504 // and pointer-to-B are equivalent. This should be kept in sync with 505 // Instruction::isSameOperationAs. 506 // Read method declaration comments for more details. 507 int FunctionComparator::cmpOperations(const Instruction *L, 508 const Instruction *R, 509 bool &needToCmpOperands) const { 510 needToCmpOperands = true; 511 if (int Res = cmpValues(L, R)) 512 return Res; 513 514 // Differences from Instruction::isSameOperationAs: 515 // * replace type comparison with calls to cmpTypes. 516 // * we test for I->getRawSubclassOptionalData (nuw/nsw/tail) at the top. 517 // * because of the above, we don't test for the tail bit on calls later on. 518 if (int Res = cmpNumbers(L->getOpcode(), R->getOpcode())) 519 return Res; 520 521 if (const GetElementPtrInst *GEPL = dyn_cast<GetElementPtrInst>(L)) { 522 needToCmpOperands = false; 523 const GetElementPtrInst *GEPR = cast<GetElementPtrInst>(R); 524 if (int Res = 525 cmpValues(GEPL->getPointerOperand(), GEPR->getPointerOperand())) 526 return Res; 527 return cmpGEPs(GEPL, GEPR); 528 } 529 530 if (int Res = cmpNumbers(L->getNumOperands(), R->getNumOperands())) 531 return Res; 532 533 if (int Res = cmpTypes(L->getType(), R->getType())) 534 return Res; 535 536 if (int Res = cmpNumbers(L->getRawSubclassOptionalData(), 537 R->getRawSubclassOptionalData())) 538 return Res; 539 540 // We have two instructions of identical opcode and #operands. Check to see 541 // if all operands are the same type 542 for (unsigned i = 0, e = L->getNumOperands(); i != e; ++i) { 543 if (int Res = 544 cmpTypes(L->getOperand(i)->getType(), R->getOperand(i)->getType())) 545 return Res; 546 } 547 548 // Check special state that is a part of some instructions. 549 if (const AllocaInst *AI = dyn_cast<AllocaInst>(L)) { 550 if (int Res = cmpTypes(AI->getAllocatedType(), 551 cast<AllocaInst>(R)->getAllocatedType())) 552 return Res; 553 return cmpNumbers(AI->getAlignment(), cast<AllocaInst>(R)->getAlignment()); 554 } 555 if (const LoadInst *LI = dyn_cast<LoadInst>(L)) { 556 if (int Res = cmpNumbers(LI->isVolatile(), cast<LoadInst>(R)->isVolatile())) 557 return Res; 558 if (int Res = 559 cmpNumbers(LI->getAlignment(), cast<LoadInst>(R)->getAlignment())) 560 return Res; 561 if (int Res = 562 cmpOrderings(LI->getOrdering(), cast<LoadInst>(R)->getOrdering())) 563 return Res; 564 if (int Res = cmpNumbers(LI->getSyncScopeID(), 565 cast<LoadInst>(R)->getSyncScopeID())) 566 return Res; 567 return cmpRangeMetadata( 568 LI->getMetadata(LLVMContext::MD_range), 569 cast<LoadInst>(R)->getMetadata(LLVMContext::MD_range)); 570 } 571 if (const StoreInst *SI = dyn_cast<StoreInst>(L)) { 572 if (int Res = 573 cmpNumbers(SI->isVolatile(), cast<StoreInst>(R)->isVolatile())) 574 return Res; 575 if (int Res = 576 cmpNumbers(SI->getAlignment(), cast<StoreInst>(R)->getAlignment())) 577 return Res; 578 if (int Res = 579 cmpOrderings(SI->getOrdering(), cast<StoreInst>(R)->getOrdering())) 580 return Res; 581 return cmpNumbers(SI->getSyncScopeID(), 582 cast<StoreInst>(R)->getSyncScopeID()); 583 } 584 if (const CmpInst *CI = dyn_cast<CmpInst>(L)) 585 return cmpNumbers(CI->getPredicate(), cast<CmpInst>(R)->getPredicate()); 586 if (auto *CBL = dyn_cast<CallBase>(L)) { 587 auto *CBR = cast<CallBase>(R); 588 if (int Res = cmpNumbers(CBL->getCallingConv(), CBR->getCallingConv())) 589 return Res; 590 if (int Res = cmpAttrs(CBL->getAttributes(), CBR->getAttributes())) 591 return Res; 592 if (int Res = cmpOperandBundlesSchema(*CBL, *CBR)) 593 return Res; 594 if (const CallInst *CI = dyn_cast<CallInst>(L)) 595 if (int Res = cmpNumbers(CI->getTailCallKind(), 596 cast<CallInst>(R)->getTailCallKind())) 597 return Res; 598 return cmpRangeMetadata(L->getMetadata(LLVMContext::MD_range), 599 R->getMetadata(LLVMContext::MD_range)); 600 } 601 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(L)) { 602 ArrayRef<unsigned> LIndices = IVI->getIndices(); 603 ArrayRef<unsigned> RIndices = cast<InsertValueInst>(R)->getIndices(); 604 if (int Res = cmpNumbers(LIndices.size(), RIndices.size())) 605 return Res; 606 for (size_t i = 0, e = LIndices.size(); i != e; ++i) { 607 if (int Res = cmpNumbers(LIndices[i], RIndices[i])) 608 return Res; 609 } 610 return 0; 611 } 612 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(L)) { 613 ArrayRef<unsigned> LIndices = EVI->getIndices(); 614 ArrayRef<unsigned> RIndices = cast<ExtractValueInst>(R)->getIndices(); 615 if (int Res = cmpNumbers(LIndices.size(), RIndices.size())) 616 return Res; 617 for (size_t i = 0, e = LIndices.size(); i != e; ++i) { 618 if (int Res = cmpNumbers(LIndices[i], RIndices[i])) 619 return Res; 620 } 621 } 622 if (const FenceInst *FI = dyn_cast<FenceInst>(L)) { 623 if (int Res = 624 cmpOrderings(FI->getOrdering(), cast<FenceInst>(R)->getOrdering())) 625 return Res; 626 return cmpNumbers(FI->getSyncScopeID(), 627 cast<FenceInst>(R)->getSyncScopeID()); 628 } 629 if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(L)) { 630 if (int Res = cmpNumbers(CXI->isVolatile(), 631 cast<AtomicCmpXchgInst>(R)->isVolatile())) 632 return Res; 633 if (int Res = 634 cmpNumbers(CXI->isWeak(), cast<AtomicCmpXchgInst>(R)->isWeak())) 635 return Res; 636 if (int Res = 637 cmpOrderings(CXI->getSuccessOrdering(), 638 cast<AtomicCmpXchgInst>(R)->getSuccessOrdering())) 639 return Res; 640 if (int Res = 641 cmpOrderings(CXI->getFailureOrdering(), 642 cast<AtomicCmpXchgInst>(R)->getFailureOrdering())) 643 return Res; 644 return cmpNumbers(CXI->getSyncScopeID(), 645 cast<AtomicCmpXchgInst>(R)->getSyncScopeID()); 646 } 647 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(L)) { 648 if (int Res = cmpNumbers(RMWI->getOperation(), 649 cast<AtomicRMWInst>(R)->getOperation())) 650 return Res; 651 if (int Res = cmpNumbers(RMWI->isVolatile(), 652 cast<AtomicRMWInst>(R)->isVolatile())) 653 return Res; 654 if (int Res = cmpOrderings(RMWI->getOrdering(), 655 cast<AtomicRMWInst>(R)->getOrdering())) 656 return Res; 657 return cmpNumbers(RMWI->getSyncScopeID(), 658 cast<AtomicRMWInst>(R)->getSyncScopeID()); 659 } 660 if (const ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(L)) { 661 ArrayRef<int> LMask = SVI->getShuffleMask(); 662 ArrayRef<int> RMask = cast<ShuffleVectorInst>(R)->getShuffleMask(); 663 if (int Res = cmpNumbers(LMask.size(), RMask.size())) 664 return Res; 665 for (size_t i = 0, e = LMask.size(); i != e; ++i) { 666 if (int Res = cmpNumbers(LMask[i], RMask[i])) 667 return Res; 668 } 669 } 670 if (const PHINode *PNL = dyn_cast<PHINode>(L)) { 671 const PHINode *PNR = cast<PHINode>(R); 672 // Ensure that in addition to the incoming values being identical 673 // (checked by the caller of this function), the incoming blocks 674 // are also identical. 675 for (unsigned i = 0, e = PNL->getNumIncomingValues(); i != e; ++i) { 676 if (int Res = 677 cmpValues(PNL->getIncomingBlock(i), PNR->getIncomingBlock(i))) 678 return Res; 679 } 680 } 681 return 0; 682 } 683 684 // Determine whether two GEP operations perform the same underlying arithmetic. 685 // Read method declaration comments for more details. 686 int FunctionComparator::cmpGEPs(const GEPOperator *GEPL, 687 const GEPOperator *GEPR) const { 688 unsigned int ASL = GEPL->getPointerAddressSpace(); 689 unsigned int ASR = GEPR->getPointerAddressSpace(); 690 691 if (int Res = cmpNumbers(ASL, ASR)) 692 return Res; 693 694 // When we have target data, we can reduce the GEP down to the value in bytes 695 // added to the address. 696 const DataLayout &DL = FnL->getParent()->getDataLayout(); 697 unsigned BitWidth = DL.getPointerSizeInBits(ASL); 698 APInt OffsetL(BitWidth, 0), OffsetR(BitWidth, 0); 699 if (GEPL->accumulateConstantOffset(DL, OffsetL) && 700 GEPR->accumulateConstantOffset(DL, OffsetR)) 701 return cmpAPInts(OffsetL, OffsetR); 702 if (int Res = 703 cmpTypes(GEPL->getSourceElementType(), GEPR->getSourceElementType())) 704 return Res; 705 706 if (int Res = cmpNumbers(GEPL->getNumOperands(), GEPR->getNumOperands())) 707 return Res; 708 709 for (unsigned i = 0, e = GEPL->getNumOperands(); i != e; ++i) { 710 if (int Res = cmpValues(GEPL->getOperand(i), GEPR->getOperand(i))) 711 return Res; 712 } 713 714 return 0; 715 } 716 717 int FunctionComparator::cmpInlineAsm(const InlineAsm *L, 718 const InlineAsm *R) const { 719 // InlineAsm's are uniqued. If they are the same pointer, obviously they are 720 // the same, otherwise compare the fields. 721 if (L == R) 722 return 0; 723 if (int Res = cmpTypes(L->getFunctionType(), R->getFunctionType())) 724 return Res; 725 if (int Res = cmpMem(L->getAsmString(), R->getAsmString())) 726 return Res; 727 if (int Res = cmpMem(L->getConstraintString(), R->getConstraintString())) 728 return Res; 729 if (int Res = cmpNumbers(L->hasSideEffects(), R->hasSideEffects())) 730 return Res; 731 if (int Res = cmpNumbers(L->isAlignStack(), R->isAlignStack())) 732 return Res; 733 if (int Res = cmpNumbers(L->getDialect(), R->getDialect())) 734 return Res; 735 assert(L->getFunctionType() != R->getFunctionType()); 736 return 0; 737 } 738 739 /// Compare two values used by the two functions under pair-wise comparison. If 740 /// this is the first time the values are seen, they're added to the mapping so 741 /// that we will detect mismatches on next use. 742 /// See comments in declaration for more details. 743 int FunctionComparator::cmpValues(const Value *L, const Value *R) const { 744 // Catch self-reference case. 745 if (L == FnL) { 746 if (R == FnR) 747 return 0; 748 return -1; 749 } 750 if (R == FnR) { 751 if (L == FnL) 752 return 0; 753 return 1; 754 } 755 756 const Constant *ConstL = dyn_cast<Constant>(L); 757 const Constant *ConstR = dyn_cast<Constant>(R); 758 if (ConstL && ConstR) { 759 if (L == R) 760 return 0; 761 return cmpConstants(ConstL, ConstR); 762 } 763 764 if (ConstL) 765 return 1; 766 if (ConstR) 767 return -1; 768 769 const InlineAsm *InlineAsmL = dyn_cast<InlineAsm>(L); 770 const InlineAsm *InlineAsmR = dyn_cast<InlineAsm>(R); 771 772 if (InlineAsmL && InlineAsmR) 773 return cmpInlineAsm(InlineAsmL, InlineAsmR); 774 if (InlineAsmL) 775 return 1; 776 if (InlineAsmR) 777 return -1; 778 779 auto LeftSN = sn_mapL.insert(std::make_pair(L, sn_mapL.size())), 780 RightSN = sn_mapR.insert(std::make_pair(R, sn_mapR.size())); 781 782 return cmpNumbers(LeftSN.first->second, RightSN.first->second); 783 } 784 785 // Test whether two basic blocks have equivalent behaviour. 786 int FunctionComparator::cmpBasicBlocks(const BasicBlock *BBL, 787 const BasicBlock *BBR) const { 788 BasicBlock::const_iterator InstL = BBL->begin(), InstLE = BBL->end(); 789 BasicBlock::const_iterator InstR = BBR->begin(), InstRE = BBR->end(); 790 791 do { 792 bool needToCmpOperands = true; 793 if (int Res = cmpOperations(&*InstL, &*InstR, needToCmpOperands)) 794 return Res; 795 if (needToCmpOperands) { 796 assert(InstL->getNumOperands() == InstR->getNumOperands()); 797 798 for (unsigned i = 0, e = InstL->getNumOperands(); i != e; ++i) { 799 Value *OpL = InstL->getOperand(i); 800 Value *OpR = InstR->getOperand(i); 801 if (int Res = cmpValues(OpL, OpR)) 802 return Res; 803 // cmpValues should ensure this is true. 804 assert(cmpTypes(OpL->getType(), OpR->getType()) == 0); 805 } 806 } 807 808 ++InstL; 809 ++InstR; 810 } while (InstL != InstLE && InstR != InstRE); 811 812 if (InstL != InstLE && InstR == InstRE) 813 return 1; 814 if (InstL == InstLE && InstR != InstRE) 815 return -1; 816 return 0; 817 } 818 819 int FunctionComparator::compareSignature() const { 820 if (int Res = cmpAttrs(FnL->getAttributes(), FnR->getAttributes())) 821 return Res; 822 823 if (int Res = cmpNumbers(FnL->hasGC(), FnR->hasGC())) 824 return Res; 825 826 if (FnL->hasGC()) { 827 if (int Res = cmpMem(FnL->getGC(), FnR->getGC())) 828 return Res; 829 } 830 831 if (int Res = cmpNumbers(FnL->hasSection(), FnR->hasSection())) 832 return Res; 833 834 if (FnL->hasSection()) { 835 if (int Res = cmpMem(FnL->getSection(), FnR->getSection())) 836 return Res; 837 } 838 839 if (int Res = cmpNumbers(FnL->isVarArg(), FnR->isVarArg())) 840 return Res; 841 842 // TODO: if it's internal and only used in direct calls, we could handle this 843 // case too. 844 if (int Res = cmpNumbers(FnL->getCallingConv(), FnR->getCallingConv())) 845 return Res; 846 847 if (int Res = cmpTypes(FnL->getFunctionType(), FnR->getFunctionType())) 848 return Res; 849 850 assert(FnL->arg_size() == FnR->arg_size() && 851 "Identically typed functions have different numbers of args!"); 852 853 // Visit the arguments so that they get enumerated in the order they're 854 // passed in. 855 for (Function::const_arg_iterator ArgLI = FnL->arg_begin(), 856 ArgRI = FnR->arg_begin(), 857 ArgLE = FnL->arg_end(); 858 ArgLI != ArgLE; ++ArgLI, ++ArgRI) { 859 if (cmpValues(&*ArgLI, &*ArgRI) != 0) 860 llvm_unreachable("Arguments repeat!"); 861 } 862 return 0; 863 } 864 865 // Test whether the two functions have equivalent behaviour. 866 int FunctionComparator::compare() { 867 beginCompare(); 868 869 if (int Res = compareSignature()) 870 return Res; 871 872 // We do a CFG-ordered walk since the actual ordering of the blocks in the 873 // linked list is immaterial. Our walk starts at the entry block for both 874 // functions, then takes each block from each terminator in order. As an 875 // artifact, this also means that unreachable blocks are ignored. 876 SmallVector<const BasicBlock *, 8> FnLBBs, FnRBBs; 877 SmallPtrSet<const BasicBlock *, 32> VisitedBBs; // in terms of F1. 878 879 FnLBBs.push_back(&FnL->getEntryBlock()); 880 FnRBBs.push_back(&FnR->getEntryBlock()); 881 882 VisitedBBs.insert(FnLBBs[0]); 883 while (!FnLBBs.empty()) { 884 const BasicBlock *BBL = FnLBBs.pop_back_val(); 885 const BasicBlock *BBR = FnRBBs.pop_back_val(); 886 887 if (int Res = cmpValues(BBL, BBR)) 888 return Res; 889 890 if (int Res = cmpBasicBlocks(BBL, BBR)) 891 return Res; 892 893 const Instruction *TermL = BBL->getTerminator(); 894 const Instruction *TermR = BBR->getTerminator(); 895 896 assert(TermL->getNumSuccessors() == TermR->getNumSuccessors()); 897 for (unsigned i = 0, e = TermL->getNumSuccessors(); i != e; ++i) { 898 if (!VisitedBBs.insert(TermL->getSuccessor(i)).second) 899 continue; 900 901 FnLBBs.push_back(TermL->getSuccessor(i)); 902 FnRBBs.push_back(TermR->getSuccessor(i)); 903 } 904 } 905 return 0; 906 } 907 908 namespace { 909 910 // Accumulate the hash of a sequence of 64-bit integers. This is similar to a 911 // hash of a sequence of 64bit ints, but the entire input does not need to be 912 // available at once. This interface is necessary for functionHash because it 913 // needs to accumulate the hash as the structure of the function is traversed 914 // without saving these values to an intermediate buffer. This form of hashing 915 // is not often needed, as usually the object to hash is just read from a 916 // buffer. 917 class HashAccumulator64 { 918 uint64_t Hash; 919 920 public: 921 // Initialize to random constant, so the state isn't zero. 922 HashAccumulator64() { Hash = 0x6acaa36bef8325c5ULL; } 923 924 void add(uint64_t V) { Hash = hashing::detail::hash_16_bytes(Hash, V); } 925 926 // No finishing is required, because the entire hash value is used. 927 uint64_t getHash() { return Hash; } 928 }; 929 930 } // end anonymous namespace 931 932 // A function hash is calculated by considering only the number of arguments and 933 // whether a function is varargs, the order of basic blocks (given by the 934 // successors of each basic block in depth first order), and the order of 935 // opcodes of each instruction within each of these basic blocks. This mirrors 936 // the strategy compare() uses to compare functions by walking the BBs in depth 937 // first order and comparing each instruction in sequence. Because this hash 938 // does not look at the operands, it is insensitive to things such as the 939 // target of calls and the constants used in the function, which makes it useful 940 // when possibly merging functions which are the same modulo constants and call 941 // targets. 942 FunctionComparator::FunctionHash FunctionComparator::functionHash(Function &F) { 943 HashAccumulator64 H; 944 H.add(F.isVarArg()); 945 H.add(F.arg_size()); 946 947 SmallVector<const BasicBlock *, 8> BBs; 948 SmallPtrSet<const BasicBlock *, 16> VisitedBBs; 949 950 // Walk the blocks in the same order as FunctionComparator::cmpBasicBlocks(), 951 // accumulating the hash of the function "structure." (BB and opcode sequence) 952 BBs.push_back(&F.getEntryBlock()); 953 VisitedBBs.insert(BBs[0]); 954 while (!BBs.empty()) { 955 const BasicBlock *BB = BBs.pop_back_val(); 956 // This random value acts as a block header, as otherwise the partition of 957 // opcodes into BBs wouldn't affect the hash, only the order of the opcodes 958 H.add(45798); 959 for (auto &Inst : *BB) { 960 H.add(Inst.getOpcode()); 961 } 962 const Instruction *Term = BB->getTerminator(); 963 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) { 964 if (!VisitedBBs.insert(Term->getSuccessor(i)).second) 965 continue; 966 BBs.push_back(Term->getSuccessor(i)); 967 } 968 } 969 return H.getHash(); 970 } 971