1 //===- DeadArgumentElimination.cpp - Eliminate dead arguments -------------===// 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 deletes dead arguments from internal functions. Dead argument 10 // elimination removes arguments which are directly dead, as well as arguments 11 // only passed into function calls as dead arguments of other functions. This 12 // pass also deletes dead return values in a similar way. 13 // 14 // This pass is often useful as a cleanup pass to run after aggressive 15 // interprocedural passes, which add possibly-dead arguments or return values. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #include "llvm/Transforms/IPO/DeadArgumentElimination.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/Statistic.h" 22 #include "llvm/IR/Argument.h" 23 #include "llvm/IR/AttributeMask.h" 24 #include "llvm/IR/Attributes.h" 25 #include "llvm/IR/BasicBlock.h" 26 #include "llvm/IR/Constants.h" 27 #include "llvm/IR/DIBuilder.h" 28 #include "llvm/IR/DerivedTypes.h" 29 #include "llvm/IR/Function.h" 30 #include "llvm/IR/IRBuilder.h" 31 #include "llvm/IR/InstrTypes.h" 32 #include "llvm/IR/Instructions.h" 33 #include "llvm/IR/IntrinsicInst.h" 34 #include "llvm/IR/Intrinsics.h" 35 #include "llvm/IR/Module.h" 36 #include "llvm/IR/NoFolder.h" 37 #include "llvm/IR/PassManager.h" 38 #include "llvm/IR/Type.h" 39 #include "llvm/IR/Use.h" 40 #include "llvm/IR/User.h" 41 #include "llvm/IR/Value.h" 42 #include "llvm/InitializePasses.h" 43 #include "llvm/Pass.h" 44 #include "llvm/Support/Casting.h" 45 #include "llvm/Support/Debug.h" 46 #include "llvm/Support/raw_ostream.h" 47 #include "llvm/Transforms/IPO.h" 48 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 49 #include <cassert> 50 #include <utility> 51 #include <vector> 52 53 using namespace llvm; 54 55 #define DEBUG_TYPE "deadargelim" 56 57 STATISTIC(NumArgumentsEliminated, "Number of unread args removed"); 58 STATISTIC(NumRetValsEliminated, "Number of unused return values removed"); 59 STATISTIC(NumArgumentsReplacedWithPoison, 60 "Number of unread args replaced with poison"); 61 62 namespace { 63 64 /// The dead argument elimination pass. 65 class DAE : public ModulePass { 66 protected: 67 // DAH uses this to specify a different ID. 68 explicit DAE(char &ID) : ModulePass(ID) {} 69 70 public: 71 static char ID; // Pass identification, replacement for typeid 72 73 DAE() : ModulePass(ID) { 74 initializeDAEPass(*PassRegistry::getPassRegistry()); 75 } 76 77 bool runOnModule(Module &M) override { 78 if (skipModule(M)) 79 return false; 80 DeadArgumentEliminationPass DAEP(shouldHackArguments()); 81 ModuleAnalysisManager DummyMAM; 82 PreservedAnalyses PA = DAEP.run(M, DummyMAM); 83 return !PA.areAllPreserved(); 84 } 85 86 virtual bool shouldHackArguments() const { return false; } 87 }; 88 89 bool isMustTailCalleeAnalyzable(const CallBase &CB) { 90 assert(CB.isMustTailCall()); 91 return CB.getCalledFunction() && !CB.getCalledFunction()->isDeclaration(); 92 } 93 94 } // end anonymous namespace 95 96 char DAE::ID = 0; 97 98 INITIALIZE_PASS(DAE, "deadargelim", "Dead Argument Elimination", false, false) 99 100 namespace { 101 102 /// The DeadArgumentHacking pass, same as dead argument elimination, but deletes 103 /// arguments to functions which are external. This is only for use by bugpoint. 104 struct DAH : public DAE { 105 static char ID; 106 107 DAH() : DAE(ID) {} 108 109 bool shouldHackArguments() const override { return true; } 110 }; 111 112 } // end anonymous namespace 113 114 char DAH::ID = 0; 115 116 INITIALIZE_PASS(DAH, "deadarghaX0r", 117 "Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)", false, 118 false) 119 120 /// This pass removes arguments from functions which are not used by the body of 121 /// the function. 122 ModulePass *llvm::createDeadArgEliminationPass() { return new DAE(); } 123 124 ModulePass *llvm::createDeadArgHackingPass() { return new DAH(); } 125 126 /// If this is an function that takes a ... list, and if llvm.vastart is never 127 /// called, the varargs list is dead for the function. 128 bool DeadArgumentEliminationPass::deleteDeadVarargs(Function &F) { 129 assert(F.getFunctionType()->isVarArg() && "Function isn't varargs!"); 130 if (F.isDeclaration() || !F.hasLocalLinkage()) 131 return false; 132 133 // Ensure that the function is only directly called. 134 if (F.hasAddressTaken()) 135 return false; 136 137 // Don't touch naked functions. The assembly might be using an argument, or 138 // otherwise rely on the frame layout in a way that this analysis will not 139 // see. 140 if (F.hasFnAttribute(Attribute::Naked)) { 141 return false; 142 } 143 144 // Okay, we know we can transform this function if safe. Scan its body 145 // looking for calls marked musttail or calls to llvm.vastart. 146 for (BasicBlock &BB : F) { 147 for (Instruction &I : BB) { 148 CallInst *CI = dyn_cast<CallInst>(&I); 149 if (!CI) 150 continue; 151 if (CI->isMustTailCall()) 152 return false; 153 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) { 154 if (II->getIntrinsicID() == Intrinsic::vastart) 155 return false; 156 } 157 } 158 } 159 160 // If we get here, there are no calls to llvm.vastart in the function body, 161 // remove the "..." and adjust all the calls. 162 163 // Start by computing a new prototype for the function, which is the same as 164 // the old function, but doesn't have isVarArg set. 165 FunctionType *FTy = F.getFunctionType(); 166 167 std::vector<Type *> Params(FTy->param_begin(), FTy->param_end()); 168 FunctionType *NFTy = FunctionType::get(FTy->getReturnType(), Params, false); 169 unsigned NumArgs = Params.size(); 170 171 // Create the new function body and insert it into the module... 172 Function *NF = Function::Create(NFTy, F.getLinkage(), F.getAddressSpace()); 173 NF->copyAttributesFrom(&F); 174 NF->setComdat(F.getComdat()); 175 F.getParent()->getFunctionList().insert(F.getIterator(), NF); 176 NF->takeName(&F); 177 NF->IsNewDbgInfoFormat = F.IsNewDbgInfoFormat; 178 179 // Loop over all the callers of the function, transforming the call sites 180 // to pass in a smaller number of arguments into the new function. 181 // 182 std::vector<Value *> Args; 183 for (User *U : llvm::make_early_inc_range(F.users())) { 184 CallBase *CB = dyn_cast<CallBase>(U); 185 if (!CB) 186 continue; 187 188 // Pass all the same arguments. 189 Args.assign(CB->arg_begin(), CB->arg_begin() + NumArgs); 190 191 // Drop any attributes that were on the vararg arguments. 192 AttributeList PAL = CB->getAttributes(); 193 if (!PAL.isEmpty()) { 194 SmallVector<AttributeSet, 8> ArgAttrs; 195 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) 196 ArgAttrs.push_back(PAL.getParamAttrs(ArgNo)); 197 PAL = AttributeList::get(F.getContext(), PAL.getFnAttrs(), 198 PAL.getRetAttrs(), ArgAttrs); 199 } 200 201 SmallVector<OperandBundleDef, 1> OpBundles; 202 CB->getOperandBundlesAsDefs(OpBundles); 203 204 CallBase *NewCB = nullptr; 205 if (InvokeInst *II = dyn_cast<InvokeInst>(CB)) { 206 NewCB = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(), 207 Args, OpBundles, "", CB->getIterator()); 208 } else { 209 NewCB = CallInst::Create(NF, Args, OpBundles, "", CB->getIterator()); 210 cast<CallInst>(NewCB)->setTailCallKind( 211 cast<CallInst>(CB)->getTailCallKind()); 212 } 213 NewCB->setCallingConv(CB->getCallingConv()); 214 NewCB->setAttributes(PAL); 215 NewCB->copyMetadata(*CB, {LLVMContext::MD_prof, LLVMContext::MD_dbg}); 216 217 Args.clear(); 218 219 if (!CB->use_empty()) 220 CB->replaceAllUsesWith(NewCB); 221 222 NewCB->takeName(CB); 223 224 // Finally, remove the old call from the program, reducing the use-count of 225 // F. 226 CB->eraseFromParent(); 227 } 228 229 // Since we have now created the new function, splice the body of the old 230 // function right into the new function, leaving the old rotting hulk of the 231 // function empty. 232 NF->splice(NF->begin(), &F); 233 234 // Loop over the argument list, transferring uses of the old arguments over to 235 // the new arguments, also transferring over the names as well. While we're 236 // at it, remove the dead arguments from the DeadArguments list. 237 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(), 238 I2 = NF->arg_begin(); 239 I != E; ++I, ++I2) { 240 // Move the name and users over to the new version. 241 I->replaceAllUsesWith(&*I2); 242 I2->takeName(&*I); 243 } 244 245 // Clone metadata from the old function, including debug info descriptor. 246 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs; 247 F.getAllMetadata(MDs); 248 for (auto [KindID, Node] : MDs) 249 NF->addMetadata(KindID, *Node); 250 251 // Fix up any BlockAddresses that refer to the function. 252 F.replaceAllUsesWith(NF); 253 // Delete the bitcast that we just created, so that NF does not 254 // appear to be address-taken. 255 NF->removeDeadConstantUsers(); 256 // Finally, nuke the old function. 257 F.eraseFromParent(); 258 return true; 259 } 260 261 /// Checks if the given function has any arguments that are unused, and changes 262 /// the caller parameters to be poison instead. 263 bool DeadArgumentEliminationPass::removeDeadArgumentsFromCallers(Function &F) { 264 // We cannot change the arguments if this TU does not define the function or 265 // if the linker may choose a function body from another TU, even if the 266 // nominal linkage indicates that other copies of the function have the same 267 // semantics. In the below example, the dead load from %p may not have been 268 // eliminated from the linker-chosen copy of f, so replacing %p with poison 269 // in callers may introduce undefined behavior. 270 // 271 // define linkonce_odr void @f(i32* %p) { 272 // %v = load i32 %p 273 // ret void 274 // } 275 if (!F.hasExactDefinition()) 276 return false; 277 278 // Functions with local linkage should already have been handled, except if 279 // they are fully alive (e.g., called indirectly) and except for the fragile 280 // (variadic) ones. In these cases, we may still be able to improve their 281 // statically known call sites. 282 if ((F.hasLocalLinkage() && !LiveFunctions.count(&F)) && 283 !F.getFunctionType()->isVarArg()) 284 return false; 285 286 // Don't touch naked functions. The assembly might be using an argument, or 287 // otherwise rely on the frame layout in a way that this analysis will not 288 // see. 289 if (F.hasFnAttribute(Attribute::Naked)) 290 return false; 291 292 if (F.use_empty()) 293 return false; 294 295 SmallVector<unsigned, 8> UnusedArgs; 296 bool Changed = false; 297 298 AttributeMask UBImplyingAttributes = 299 AttributeFuncs::getUBImplyingAttributes(); 300 for (Argument &Arg : F.args()) { 301 if (!Arg.hasSwiftErrorAttr() && Arg.use_empty() && 302 !Arg.hasPassPointeeByValueCopyAttr()) { 303 if (Arg.isUsedByMetadata()) { 304 Arg.replaceAllUsesWith(PoisonValue::get(Arg.getType())); 305 Changed = true; 306 } 307 UnusedArgs.push_back(Arg.getArgNo()); 308 F.removeParamAttrs(Arg.getArgNo(), UBImplyingAttributes); 309 } 310 } 311 312 if (UnusedArgs.empty()) 313 return false; 314 315 for (Use &U : F.uses()) { 316 CallBase *CB = dyn_cast<CallBase>(U.getUser()); 317 if (!CB || !CB->isCallee(&U) || 318 CB->getFunctionType() != F.getFunctionType()) 319 continue; 320 321 // Now go through all unused args and replace them with poison. 322 for (unsigned ArgNo : UnusedArgs) { 323 Value *Arg = CB->getArgOperand(ArgNo); 324 CB->setArgOperand(ArgNo, PoisonValue::get(Arg->getType())); 325 CB->removeParamAttrs(ArgNo, UBImplyingAttributes); 326 327 ++NumArgumentsReplacedWithPoison; 328 Changed = true; 329 } 330 } 331 332 return Changed; 333 } 334 335 /// Convenience function that returns the number of return values. It returns 0 336 /// for void functions and 1 for functions not returning a struct. It returns 337 /// the number of struct elements for functions returning a struct. 338 static unsigned numRetVals(const Function *F) { 339 Type *RetTy = F->getReturnType(); 340 if (RetTy->isVoidTy()) 341 return 0; 342 if (StructType *STy = dyn_cast<StructType>(RetTy)) 343 return STy->getNumElements(); 344 if (ArrayType *ATy = dyn_cast<ArrayType>(RetTy)) 345 return ATy->getNumElements(); 346 return 1; 347 } 348 349 /// Returns the sub-type a function will return at a given Idx. Should 350 /// correspond to the result type of an ExtractValue instruction executed with 351 /// just that one Idx (i.e. only top-level structure is considered). 352 static Type *getRetComponentType(const Function *F, unsigned Idx) { 353 Type *RetTy = F->getReturnType(); 354 assert(!RetTy->isVoidTy() && "void type has no subtype"); 355 356 if (StructType *STy = dyn_cast<StructType>(RetTy)) 357 return STy->getElementType(Idx); 358 if (ArrayType *ATy = dyn_cast<ArrayType>(RetTy)) 359 return ATy->getElementType(); 360 return RetTy; 361 } 362 363 /// Checks Use for liveness in LiveValues. If Use is not live, it adds Use to 364 /// the MaybeLiveUses argument. Returns the determined liveness of Use. 365 DeadArgumentEliminationPass::Liveness 366 DeadArgumentEliminationPass::markIfNotLive(RetOrArg Use, 367 UseVector &MaybeLiveUses) { 368 // We're live if our use or its Function is already marked as live. 369 if (isLive(Use)) 370 return Live; 371 372 // We're maybe live otherwise, but remember that we must become live if 373 // Use becomes live. 374 MaybeLiveUses.push_back(Use); 375 return MaybeLive; 376 } 377 378 /// Looks at a single use of an argument or return value and determines if it 379 /// should be alive or not. Adds this use to MaybeLiveUses if it causes the 380 /// used value to become MaybeLive. 381 /// 382 /// RetValNum is the return value number to use when this use is used in a 383 /// return instruction. This is used in the recursion, you should always leave 384 /// it at 0. 385 DeadArgumentEliminationPass::Liveness 386 DeadArgumentEliminationPass::surveyUse(const Use *U, UseVector &MaybeLiveUses, 387 unsigned RetValNum) { 388 const User *V = U->getUser(); 389 if (const ReturnInst *RI = dyn_cast<ReturnInst>(V)) { 390 // The value is returned from a function. It's only live when the 391 // function's return value is live. We use RetValNum here, for the case 392 // that U is really a use of an insertvalue instruction that uses the 393 // original Use. 394 const Function *F = RI->getParent()->getParent(); 395 if (RetValNum != -1U) { 396 RetOrArg Use = createRet(F, RetValNum); 397 // We might be live, depending on the liveness of Use. 398 return markIfNotLive(Use, MaybeLiveUses); 399 } 400 401 DeadArgumentEliminationPass::Liveness Result = MaybeLive; 402 for (unsigned Ri = 0; Ri < numRetVals(F); ++Ri) { 403 RetOrArg Use = createRet(F, Ri); 404 // We might be live, depending on the liveness of Use. If any 405 // sub-value is live, then the entire value is considered live. This 406 // is a conservative choice, and better tracking is possible. 407 DeadArgumentEliminationPass::Liveness SubResult = 408 markIfNotLive(Use, MaybeLiveUses); 409 if (Result != Live) 410 Result = SubResult; 411 } 412 return Result; 413 } 414 415 if (const InsertValueInst *IV = dyn_cast<InsertValueInst>(V)) { 416 if (U->getOperandNo() != InsertValueInst::getAggregateOperandIndex() && 417 IV->hasIndices()) 418 // The use we are examining is inserted into an aggregate. Our liveness 419 // depends on all uses of that aggregate, but if it is used as a return 420 // value, only index at which we were inserted counts. 421 RetValNum = *IV->idx_begin(); 422 423 // Note that if we are used as the aggregate operand to the insertvalue, 424 // we don't change RetValNum, but do survey all our uses. 425 426 Liveness Result = MaybeLive; 427 for (const Use &UU : IV->uses()) { 428 Result = surveyUse(&UU, MaybeLiveUses, RetValNum); 429 if (Result == Live) 430 break; 431 } 432 return Result; 433 } 434 435 if (const auto *CB = dyn_cast<CallBase>(V)) { 436 const Function *F = CB->getCalledFunction(); 437 if (F) { 438 // Used in a direct call. 439 440 // The function argument is live if it is used as a bundle operand. 441 if (CB->isBundleOperand(U)) 442 return Live; 443 444 // Find the argument number. We know for sure that this use is an 445 // argument, since if it was the function argument this would be an 446 // indirect call and that we know can't be looking at a value of the 447 // label type (for the invoke instruction). 448 unsigned ArgNo = CB->getArgOperandNo(U); 449 450 if (ArgNo >= F->getFunctionType()->getNumParams()) 451 // The value is passed in through a vararg! Must be live. 452 return Live; 453 454 assert(CB->getArgOperand(ArgNo) == CB->getOperand(U->getOperandNo()) && 455 "Argument is not where we expected it"); 456 457 // Value passed to a normal call. It's only live when the corresponding 458 // argument to the called function turns out live. 459 RetOrArg Use = createArg(F, ArgNo); 460 return markIfNotLive(Use, MaybeLiveUses); 461 } 462 } 463 // Used in any other way? Value must be live. 464 return Live; 465 } 466 467 /// Looks at all the uses of the given value 468 /// Returns the Liveness deduced from the uses of this value. 469 /// 470 /// Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses. If 471 /// the result is Live, MaybeLiveUses might be modified but its content should 472 /// be ignored (since it might not be complete). 473 DeadArgumentEliminationPass::Liveness 474 DeadArgumentEliminationPass::surveyUses(const Value *V, 475 UseVector &MaybeLiveUses) { 476 // Assume it's dead (which will only hold if there are no uses at all..). 477 Liveness Result = MaybeLive; 478 // Check each use. 479 for (const Use &U : V->uses()) { 480 Result = surveyUse(&U, MaybeLiveUses); 481 if (Result == Live) 482 break; 483 } 484 return Result; 485 } 486 487 /// Performs the initial survey of the specified function, checking out whether 488 /// it uses any of its incoming arguments or whether any callers use the return 489 /// value. This fills in the LiveValues set and Uses map. 490 /// 491 /// We consider arguments of non-internal functions to be intrinsically alive as 492 /// well as arguments to functions which have their "address taken". 493 void DeadArgumentEliminationPass::surveyFunction(const Function &F) { 494 // Functions with inalloca/preallocated parameters are expecting args in a 495 // particular register and memory layout. 496 if (F.getAttributes().hasAttrSomewhere(Attribute::InAlloca) || 497 F.getAttributes().hasAttrSomewhere(Attribute::Preallocated)) { 498 markLive(F); 499 return; 500 } 501 502 // Don't touch naked functions. The assembly might be using an argument, or 503 // otherwise rely on the frame layout in a way that this analysis will not 504 // see. 505 if (F.hasFnAttribute(Attribute::Naked)) { 506 markLive(F); 507 return; 508 } 509 510 unsigned RetCount = numRetVals(&F); 511 512 // Assume all return values are dead 513 using RetVals = SmallVector<Liveness, 5>; 514 515 RetVals RetValLiveness(RetCount, MaybeLive); 516 517 using RetUses = SmallVector<UseVector, 5>; 518 519 // These vectors map each return value to the uses that make it MaybeLive, so 520 // we can add those to the Uses map if the return value really turns out to be 521 // MaybeLive. Initialized to a list of RetCount empty lists. 522 RetUses MaybeLiveRetUses(RetCount); 523 524 bool HasMustTailCalls = false; 525 for (const BasicBlock &BB : F) { 526 // If we have any returns of `musttail` results - the signature can't 527 // change 528 if (const auto *TC = BB.getTerminatingMustTailCall()) { 529 HasMustTailCalls = true; 530 // In addition, if the called function is not locally defined (or unknown, 531 // if this is an indirect call), we can't change the callsite and thus 532 // can't change this function's signature either. 533 if (!isMustTailCalleeAnalyzable(*TC)) { 534 markLive(F); 535 return; 536 } 537 } 538 } 539 540 if (HasMustTailCalls) { 541 LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - " << F.getName() 542 << " has musttail calls\n"); 543 } 544 545 if (!F.hasLocalLinkage() && (!ShouldHackArguments || F.isIntrinsic())) { 546 markLive(F); 547 return; 548 } 549 550 LLVM_DEBUG( 551 dbgs() << "DeadArgumentEliminationPass - Inspecting callers for fn: " 552 << F.getName() << "\n"); 553 // Keep track of the number of live retvals, so we can skip checks once all 554 // of them turn out to be live. 555 unsigned NumLiveRetVals = 0; 556 557 bool HasMustTailCallers = false; 558 559 // Loop all uses of the function. 560 for (const Use &U : F.uses()) { 561 // If the function is PASSED IN as an argument, its address has been 562 // taken. 563 const auto *CB = dyn_cast<CallBase>(U.getUser()); 564 if (!CB || !CB->isCallee(&U) || 565 CB->getFunctionType() != F.getFunctionType()) { 566 markLive(F); 567 return; 568 } 569 570 // The number of arguments for `musttail` call must match the number of 571 // arguments of the caller 572 if (CB->isMustTailCall()) 573 HasMustTailCallers = true; 574 575 // If we end up here, we are looking at a direct call to our function. 576 577 // Now, check how our return value(s) is/are used in this caller. Don't 578 // bother checking return values if all of them are live already. 579 if (NumLiveRetVals == RetCount) 580 continue; 581 582 // Check all uses of the return value. 583 for (const Use &UU : CB->uses()) { 584 if (ExtractValueInst *Ext = dyn_cast<ExtractValueInst>(UU.getUser())) { 585 // This use uses a part of our return value, survey the uses of 586 // that part and store the results for this index only. 587 unsigned Idx = *Ext->idx_begin(); 588 if (RetValLiveness[Idx] != Live) { 589 RetValLiveness[Idx] = surveyUses(Ext, MaybeLiveRetUses[Idx]); 590 if (RetValLiveness[Idx] == Live) 591 NumLiveRetVals++; 592 } 593 } else { 594 // Used by something else than extractvalue. Survey, but assume that the 595 // result applies to all sub-values. 596 UseVector MaybeLiveAggregateUses; 597 if (surveyUse(&UU, MaybeLiveAggregateUses) == Live) { 598 NumLiveRetVals = RetCount; 599 RetValLiveness.assign(RetCount, Live); 600 break; 601 } 602 603 for (unsigned Ri = 0; Ri != RetCount; ++Ri) { 604 if (RetValLiveness[Ri] != Live) 605 MaybeLiveRetUses[Ri].append(MaybeLiveAggregateUses.begin(), 606 MaybeLiveAggregateUses.end()); 607 } 608 } 609 } 610 } 611 612 if (HasMustTailCallers) { 613 LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - " << F.getName() 614 << " has musttail callers\n"); 615 } 616 617 // Now we've inspected all callers, record the liveness of our return values. 618 for (unsigned Ri = 0; Ri != RetCount; ++Ri) 619 markValue(createRet(&F, Ri), RetValLiveness[Ri], MaybeLiveRetUses[Ri]); 620 621 LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Inspecting args for fn: " 622 << F.getName() << "\n"); 623 624 // Now, check all of our arguments. 625 unsigned ArgI = 0; 626 UseVector MaybeLiveArgUses; 627 for (Function::const_arg_iterator AI = F.arg_begin(), E = F.arg_end(); 628 AI != E; ++AI, ++ArgI) { 629 Liveness Result; 630 if (F.getFunctionType()->isVarArg() || HasMustTailCallers || 631 HasMustTailCalls) { 632 // Variadic functions will already have a va_arg function expanded inside 633 // them, making them potentially very sensitive to ABI changes resulting 634 // from removing arguments entirely, so don't. For example AArch64 handles 635 // register and stack HFAs very differently, and this is reflected in the 636 // IR which has already been generated. 637 // 638 // `musttail` calls to this function restrict argument removal attempts. 639 // The signature of the caller must match the signature of the function. 640 // 641 // `musttail` calls in this function prevents us from changing its 642 // signature 643 Result = Live; 644 } else { 645 // See what the effect of this use is (recording any uses that cause 646 // MaybeLive in MaybeLiveArgUses). 647 Result = surveyUses(&*AI, MaybeLiveArgUses); 648 } 649 650 // Mark the result. 651 markValue(createArg(&F, ArgI), Result, MaybeLiveArgUses); 652 // Clear the vector again for the next iteration. 653 MaybeLiveArgUses.clear(); 654 } 655 } 656 657 /// Marks the liveness of RA depending on L. If L is MaybeLive, it also takes 658 /// all uses in MaybeLiveUses and records them in Uses, such that RA will be 659 /// marked live if any use in MaybeLiveUses gets marked live later on. 660 void DeadArgumentEliminationPass::markValue(const RetOrArg &RA, Liveness L, 661 const UseVector &MaybeLiveUses) { 662 switch (L) { 663 case Live: 664 markLive(RA); 665 break; 666 case MaybeLive: 667 assert(!isLive(RA) && "Use is already live!"); 668 for (const auto &MaybeLiveUse : MaybeLiveUses) { 669 if (isLive(MaybeLiveUse)) { 670 // A use is live, so this value is live. 671 markLive(RA); 672 break; 673 } 674 // Note any uses of this value, so this value can be 675 // marked live whenever one of the uses becomes live. 676 Uses.emplace(MaybeLiveUse, RA); 677 } 678 break; 679 } 680 } 681 682 /// Mark the given Function as alive, meaning that it cannot be changed in any 683 /// way. Additionally, mark any values that are used as this function's 684 /// parameters or by its return values (according to Uses) live as well. 685 void DeadArgumentEliminationPass::markLive(const Function &F) { 686 LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Intrinsically live fn: " 687 << F.getName() << "\n"); 688 // Mark the function as live. 689 LiveFunctions.insert(&F); 690 // Mark all arguments as live. 691 for (unsigned ArgI = 0, E = F.arg_size(); ArgI != E; ++ArgI) 692 propagateLiveness(createArg(&F, ArgI)); 693 // Mark all return values as live. 694 for (unsigned Ri = 0, E = numRetVals(&F); Ri != E; ++Ri) 695 propagateLiveness(createRet(&F, Ri)); 696 } 697 698 /// Mark the given return value or argument as live. Additionally, mark any 699 /// values that are used by this value (according to Uses) live as well. 700 void DeadArgumentEliminationPass::markLive(const RetOrArg &RA) { 701 if (isLive(RA)) 702 return; // Already marked Live. 703 704 LiveValues.insert(RA); 705 706 LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Marking " 707 << RA.getDescription() << " live\n"); 708 propagateLiveness(RA); 709 } 710 711 bool DeadArgumentEliminationPass::isLive(const RetOrArg &RA) { 712 return LiveFunctions.count(RA.F) || LiveValues.count(RA); 713 } 714 715 /// Given that RA is a live value, propagate it's liveness to any other values 716 /// it uses (according to Uses). 717 void DeadArgumentEliminationPass::propagateLiveness(const RetOrArg &RA) { 718 // We don't use upper_bound (or equal_range) here, because our recursive call 719 // to ourselves is likely to cause the upper_bound (which is the first value 720 // not belonging to RA) to become erased and the iterator invalidated. 721 UseMap::iterator Begin = Uses.lower_bound(RA); 722 UseMap::iterator E = Uses.end(); 723 UseMap::iterator I; 724 for (I = Begin; I != E && I->first == RA; ++I) 725 markLive(I->second); 726 727 // Erase RA from the Uses map (from the lower bound to wherever we ended up 728 // after the loop). 729 Uses.erase(Begin, I); 730 } 731 732 /// Remove any arguments and return values from F that are not in LiveValues. 733 /// Transform the function and all the callees of the function to not have these 734 /// arguments and return values. 735 bool DeadArgumentEliminationPass::removeDeadStuffFromFunction(Function *F) { 736 // Don't modify fully live functions 737 if (LiveFunctions.count(F)) 738 return false; 739 740 // Start by computing a new prototype for the function, which is the same as 741 // the old function, but has fewer arguments and a different return type. 742 FunctionType *FTy = F->getFunctionType(); 743 std::vector<Type *> Params; 744 745 // Keep track of if we have a live 'returned' argument 746 bool HasLiveReturnedArg = false; 747 748 // Set up to build a new list of parameter attributes. 749 SmallVector<AttributeSet, 8> ArgAttrVec; 750 const AttributeList &PAL = F->getAttributes(); 751 752 // Remember which arguments are still alive. 753 SmallVector<bool, 10> ArgAlive(FTy->getNumParams(), false); 754 // Construct the new parameter list from non-dead arguments. Also construct 755 // a new set of parameter attributes to correspond. Skip the first parameter 756 // attribute, since that belongs to the return value. 757 unsigned ArgI = 0; 758 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; 759 ++I, ++ArgI) { 760 RetOrArg Arg = createArg(F, ArgI); 761 if (LiveValues.erase(Arg)) { 762 Params.push_back(I->getType()); 763 ArgAlive[ArgI] = true; 764 ArgAttrVec.push_back(PAL.getParamAttrs(ArgI)); 765 HasLiveReturnedArg |= PAL.hasParamAttr(ArgI, Attribute::Returned); 766 } else { 767 ++NumArgumentsEliminated; 768 LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Removing argument " 769 << ArgI << " (" << I->getName() << ") from " 770 << F->getName() << "\n"); 771 } 772 } 773 774 // Find out the new return value. 775 Type *RetTy = FTy->getReturnType(); 776 Type *NRetTy = nullptr; 777 unsigned RetCount = numRetVals(F); 778 779 // -1 means unused, other numbers are the new index 780 SmallVector<int, 5> NewRetIdxs(RetCount, -1); 781 std::vector<Type *> RetTypes; 782 783 // If there is a function with a live 'returned' argument but a dead return 784 // value, then there are two possible actions: 785 // 1) Eliminate the return value and take off the 'returned' attribute on the 786 // argument. 787 // 2) Retain the 'returned' attribute and treat the return value (but not the 788 // entire function) as live so that it is not eliminated. 789 // 790 // It's not clear in the general case which option is more profitable because, 791 // even in the absence of explicit uses of the return value, code generation 792 // is free to use the 'returned' attribute to do things like eliding 793 // save/restores of registers across calls. Whether this happens is target and 794 // ABI-specific as well as depending on the amount of register pressure, so 795 // there's no good way for an IR-level pass to figure this out. 796 // 797 // Fortunately, the only places where 'returned' is currently generated by 798 // the FE are places where 'returned' is basically free and almost always a 799 // performance win, so the second option can just be used always for now. 800 // 801 // This should be revisited if 'returned' is ever applied more liberally. 802 if (RetTy->isVoidTy() || HasLiveReturnedArg) { 803 NRetTy = RetTy; 804 } else { 805 // Look at each of the original return values individually. 806 for (unsigned Ri = 0; Ri != RetCount; ++Ri) { 807 RetOrArg Ret = createRet(F, Ri); 808 if (LiveValues.erase(Ret)) { 809 RetTypes.push_back(getRetComponentType(F, Ri)); 810 NewRetIdxs[Ri] = RetTypes.size() - 1; 811 } else { 812 ++NumRetValsEliminated; 813 LLVM_DEBUG( 814 dbgs() << "DeadArgumentEliminationPass - Removing return value " 815 << Ri << " from " << F->getName() << "\n"); 816 } 817 } 818 if (RetTypes.size() > 1) { 819 // More than one return type? Reduce it down to size. 820 if (StructType *STy = dyn_cast<StructType>(RetTy)) { 821 // Make the new struct packed if we used to return a packed struct 822 // already. 823 NRetTy = StructType::get(STy->getContext(), RetTypes, STy->isPacked()); 824 } else { 825 assert(isa<ArrayType>(RetTy) && "unexpected multi-value return"); 826 NRetTy = ArrayType::get(RetTypes[0], RetTypes.size()); 827 } 828 } else if (RetTypes.size() == 1) 829 // One return type? Just a simple value then, but only if we didn't use to 830 // return a struct with that simple value before. 831 NRetTy = RetTypes.front(); 832 else if (RetTypes.empty()) 833 // No return types? Make it void, but only if we didn't use to return {}. 834 NRetTy = Type::getVoidTy(F->getContext()); 835 } 836 837 assert(NRetTy && "No new return type found?"); 838 839 // The existing function return attributes. 840 AttrBuilder RAttrs(F->getContext(), PAL.getRetAttrs()); 841 842 // Remove any incompatible attributes, but only if we removed all return 843 // values. Otherwise, ensure that we don't have any conflicting attributes 844 // here. Currently, this should not be possible, but special handling might be 845 // required when new return value attributes are added. 846 if (NRetTy->isVoidTy()) 847 RAttrs.remove(AttributeFuncs::typeIncompatible(NRetTy)); 848 else 849 assert(!RAttrs.overlaps(AttributeFuncs::typeIncompatible(NRetTy)) && 850 "Return attributes no longer compatible?"); 851 852 AttributeSet RetAttrs = AttributeSet::get(F->getContext(), RAttrs); 853 854 // Strip allocsize attributes. They might refer to the deleted arguments. 855 AttributeSet FnAttrs = 856 PAL.getFnAttrs().removeAttribute(F->getContext(), Attribute::AllocSize); 857 858 // Reconstruct the AttributesList based on the vector we constructed. 859 assert(ArgAttrVec.size() == Params.size()); 860 AttributeList NewPAL = 861 AttributeList::get(F->getContext(), FnAttrs, RetAttrs, ArgAttrVec); 862 863 // Create the new function type based on the recomputed parameters. 864 FunctionType *NFTy = FunctionType::get(NRetTy, Params, FTy->isVarArg()); 865 866 // No change? 867 if (NFTy == FTy) 868 return false; 869 870 // Create the new function body and insert it into the module... 871 Function *NF = Function::Create(NFTy, F->getLinkage(), F->getAddressSpace()); 872 NF->copyAttributesFrom(F); 873 NF->setComdat(F->getComdat()); 874 NF->setAttributes(NewPAL); 875 // Insert the new function before the old function, so we won't be processing 876 // it again. 877 F->getParent()->getFunctionList().insert(F->getIterator(), NF); 878 NF->takeName(F); 879 NF->IsNewDbgInfoFormat = F->IsNewDbgInfoFormat; 880 881 // Loop over all the callers of the function, transforming the call sites to 882 // pass in a smaller number of arguments into the new function. 883 std::vector<Value *> Args; 884 while (!F->use_empty()) { 885 CallBase &CB = cast<CallBase>(*F->user_back()); 886 887 ArgAttrVec.clear(); 888 const AttributeList &CallPAL = CB.getAttributes(); 889 890 // Adjust the call return attributes in case the function was changed to 891 // return void. 892 AttrBuilder RAttrs(F->getContext(), CallPAL.getRetAttrs()); 893 RAttrs.remove(AttributeFuncs::typeIncompatible(NRetTy)); 894 AttributeSet RetAttrs = AttributeSet::get(F->getContext(), RAttrs); 895 896 // Declare these outside of the loops, so we can reuse them for the second 897 // loop, which loops the varargs. 898 auto *I = CB.arg_begin(); 899 unsigned Pi = 0; 900 // Loop over those operands, corresponding to the normal arguments to the 901 // original function, and add those that are still alive. 902 for (unsigned E = FTy->getNumParams(); Pi != E; ++I, ++Pi) 903 if (ArgAlive[Pi]) { 904 Args.push_back(*I); 905 // Get original parameter attributes, but skip return attributes. 906 AttributeSet Attrs = CallPAL.getParamAttrs(Pi); 907 if (NRetTy != RetTy && Attrs.hasAttribute(Attribute::Returned)) { 908 // If the return type has changed, then get rid of 'returned' on the 909 // call site. The alternative is to make all 'returned' attributes on 910 // call sites keep the return value alive just like 'returned' 911 // attributes on function declaration, but it's less clearly a win and 912 // this is not an expected case anyway 913 ArgAttrVec.push_back(AttributeSet::get( 914 F->getContext(), AttrBuilder(F->getContext(), Attrs) 915 .removeAttribute(Attribute::Returned))); 916 } else { 917 // Otherwise, use the original attributes. 918 ArgAttrVec.push_back(Attrs); 919 } 920 } 921 922 // Push any varargs arguments on the list. Don't forget their attributes. 923 for (auto *E = CB.arg_end(); I != E; ++I, ++Pi) { 924 Args.push_back(*I); 925 ArgAttrVec.push_back(CallPAL.getParamAttrs(Pi)); 926 } 927 928 // Reconstruct the AttributesList based on the vector we constructed. 929 assert(ArgAttrVec.size() == Args.size()); 930 931 // Again, be sure to remove any allocsize attributes, since their indices 932 // may now be incorrect. 933 AttributeSet FnAttrs = CallPAL.getFnAttrs().removeAttribute( 934 F->getContext(), Attribute::AllocSize); 935 936 AttributeList NewCallPAL = 937 AttributeList::get(F->getContext(), FnAttrs, RetAttrs, ArgAttrVec); 938 939 SmallVector<OperandBundleDef, 1> OpBundles; 940 CB.getOperandBundlesAsDefs(OpBundles); 941 942 CallBase *NewCB = nullptr; 943 if (InvokeInst *II = dyn_cast<InvokeInst>(&CB)) { 944 NewCB = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(), 945 Args, OpBundles, "", CB.getParent()); 946 } else { 947 NewCB = CallInst::Create(NFTy, NF, Args, OpBundles, "", CB.getIterator()); 948 cast<CallInst>(NewCB)->setTailCallKind( 949 cast<CallInst>(&CB)->getTailCallKind()); 950 } 951 NewCB->setCallingConv(CB.getCallingConv()); 952 NewCB->setAttributes(NewCallPAL); 953 NewCB->copyMetadata(CB, {LLVMContext::MD_prof, LLVMContext::MD_dbg}); 954 Args.clear(); 955 ArgAttrVec.clear(); 956 957 if (!CB.use_empty() || CB.isUsedByMetadata()) { 958 if (NewCB->getType() == CB.getType()) { 959 // Return type not changed? Just replace users then. 960 CB.replaceAllUsesWith(NewCB); 961 NewCB->takeName(&CB); 962 } else if (NewCB->getType()->isVoidTy()) { 963 // If the return value is dead, replace any uses of it with poison 964 // (any non-debug value uses will get removed later on). 965 if (!CB.getType()->isX86_MMXTy()) 966 CB.replaceAllUsesWith(PoisonValue::get(CB.getType())); 967 } else { 968 assert((RetTy->isStructTy() || RetTy->isArrayTy()) && 969 "Return type changed, but not into a void. The old return type" 970 " must have been a struct or an array!"); 971 Instruction *InsertPt = &CB; 972 if (InvokeInst *II = dyn_cast<InvokeInst>(&CB)) { 973 BasicBlock *NewEdge = 974 SplitEdge(NewCB->getParent(), II->getNormalDest()); 975 InsertPt = &*NewEdge->getFirstInsertionPt(); 976 } 977 978 // We used to return a struct or array. Instead of doing smart stuff 979 // with all the uses, we will just rebuild it using extract/insertvalue 980 // chaining and let instcombine clean that up. 981 // 982 // Start out building up our return value from poison 983 Value *RetVal = PoisonValue::get(RetTy); 984 for (unsigned Ri = 0; Ri != RetCount; ++Ri) 985 if (NewRetIdxs[Ri] != -1) { 986 Value *V; 987 IRBuilder<NoFolder> IRB(InsertPt); 988 if (RetTypes.size() > 1) 989 // We are still returning a struct, so extract the value from our 990 // return value 991 V = IRB.CreateExtractValue(NewCB, NewRetIdxs[Ri], "newret"); 992 else 993 // We are now returning a single element, so just insert that 994 V = NewCB; 995 // Insert the value at the old position 996 RetVal = IRB.CreateInsertValue(RetVal, V, Ri, "oldret"); 997 } 998 // Now, replace all uses of the old call instruction with the return 999 // struct we built 1000 CB.replaceAllUsesWith(RetVal); 1001 NewCB->takeName(&CB); 1002 } 1003 } 1004 1005 // Finally, remove the old call from the program, reducing the use-count of 1006 // F. 1007 CB.eraseFromParent(); 1008 } 1009 1010 // Since we have now created the new function, splice the body of the old 1011 // function right into the new function, leaving the old rotting hulk of the 1012 // function empty. 1013 NF->splice(NF->begin(), F); 1014 1015 // Loop over the argument list, transferring uses of the old arguments over to 1016 // the new arguments, also transferring over the names as well. 1017 ArgI = 0; 1018 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(), 1019 I2 = NF->arg_begin(); 1020 I != E; ++I, ++ArgI) 1021 if (ArgAlive[ArgI]) { 1022 // If this is a live argument, move the name and users over to the new 1023 // version. 1024 I->replaceAllUsesWith(&*I2); 1025 I2->takeName(&*I); 1026 ++I2; 1027 } else { 1028 // If this argument is dead, replace any uses of it with poison 1029 // (any non-debug value uses will get removed later on). 1030 if (!I->getType()->isX86_MMXTy()) 1031 I->replaceAllUsesWith(PoisonValue::get(I->getType())); 1032 } 1033 1034 // If we change the return value of the function we must rewrite any return 1035 // instructions. Check this now. 1036 if (F->getReturnType() != NF->getReturnType()) 1037 for (BasicBlock &BB : *NF) 1038 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB.getTerminator())) { 1039 IRBuilder<NoFolder> IRB(RI); 1040 Value *RetVal = nullptr; 1041 1042 if (!NFTy->getReturnType()->isVoidTy()) { 1043 assert(RetTy->isStructTy() || RetTy->isArrayTy()); 1044 // The original return value was a struct or array, insert 1045 // extractvalue/insertvalue chains to extract only the values we need 1046 // to return and insert them into our new result. 1047 // This does generate messy code, but we'll let it to instcombine to 1048 // clean that up. 1049 Value *OldRet = RI->getOperand(0); 1050 // Start out building up our return value from poison 1051 RetVal = PoisonValue::get(NRetTy); 1052 for (unsigned RetI = 0; RetI != RetCount; ++RetI) 1053 if (NewRetIdxs[RetI] != -1) { 1054 Value *EV = IRB.CreateExtractValue(OldRet, RetI, "oldret"); 1055 1056 if (RetTypes.size() > 1) { 1057 // We're still returning a struct, so reinsert the value into 1058 // our new return value at the new index 1059 1060 RetVal = IRB.CreateInsertValue(RetVal, EV, NewRetIdxs[RetI], 1061 "newret"); 1062 } else { 1063 // We are now only returning a simple value, so just return the 1064 // extracted value. 1065 RetVal = EV; 1066 } 1067 } 1068 } 1069 // Replace the return instruction with one returning the new return 1070 // value (possibly 0 if we became void). 1071 auto *NewRet = 1072 ReturnInst::Create(F->getContext(), RetVal, RI->getIterator()); 1073 NewRet->setDebugLoc(RI->getDebugLoc()); 1074 RI->eraseFromParent(); 1075 } 1076 1077 // Clone metadata from the old function, including debug info descriptor. 1078 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs; 1079 F->getAllMetadata(MDs); 1080 for (auto [KindID, Node] : MDs) 1081 NF->addMetadata(KindID, *Node); 1082 1083 // If either the return value(s) or argument(s) are removed, then probably the 1084 // function does not follow standard calling conventions anymore. Hence, add 1085 // DW_CC_nocall to DISubroutineType to inform debugger that it may not be safe 1086 // to call this function or try to interpret the return value. 1087 if (NFTy != FTy && NF->getSubprogram()) { 1088 DISubprogram *SP = NF->getSubprogram(); 1089 auto Temp = SP->getType()->cloneWithCC(llvm::dwarf::DW_CC_nocall); 1090 SP->replaceType(MDNode::replaceWithPermanent(std::move(Temp))); 1091 } 1092 1093 // Now that the old function is dead, delete it. 1094 F->eraseFromParent(); 1095 1096 return true; 1097 } 1098 1099 void DeadArgumentEliminationPass::propagateVirtMustcallLiveness( 1100 const Module &M) { 1101 // If a function was marked "live", and it has musttail callers, they in turn 1102 // can't change either. 1103 LiveFuncSet NewLiveFuncs(LiveFunctions); 1104 while (!NewLiveFuncs.empty()) { 1105 LiveFuncSet Temp; 1106 for (const auto *F : NewLiveFuncs) 1107 for (const auto *U : F->users()) 1108 if (const auto *CB = dyn_cast<CallBase>(U)) 1109 if (CB->isMustTailCall()) 1110 if (!LiveFunctions.count(CB->getParent()->getParent())) 1111 Temp.insert(CB->getParent()->getParent()); 1112 NewLiveFuncs.clear(); 1113 NewLiveFuncs.insert(Temp.begin(), Temp.end()); 1114 for (const auto *F : Temp) 1115 markLive(*F); 1116 } 1117 } 1118 1119 PreservedAnalyses DeadArgumentEliminationPass::run(Module &M, 1120 ModuleAnalysisManager &) { 1121 bool Changed = false; 1122 1123 // First pass: Do a simple check to see if any functions can have their "..." 1124 // removed. We can do this if they never call va_start. This loop cannot be 1125 // fused with the next loop, because deleting a function invalidates 1126 // information computed while surveying other functions. 1127 LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Deleting dead varargs\n"); 1128 for (Function &F : llvm::make_early_inc_range(M)) 1129 if (F.getFunctionType()->isVarArg()) 1130 Changed |= deleteDeadVarargs(F); 1131 1132 // Second phase: Loop through the module, determining which arguments are 1133 // live. We assume all arguments are dead unless proven otherwise (allowing us 1134 // to determine that dead arguments passed into recursive functions are dead). 1135 LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Determining liveness\n"); 1136 for (auto &F : M) 1137 surveyFunction(F); 1138 1139 propagateVirtMustcallLiveness(M); 1140 1141 // Now, remove all dead arguments and return values from each function in 1142 // turn. We use make_early_inc_range here because functions will probably get 1143 // removed (i.e. replaced by new ones). 1144 for (Function &F : llvm::make_early_inc_range(M)) 1145 Changed |= removeDeadStuffFromFunction(&F); 1146 1147 // Finally, look for any unused parameters in functions with non-local 1148 // linkage and replace the passed in parameters with poison. 1149 for (auto &F : M) 1150 Changed |= removeDeadArgumentsFromCallers(F); 1151 1152 if (!Changed) 1153 return PreservedAnalyses::all(); 1154 return PreservedAnalyses::none(); 1155 } 1156