1 //===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements sparse conditional constant propagation and merging:
10 //
11 // Specifically, this:
12 //   * Assumes values are constant unless proven otherwise
13 //   * Assumes BasicBlocks are dead unless proven otherwise
14 //   * Proves values to be constant, and replaces them with constants
15 //   * Proves conditional branches to be unconditional
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #include "llvm/Transforms/Scalar/SCCP.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/MapVector.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SetVector.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/Analysis/DomTreeUpdater.h"
28 #include "llvm/Analysis/GlobalsModRef.h"
29 #include "llvm/Analysis/TargetLibraryInfo.h"
30 #include "llvm/Analysis/ValueLattice.h"
31 #include "llvm/Analysis/ValueLatticeUtils.h"
32 #include "llvm/Analysis/ValueTracking.h"
33 #include "llvm/IR/BasicBlock.h"
34 #include "llvm/IR/Constant.h"
35 #include "llvm/IR/Constants.h"
36 #include "llvm/IR/DerivedTypes.h"
37 #include "llvm/IR/Function.h"
38 #include "llvm/IR/GlobalVariable.h"
39 #include "llvm/IR/InstrTypes.h"
40 #include "llvm/IR/Instruction.h"
41 #include "llvm/IR/Instructions.h"
42 #include "llvm/IR/IntrinsicInst.h"
43 #include "llvm/IR/Module.h"
44 #include "llvm/IR/PassManager.h"
45 #include "llvm/IR/Type.h"
46 #include "llvm/IR/User.h"
47 #include "llvm/IR/Value.h"
48 #include "llvm/InitializePasses.h"
49 #include "llvm/Pass.h"
50 #include "llvm/Support/Casting.h"
51 #include "llvm/Support/Debug.h"
52 #include "llvm/Support/ErrorHandling.h"
53 #include "llvm/Support/raw_ostream.h"
54 #include "llvm/Transforms/Scalar.h"
55 #include "llvm/Transforms/Utils/Local.h"
56 #include "llvm/Transforms/Utils/SCCPSolver.h"
57 #include <cassert>
58 #include <utility>
59 #include <vector>
60 
61 using namespace llvm;
62 
63 #define DEBUG_TYPE "sccp"
64 
65 STATISTIC(NumInstRemoved, "Number of instructions removed");
66 STATISTIC(NumDeadBlocks , "Number of basic blocks unreachable");
67 STATISTIC(NumInstReplaced,
68           "Number of instructions replaced with (simpler) instruction");
69 
70 STATISTIC(IPNumInstRemoved, "Number of instructions removed by IPSCCP");
71 STATISTIC(IPNumArgsElimed ,"Number of arguments constant propagated by IPSCCP");
72 STATISTIC(IPNumGlobalConst, "Number of globals found to be constant by IPSCCP");
73 STATISTIC(
74     IPNumInstReplaced,
75     "Number of instructions replaced with (simpler) instruction by IPSCCP");
76 
77 // Helper to check if \p LV is either a constant or a constant
78 // range with a single element. This should cover exactly the same cases as the
79 // old ValueLatticeElement::isConstant() and is intended to be used in the
80 // transition to ValueLatticeElement.
81 static bool isConstant(const ValueLatticeElement &LV) {
82   return LV.isConstant() ||
83          (LV.isConstantRange() && LV.getConstantRange().isSingleElement());
84 }
85 
86 // Helper to check if \p LV is either overdefined or a constant range with more
87 // than a single element. This should cover exactly the same cases as the old
88 // ValueLatticeElement::isOverdefined() and is intended to be used in the
89 // transition to ValueLatticeElement.
90 static bool isOverdefined(const ValueLatticeElement &LV) {
91   return !LV.isUnknownOrUndef() && !isConstant(LV);
92 }
93 
94 static bool canRemoveInstruction(Instruction *I) {
95   if (wouldInstructionBeTriviallyDead(I))
96     return true;
97 
98   // Some instructions can be handled but are rejected above. Catch
99   // those cases by falling through to here.
100   // TODO: Mark globals as being constant earlier, so
101   // TODO: wouldInstructionBeTriviallyDead() knows that atomic loads
102   // TODO: are safe to remove.
103   return isa<LoadInst>(I);
104 }
105 
106 static bool tryToReplaceWithConstant(SCCPSolver &Solver, Value *V) {
107   Constant *Const = nullptr;
108   if (V->getType()->isStructTy()) {
109     std::vector<ValueLatticeElement> IVs = Solver.getStructLatticeValueFor(V);
110     if (llvm::any_of(IVs, isOverdefined))
111       return false;
112     std::vector<Constant *> ConstVals;
113     auto *ST = cast<StructType>(V->getType());
114     for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
115       ValueLatticeElement V = IVs[i];
116       ConstVals.push_back(isConstant(V)
117                               ? Solver.getConstant(V)
118                               : UndefValue::get(ST->getElementType(i)));
119     }
120     Const = ConstantStruct::get(ST, ConstVals);
121   } else {
122     const ValueLatticeElement &IV = Solver.getLatticeValueFor(V);
123     if (isOverdefined(IV))
124       return false;
125 
126     Const =
127         isConstant(IV) ? Solver.getConstant(IV) : UndefValue::get(V->getType());
128   }
129   assert(Const && "Constant is nullptr here!");
130 
131   // Replacing `musttail` instructions with constant breaks `musttail` invariant
132   // unless the call itself can be removed.
133   // Calls with "clang.arc.attachedcall" implicitly use the return value and
134   // those uses cannot be updated with a constant.
135   CallBase *CB = dyn_cast<CallBase>(V);
136   if (CB && ((CB->isMustTailCall() &&
137               !canRemoveInstruction(CB)) ||
138              CB->getOperandBundle(LLVMContext::OB_clang_arc_attachedcall))) {
139     Function *F = CB->getCalledFunction();
140 
141     // Don't zap returns of the callee
142     if (F)
143       Solver.addToMustPreserveReturnsInFunctions(F);
144 
145     LLVM_DEBUG(dbgs() << "  Can\'t treat the result of call " << *CB
146                       << " as a constant\n");
147     return false;
148   }
149 
150   LLVM_DEBUG(dbgs() << "  Constant: " << *Const << " = " << *V << '\n');
151 
152   // Replaces all of the uses of a variable with uses of the constant.
153   V->replaceAllUsesWith(Const);
154   return true;
155 }
156 
157 static bool simplifyInstsInBlock(SCCPSolver &Solver, BasicBlock &BB,
158                                  SmallPtrSetImpl<Value *> &InsertedValues,
159                                  Statistic &InstRemovedStat,
160                                  Statistic &InstReplacedStat) {
161   bool MadeChanges = false;
162   for (Instruction &Inst : make_early_inc_range(BB)) {
163     if (Inst.getType()->isVoidTy())
164       continue;
165     if (tryToReplaceWithConstant(Solver, &Inst)) {
166       if (canRemoveInstruction(&Inst))
167         Inst.eraseFromParent();
168 
169       MadeChanges = true;
170       ++InstRemovedStat;
171     } else if (isa<SExtInst>(&Inst)) {
172       Value *ExtOp = Inst.getOperand(0);
173       if (isa<Constant>(ExtOp) || InsertedValues.count(ExtOp))
174         continue;
175       const ValueLatticeElement &IV = Solver.getLatticeValueFor(ExtOp);
176       if (!IV.isConstantRange(/*UndefAllowed=*/false))
177         continue;
178       if (IV.getConstantRange().isAllNonNegative()) {
179         auto *ZExt = new ZExtInst(ExtOp, Inst.getType(), "", &Inst);
180         ZExt->takeName(&Inst);
181         InsertedValues.insert(ZExt);
182         Inst.replaceAllUsesWith(ZExt);
183         Solver.removeLatticeValueFor(&Inst);
184         Inst.eraseFromParent();
185         InstReplacedStat++;
186         MadeChanges = true;
187       }
188     }
189   }
190   return MadeChanges;
191 }
192 
193 // runSCCP() - Run the Sparse Conditional Constant Propagation algorithm,
194 // and return true if the function was modified.
195 static bool runSCCP(Function &F, const DataLayout &DL,
196                     const TargetLibraryInfo *TLI) {
197   LLVM_DEBUG(dbgs() << "SCCP on function '" << F.getName() << "'\n");
198   SCCPSolver Solver(
199       DL, [TLI](Function &F) -> const TargetLibraryInfo & { return *TLI; },
200       F.getContext());
201 
202   // Mark the first block of the function as being executable.
203   Solver.markBlockExecutable(&F.front());
204 
205   // Mark all arguments to the function as being overdefined.
206   for (Argument &AI : F.args())
207     Solver.markOverdefined(&AI);
208 
209   // Solve for constants.
210   bool ResolvedUndefs = true;
211   while (ResolvedUndefs) {
212     Solver.solve();
213     LLVM_DEBUG(dbgs() << "RESOLVING UNDEFs\n");
214     ResolvedUndefs = Solver.resolvedUndefsIn(F);
215   }
216 
217   bool MadeChanges = false;
218 
219   // If we decided that there are basic blocks that are dead in this function,
220   // delete their contents now.  Note that we cannot actually delete the blocks,
221   // as we cannot modify the CFG of the function.
222 
223   SmallPtrSet<Value *, 32> InsertedValues;
224   for (BasicBlock &BB : F) {
225     if (!Solver.isBlockExecutable(&BB)) {
226       LLVM_DEBUG(dbgs() << "  BasicBlock Dead:" << BB);
227 
228       ++NumDeadBlocks;
229       NumInstRemoved += removeAllNonTerminatorAndEHPadInstructions(&BB).first;
230 
231       MadeChanges = true;
232       continue;
233     }
234 
235     MadeChanges |= simplifyInstsInBlock(Solver, BB, InsertedValues,
236                                         NumInstRemoved, NumInstReplaced);
237   }
238 
239   return MadeChanges;
240 }
241 
242 PreservedAnalyses SCCPPass::run(Function &F, FunctionAnalysisManager &AM) {
243   const DataLayout &DL = F.getParent()->getDataLayout();
244   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
245   if (!runSCCP(F, DL, &TLI))
246     return PreservedAnalyses::all();
247 
248   auto PA = PreservedAnalyses();
249   PA.preserveSet<CFGAnalyses>();
250   return PA;
251 }
252 
253 namespace {
254 
255 //===--------------------------------------------------------------------===//
256 //
257 /// SCCP Class - This class uses the SCCPSolver to implement a per-function
258 /// Sparse Conditional Constant Propagator.
259 ///
260 class SCCPLegacyPass : public FunctionPass {
261 public:
262   // Pass identification, replacement for typeid
263   static char ID;
264 
265   SCCPLegacyPass() : FunctionPass(ID) {
266     initializeSCCPLegacyPassPass(*PassRegistry::getPassRegistry());
267   }
268 
269   void getAnalysisUsage(AnalysisUsage &AU) const override {
270     AU.addRequired<TargetLibraryInfoWrapperPass>();
271     AU.addPreserved<GlobalsAAWrapperPass>();
272     AU.setPreservesCFG();
273   }
274 
275   // runOnFunction - Run the Sparse Conditional Constant Propagation
276   // algorithm, and return true if the function was modified.
277   bool runOnFunction(Function &F) override {
278     if (skipFunction(F))
279       return false;
280     const DataLayout &DL = F.getParent()->getDataLayout();
281     const TargetLibraryInfo *TLI =
282         &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
283     return runSCCP(F, DL, TLI);
284   }
285 };
286 
287 } // end anonymous namespace
288 
289 char SCCPLegacyPass::ID = 0;
290 
291 INITIALIZE_PASS_BEGIN(SCCPLegacyPass, "sccp",
292                       "Sparse Conditional Constant Propagation", false, false)
293 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
294 INITIALIZE_PASS_END(SCCPLegacyPass, "sccp",
295                     "Sparse Conditional Constant Propagation", false, false)
296 
297 // createSCCPPass - This is the public interface to this file.
298 FunctionPass *llvm::createSCCPPass() { return new SCCPLegacyPass(); }
299 
300 static void findReturnsToZap(Function &F,
301                              SmallVector<ReturnInst *, 8> &ReturnsToZap,
302                              SCCPSolver &Solver) {
303   // We can only do this if we know that nothing else can call the function.
304   if (!Solver.isArgumentTrackedFunction(&F))
305     return;
306 
307   if (Solver.mustPreserveReturn(&F)) {
308     LLVM_DEBUG(
309         dbgs()
310         << "Can't zap returns of the function : " << F.getName()
311         << " due to present musttail or \"clang.arc.attachedcall\" call of "
312            "it\n");
313     return;
314   }
315 
316   assert(
317       all_of(F.users(),
318              [&Solver](User *U) {
319                if (isa<Instruction>(U) &&
320                    !Solver.isBlockExecutable(cast<Instruction>(U)->getParent()))
321                  return true;
322                // Non-callsite uses are not impacted by zapping. Also, constant
323                // uses (like blockaddresses) could stuck around, without being
324                // used in the underlying IR, meaning we do not have lattice
325                // values for them.
326                if (!isa<CallBase>(U))
327                  return true;
328                if (U->getType()->isStructTy()) {
329                  return all_of(Solver.getStructLatticeValueFor(U),
330                                [](const ValueLatticeElement &LV) {
331                                  return !isOverdefined(LV);
332                                });
333                }
334                return !isOverdefined(Solver.getLatticeValueFor(U));
335              }) &&
336       "We can only zap functions where all live users have a concrete value");
337 
338   for (BasicBlock &BB : F) {
339     if (CallInst *CI = BB.getTerminatingMustTailCall()) {
340       LLVM_DEBUG(dbgs() << "Can't zap return of the block due to present "
341                         << "musttail call : " << *CI << "\n");
342       (void)CI;
343       return;
344     }
345 
346     if (auto *RI = dyn_cast<ReturnInst>(BB.getTerminator()))
347       if (!isa<UndefValue>(RI->getOperand(0)))
348         ReturnsToZap.push_back(RI);
349   }
350 }
351 
352 static bool removeNonFeasibleEdges(const SCCPSolver &Solver, BasicBlock *BB,
353                                    DomTreeUpdater &DTU,
354                                    BasicBlock *&NewUnreachableBB) {
355   SmallPtrSet<BasicBlock *, 8> FeasibleSuccessors;
356   bool HasNonFeasibleEdges = false;
357   for (BasicBlock *Succ : successors(BB)) {
358     if (Solver.isEdgeFeasible(BB, Succ))
359       FeasibleSuccessors.insert(Succ);
360     else
361       HasNonFeasibleEdges = true;
362   }
363 
364   // All edges feasible, nothing to do.
365   if (!HasNonFeasibleEdges)
366     return false;
367 
368   // SCCP can only determine non-feasible edges for br, switch and indirectbr.
369   Instruction *TI = BB->getTerminator();
370   assert((isa<BranchInst>(TI) || isa<SwitchInst>(TI) ||
371           isa<IndirectBrInst>(TI)) &&
372          "Terminator must be a br, switch or indirectbr");
373 
374   if (FeasibleSuccessors.size() == 1) {
375     // Replace with an unconditional branch to the only feasible successor.
376     BasicBlock *OnlyFeasibleSuccessor = *FeasibleSuccessors.begin();
377     SmallVector<DominatorTree::UpdateType, 8> Updates;
378     bool HaveSeenOnlyFeasibleSuccessor = false;
379     for (BasicBlock *Succ : successors(BB)) {
380       if (Succ == OnlyFeasibleSuccessor && !HaveSeenOnlyFeasibleSuccessor) {
381         // Don't remove the edge to the only feasible successor the first time
382         // we see it. We still do need to remove any multi-edges to it though.
383         HaveSeenOnlyFeasibleSuccessor = true;
384         continue;
385       }
386 
387       Succ->removePredecessor(BB);
388       Updates.push_back({DominatorTree::Delete, BB, Succ});
389     }
390 
391     BranchInst::Create(OnlyFeasibleSuccessor, BB);
392     TI->eraseFromParent();
393     DTU.applyUpdatesPermissive(Updates);
394   } else if (FeasibleSuccessors.size() > 1) {
395     SwitchInstProfUpdateWrapper SI(*cast<SwitchInst>(TI));
396     SmallVector<DominatorTree::UpdateType, 8> Updates;
397 
398     // If the default destination is unfeasible it will never be taken. Replace
399     // it with a new block with a single Unreachable instruction.
400     BasicBlock *DefaultDest = SI->getDefaultDest();
401     if (!FeasibleSuccessors.contains(DefaultDest)) {
402       if (!NewUnreachableBB) {
403         NewUnreachableBB =
404             BasicBlock::Create(DefaultDest->getContext(), "default.unreachable",
405                                DefaultDest->getParent(), DefaultDest);
406         new UnreachableInst(DefaultDest->getContext(), NewUnreachableBB);
407       }
408 
409       SI->setDefaultDest(NewUnreachableBB);
410       Updates.push_back({DominatorTree::Delete, BB, DefaultDest});
411       Updates.push_back({DominatorTree::Insert, BB, NewUnreachableBB});
412     }
413 
414     for (auto CI = SI->case_begin(); CI != SI->case_end();) {
415       if (FeasibleSuccessors.contains(CI->getCaseSuccessor())) {
416         ++CI;
417         continue;
418       }
419 
420       BasicBlock *Succ = CI->getCaseSuccessor();
421       Succ->removePredecessor(BB);
422       Updates.push_back({DominatorTree::Delete, BB, Succ});
423       SI.removeCase(CI);
424       // Don't increment CI, as we removed a case.
425     }
426 
427     DTU.applyUpdatesPermissive(Updates);
428   } else {
429     llvm_unreachable("Must have at least one feasible successor");
430   }
431   return true;
432 }
433 
434 bool llvm::runIPSCCP(
435     Module &M, const DataLayout &DL,
436     std::function<const TargetLibraryInfo &(Function &)> GetTLI,
437     function_ref<AnalysisResultsForFn(Function &)> getAnalysis) {
438   SCCPSolver Solver(DL, GetTLI, M.getContext());
439 
440   // Loop over all functions, marking arguments to those with their addresses
441   // taken or that are external as overdefined.
442   for (Function &F : M) {
443     if (F.isDeclaration())
444       continue;
445 
446     Solver.addAnalysis(F, getAnalysis(F));
447 
448     // Determine if we can track the function's return values. If so, add the
449     // function to the solver's set of return-tracked functions.
450     if (canTrackReturnsInterprocedurally(&F))
451       Solver.addTrackedFunction(&F);
452 
453     // Determine if we can track the function's arguments. If so, add the
454     // function to the solver's set of argument-tracked functions.
455     if (canTrackArgumentsInterprocedurally(&F)) {
456       Solver.addArgumentTrackedFunction(&F);
457       continue;
458     }
459 
460     // Assume the function is called.
461     Solver.markBlockExecutable(&F.front());
462 
463     // Assume nothing about the incoming arguments.
464     for (Argument &AI : F.args())
465       Solver.markOverdefined(&AI);
466   }
467 
468   // Determine if we can track any of the module's global variables. If so, add
469   // the global variables we can track to the solver's set of tracked global
470   // variables.
471   for (GlobalVariable &G : M.globals()) {
472     G.removeDeadConstantUsers();
473     if (canTrackGlobalVariableInterprocedurally(&G))
474       Solver.trackValueOfGlobalVariable(&G);
475   }
476 
477   // Solve for constants.
478   bool ResolvedUndefs = true;
479   Solver.solve();
480   while (ResolvedUndefs) {
481     LLVM_DEBUG(dbgs() << "RESOLVING UNDEFS\n");
482     ResolvedUndefs = false;
483     for (Function &F : M) {
484       if (Solver.resolvedUndefsIn(F))
485         ResolvedUndefs = true;
486     }
487     if (ResolvedUndefs)
488       Solver.solve();
489   }
490 
491   bool MadeChanges = false;
492 
493   // Iterate over all of the instructions in the module, replacing them with
494   // constants if we have found them to be of constant values.
495 
496   for (Function &F : M) {
497     if (F.isDeclaration())
498       continue;
499 
500     SmallVector<BasicBlock *, 512> BlocksToErase;
501 
502     if (Solver.isBlockExecutable(&F.front())) {
503       bool ReplacedPointerArg = false;
504       for (Argument &Arg : F.args()) {
505         if (!Arg.use_empty() && tryToReplaceWithConstant(Solver, &Arg)) {
506           ReplacedPointerArg |= Arg.getType()->isPointerTy();
507           ++IPNumArgsElimed;
508         }
509       }
510 
511       // If we replaced an argument, the argmemonly and
512       // inaccessiblemem_or_argmemonly attributes do not hold any longer. Remove
513       // them from both the function and callsites.
514       if (ReplacedPointerArg) {
515         AttributeMask AttributesToRemove;
516         AttributesToRemove.addAttribute(Attribute::ArgMemOnly);
517         AttributesToRemove.addAttribute(Attribute::InaccessibleMemOrArgMemOnly);
518         F.removeFnAttrs(AttributesToRemove);
519 
520         for (User *U : F.users()) {
521           auto *CB = dyn_cast<CallBase>(U);
522           if (!CB || CB->getCalledFunction() != &F)
523             continue;
524 
525           CB->removeFnAttrs(AttributesToRemove);
526         }
527       }
528       MadeChanges |= ReplacedPointerArg;
529     }
530 
531     SmallPtrSet<Value *, 32> InsertedValues;
532     for (BasicBlock &BB : F) {
533       if (!Solver.isBlockExecutable(&BB)) {
534         LLVM_DEBUG(dbgs() << "  BasicBlock Dead:" << BB);
535         ++NumDeadBlocks;
536 
537         MadeChanges = true;
538 
539         if (&BB != &F.front())
540           BlocksToErase.push_back(&BB);
541         continue;
542       }
543 
544       MadeChanges |= simplifyInstsInBlock(Solver, BB, InsertedValues,
545                                           IPNumInstRemoved, IPNumInstReplaced);
546     }
547 
548     DomTreeUpdater DTU = Solver.getDTU(F);
549     // Change dead blocks to unreachable. We do it after replacing constants
550     // in all executable blocks, because changeToUnreachable may remove PHI
551     // nodes in executable blocks we found values for. The function's entry
552     // block is not part of BlocksToErase, so we have to handle it separately.
553     for (BasicBlock *BB : BlocksToErase) {
554       NumInstRemoved += changeToUnreachable(BB->getFirstNonPHI(),
555                                             /*PreserveLCSSA=*/false, &DTU);
556     }
557     if (!Solver.isBlockExecutable(&F.front()))
558       NumInstRemoved += changeToUnreachable(F.front().getFirstNonPHI(),
559                                             /*PreserveLCSSA=*/false, &DTU);
560 
561     BasicBlock *NewUnreachableBB = nullptr;
562     for (BasicBlock &BB : F)
563       MadeChanges |= removeNonFeasibleEdges(Solver, &BB, DTU, NewUnreachableBB);
564 
565     for (BasicBlock *DeadBB : BlocksToErase)
566       if (!DeadBB->hasAddressTaken())
567         DTU.deleteBB(DeadBB);
568 
569     for (BasicBlock &BB : F) {
570       for (Instruction &Inst : llvm::make_early_inc_range(BB)) {
571         if (Solver.getPredicateInfoFor(&Inst)) {
572           if (auto *II = dyn_cast<IntrinsicInst>(&Inst)) {
573             if (II->getIntrinsicID() == Intrinsic::ssa_copy) {
574               Value *Op = II->getOperand(0);
575               Inst.replaceAllUsesWith(Op);
576               Inst.eraseFromParent();
577             }
578           }
579         }
580       }
581     }
582   }
583 
584   // If we inferred constant or undef return values for a function, we replaced
585   // all call uses with the inferred value.  This means we don't need to bother
586   // actually returning anything from the function.  Replace all return
587   // instructions with return undef.
588   //
589   // Do this in two stages: first identify the functions we should process, then
590   // actually zap their returns.  This is important because we can only do this
591   // if the address of the function isn't taken.  In cases where a return is the
592   // last use of a function, the order of processing functions would affect
593   // whether other functions are optimizable.
594   SmallVector<ReturnInst*, 8> ReturnsToZap;
595 
596   for (const auto &I : Solver.getTrackedRetVals()) {
597     Function *F = I.first;
598     const ValueLatticeElement &ReturnValue = I.second;
599 
600     // If there is a known constant range for the return value, add !range
601     // metadata to the function's call sites.
602     if (ReturnValue.isConstantRange() &&
603         !ReturnValue.getConstantRange().isSingleElement()) {
604       // Do not add range metadata if the return value may include undef.
605       if (ReturnValue.isConstantRangeIncludingUndef())
606         continue;
607 
608       auto &CR = ReturnValue.getConstantRange();
609       for (User *User : F->users()) {
610         auto *CB = dyn_cast<CallBase>(User);
611         if (!CB || CB->getCalledFunction() != F)
612           continue;
613 
614         // Limit to cases where the return value is guaranteed to be neither
615         // poison nor undef. Poison will be outside any range and currently
616         // values outside of the specified range cause immediate undefined
617         // behavior.
618         if (!isGuaranteedNotToBeUndefOrPoison(CB, nullptr, CB))
619           continue;
620 
621         // Do not touch existing metadata for now.
622         // TODO: We should be able to take the intersection of the existing
623         // metadata and the inferred range.
624         if (CB->getMetadata(LLVMContext::MD_range))
625           continue;
626 
627         LLVMContext &Context = CB->getParent()->getContext();
628         Metadata *RangeMD[] = {
629             ConstantAsMetadata::get(ConstantInt::get(Context, CR.getLower())),
630             ConstantAsMetadata::get(ConstantInt::get(Context, CR.getUpper()))};
631         CB->setMetadata(LLVMContext::MD_range, MDNode::get(Context, RangeMD));
632       }
633       continue;
634     }
635     if (F->getReturnType()->isVoidTy())
636       continue;
637     if (isConstant(ReturnValue) || ReturnValue.isUnknownOrUndef())
638       findReturnsToZap(*F, ReturnsToZap, Solver);
639   }
640 
641   for (auto F : Solver.getMRVFunctionsTracked()) {
642     assert(F->getReturnType()->isStructTy() &&
643            "The return type should be a struct");
644     StructType *STy = cast<StructType>(F->getReturnType());
645     if (Solver.isStructLatticeConstant(F, STy))
646       findReturnsToZap(*F, ReturnsToZap, Solver);
647   }
648 
649   // Zap all returns which we've identified as zap to change.
650   SmallSetVector<Function *, 8> FuncZappedReturn;
651   for (unsigned i = 0, e = ReturnsToZap.size(); i != e; ++i) {
652     Function *F = ReturnsToZap[i]->getParent()->getParent();
653     ReturnsToZap[i]->setOperand(0, UndefValue::get(F->getReturnType()));
654     // Record all functions that are zapped.
655     FuncZappedReturn.insert(F);
656   }
657 
658   // Remove the returned attribute for zapped functions and the
659   // corresponding call sites.
660   for (Function *F : FuncZappedReturn) {
661     for (Argument &A : F->args())
662       F->removeParamAttr(A.getArgNo(), Attribute::Returned);
663     for (Use &U : F->uses()) {
664       // Skip over blockaddr users.
665       if (isa<BlockAddress>(U.getUser()))
666         continue;
667       CallBase *CB = cast<CallBase>(U.getUser());
668       for (Use &Arg : CB->args())
669         CB->removeParamAttr(CB->getArgOperandNo(&Arg), Attribute::Returned);
670     }
671   }
672 
673   // If we inferred constant or undef values for globals variables, we can
674   // delete the global and any stores that remain to it.
675   for (auto &I : make_early_inc_range(Solver.getTrackedGlobals())) {
676     GlobalVariable *GV = I.first;
677     if (isOverdefined(I.second))
678       continue;
679     LLVM_DEBUG(dbgs() << "Found that GV '" << GV->getName()
680                       << "' is constant!\n");
681     while (!GV->use_empty()) {
682       StoreInst *SI = cast<StoreInst>(GV->user_back());
683       SI->eraseFromParent();
684       MadeChanges = true;
685     }
686     M.getGlobalList().erase(GV);
687     ++IPNumGlobalConst;
688   }
689 
690   return MadeChanges;
691 }
692