SCCP.cpp 24 KB

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