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