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