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 static bool removeNonFeasibleEdges(const SCCPSolver &Solver, BasicBlock *BB,
194                                    DomTreeUpdater &DTU,
195                                    BasicBlock *&NewUnreachableBB);
196 
197 // runSCCP() - Run the Sparse Conditional Constant Propagation algorithm,
198 // and return true if the function was modified.
199 static bool runSCCP(Function &F, const DataLayout &DL,
200                     const TargetLibraryInfo *TLI, DomTreeUpdater &DTU) {
201   LLVM_DEBUG(dbgs() << "SCCP on function '" << F.getName() << "'\n");
202   SCCPSolver Solver(
203       DL, [TLI](Function &F) -> const TargetLibraryInfo & { return *TLI; },
204       F.getContext());
205 
206   // Mark the first block of the function as being executable.
207   Solver.markBlockExecutable(&F.front());
208 
209   // Mark all arguments to the function as being overdefined.
210   for (Argument &AI : F.args())
211     Solver.markOverdefined(&AI);
212 
213   // Solve for constants.
214   bool ResolvedUndefs = true;
215   while (ResolvedUndefs) {
216     Solver.solve();
217     LLVM_DEBUG(dbgs() << "RESOLVING UNDEFs\n");
218     ResolvedUndefs = Solver.resolvedUndefsIn(F);
219   }
220 
221   bool MadeChanges = false;
222 
223   // If we decided that there are basic blocks that are dead in this function,
224   // delete their contents now.  Note that we cannot actually delete the blocks,
225   // as we cannot modify the CFG of the function.
226 
227   SmallPtrSet<Value *, 32> InsertedValues;
228   SmallVector<BasicBlock *, 8> BlocksToErase;
229   for (BasicBlock &BB : F) {
230     if (!Solver.isBlockExecutable(&BB)) {
231       LLVM_DEBUG(dbgs() << "  BasicBlock Dead:" << BB);
232       ++NumDeadBlocks;
233       BlocksToErase.push_back(&BB);
234       MadeChanges = true;
235       continue;
236     }
237 
238     MadeChanges |= simplifyInstsInBlock(Solver, BB, InsertedValues,
239                                         NumInstRemoved, NumInstReplaced);
240   }
241 
242   // Remove unreachable blocks and non-feasible edges.
243   for (BasicBlock *DeadBB : BlocksToErase)
244     NumInstRemoved += changeToUnreachable(DeadBB->getFirstNonPHI(),
245                                           /*PreserveLCSSA=*/false, &DTU);
246 
247   BasicBlock *NewUnreachableBB = nullptr;
248   for (BasicBlock &BB : F)
249     MadeChanges |= removeNonFeasibleEdges(Solver, &BB, DTU, NewUnreachableBB);
250 
251   for (BasicBlock *DeadBB : BlocksToErase)
252     if (!DeadBB->hasAddressTaken())
253       DTU.deleteBB(DeadBB);
254 
255   return MadeChanges;
256 }
257 
258 PreservedAnalyses SCCPPass::run(Function &F, FunctionAnalysisManager &AM) {
259   const DataLayout &DL = F.getParent()->getDataLayout();
260   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
261   auto *DT = AM.getCachedResult<DominatorTreeAnalysis>(F);
262   DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
263   if (!runSCCP(F, DL, &TLI, DTU))
264     return PreservedAnalyses::all();
265 
266   auto PA = PreservedAnalyses();
267   PA.preserve<DominatorTreeAnalysis>();
268   return PA;
269 }
270 
271 namespace {
272 
273 //===--------------------------------------------------------------------===//
274 //
275 /// SCCP Class - This class uses the SCCPSolver to implement a per-function
276 /// Sparse Conditional Constant Propagator.
277 ///
278 class SCCPLegacyPass : public FunctionPass {
279 public:
280   // Pass identification, replacement for typeid
281   static char ID;
282 
283   SCCPLegacyPass() : FunctionPass(ID) {
284     initializeSCCPLegacyPassPass(*PassRegistry::getPassRegistry());
285   }
286 
287   void getAnalysisUsage(AnalysisUsage &AU) const override {
288     AU.addRequired<TargetLibraryInfoWrapperPass>();
289     AU.addPreserved<GlobalsAAWrapperPass>();
290     AU.addPreserved<DominatorTreeWrapperPass>();
291   }
292 
293   // runOnFunction - Run the Sparse Conditional Constant Propagation
294   // algorithm, and return true if the function was modified.
295   bool runOnFunction(Function &F) override {
296     if (skipFunction(F))
297       return false;
298     const DataLayout &DL = F.getParent()->getDataLayout();
299     const TargetLibraryInfo *TLI =
300         &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
301     auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
302     DomTreeUpdater DTU(DTWP ? &DTWP->getDomTree() : nullptr,
303                        DomTreeUpdater::UpdateStrategy::Lazy);
304     return runSCCP(F, DL, TLI, DTU);
305   }
306 };
307 
308 } // end anonymous namespace
309 
310 char SCCPLegacyPass::ID = 0;
311 
312 INITIALIZE_PASS_BEGIN(SCCPLegacyPass, "sccp",
313                       "Sparse Conditional Constant Propagation", false, false)
314 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
315 INITIALIZE_PASS_END(SCCPLegacyPass, "sccp",
316                     "Sparse Conditional Constant Propagation", false, false)
317 
318 // createSCCPPass - This is the public interface to this file.
319 FunctionPass *llvm::createSCCPPass() { return new SCCPLegacyPass(); }
320 
321 static void findReturnsToZap(Function &F,
322                              SmallVector<ReturnInst *, 8> &ReturnsToZap,
323                              SCCPSolver &Solver) {
324   // We can only do this if we know that nothing else can call the function.
325   if (!Solver.isArgumentTrackedFunction(&F))
326     return;
327 
328   if (Solver.mustPreserveReturn(&F)) {
329     LLVM_DEBUG(
330         dbgs()
331         << "Can't zap returns of the function : " << F.getName()
332         << " due to present musttail or \"clang.arc.attachedcall\" call of "
333            "it\n");
334     return;
335   }
336 
337   assert(
338       all_of(F.users(),
339              [&Solver](User *U) {
340                if (isa<Instruction>(U) &&
341                    !Solver.isBlockExecutable(cast<Instruction>(U)->getParent()))
342                  return true;
343                // Non-callsite uses are not impacted by zapping. Also, constant
344                // uses (like blockaddresses) could stuck around, without being
345                // used in the underlying IR, meaning we do not have lattice
346                // values for them.
347                if (!isa<CallBase>(U))
348                  return true;
349                if (U->getType()->isStructTy()) {
350                  return all_of(Solver.getStructLatticeValueFor(U),
351                                [](const ValueLatticeElement &LV) {
352                                  return !isOverdefined(LV);
353                                });
354                }
355                return !isOverdefined(Solver.getLatticeValueFor(U));
356              }) &&
357       "We can only zap functions where all live users have a concrete value");
358 
359   for (BasicBlock &BB : F) {
360     if (CallInst *CI = BB.getTerminatingMustTailCall()) {
361       LLVM_DEBUG(dbgs() << "Can't zap return of the block due to present "
362                         << "musttail call : " << *CI << "\n");
363       (void)CI;
364       return;
365     }
366 
367     if (auto *RI = dyn_cast<ReturnInst>(BB.getTerminator()))
368       if (!isa<UndefValue>(RI->getOperand(0)))
369         ReturnsToZap.push_back(RI);
370   }
371 }
372 
373 static bool removeNonFeasibleEdges(const SCCPSolver &Solver, BasicBlock *BB,
374                                    DomTreeUpdater &DTU,
375                                    BasicBlock *&NewUnreachableBB) {
376   SmallPtrSet<BasicBlock *, 8> FeasibleSuccessors;
377   bool HasNonFeasibleEdges = false;
378   for (BasicBlock *Succ : successors(BB)) {
379     if (Solver.isEdgeFeasible(BB, Succ))
380       FeasibleSuccessors.insert(Succ);
381     else
382       HasNonFeasibleEdges = true;
383   }
384 
385   // All edges feasible, nothing to do.
386   if (!HasNonFeasibleEdges)
387     return false;
388 
389   // SCCP can only determine non-feasible edges for br, switch and indirectbr.
390   Instruction *TI = BB->getTerminator();
391   assert((isa<BranchInst>(TI) || isa<SwitchInst>(TI) ||
392           isa<IndirectBrInst>(TI)) &&
393          "Terminator must be a br, switch or indirectbr");
394 
395   if (FeasibleSuccessors.size() == 0) {
396     // Branch on undef/poison, replace with unreachable.
397     SmallPtrSet<BasicBlock *, 8> SeenSuccs;
398     SmallVector<DominatorTree::UpdateType, 8> Updates;
399     for (BasicBlock *Succ : successors(BB)) {
400       Succ->removePredecessor(BB);
401       if (SeenSuccs.insert(Succ).second)
402         Updates.push_back({DominatorTree::Delete, BB, Succ});
403     }
404     TI->eraseFromParent();
405     new UnreachableInst(BB->getContext(), BB);
406     DTU.applyUpdatesPermissive(Updates);
407   } else if (FeasibleSuccessors.size() == 1) {
408     // Replace with an unconditional branch to the only feasible successor.
409     BasicBlock *OnlyFeasibleSuccessor = *FeasibleSuccessors.begin();
410     SmallVector<DominatorTree::UpdateType, 8> Updates;
411     bool HaveSeenOnlyFeasibleSuccessor = false;
412     for (BasicBlock *Succ : successors(BB)) {
413       if (Succ == OnlyFeasibleSuccessor && !HaveSeenOnlyFeasibleSuccessor) {
414         // Don't remove the edge to the only feasible successor the first time
415         // we see it. We still do need to remove any multi-edges to it though.
416         HaveSeenOnlyFeasibleSuccessor = true;
417         continue;
418       }
419 
420       Succ->removePredecessor(BB);
421       Updates.push_back({DominatorTree::Delete, BB, Succ});
422     }
423 
424     BranchInst::Create(OnlyFeasibleSuccessor, BB);
425     TI->eraseFromParent();
426     DTU.applyUpdatesPermissive(Updates);
427   } else if (FeasibleSuccessors.size() > 1) {
428     SwitchInstProfUpdateWrapper SI(*cast<SwitchInst>(TI));
429     SmallVector<DominatorTree::UpdateType, 8> Updates;
430 
431     // If the default destination is unfeasible it will never be taken. Replace
432     // it with a new block with a single Unreachable instruction.
433     BasicBlock *DefaultDest = SI->getDefaultDest();
434     if (!FeasibleSuccessors.contains(DefaultDest)) {
435       if (!NewUnreachableBB) {
436         NewUnreachableBB =
437             BasicBlock::Create(DefaultDest->getContext(), "default.unreachable",
438                                DefaultDest->getParent(), DefaultDest);
439         new UnreachableInst(DefaultDest->getContext(), NewUnreachableBB);
440       }
441 
442       SI->setDefaultDest(NewUnreachableBB);
443       Updates.push_back({DominatorTree::Delete, BB, DefaultDest});
444       Updates.push_back({DominatorTree::Insert, BB, NewUnreachableBB});
445     }
446 
447     for (auto CI = SI->case_begin(); CI != SI->case_end();) {
448       if (FeasibleSuccessors.contains(CI->getCaseSuccessor())) {
449         ++CI;
450         continue;
451       }
452 
453       BasicBlock *Succ = CI->getCaseSuccessor();
454       Succ->removePredecessor(BB);
455       Updates.push_back({DominatorTree::Delete, BB, Succ});
456       SI.removeCase(CI);
457       // Don't increment CI, as we removed a case.
458     }
459 
460     DTU.applyUpdatesPermissive(Updates);
461   } else {
462     llvm_unreachable("Must have at least one feasible successor");
463   }
464   return true;
465 }
466 
467 bool llvm::runIPSCCP(
468     Module &M, const DataLayout &DL,
469     std::function<const TargetLibraryInfo &(Function &)> GetTLI,
470     function_ref<AnalysisResultsForFn(Function &)> getAnalysis) {
471   SCCPSolver Solver(DL, GetTLI, M.getContext());
472 
473   // Loop over all functions, marking arguments to those with their addresses
474   // taken or that are external as overdefined.
475   for (Function &F : M) {
476     if (F.isDeclaration())
477       continue;
478 
479     Solver.addAnalysis(F, getAnalysis(F));
480 
481     // Determine if we can track the function's return values. If so, add the
482     // function to the solver's set of return-tracked functions.
483     if (canTrackReturnsInterprocedurally(&F))
484       Solver.addTrackedFunction(&F);
485 
486     // Determine if we can track the function's arguments. If so, add the
487     // function to the solver's set of argument-tracked functions.
488     if (canTrackArgumentsInterprocedurally(&F)) {
489       Solver.addArgumentTrackedFunction(&F);
490       continue;
491     }
492 
493     // Assume the function is called.
494     Solver.markBlockExecutable(&F.front());
495 
496     // Assume nothing about the incoming arguments.
497     for (Argument &AI : F.args())
498       Solver.markOverdefined(&AI);
499   }
500 
501   // Determine if we can track any of the module's global variables. If so, add
502   // the global variables we can track to the solver's set of tracked global
503   // variables.
504   for (GlobalVariable &G : M.globals()) {
505     G.removeDeadConstantUsers();
506     if (canTrackGlobalVariableInterprocedurally(&G))
507       Solver.trackValueOfGlobalVariable(&G);
508   }
509 
510   // Solve for constants.
511   bool ResolvedUndefs = true;
512   Solver.solve();
513   while (ResolvedUndefs) {
514     LLVM_DEBUG(dbgs() << "RESOLVING UNDEFS\n");
515     ResolvedUndefs = false;
516     for (Function &F : M) {
517       if (Solver.resolvedUndefsIn(F))
518         ResolvedUndefs = true;
519     }
520     if (ResolvedUndefs)
521       Solver.solve();
522   }
523 
524   bool MadeChanges = false;
525 
526   // Iterate over all of the instructions in the module, replacing them with
527   // constants if we have found them to be of constant values.
528 
529   for (Function &F : M) {
530     if (F.isDeclaration())
531       continue;
532 
533     SmallVector<BasicBlock *, 512> BlocksToErase;
534 
535     if (Solver.isBlockExecutable(&F.front())) {
536       bool ReplacedPointerArg = false;
537       for (Argument &Arg : F.args()) {
538         if (!Arg.use_empty() && tryToReplaceWithConstant(Solver, &Arg)) {
539           ReplacedPointerArg |= Arg.getType()->isPointerTy();
540           ++IPNumArgsElimed;
541         }
542       }
543 
544       // If we replaced an argument, the argmemonly and
545       // inaccessiblemem_or_argmemonly attributes do not hold any longer. Remove
546       // them from both the function and callsites.
547       if (ReplacedPointerArg) {
548         AttributeMask AttributesToRemove;
549         AttributesToRemove.addAttribute(Attribute::ArgMemOnly);
550         AttributesToRemove.addAttribute(Attribute::InaccessibleMemOrArgMemOnly);
551         F.removeFnAttrs(AttributesToRemove);
552 
553         for (User *U : F.users()) {
554           auto *CB = dyn_cast<CallBase>(U);
555           if (!CB || CB->getCalledFunction() != &F)
556             continue;
557 
558           CB->removeFnAttrs(AttributesToRemove);
559         }
560       }
561       MadeChanges |= ReplacedPointerArg;
562     }
563 
564     SmallPtrSet<Value *, 32> InsertedValues;
565     for (BasicBlock &BB : F) {
566       if (!Solver.isBlockExecutable(&BB)) {
567         LLVM_DEBUG(dbgs() << "  BasicBlock Dead:" << BB);
568         ++NumDeadBlocks;
569 
570         MadeChanges = true;
571 
572         if (&BB != &F.front())
573           BlocksToErase.push_back(&BB);
574         continue;
575       }
576 
577       MadeChanges |= simplifyInstsInBlock(Solver, BB, InsertedValues,
578                                           IPNumInstRemoved, IPNumInstReplaced);
579     }
580 
581     DomTreeUpdater DTU = Solver.getDTU(F);
582     // Change dead blocks to unreachable. We do it after replacing constants
583     // in all executable blocks, because changeToUnreachable may remove PHI
584     // nodes in executable blocks we found values for. The function's entry
585     // block is not part of BlocksToErase, so we have to handle it separately.
586     for (BasicBlock *BB : BlocksToErase) {
587       NumInstRemoved += changeToUnreachable(BB->getFirstNonPHI(),
588                                             /*PreserveLCSSA=*/false, &DTU);
589     }
590     if (!Solver.isBlockExecutable(&F.front()))
591       NumInstRemoved += changeToUnreachable(F.front().getFirstNonPHI(),
592                                             /*PreserveLCSSA=*/false, &DTU);
593 
594     BasicBlock *NewUnreachableBB = nullptr;
595     for (BasicBlock &BB : F)
596       MadeChanges |= removeNonFeasibleEdges(Solver, &BB, DTU, NewUnreachableBB);
597 
598     for (BasicBlock *DeadBB : BlocksToErase)
599       if (!DeadBB->hasAddressTaken())
600         DTU.deleteBB(DeadBB);
601 
602     for (BasicBlock &BB : F) {
603       for (Instruction &Inst : llvm::make_early_inc_range(BB)) {
604         if (Solver.getPredicateInfoFor(&Inst)) {
605           if (auto *II = dyn_cast<IntrinsicInst>(&Inst)) {
606             if (II->getIntrinsicID() == Intrinsic::ssa_copy) {
607               Value *Op = II->getOperand(0);
608               Inst.replaceAllUsesWith(Op);
609               Inst.eraseFromParent();
610             }
611           }
612         }
613       }
614     }
615   }
616 
617   // If we inferred constant or undef return values for a function, we replaced
618   // all call uses with the inferred value.  This means we don't need to bother
619   // actually returning anything from the function.  Replace all return
620   // instructions with return undef.
621   //
622   // Do this in two stages: first identify the functions we should process, then
623   // actually zap their returns.  This is important because we can only do this
624   // if the address of the function isn't taken.  In cases where a return is the
625   // last use of a function, the order of processing functions would affect
626   // whether other functions are optimizable.
627   SmallVector<ReturnInst*, 8> ReturnsToZap;
628 
629   for (const auto &I : Solver.getTrackedRetVals()) {
630     Function *F = I.first;
631     const ValueLatticeElement &ReturnValue = I.second;
632 
633     // If there is a known constant range for the return value, add !range
634     // metadata to the function's call sites.
635     if (ReturnValue.isConstantRange() &&
636         !ReturnValue.getConstantRange().isSingleElement()) {
637       // Do not add range metadata if the return value may include undef.
638       if (ReturnValue.isConstantRangeIncludingUndef())
639         continue;
640 
641       auto &CR = ReturnValue.getConstantRange();
642       for (User *User : F->users()) {
643         auto *CB = dyn_cast<CallBase>(User);
644         if (!CB || CB->getCalledFunction() != F)
645           continue;
646 
647         // Limit to cases where the return value is guaranteed to be neither
648         // poison nor undef. Poison will be outside any range and currently
649         // values outside of the specified range cause immediate undefined
650         // behavior.
651         if (!isGuaranteedNotToBeUndefOrPoison(CB, nullptr, CB))
652           continue;
653 
654         // Do not touch existing metadata for now.
655         // TODO: We should be able to take the intersection of the existing
656         // metadata and the inferred range.
657         if (CB->getMetadata(LLVMContext::MD_range))
658           continue;
659 
660         LLVMContext &Context = CB->getParent()->getContext();
661         Metadata *RangeMD[] = {
662             ConstantAsMetadata::get(ConstantInt::get(Context, CR.getLower())),
663             ConstantAsMetadata::get(ConstantInt::get(Context, CR.getUpper()))};
664         CB->setMetadata(LLVMContext::MD_range, MDNode::get(Context, RangeMD));
665       }
666       continue;
667     }
668     if (F->getReturnType()->isVoidTy())
669       continue;
670     if (isConstant(ReturnValue) || ReturnValue.isUnknownOrUndef())
671       findReturnsToZap(*F, ReturnsToZap, Solver);
672   }
673 
674   for (auto F : Solver.getMRVFunctionsTracked()) {
675     assert(F->getReturnType()->isStructTy() &&
676            "The return type should be a struct");
677     StructType *STy = cast<StructType>(F->getReturnType());
678     if (Solver.isStructLatticeConstant(F, STy))
679       findReturnsToZap(*F, ReturnsToZap, Solver);
680   }
681 
682   // Zap all returns which we've identified as zap to change.
683   SmallSetVector<Function *, 8> FuncZappedReturn;
684   for (unsigned i = 0, e = ReturnsToZap.size(); i != e; ++i) {
685     Function *F = ReturnsToZap[i]->getParent()->getParent();
686     ReturnsToZap[i]->setOperand(0, UndefValue::get(F->getReturnType()));
687     // Record all functions that are zapped.
688     FuncZappedReturn.insert(F);
689   }
690 
691   // Remove the returned attribute for zapped functions and the
692   // corresponding call sites.
693   for (Function *F : FuncZappedReturn) {
694     for (Argument &A : F->args())
695       F->removeParamAttr(A.getArgNo(), Attribute::Returned);
696     for (Use &U : F->uses()) {
697       // Skip over blockaddr users.
698       if (isa<BlockAddress>(U.getUser()))
699         continue;
700       CallBase *CB = cast<CallBase>(U.getUser());
701       for (Use &Arg : CB->args())
702         CB->removeParamAttr(CB->getArgOperandNo(&Arg), Attribute::Returned);
703     }
704   }
705 
706   // If we inferred constant or undef values for globals variables, we can
707   // delete the global and any stores that remain to it.
708   for (auto &I : make_early_inc_range(Solver.getTrackedGlobals())) {
709     GlobalVariable *GV = I.first;
710     if (isOverdefined(I.second))
711       continue;
712     LLVM_DEBUG(dbgs() << "Found that GV '" << GV->getName()
713                       << "' is constant!\n");
714     while (!GV->use_empty()) {
715       StoreInst *SI = cast<StoreInst>(GV->user_back());
716       SI->eraseFromParent();
717       MadeChanges = true;
718     }
719     M.getGlobalList().erase(GV);
720     ++IPNumGlobalConst;
721   }
722 
723   return MadeChanges;
724 }
725