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