1 //===- MergeFunctions.cpp - Merge identical functions ---------------------===// 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 pass looks for equivalent functions that are mergable and folds them. 10 // 11 // Order relation is defined on set of functions. It was made through 12 // special function comparison procedure that returns 13 // 0 when functions are equal, 14 // -1 when Left function is less than right function, and 15 // 1 for opposite case. We need total-ordering, so we need to maintain 16 // four properties on the functions set: 17 // a <= a (reflexivity) 18 // if a <= b and b <= a then a = b (antisymmetry) 19 // if a <= b and b <= c then a <= c (transitivity). 20 // for all a and b: a <= b or b <= a (totality). 21 // 22 // Comparison iterates through each instruction in each basic block. 23 // Functions are kept on binary tree. For each new function F we perform 24 // lookup in binary tree. 25 // In practice it works the following way: 26 // -- We define Function* container class with custom "operator<" (FunctionPtr). 27 // -- "FunctionPtr" instances are stored in std::set collection, so every 28 // std::set::insert operation will give you result in log(N) time. 29 // 30 // As an optimization, a hash of the function structure is calculated first, and 31 // two functions are only compared if they have the same hash. This hash is 32 // cheap to compute, and has the property that if function F == G according to 33 // the comparison function, then hash(F) == hash(G). This consistency property 34 // is critical to ensuring all possible merging opportunities are exploited. 35 // Collisions in the hash affect the speed of the pass but not the correctness 36 // or determinism of the resulting transformation. 37 // 38 // When a match is found the functions are folded. If both functions are 39 // overridable, we move the functionality into a new internal function and 40 // leave two overridable thunks to it. 41 // 42 //===----------------------------------------------------------------------===// 43 // 44 // Future work: 45 // 46 // * virtual functions. 47 // 48 // Many functions have their address taken by the virtual function table for 49 // the object they belong to. However, as long as it's only used for a lookup 50 // and call, this is irrelevant, and we'd like to fold such functions. 51 // 52 // * be smarter about bitcasts. 53 // 54 // In order to fold functions, we will sometimes add either bitcast instructions 55 // or bitcast constant expressions. Unfortunately, this can confound further 56 // analysis since the two functions differ where one has a bitcast and the 57 // other doesn't. We should learn to look through bitcasts. 58 // 59 // * Compare complex types with pointer types inside. 60 // * Compare cross-reference cases. 61 // * Compare complex expressions. 62 // 63 // All the three issues above could be described as ability to prove that 64 // fA == fB == fC == fE == fF == fG in example below: 65 // 66 // void fA() { 67 // fB(); 68 // } 69 // void fB() { 70 // fA(); 71 // } 72 // 73 // void fE() { 74 // fF(); 75 // } 76 // void fF() { 77 // fG(); 78 // } 79 // void fG() { 80 // fE(); 81 // } 82 // 83 // Simplest cross-reference case (fA <--> fB) was implemented in previous 84 // versions of MergeFunctions, though it presented only in two function pairs 85 // in test-suite (that counts >50k functions) 86 // Though possibility to detect complex cross-referencing (e.g.: A->B->C->D->A) 87 // could cover much more cases. 88 // 89 //===----------------------------------------------------------------------===// 90 91 #include "llvm/ADT/ArrayRef.h" 92 #include "llvm/ADT/SmallPtrSet.h" 93 #include "llvm/ADT/SmallVector.h" 94 #include "llvm/ADT/Statistic.h" 95 #include "llvm/IR/Argument.h" 96 #include "llvm/IR/Attributes.h" 97 #include "llvm/IR/BasicBlock.h" 98 #include "llvm/IR/CallSite.h" 99 #include "llvm/IR/Constant.h" 100 #include "llvm/IR/Constants.h" 101 #include "llvm/IR/DebugInfoMetadata.h" 102 #include "llvm/IR/DebugLoc.h" 103 #include "llvm/IR/DerivedTypes.h" 104 #include "llvm/IR/Function.h" 105 #include "llvm/IR/GlobalValue.h" 106 #include "llvm/IR/IRBuilder.h" 107 #include "llvm/IR/InstrTypes.h" 108 #include "llvm/IR/Instruction.h" 109 #include "llvm/IR/Instructions.h" 110 #include "llvm/IR/IntrinsicInst.h" 111 #include "llvm/IR/Module.h" 112 #include "llvm/IR/Type.h" 113 #include "llvm/IR/Use.h" 114 #include "llvm/IR/User.h" 115 #include "llvm/IR/Value.h" 116 #include "llvm/IR/ValueHandle.h" 117 #include "llvm/IR/ValueMap.h" 118 #include "llvm/InitializePasses.h" 119 #include "llvm/Pass.h" 120 #include "llvm/Support/Casting.h" 121 #include "llvm/Support/CommandLine.h" 122 #include "llvm/Support/Debug.h" 123 #include "llvm/Support/raw_ostream.h" 124 #include "llvm/Transforms/IPO.h" 125 #include "llvm/Transforms/Utils/FunctionComparator.h" 126 #include <algorithm> 127 #include <cassert> 128 #include <iterator> 129 #include <set> 130 #include <utility> 131 #include <vector> 132 133 using namespace llvm; 134 135 #define DEBUG_TYPE "mergefunc" 136 137 STATISTIC(NumFunctionsMerged, "Number of functions merged"); 138 STATISTIC(NumThunksWritten, "Number of thunks generated"); 139 STATISTIC(NumAliasesWritten, "Number of aliases generated"); 140 STATISTIC(NumDoubleWeak, "Number of new functions created"); 141 142 static cl::opt<unsigned> NumFunctionsForSanityCheck( 143 "mergefunc-sanity", 144 cl::desc("How many functions in module could be used for " 145 "MergeFunctions pass sanity check. " 146 "'0' disables this check. Works only with '-debug' key."), 147 cl::init(0), cl::Hidden); 148 149 // Under option -mergefunc-preserve-debug-info we: 150 // - Do not create a new function for a thunk. 151 // - Retain the debug info for a thunk's parameters (and associated 152 // instructions for the debug info) from the entry block. 153 // Note: -debug will display the algorithm at work. 154 // - Create debug-info for the call (to the shared implementation) made by 155 // a thunk and its return value. 156 // - Erase the rest of the function, retaining the (minimally sized) entry 157 // block to create a thunk. 158 // - Preserve a thunk's call site to point to the thunk even when both occur 159 // within the same translation unit, to aid debugability. Note that this 160 // behaviour differs from the underlying -mergefunc implementation which 161 // modifies the thunk's call site to point to the shared implementation 162 // when both occur within the same translation unit. 163 static cl::opt<bool> 164 MergeFunctionsPDI("mergefunc-preserve-debug-info", cl::Hidden, 165 cl::init(false), 166 cl::desc("Preserve debug info in thunk when mergefunc " 167 "transformations are made.")); 168 169 static cl::opt<bool> 170 MergeFunctionsAliases("mergefunc-use-aliases", cl::Hidden, 171 cl::init(false), 172 cl::desc("Allow mergefunc to create aliases")); 173 174 namespace { 175 176 class FunctionNode { 177 mutable AssertingVH<Function> F; 178 FunctionComparator::FunctionHash Hash; 179 180 public: 181 // Note the hash is recalculated potentially multiple times, but it is cheap. 182 FunctionNode(Function *F) 183 : F(F), Hash(FunctionComparator::functionHash(*F)) {} 184 185 Function *getFunc() const { return F; } 186 FunctionComparator::FunctionHash getHash() const { return Hash; } 187 188 /// Replace the reference to the function F by the function G, assuming their 189 /// implementations are equal. 190 void replaceBy(Function *G) const { 191 F = G; 192 } 193 }; 194 195 /// MergeFunctions finds functions which will generate identical machine code, 196 /// by considering all pointer types to be equivalent. Once identified, 197 /// MergeFunctions will fold them by replacing a call to one to a call to a 198 /// bitcast of the other. 199 class MergeFunctions : public ModulePass { 200 public: 201 static char ID; 202 203 MergeFunctions() 204 : ModulePass(ID), FnTree(FunctionNodeCmp(&GlobalNumbers)) { 205 initializeMergeFunctionsPass(*PassRegistry::getPassRegistry()); 206 } 207 208 bool runOnModule(Module &M) override; 209 210 private: 211 // The function comparison operator is provided here so that FunctionNodes do 212 // not need to become larger with another pointer. 213 class FunctionNodeCmp { 214 GlobalNumberState* GlobalNumbers; 215 216 public: 217 FunctionNodeCmp(GlobalNumberState* GN) : GlobalNumbers(GN) {} 218 219 bool operator()(const FunctionNode &LHS, const FunctionNode &RHS) const { 220 // Order first by hashes, then full function comparison. 221 if (LHS.getHash() != RHS.getHash()) 222 return LHS.getHash() < RHS.getHash(); 223 FunctionComparator FCmp(LHS.getFunc(), RHS.getFunc(), GlobalNumbers); 224 return FCmp.compare() == -1; 225 } 226 }; 227 using FnTreeType = std::set<FunctionNode, FunctionNodeCmp>; 228 229 GlobalNumberState GlobalNumbers; 230 231 /// A work queue of functions that may have been modified and should be 232 /// analyzed again. 233 std::vector<WeakTrackingVH> Deferred; 234 235 #ifndef NDEBUG 236 /// Checks the rules of order relation introduced among functions set. 237 /// Returns true, if sanity check has been passed, and false if failed. 238 bool doSanityCheck(std::vector<WeakTrackingVH> &Worklist); 239 #endif 240 241 /// Insert a ComparableFunction into the FnTree, or merge it away if it's 242 /// equal to one that's already present. 243 bool insert(Function *NewFunction); 244 245 /// Remove a Function from the FnTree and queue it up for a second sweep of 246 /// analysis. 247 void remove(Function *F); 248 249 /// Find the functions that use this Value and remove them from FnTree and 250 /// queue the functions. 251 void removeUsers(Value *V); 252 253 /// Replace all direct calls of Old with calls of New. Will bitcast New if 254 /// necessary to make types match. 255 void replaceDirectCallers(Function *Old, Function *New); 256 257 /// Merge two equivalent functions. Upon completion, G may be deleted, or may 258 /// be converted into a thunk. In either case, it should never be visited 259 /// again. 260 void mergeTwoFunctions(Function *F, Function *G); 261 262 /// Fill PDIUnrelatedWL with instructions from the entry block that are 263 /// unrelated to parameter related debug info. 264 void filterInstsUnrelatedToPDI(BasicBlock *GEntryBlock, 265 std::vector<Instruction *> &PDIUnrelatedWL); 266 267 /// Erase the rest of the CFG (i.e. barring the entry block). 268 void eraseTail(Function *G); 269 270 /// Erase the instructions in PDIUnrelatedWL as they are unrelated to the 271 /// parameter debug info, from the entry block. 272 void eraseInstsUnrelatedToPDI(std::vector<Instruction *> &PDIUnrelatedWL); 273 274 /// Replace G with a simple tail call to bitcast(F). Also (unless 275 /// MergeFunctionsPDI holds) replace direct uses of G with bitcast(F), 276 /// delete G. 277 void writeThunk(Function *F, Function *G); 278 279 // Replace G with an alias to F (deleting function G) 280 void writeAlias(Function *F, Function *G); 281 282 // Replace G with an alias to F if possible, or a thunk to F if possible. 283 // Returns false if neither is the case. 284 bool writeThunkOrAlias(Function *F, Function *G); 285 286 /// Replace function F with function G in the function tree. 287 void replaceFunctionInTree(const FunctionNode &FN, Function *G); 288 289 /// The set of all distinct functions. Use the insert() and remove() methods 290 /// to modify it. The map allows efficient lookup and deferring of Functions. 291 FnTreeType FnTree; 292 293 // Map functions to the iterators of the FunctionNode which contains them 294 // in the FnTree. This must be updated carefully whenever the FnTree is 295 // modified, i.e. in insert(), remove(), and replaceFunctionInTree(), to avoid 296 // dangling iterators into FnTree. The invariant that preserves this is that 297 // there is exactly one mapping F -> FN for each FunctionNode FN in FnTree. 298 DenseMap<AssertingVH<Function>, FnTreeType::iterator> FNodesInTree; 299 }; 300 301 } // end anonymous namespace 302 303 char MergeFunctions::ID = 0; 304 305 INITIALIZE_PASS(MergeFunctions, "mergefunc", "Merge Functions", false, false) 306 307 ModulePass *llvm::createMergeFunctionsPass() { 308 return new MergeFunctions(); 309 } 310 311 #ifndef NDEBUG 312 bool MergeFunctions::doSanityCheck(std::vector<WeakTrackingVH> &Worklist) { 313 if (const unsigned Max = NumFunctionsForSanityCheck) { 314 unsigned TripleNumber = 0; 315 bool Valid = true; 316 317 dbgs() << "MERGEFUNC-SANITY: Started for first " << Max << " functions.\n"; 318 319 unsigned i = 0; 320 for (std::vector<WeakTrackingVH>::iterator I = Worklist.begin(), 321 E = Worklist.end(); 322 I != E && i < Max; ++I, ++i) { 323 unsigned j = i; 324 for (std::vector<WeakTrackingVH>::iterator J = I; J != E && j < Max; 325 ++J, ++j) { 326 Function *F1 = cast<Function>(*I); 327 Function *F2 = cast<Function>(*J); 328 int Res1 = FunctionComparator(F1, F2, &GlobalNumbers).compare(); 329 int Res2 = FunctionComparator(F2, F1, &GlobalNumbers).compare(); 330 331 // If F1 <= F2, then F2 >= F1, otherwise report failure. 332 if (Res1 != -Res2) { 333 dbgs() << "MERGEFUNC-SANITY: Non-symmetric; triple: " << TripleNumber 334 << "\n"; 335 dbgs() << *F1 << '\n' << *F2 << '\n'; 336 Valid = false; 337 } 338 339 if (Res1 == 0) 340 continue; 341 342 unsigned k = j; 343 for (std::vector<WeakTrackingVH>::iterator K = J; K != E && k < Max; 344 ++k, ++K, ++TripleNumber) { 345 if (K == J) 346 continue; 347 348 Function *F3 = cast<Function>(*K); 349 int Res3 = FunctionComparator(F1, F3, &GlobalNumbers).compare(); 350 int Res4 = FunctionComparator(F2, F3, &GlobalNumbers).compare(); 351 352 bool Transitive = true; 353 354 if (Res1 != 0 && Res1 == Res4) { 355 // F1 > F2, F2 > F3 => F1 > F3 356 Transitive = Res3 == Res1; 357 } else if (Res3 != 0 && Res3 == -Res4) { 358 // F1 > F3, F3 > F2 => F1 > F2 359 Transitive = Res3 == Res1; 360 } else if (Res4 != 0 && -Res3 == Res4) { 361 // F2 > F3, F3 > F1 => F2 > F1 362 Transitive = Res4 == -Res1; 363 } 364 365 if (!Transitive) { 366 dbgs() << "MERGEFUNC-SANITY: Non-transitive; triple: " 367 << TripleNumber << "\n"; 368 dbgs() << "Res1, Res3, Res4: " << Res1 << ", " << Res3 << ", " 369 << Res4 << "\n"; 370 dbgs() << *F1 << '\n' << *F2 << '\n' << *F3 << '\n'; 371 Valid = false; 372 } 373 } 374 } 375 } 376 377 dbgs() << "MERGEFUNC-SANITY: " << (Valid ? "Passed." : "Failed.") << "\n"; 378 return Valid; 379 } 380 return true; 381 } 382 #endif 383 384 /// Check whether \p F is eligible for function merging. 385 static bool isEligibleForMerging(Function &F) { 386 return !F.isDeclaration() && !F.hasAvailableExternallyLinkage(); 387 } 388 389 bool MergeFunctions::runOnModule(Module &M) { 390 if (skipModule(M)) 391 return false; 392 393 bool Changed = false; 394 395 // All functions in the module, ordered by hash. Functions with a unique 396 // hash value are easily eliminated. 397 std::vector<std::pair<FunctionComparator::FunctionHash, Function *>> 398 HashedFuncs; 399 for (Function &Func : M) { 400 if (isEligibleForMerging(Func)) { 401 HashedFuncs.push_back({FunctionComparator::functionHash(Func), &Func}); 402 } 403 } 404 405 llvm::stable_sort(HashedFuncs, less_first()); 406 407 auto S = HashedFuncs.begin(); 408 for (auto I = HashedFuncs.begin(), IE = HashedFuncs.end(); I != IE; ++I) { 409 // If the hash value matches the previous value or the next one, we must 410 // consider merging it. Otherwise it is dropped and never considered again. 411 if ((I != S && std::prev(I)->first == I->first) || 412 (std::next(I) != IE && std::next(I)->first == I->first) ) { 413 Deferred.push_back(WeakTrackingVH(I->second)); 414 } 415 } 416 417 do { 418 std::vector<WeakTrackingVH> Worklist; 419 Deferred.swap(Worklist); 420 421 LLVM_DEBUG(doSanityCheck(Worklist)); 422 423 LLVM_DEBUG(dbgs() << "size of module: " << M.size() << '\n'); 424 LLVM_DEBUG(dbgs() << "size of worklist: " << Worklist.size() << '\n'); 425 426 // Insert functions and merge them. 427 for (WeakTrackingVH &I : Worklist) { 428 if (!I) 429 continue; 430 Function *F = cast<Function>(I); 431 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage()) { 432 Changed |= insert(F); 433 } 434 } 435 LLVM_DEBUG(dbgs() << "size of FnTree: " << FnTree.size() << '\n'); 436 } while (!Deferred.empty()); 437 438 FnTree.clear(); 439 FNodesInTree.clear(); 440 GlobalNumbers.clear(); 441 442 return Changed; 443 } 444 445 // Replace direct callers of Old with New. 446 void MergeFunctions::replaceDirectCallers(Function *Old, Function *New) { 447 Constant *BitcastNew = ConstantExpr::getBitCast(New, Old->getType()); 448 for (auto UI = Old->use_begin(), UE = Old->use_end(); UI != UE;) { 449 Use *U = &*UI; 450 ++UI; 451 CallSite CS(U->getUser()); 452 if (CS && CS.isCallee(U)) { 453 // Do not copy attributes from the called function to the call-site. 454 // Function comparison ensures that the attributes are the same up to 455 // type congruences in byval(), in which case we need to keep the byval 456 // type of the call-site, not the callee function. 457 remove(CS.getInstruction()->getFunction()); 458 U->set(BitcastNew); 459 } 460 } 461 } 462 463 // Helper for writeThunk, 464 // Selects proper bitcast operation, 465 // but a bit simpler then CastInst::getCastOpcode. 466 static Value *createCast(IRBuilder<> &Builder, Value *V, Type *DestTy) { 467 Type *SrcTy = V->getType(); 468 if (SrcTy->isStructTy()) { 469 assert(DestTy->isStructTy()); 470 assert(SrcTy->getStructNumElements() == DestTy->getStructNumElements()); 471 Value *Result = UndefValue::get(DestTy); 472 for (unsigned int I = 0, E = SrcTy->getStructNumElements(); I < E; ++I) { 473 Value *Element = createCast( 474 Builder, Builder.CreateExtractValue(V, makeArrayRef(I)), 475 DestTy->getStructElementType(I)); 476 477 Result = 478 Builder.CreateInsertValue(Result, Element, makeArrayRef(I)); 479 } 480 return Result; 481 } 482 assert(!DestTy->isStructTy()); 483 if (SrcTy->isIntegerTy() && DestTy->isPointerTy()) 484 return Builder.CreateIntToPtr(V, DestTy); 485 else if (SrcTy->isPointerTy() && DestTy->isIntegerTy()) 486 return Builder.CreatePtrToInt(V, DestTy); 487 else 488 return Builder.CreateBitCast(V, DestTy); 489 } 490 491 // Erase the instructions in PDIUnrelatedWL as they are unrelated to the 492 // parameter debug info, from the entry block. 493 void MergeFunctions::eraseInstsUnrelatedToPDI( 494 std::vector<Instruction *> &PDIUnrelatedWL) { 495 LLVM_DEBUG( 496 dbgs() << " Erasing instructions (in reverse order of appearance in " 497 "entry block) unrelated to parameter debug info from entry " 498 "block: {\n"); 499 while (!PDIUnrelatedWL.empty()) { 500 Instruction *I = PDIUnrelatedWL.back(); 501 LLVM_DEBUG(dbgs() << " Deleting Instruction: "); 502 LLVM_DEBUG(I->print(dbgs())); 503 LLVM_DEBUG(dbgs() << "\n"); 504 I->eraseFromParent(); 505 PDIUnrelatedWL.pop_back(); 506 } 507 LLVM_DEBUG(dbgs() << " } // Done erasing instructions unrelated to parameter " 508 "debug info from entry block. \n"); 509 } 510 511 // Reduce G to its entry block. 512 void MergeFunctions::eraseTail(Function *G) { 513 std::vector<BasicBlock *> WorklistBB; 514 for (Function::iterator BBI = std::next(G->begin()), BBE = G->end(); 515 BBI != BBE; ++BBI) { 516 BBI->dropAllReferences(); 517 WorklistBB.push_back(&*BBI); 518 } 519 while (!WorklistBB.empty()) { 520 BasicBlock *BB = WorklistBB.back(); 521 BB->eraseFromParent(); 522 WorklistBB.pop_back(); 523 } 524 } 525 526 // We are interested in the following instructions from the entry block as being 527 // related to parameter debug info: 528 // - @llvm.dbg.declare 529 // - stores from the incoming parameters to locations on the stack-frame 530 // - allocas that create these locations on the stack-frame 531 // - @llvm.dbg.value 532 // - the entry block's terminator 533 // The rest are unrelated to debug info for the parameters; fill up 534 // PDIUnrelatedWL with such instructions. 535 void MergeFunctions::filterInstsUnrelatedToPDI( 536 BasicBlock *GEntryBlock, std::vector<Instruction *> &PDIUnrelatedWL) { 537 std::set<Instruction *> PDIRelated; 538 for (BasicBlock::iterator BI = GEntryBlock->begin(), BIE = GEntryBlock->end(); 539 BI != BIE; ++BI) { 540 if (auto *DVI = dyn_cast<DbgValueInst>(&*BI)) { 541 LLVM_DEBUG(dbgs() << " Deciding: "); 542 LLVM_DEBUG(BI->print(dbgs())); 543 LLVM_DEBUG(dbgs() << "\n"); 544 DILocalVariable *DILocVar = DVI->getVariable(); 545 if (DILocVar->isParameter()) { 546 LLVM_DEBUG(dbgs() << " Include (parameter): "); 547 LLVM_DEBUG(BI->print(dbgs())); 548 LLVM_DEBUG(dbgs() << "\n"); 549 PDIRelated.insert(&*BI); 550 } else { 551 LLVM_DEBUG(dbgs() << " Delete (!parameter): "); 552 LLVM_DEBUG(BI->print(dbgs())); 553 LLVM_DEBUG(dbgs() << "\n"); 554 } 555 } else if (auto *DDI = dyn_cast<DbgDeclareInst>(&*BI)) { 556 LLVM_DEBUG(dbgs() << " Deciding: "); 557 LLVM_DEBUG(BI->print(dbgs())); 558 LLVM_DEBUG(dbgs() << "\n"); 559 DILocalVariable *DILocVar = DDI->getVariable(); 560 if (DILocVar->isParameter()) { 561 LLVM_DEBUG(dbgs() << " Parameter: "); 562 LLVM_DEBUG(DILocVar->print(dbgs())); 563 AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress()); 564 if (AI) { 565 LLVM_DEBUG(dbgs() << " Processing alloca users: "); 566 LLVM_DEBUG(dbgs() << "\n"); 567 for (User *U : AI->users()) { 568 if (StoreInst *SI = dyn_cast<StoreInst>(U)) { 569 if (Value *Arg = SI->getValueOperand()) { 570 if (dyn_cast<Argument>(Arg)) { 571 LLVM_DEBUG(dbgs() << " Include: "); 572 LLVM_DEBUG(AI->print(dbgs())); 573 LLVM_DEBUG(dbgs() << "\n"); 574 PDIRelated.insert(AI); 575 LLVM_DEBUG(dbgs() << " Include (parameter): "); 576 LLVM_DEBUG(SI->print(dbgs())); 577 LLVM_DEBUG(dbgs() << "\n"); 578 PDIRelated.insert(SI); 579 LLVM_DEBUG(dbgs() << " Include: "); 580 LLVM_DEBUG(BI->print(dbgs())); 581 LLVM_DEBUG(dbgs() << "\n"); 582 PDIRelated.insert(&*BI); 583 } else { 584 LLVM_DEBUG(dbgs() << " Delete (!parameter): "); 585 LLVM_DEBUG(SI->print(dbgs())); 586 LLVM_DEBUG(dbgs() << "\n"); 587 } 588 } 589 } else { 590 LLVM_DEBUG(dbgs() << " Defer: "); 591 LLVM_DEBUG(U->print(dbgs())); 592 LLVM_DEBUG(dbgs() << "\n"); 593 } 594 } 595 } else { 596 LLVM_DEBUG(dbgs() << " Delete (alloca NULL): "); 597 LLVM_DEBUG(BI->print(dbgs())); 598 LLVM_DEBUG(dbgs() << "\n"); 599 } 600 } else { 601 LLVM_DEBUG(dbgs() << " Delete (!parameter): "); 602 LLVM_DEBUG(BI->print(dbgs())); 603 LLVM_DEBUG(dbgs() << "\n"); 604 } 605 } else if (BI->isTerminator() && &*BI == GEntryBlock->getTerminator()) { 606 LLVM_DEBUG(dbgs() << " Will Include Terminator: "); 607 LLVM_DEBUG(BI->print(dbgs())); 608 LLVM_DEBUG(dbgs() << "\n"); 609 PDIRelated.insert(&*BI); 610 } else { 611 LLVM_DEBUG(dbgs() << " Defer: "); 612 LLVM_DEBUG(BI->print(dbgs())); 613 LLVM_DEBUG(dbgs() << "\n"); 614 } 615 } 616 LLVM_DEBUG( 617 dbgs() 618 << " Report parameter debug info related/related instructions: {\n"); 619 for (BasicBlock::iterator BI = GEntryBlock->begin(), BE = GEntryBlock->end(); 620 BI != BE; ++BI) { 621 622 Instruction *I = &*BI; 623 if (PDIRelated.find(I) == PDIRelated.end()) { 624 LLVM_DEBUG(dbgs() << " !PDIRelated: "); 625 LLVM_DEBUG(I->print(dbgs())); 626 LLVM_DEBUG(dbgs() << "\n"); 627 PDIUnrelatedWL.push_back(I); 628 } else { 629 LLVM_DEBUG(dbgs() << " PDIRelated: "); 630 LLVM_DEBUG(I->print(dbgs())); 631 LLVM_DEBUG(dbgs() << "\n"); 632 } 633 } 634 LLVM_DEBUG(dbgs() << " }\n"); 635 } 636 637 /// Whether this function may be replaced by a forwarding thunk. 638 static bool canCreateThunkFor(Function *F) { 639 if (F->isVarArg()) 640 return false; 641 642 // Don't merge tiny functions using a thunk, since it can just end up 643 // making the function larger. 644 if (F->size() == 1) { 645 if (F->front().size() <= 2) { 646 LLVM_DEBUG(dbgs() << "canCreateThunkFor: " << F->getName() 647 << " is too small to bother creating a thunk for\n"); 648 return false; 649 } 650 } 651 return true; 652 } 653 654 // Replace G with a simple tail call to bitcast(F). Also (unless 655 // MergeFunctionsPDI holds) replace direct uses of G with bitcast(F), 656 // delete G. Under MergeFunctionsPDI, we use G itself for creating 657 // the thunk as we preserve the debug info (and associated instructions) 658 // from G's entry block pertaining to G's incoming arguments which are 659 // passed on as corresponding arguments in the call that G makes to F. 660 // For better debugability, under MergeFunctionsPDI, we do not modify G's 661 // call sites to point to F even when within the same translation unit. 662 void MergeFunctions::writeThunk(Function *F, Function *G) { 663 BasicBlock *GEntryBlock = nullptr; 664 std::vector<Instruction *> PDIUnrelatedWL; 665 BasicBlock *BB = nullptr; 666 Function *NewG = nullptr; 667 if (MergeFunctionsPDI) { 668 LLVM_DEBUG(dbgs() << "writeThunk: (MergeFunctionsPDI) Do not create a new " 669 "function as thunk; retain original: " 670 << G->getName() << "()\n"); 671 GEntryBlock = &G->getEntryBlock(); 672 LLVM_DEBUG( 673 dbgs() << "writeThunk: (MergeFunctionsPDI) filter parameter related " 674 "debug info for " 675 << G->getName() << "() {\n"); 676 filterInstsUnrelatedToPDI(GEntryBlock, PDIUnrelatedWL); 677 GEntryBlock->getTerminator()->eraseFromParent(); 678 BB = GEntryBlock; 679 } else { 680 NewG = Function::Create(G->getFunctionType(), G->getLinkage(), 681 G->getAddressSpace(), "", G->getParent()); 682 NewG->setComdat(G->getComdat()); 683 BB = BasicBlock::Create(F->getContext(), "", NewG); 684 } 685 686 IRBuilder<> Builder(BB); 687 Function *H = MergeFunctionsPDI ? G : NewG; 688 SmallVector<Value *, 16> Args; 689 unsigned i = 0; 690 FunctionType *FFTy = F->getFunctionType(); 691 for (Argument &AI : H->args()) { 692 Args.push_back(createCast(Builder, &AI, FFTy->getParamType(i))); 693 ++i; 694 } 695 696 CallInst *CI = Builder.CreateCall(F, Args); 697 ReturnInst *RI = nullptr; 698 CI->setTailCall(); 699 CI->setCallingConv(F->getCallingConv()); 700 CI->setAttributes(F->getAttributes()); 701 if (H->getReturnType()->isVoidTy()) { 702 RI = Builder.CreateRetVoid(); 703 } else { 704 RI = Builder.CreateRet(createCast(Builder, CI, H->getReturnType())); 705 } 706 707 if (MergeFunctionsPDI) { 708 DISubprogram *DIS = G->getSubprogram(); 709 if (DIS) { 710 DebugLoc CIDbgLoc = DebugLoc::get(DIS->getScopeLine(), 0, DIS); 711 DebugLoc RIDbgLoc = DebugLoc::get(DIS->getScopeLine(), 0, DIS); 712 CI->setDebugLoc(CIDbgLoc); 713 RI->setDebugLoc(RIDbgLoc); 714 } else { 715 LLVM_DEBUG( 716 dbgs() << "writeThunk: (MergeFunctionsPDI) No DISubprogram for " 717 << G->getName() << "()\n"); 718 } 719 eraseTail(G); 720 eraseInstsUnrelatedToPDI(PDIUnrelatedWL); 721 LLVM_DEBUG( 722 dbgs() << "} // End of parameter related debug info filtering for: " 723 << G->getName() << "()\n"); 724 } else { 725 NewG->copyAttributesFrom(G); 726 NewG->takeName(G); 727 removeUsers(G); 728 G->replaceAllUsesWith(NewG); 729 G->eraseFromParent(); 730 } 731 732 LLVM_DEBUG(dbgs() << "writeThunk: " << H->getName() << '\n'); 733 ++NumThunksWritten; 734 } 735 736 // Whether this function may be replaced by an alias 737 static bool canCreateAliasFor(Function *F) { 738 if (!MergeFunctionsAliases || !F->hasGlobalUnnamedAddr()) 739 return false; 740 741 // We should only see linkages supported by aliases here 742 assert(F->hasLocalLinkage() || F->hasExternalLinkage() 743 || F->hasWeakLinkage() || F->hasLinkOnceLinkage()); 744 return true; 745 } 746 747 // Replace G with an alias to F (deleting function G) 748 void MergeFunctions::writeAlias(Function *F, Function *G) { 749 Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType()); 750 PointerType *PtrType = G->getType(); 751 auto *GA = GlobalAlias::create( 752 PtrType->getElementType(), PtrType->getAddressSpace(), 753 G->getLinkage(), "", BitcastF, G->getParent()); 754 755 F->setAlignment(MaybeAlign(std::max(F->getAlignment(), G->getAlignment()))); 756 GA->takeName(G); 757 GA->setVisibility(G->getVisibility()); 758 GA->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); 759 760 removeUsers(G); 761 G->replaceAllUsesWith(GA); 762 G->eraseFromParent(); 763 764 LLVM_DEBUG(dbgs() << "writeAlias: " << GA->getName() << '\n'); 765 ++NumAliasesWritten; 766 } 767 768 // Replace G with an alias to F if possible, or a thunk to F if 769 // profitable. Returns false if neither is the case. 770 bool MergeFunctions::writeThunkOrAlias(Function *F, Function *G) { 771 if (canCreateAliasFor(G)) { 772 writeAlias(F, G); 773 return true; 774 } 775 if (canCreateThunkFor(F)) { 776 writeThunk(F, G); 777 return true; 778 } 779 return false; 780 } 781 782 // Merge two equivalent functions. Upon completion, Function G is deleted. 783 void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) { 784 if (F->isInterposable()) { 785 assert(G->isInterposable()); 786 787 // Both writeThunkOrAlias() calls below must succeed, either because we can 788 // create aliases for G and NewF, or because a thunk for F is profitable. 789 // F here has the same signature as NewF below, so that's what we check. 790 if (!canCreateThunkFor(F) && 791 (!canCreateAliasFor(F) || !canCreateAliasFor(G))) 792 return; 793 794 // Make them both thunks to the same internal function. 795 Function *NewF = Function::Create(F->getFunctionType(), F->getLinkage(), 796 F->getAddressSpace(), "", F->getParent()); 797 NewF->copyAttributesFrom(F); 798 NewF->takeName(F); 799 removeUsers(F); 800 F->replaceAllUsesWith(NewF); 801 802 MaybeAlign MaxAlignment(std::max(G->getAlignment(), NewF->getAlignment())); 803 804 writeThunkOrAlias(F, G); 805 writeThunkOrAlias(F, NewF); 806 807 F->setAlignment(MaxAlignment); 808 F->setLinkage(GlobalValue::PrivateLinkage); 809 ++NumDoubleWeak; 810 ++NumFunctionsMerged; 811 } else { 812 // For better debugability, under MergeFunctionsPDI, we do not modify G's 813 // call sites to point to F even when within the same translation unit. 814 if (!G->isInterposable() && !MergeFunctionsPDI) { 815 if (G->hasGlobalUnnamedAddr()) { 816 // G might have been a key in our GlobalNumberState, and it's illegal 817 // to replace a key in ValueMap<GlobalValue *> with a non-global. 818 GlobalNumbers.erase(G); 819 // If G's address is not significant, replace it entirely. 820 Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType()); 821 removeUsers(G); 822 G->replaceAllUsesWith(BitcastF); 823 } else { 824 // Redirect direct callers of G to F. (See note on MergeFunctionsPDI 825 // above). 826 replaceDirectCallers(G, F); 827 } 828 } 829 830 // If G was internal then we may have replaced all uses of G with F. If so, 831 // stop here and delete G. There's no need for a thunk. (See note on 832 // MergeFunctionsPDI above). 833 if (G->isDiscardableIfUnused() && G->use_empty() && !MergeFunctionsPDI) { 834 G->eraseFromParent(); 835 ++NumFunctionsMerged; 836 return; 837 } 838 839 if (writeThunkOrAlias(F, G)) { 840 ++NumFunctionsMerged; 841 } 842 } 843 } 844 845 /// Replace function F by function G. 846 void MergeFunctions::replaceFunctionInTree(const FunctionNode &FN, 847 Function *G) { 848 Function *F = FN.getFunc(); 849 assert(FunctionComparator(F, G, &GlobalNumbers).compare() == 0 && 850 "The two functions must be equal"); 851 852 auto I = FNodesInTree.find(F); 853 assert(I != FNodesInTree.end() && "F should be in FNodesInTree"); 854 assert(FNodesInTree.count(G) == 0 && "FNodesInTree should not contain G"); 855 856 FnTreeType::iterator IterToFNInFnTree = I->second; 857 assert(&(*IterToFNInFnTree) == &FN && "F should map to FN in FNodesInTree."); 858 // Remove F -> FN and insert G -> FN 859 FNodesInTree.erase(I); 860 FNodesInTree.insert({G, IterToFNInFnTree}); 861 // Replace F with G in FN, which is stored inside the FnTree. 862 FN.replaceBy(G); 863 } 864 865 // Ordering for functions that are equal under FunctionComparator 866 static bool isFuncOrderCorrect(const Function *F, const Function *G) { 867 if (F->isInterposable() != G->isInterposable()) { 868 // Strong before weak, because the weak function may call the strong 869 // one, but not the other way around. 870 return !F->isInterposable(); 871 } 872 if (F->hasLocalLinkage() != G->hasLocalLinkage()) { 873 // External before local, because we definitely have to keep the external 874 // function, but may be able to drop the local one. 875 return !F->hasLocalLinkage(); 876 } 877 // Impose a total order (by name) on the replacement of functions. This is 878 // important when operating on more than one module independently to prevent 879 // cycles of thunks calling each other when the modules are linked together. 880 return F->getName() <= G->getName(); 881 } 882 883 // Insert a ComparableFunction into the FnTree, or merge it away if equal to one 884 // that was already inserted. 885 bool MergeFunctions::insert(Function *NewFunction) { 886 std::pair<FnTreeType::iterator, bool> Result = 887 FnTree.insert(FunctionNode(NewFunction)); 888 889 if (Result.second) { 890 assert(FNodesInTree.count(NewFunction) == 0); 891 FNodesInTree.insert({NewFunction, Result.first}); 892 LLVM_DEBUG(dbgs() << "Inserting as unique: " << NewFunction->getName() 893 << '\n'); 894 return false; 895 } 896 897 const FunctionNode &OldF = *Result.first; 898 899 if (!isFuncOrderCorrect(OldF.getFunc(), NewFunction)) { 900 // Swap the two functions. 901 Function *F = OldF.getFunc(); 902 replaceFunctionInTree(*Result.first, NewFunction); 903 NewFunction = F; 904 assert(OldF.getFunc() != F && "Must have swapped the functions."); 905 } 906 907 LLVM_DEBUG(dbgs() << " " << OldF.getFunc()->getName() 908 << " == " << NewFunction->getName() << '\n'); 909 910 Function *DeleteF = NewFunction; 911 mergeTwoFunctions(OldF.getFunc(), DeleteF); 912 return true; 913 } 914 915 // Remove a function from FnTree. If it was already in FnTree, add 916 // it to Deferred so that we'll look at it in the next round. 917 void MergeFunctions::remove(Function *F) { 918 auto I = FNodesInTree.find(F); 919 if (I != FNodesInTree.end()) { 920 LLVM_DEBUG(dbgs() << "Deferred " << F->getName() << ".\n"); 921 FnTree.erase(I->second); 922 // I->second has been invalidated, remove it from the FNodesInTree map to 923 // preserve the invariant. 924 FNodesInTree.erase(I); 925 Deferred.emplace_back(F); 926 } 927 } 928 929 // For each instruction used by the value, remove() the function that contains 930 // the instruction. This should happen right before a call to RAUW. 931 void MergeFunctions::removeUsers(Value *V) { 932 for (User *U : V->users()) 933 if (auto *I = dyn_cast<Instruction>(U)) 934 remove(I->getFunction()); 935 } 936