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() == 0) {
375     // Branch on undef/poison, replace with unreachable.
376     SmallPtrSet<BasicBlock *, 8> SeenSuccs;
377     SmallVector<DominatorTree::UpdateType, 8> Updates;
378     for (BasicBlock *Succ : successors(BB)) {
379       Succ->removePredecessor(BB);
380       if (SeenSuccs.insert(Succ).second)
381         Updates.push_back({DominatorTree::Delete, BB, Succ});
382     }
383     TI->eraseFromParent();
384     new UnreachableInst(BB->getContext(), BB);
385     DTU.applyUpdatesPermissive(Updates);
386   } else if (FeasibleSuccessors.size() == 1) {
387     // Replace with an unconditional branch to the only feasible successor.
388     BasicBlock *OnlyFeasibleSuccessor = *FeasibleSuccessors.begin();
389     SmallVector<DominatorTree::UpdateType, 8> Updates;
390     bool HaveSeenOnlyFeasibleSuccessor = false;
391     for (BasicBlock *Succ : successors(BB)) {
392       if (Succ == OnlyFeasibleSuccessor && !HaveSeenOnlyFeasibleSuccessor) {
393         // Don't remove the edge to the only feasible successor the first time
394         // we see it. We still do need to remove any multi-edges to it though.
395         HaveSeenOnlyFeasibleSuccessor = true;
396         continue;
397       }
398 
399       Succ->removePredecessor(BB);
400       Updates.push_back({DominatorTree::Delete, BB, Succ});
401     }
402 
403     BranchInst::Create(OnlyFeasibleSuccessor, BB);
404     TI->eraseFromParent();
405     DTU.applyUpdatesPermissive(Updates);
406   } else if (FeasibleSuccessors.size() > 1) {
407     SwitchInstProfUpdateWrapper SI(*cast<SwitchInst>(TI));
408     SmallVector<DominatorTree::UpdateType, 8> Updates;
409 
410     // If the default destination is unfeasible it will never be taken. Replace
411     // it with a new block with a single Unreachable instruction.
412     BasicBlock *DefaultDest = SI->getDefaultDest();
413     if (!FeasibleSuccessors.contains(DefaultDest)) {
414       if (!NewUnreachableBB) {
415         NewUnreachableBB =
416             BasicBlock::Create(DefaultDest->getContext(), "default.unreachable",
417                                DefaultDest->getParent(), DefaultDest);
418         new UnreachableInst(DefaultDest->getContext(), NewUnreachableBB);
419       }
420 
421       SI->setDefaultDest(NewUnreachableBB);
422       Updates.push_back({DominatorTree::Delete, BB, DefaultDest});
423       Updates.push_back({DominatorTree::Insert, BB, NewUnreachableBB});
424     }
425 
426     for (auto CI = SI->case_begin(); CI != SI->case_end();) {
427       if (FeasibleSuccessors.contains(CI->getCaseSuccessor())) {
428         ++CI;
429         continue;
430       }
431 
432       BasicBlock *Succ = CI->getCaseSuccessor();
433       Succ->removePredecessor(BB);
434       Updates.push_back({DominatorTree::Delete, BB, Succ});
435       SI.removeCase(CI);
436       // Don't increment CI, as we removed a case.
437     }
438 
439     DTU.applyUpdatesPermissive(Updates);
440   } else {
441     llvm_unreachable("Must have at least one feasible successor");
442   }
443   return true;
444 }
445 
446 bool llvm::runIPSCCP(
447     Module &M, const DataLayout &DL,
448     std::function<const TargetLibraryInfo &(Function &)> GetTLI,
449     function_ref<AnalysisResultsForFn(Function &)> getAnalysis) {
450   SCCPSolver Solver(DL, GetTLI, M.getContext());
451 
452   // Loop over all functions, marking arguments to those with their addresses
453   // taken or that are external as overdefined.
454   for (Function &F : M) {
455     if (F.isDeclaration())
456       continue;
457 
458     Solver.addAnalysis(F, getAnalysis(F));
459 
460     // Determine if we can track the function's return values. If so, add the
461     // function to the solver's set of return-tracked functions.
462     if (canTrackReturnsInterprocedurally(&F))
463       Solver.addTrackedFunction(&F);
464 
465     // Determine if we can track the function's arguments. If so, add the
466     // function to the solver's set of argument-tracked functions.
467     if (canTrackArgumentsInterprocedurally(&F)) {
468       Solver.addArgumentTrackedFunction(&F);
469       continue;
470     }
471 
472     // Assume the function is called.
473     Solver.markBlockExecutable(&F.front());
474 
475     // Assume nothing about the incoming arguments.
476     for (Argument &AI : F.args())
477       Solver.markOverdefined(&AI);
478   }
479 
480   // Determine if we can track any of the module's global variables. If so, add
481   // the global variables we can track to the solver's set of tracked global
482   // variables.
483   for (GlobalVariable &G : M.globals()) {
484     G.removeDeadConstantUsers();
485     if (canTrackGlobalVariableInterprocedurally(&G))
486       Solver.trackValueOfGlobalVariable(&G);
487   }
488 
489   // Solve for constants.
490   bool ResolvedUndefs = true;
491   Solver.solve();
492   while (ResolvedUndefs) {
493     LLVM_DEBUG(dbgs() << "RESOLVING UNDEFS\n");
494     ResolvedUndefs = false;
495     for (Function &F : M) {
496       if (Solver.resolvedUndefsIn(F))
497         ResolvedUndefs = true;
498     }
499     if (ResolvedUndefs)
500       Solver.solve();
501   }
502 
503   bool MadeChanges = false;
504 
505   // Iterate over all of the instructions in the module, replacing them with
506   // constants if we have found them to be of constant values.
507 
508   for (Function &F : M) {
509     if (F.isDeclaration())
510       continue;
511 
512     SmallVector<BasicBlock *, 512> BlocksToErase;
513 
514     if (Solver.isBlockExecutable(&F.front())) {
515       bool ReplacedPointerArg = false;
516       for (Argument &Arg : F.args()) {
517         if (!Arg.use_empty() && tryToReplaceWithConstant(Solver, &Arg)) {
518           ReplacedPointerArg |= Arg.getType()->isPointerTy();
519           ++IPNumArgsElimed;
520         }
521       }
522 
523       // If we replaced an argument, the argmemonly and
524       // inaccessiblemem_or_argmemonly attributes do not hold any longer. Remove
525       // them from both the function and callsites.
526       if (ReplacedPointerArg) {
527         AttributeMask AttributesToRemove;
528         AttributesToRemove.addAttribute(Attribute::ArgMemOnly);
529         AttributesToRemove.addAttribute(Attribute::InaccessibleMemOrArgMemOnly);
530         F.removeFnAttrs(AttributesToRemove);
531 
532         for (User *U : F.users()) {
533           auto *CB = dyn_cast<CallBase>(U);
534           if (!CB || CB->getCalledFunction() != &F)
535             continue;
536 
537           CB->removeFnAttrs(AttributesToRemove);
538         }
539       }
540       MadeChanges |= ReplacedPointerArg;
541     }
542 
543     SmallPtrSet<Value *, 32> InsertedValues;
544     for (BasicBlock &BB : F) {
545       if (!Solver.isBlockExecutable(&BB)) {
546         LLVM_DEBUG(dbgs() << "  BasicBlock Dead:" << BB);
547         ++NumDeadBlocks;
548 
549         MadeChanges = true;
550 
551         if (&BB != &F.front())
552           BlocksToErase.push_back(&BB);
553         continue;
554       }
555 
556       MadeChanges |= simplifyInstsInBlock(Solver, BB, InsertedValues,
557                                           IPNumInstRemoved, IPNumInstReplaced);
558     }
559 
560     DomTreeUpdater DTU = Solver.getDTU(F);
561     // Change dead blocks to unreachable. We do it after replacing constants
562     // in all executable blocks, because changeToUnreachable may remove PHI
563     // nodes in executable blocks we found values for. The function's entry
564     // block is not part of BlocksToErase, so we have to handle it separately.
565     for (BasicBlock *BB : BlocksToErase) {
566       NumInstRemoved += changeToUnreachable(BB->getFirstNonPHI(),
567                                             /*PreserveLCSSA=*/false, &DTU);
568     }
569     if (!Solver.isBlockExecutable(&F.front()))
570       NumInstRemoved += changeToUnreachable(F.front().getFirstNonPHI(),
571                                             /*PreserveLCSSA=*/false, &DTU);
572 
573     BasicBlock *NewUnreachableBB = nullptr;
574     for (BasicBlock &BB : F)
575       MadeChanges |= removeNonFeasibleEdges(Solver, &BB, DTU, NewUnreachableBB);
576 
577     for (BasicBlock *DeadBB : BlocksToErase)
578       if (!DeadBB->hasAddressTaken())
579         DTU.deleteBB(DeadBB);
580 
581     for (BasicBlock &BB : F) {
582       for (Instruction &Inst : llvm::make_early_inc_range(BB)) {
583         if (Solver.getPredicateInfoFor(&Inst)) {
584           if (auto *II = dyn_cast<IntrinsicInst>(&Inst)) {
585             if (II->getIntrinsicID() == Intrinsic::ssa_copy) {
586               Value *Op = II->getOperand(0);
587               Inst.replaceAllUsesWith(Op);
588               Inst.eraseFromParent();
589             }
590           }
591         }
592       }
593     }
594   }
595 
596   // If we inferred constant or undef return values for a function, we replaced
597   // all call uses with the inferred value.  This means we don't need to bother
598   // actually returning anything from the function.  Replace all return
599   // instructions with return undef.
600   //
601   // Do this in two stages: first identify the functions we should process, then
602   // actually zap their returns.  This is important because we can only do this
603   // if the address of the function isn't taken.  In cases where a return is the
604   // last use of a function, the order of processing functions would affect
605   // whether other functions are optimizable.
606   SmallVector<ReturnInst*, 8> ReturnsToZap;
607 
608   for (const auto &I : Solver.getTrackedRetVals()) {
609     Function *F = I.first;
610     const ValueLatticeElement &ReturnValue = I.second;
611 
612     // If there is a known constant range for the return value, add !range
613     // metadata to the function's call sites.
614     if (ReturnValue.isConstantRange() &&
615         !ReturnValue.getConstantRange().isSingleElement()) {
616       // Do not add range metadata if the return value may include undef.
617       if (ReturnValue.isConstantRangeIncludingUndef())
618         continue;
619 
620       auto &CR = ReturnValue.getConstantRange();
621       for (User *User : F->users()) {
622         auto *CB = dyn_cast<CallBase>(User);
623         if (!CB || CB->getCalledFunction() != F)
624           continue;
625 
626         // Limit to cases where the return value is guaranteed to be neither
627         // poison nor undef. Poison will be outside any range and currently
628         // values outside of the specified range cause immediate undefined
629         // behavior.
630         if (!isGuaranteedNotToBeUndefOrPoison(CB, nullptr, CB))
631           continue;
632 
633         // Do not touch existing metadata for now.
634         // TODO: We should be able to take the intersection of the existing
635         // metadata and the inferred range.
636         if (CB->getMetadata(LLVMContext::MD_range))
637           continue;
638 
639         LLVMContext &Context = CB->getParent()->getContext();
640         Metadata *RangeMD[] = {
641             ConstantAsMetadata::get(ConstantInt::get(Context, CR.getLower())),
642             ConstantAsMetadata::get(ConstantInt::get(Context, CR.getUpper()))};
643         CB->setMetadata(LLVMContext::MD_range, MDNode::get(Context, RangeMD));
644       }
645       continue;
646     }
647     if (F->getReturnType()->isVoidTy())
648       continue;
649     if (isConstant(ReturnValue) || ReturnValue.isUnknownOrUndef())
650       findReturnsToZap(*F, ReturnsToZap, Solver);
651   }
652 
653   for (auto F : Solver.getMRVFunctionsTracked()) {
654     assert(F->getReturnType()->isStructTy() &&
655            "The return type should be a struct");
656     StructType *STy = cast<StructType>(F->getReturnType());
657     if (Solver.isStructLatticeConstant(F, STy))
658       findReturnsToZap(*F, ReturnsToZap, Solver);
659   }
660 
661   // Zap all returns which we've identified as zap to change.
662   SmallSetVector<Function *, 8> FuncZappedReturn;
663   for (unsigned i = 0, e = ReturnsToZap.size(); i != e; ++i) {
664     Function *F = ReturnsToZap[i]->getParent()->getParent();
665     ReturnsToZap[i]->setOperand(0, UndefValue::get(F->getReturnType()));
666     // Record all functions that are zapped.
667     FuncZappedReturn.insert(F);
668   }
669 
670   // Remove the returned attribute for zapped functions and the
671   // corresponding call sites.
672   for (Function *F : FuncZappedReturn) {
673     for (Argument &A : F->args())
674       F->removeParamAttr(A.getArgNo(), Attribute::Returned);
675     for (Use &U : F->uses()) {
676       // Skip over blockaddr users.
677       if (isa<BlockAddress>(U.getUser()))
678         continue;
679       CallBase *CB = cast<CallBase>(U.getUser());
680       for (Use &Arg : CB->args())
681         CB->removeParamAttr(CB->getArgOperandNo(&Arg), Attribute::Returned);
682     }
683   }
684 
685   // If we inferred constant or undef values for globals variables, we can
686   // delete the global and any stores that remain to it.
687   for (auto &I : make_early_inc_range(Solver.getTrackedGlobals())) {
688     GlobalVariable *GV = I.first;
689     if (isOverdefined(I.second))
690       continue;
691     LLVM_DEBUG(dbgs() << "Found that GV '" << GV->getName()
692                       << "' is constant!\n");
693     while (!GV->use_empty()) {
694       StoreInst *SI = cast<StoreInst>(GV->user_back());
695       SI->eraseFromParent();
696       MadeChanges = true;
697     }
698     M.getGlobalList().erase(GV);
699     ++IPNumGlobalConst;
700   }
701 
702   return MadeChanges;
703 }
704