SCCP.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. //===-- SCCP.cpp ----------------------------------------------------------===//
  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 Interprocedural Sparse Conditional Constant Propagation.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Transforms/IPO/SCCP.h"
  13. #include "llvm/ADT/SetVector.h"
  14. #include "llvm/Analysis/AssumptionCache.h"
  15. #include "llvm/Analysis/LoopInfo.h"
  16. #include "llvm/Analysis/PostDominators.h"
  17. #include "llvm/Analysis/TargetLibraryInfo.h"
  18. #include "llvm/Analysis/TargetTransformInfo.h"
  19. #include "llvm/Analysis/ValueLattice.h"
  20. #include "llvm/Analysis/ValueLatticeUtils.h"
  21. #include "llvm/Analysis/ValueTracking.h"
  22. #include "llvm/InitializePasses.h"
  23. #include "llvm/IR/Constants.h"
  24. #include "llvm/IR/IntrinsicInst.h"
  25. #include "llvm/Support/CommandLine.h"
  26. #include "llvm/Support/ModRef.h"
  27. #include "llvm/Transforms/IPO.h"
  28. #include "llvm/Transforms/IPO/FunctionSpecialization.h"
  29. #include "llvm/Transforms/Scalar/SCCP.h"
  30. #include "llvm/Transforms/Utils/Local.h"
  31. #include "llvm/Transforms/Utils/SCCPSolver.h"
  32. using namespace llvm;
  33. #define DEBUG_TYPE "sccp"
  34. STATISTIC(NumInstRemoved, "Number of instructions removed");
  35. STATISTIC(NumArgsElimed ,"Number of arguments constant propagated");
  36. STATISTIC(NumGlobalConst, "Number of globals found to be constant");
  37. STATISTIC(NumDeadBlocks , "Number of basic blocks unreachable");
  38. STATISTIC(NumInstReplaced,
  39. "Number of instructions replaced with (simpler) instruction");
  40. static cl::opt<unsigned> FuncSpecializationMaxIters(
  41. "func-specialization-max-iters", cl::init(1), cl::Hidden, cl::desc(
  42. "The maximum number of iterations function specialization is run"));
  43. static void findReturnsToZap(Function &F,
  44. SmallVector<ReturnInst *, 8> &ReturnsToZap,
  45. SCCPSolver &Solver) {
  46. // We can only do this if we know that nothing else can call the function.
  47. if (!Solver.isArgumentTrackedFunction(&F))
  48. return;
  49. if (Solver.mustPreserveReturn(&F)) {
  50. LLVM_DEBUG(
  51. dbgs()
  52. << "Can't zap returns of the function : " << F.getName()
  53. << " due to present musttail or \"clang.arc.attachedcall\" call of "
  54. "it\n");
  55. return;
  56. }
  57. assert(
  58. all_of(F.users(),
  59. [&Solver](User *U) {
  60. if (isa<Instruction>(U) &&
  61. !Solver.isBlockExecutable(cast<Instruction>(U)->getParent()))
  62. return true;
  63. // Non-callsite uses are not impacted by zapping. Also, constant
  64. // uses (like blockaddresses) could stuck around, without being
  65. // used in the underlying IR, meaning we do not have lattice
  66. // values for them.
  67. if (!isa<CallBase>(U))
  68. return true;
  69. if (U->getType()->isStructTy()) {
  70. return all_of(Solver.getStructLatticeValueFor(U),
  71. [](const ValueLatticeElement &LV) {
  72. return !SCCPSolver::isOverdefined(LV);
  73. });
  74. }
  75. // We don't consider assume-like intrinsics to be actual address
  76. // captures.
  77. if (auto *II = dyn_cast<IntrinsicInst>(U)) {
  78. if (II->isAssumeLikeIntrinsic())
  79. return true;
  80. }
  81. return !SCCPSolver::isOverdefined(Solver.getLatticeValueFor(U));
  82. }) &&
  83. "We can only zap functions where all live users have a concrete value");
  84. for (BasicBlock &BB : F) {
  85. if (CallInst *CI = BB.getTerminatingMustTailCall()) {
  86. LLVM_DEBUG(dbgs() << "Can't zap return of the block due to present "
  87. << "musttail call : " << *CI << "\n");
  88. (void)CI;
  89. return;
  90. }
  91. if (auto *RI = dyn_cast<ReturnInst>(BB.getTerminator()))
  92. if (!isa<UndefValue>(RI->getOperand(0)))
  93. ReturnsToZap.push_back(RI);
  94. }
  95. }
  96. static bool runIPSCCP(
  97. Module &M, const DataLayout &DL, FunctionAnalysisManager *FAM,
  98. std::function<const TargetLibraryInfo &(Function &)> GetTLI,
  99. std::function<TargetTransformInfo &(Function &)> GetTTI,
  100. std::function<AssumptionCache &(Function &)> GetAC,
  101. function_ref<AnalysisResultsForFn(Function &)> getAnalysis,
  102. bool IsFuncSpecEnabled) {
  103. SCCPSolver Solver(DL, GetTLI, M.getContext());
  104. FunctionSpecializer Specializer(Solver, M, FAM, GetTLI, GetTTI, GetAC);
  105. // Loop over all functions, marking arguments to those with their addresses
  106. // taken or that are external as overdefined.
  107. for (Function &F : M) {
  108. if (F.isDeclaration())
  109. continue;
  110. Solver.addAnalysis(F, getAnalysis(F));
  111. // Determine if we can track the function's return values. If so, add the
  112. // function to the solver's set of return-tracked functions.
  113. if (canTrackReturnsInterprocedurally(&F))
  114. Solver.addTrackedFunction(&F);
  115. // Determine if we can track the function's arguments. If so, add the
  116. // function to the solver's set of argument-tracked functions.
  117. if (canTrackArgumentsInterprocedurally(&F)) {
  118. Solver.addArgumentTrackedFunction(&F);
  119. continue;
  120. }
  121. // Assume the function is called.
  122. Solver.markBlockExecutable(&F.front());
  123. // Assume nothing about the incoming arguments.
  124. for (Argument &AI : F.args())
  125. Solver.markOverdefined(&AI);
  126. }
  127. // Determine if we can track any of the module's global variables. If so, add
  128. // the global variables we can track to the solver's set of tracked global
  129. // variables.
  130. for (GlobalVariable &G : M.globals()) {
  131. G.removeDeadConstantUsers();
  132. if (canTrackGlobalVariableInterprocedurally(&G))
  133. Solver.trackValueOfGlobalVariable(&G);
  134. }
  135. // Solve for constants.
  136. Solver.solveWhileResolvedUndefsIn(M);
  137. if (IsFuncSpecEnabled) {
  138. unsigned Iters = 0;
  139. while (Iters++ < FuncSpecializationMaxIters && Specializer.run());
  140. }
  141. // Iterate over all of the instructions in the module, replacing them with
  142. // constants if we have found them to be of constant values.
  143. bool MadeChanges = false;
  144. for (Function &F : M) {
  145. if (F.isDeclaration())
  146. continue;
  147. SmallVector<BasicBlock *, 512> BlocksToErase;
  148. if (Solver.isBlockExecutable(&F.front())) {
  149. bool ReplacedPointerArg = false;
  150. for (Argument &Arg : F.args()) {
  151. if (!Arg.use_empty() && Solver.tryToReplaceWithConstant(&Arg)) {
  152. ReplacedPointerArg |= Arg.getType()->isPointerTy();
  153. ++NumArgsElimed;
  154. }
  155. }
  156. // If we replaced an argument, we may now also access a global (currently
  157. // classified as "other" memory). Update memory attribute to reflect this.
  158. if (ReplacedPointerArg) {
  159. auto UpdateAttrs = [&](AttributeList AL) {
  160. MemoryEffects ME = AL.getMemoryEffects();
  161. if (ME == MemoryEffects::unknown())
  162. return AL;
  163. ME |= MemoryEffects(MemoryEffects::Other,
  164. ME.getModRef(MemoryEffects::ArgMem));
  165. return AL.addFnAttribute(
  166. F.getContext(),
  167. Attribute::getWithMemoryEffects(F.getContext(), ME));
  168. };
  169. F.setAttributes(UpdateAttrs(F.getAttributes()));
  170. for (User *U : F.users()) {
  171. auto *CB = dyn_cast<CallBase>(U);
  172. if (!CB || CB->getCalledFunction() != &F)
  173. continue;
  174. CB->setAttributes(UpdateAttrs(CB->getAttributes()));
  175. }
  176. }
  177. MadeChanges |= ReplacedPointerArg;
  178. }
  179. SmallPtrSet<Value *, 32> InsertedValues;
  180. for (BasicBlock &BB : F) {
  181. if (!Solver.isBlockExecutable(&BB)) {
  182. LLVM_DEBUG(dbgs() << " BasicBlock Dead:" << BB);
  183. ++NumDeadBlocks;
  184. MadeChanges = true;
  185. if (&BB != &F.front())
  186. BlocksToErase.push_back(&BB);
  187. continue;
  188. }
  189. MadeChanges |= Solver.simplifyInstsInBlock(
  190. BB, InsertedValues, NumInstRemoved, NumInstReplaced);
  191. }
  192. DomTreeUpdater DTU = IsFuncSpecEnabled && Specializer.isClonedFunction(&F)
  193. ? DomTreeUpdater(DomTreeUpdater::UpdateStrategy::Lazy)
  194. : Solver.getDTU(F);
  195. // Change dead blocks to unreachable. We do it after replacing constants
  196. // in all executable blocks, because changeToUnreachable may remove PHI
  197. // nodes in executable blocks we found values for. The function's entry
  198. // block is not part of BlocksToErase, so we have to handle it separately.
  199. for (BasicBlock *BB : BlocksToErase) {
  200. NumInstRemoved += changeToUnreachable(BB->getFirstNonPHI(),
  201. /*PreserveLCSSA=*/false, &DTU);
  202. }
  203. if (!Solver.isBlockExecutable(&F.front()))
  204. NumInstRemoved += changeToUnreachable(F.front().getFirstNonPHI(),
  205. /*PreserveLCSSA=*/false, &DTU);
  206. BasicBlock *NewUnreachableBB = nullptr;
  207. for (BasicBlock &BB : F)
  208. MadeChanges |= Solver.removeNonFeasibleEdges(&BB, DTU, NewUnreachableBB);
  209. for (BasicBlock *DeadBB : BlocksToErase)
  210. if (!DeadBB->hasAddressTaken())
  211. DTU.deleteBB(DeadBB);
  212. for (BasicBlock &BB : F) {
  213. for (Instruction &Inst : llvm::make_early_inc_range(BB)) {
  214. if (Solver.getPredicateInfoFor(&Inst)) {
  215. if (auto *II = dyn_cast<IntrinsicInst>(&Inst)) {
  216. if (II->getIntrinsicID() == Intrinsic::ssa_copy) {
  217. Value *Op = II->getOperand(0);
  218. Inst.replaceAllUsesWith(Op);
  219. Inst.eraseFromParent();
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }
  226. // If we inferred constant or undef return values for a function, we replaced
  227. // all call uses with the inferred value. This means we don't need to bother
  228. // actually returning anything from the function. Replace all return
  229. // instructions with return undef.
  230. //
  231. // Do this in two stages: first identify the functions we should process, then
  232. // actually zap their returns. This is important because we can only do this
  233. // if the address of the function isn't taken. In cases where a return is the
  234. // last use of a function, the order of processing functions would affect
  235. // whether other functions are optimizable.
  236. SmallVector<ReturnInst*, 8> ReturnsToZap;
  237. for (const auto &I : Solver.getTrackedRetVals()) {
  238. Function *F = I.first;
  239. const ValueLatticeElement &ReturnValue = I.second;
  240. // If there is a known constant range for the return value, add !range
  241. // metadata to the function's call sites.
  242. if (ReturnValue.isConstantRange() &&
  243. !ReturnValue.getConstantRange().isSingleElement()) {
  244. // Do not add range metadata if the return value may include undef.
  245. if (ReturnValue.isConstantRangeIncludingUndef())
  246. continue;
  247. auto &CR = ReturnValue.getConstantRange();
  248. for (User *User : F->users()) {
  249. auto *CB = dyn_cast<CallBase>(User);
  250. if (!CB || CB->getCalledFunction() != F)
  251. continue;
  252. // Limit to cases where the return value is guaranteed to be neither
  253. // poison nor undef. Poison will be outside any range and currently
  254. // values outside of the specified range cause immediate undefined
  255. // behavior.
  256. if (!isGuaranteedNotToBeUndefOrPoison(CB, nullptr, CB))
  257. continue;
  258. // Do not touch existing metadata for now.
  259. // TODO: We should be able to take the intersection of the existing
  260. // metadata and the inferred range.
  261. if (CB->getMetadata(LLVMContext::MD_range))
  262. continue;
  263. LLVMContext &Context = CB->getParent()->getContext();
  264. Metadata *RangeMD[] = {
  265. ConstantAsMetadata::get(ConstantInt::get(Context, CR.getLower())),
  266. ConstantAsMetadata::get(ConstantInt::get(Context, CR.getUpper()))};
  267. CB->setMetadata(LLVMContext::MD_range, MDNode::get(Context, RangeMD));
  268. }
  269. continue;
  270. }
  271. if (F->getReturnType()->isVoidTy())
  272. continue;
  273. if (SCCPSolver::isConstant(ReturnValue) || ReturnValue.isUnknownOrUndef())
  274. findReturnsToZap(*F, ReturnsToZap, Solver);
  275. }
  276. for (auto *F : Solver.getMRVFunctionsTracked()) {
  277. assert(F->getReturnType()->isStructTy() &&
  278. "The return type should be a struct");
  279. StructType *STy = cast<StructType>(F->getReturnType());
  280. if (Solver.isStructLatticeConstant(F, STy))
  281. findReturnsToZap(*F, ReturnsToZap, Solver);
  282. }
  283. // Zap all returns which we've identified as zap to change.
  284. SmallSetVector<Function *, 8> FuncZappedReturn;
  285. for (ReturnInst *RI : ReturnsToZap) {
  286. Function *F = RI->getParent()->getParent();
  287. RI->setOperand(0, UndefValue::get(F->getReturnType()));
  288. // Record all functions that are zapped.
  289. FuncZappedReturn.insert(F);
  290. }
  291. // Remove the returned attribute for zapped functions and the
  292. // corresponding call sites.
  293. for (Function *F : FuncZappedReturn) {
  294. for (Argument &A : F->args())
  295. F->removeParamAttr(A.getArgNo(), Attribute::Returned);
  296. for (Use &U : F->uses()) {
  297. CallBase *CB = dyn_cast<CallBase>(U.getUser());
  298. if (!CB) {
  299. assert(isa<BlockAddress>(U.getUser()) ||
  300. (isa<Constant>(U.getUser()) &&
  301. all_of(U.getUser()->users(), [](const User *UserUser) {
  302. return cast<IntrinsicInst>(UserUser)->isAssumeLikeIntrinsic();
  303. })));
  304. continue;
  305. }
  306. for (Use &Arg : CB->args())
  307. CB->removeParamAttr(CB->getArgOperandNo(&Arg), Attribute::Returned);
  308. }
  309. }
  310. // If we inferred constant or undef values for globals variables, we can
  311. // delete the global and any stores that remain to it.
  312. for (const auto &I : make_early_inc_range(Solver.getTrackedGlobals())) {
  313. GlobalVariable *GV = I.first;
  314. if (SCCPSolver::isOverdefined(I.second))
  315. continue;
  316. LLVM_DEBUG(dbgs() << "Found that GV '" << GV->getName()
  317. << "' is constant!\n");
  318. while (!GV->use_empty()) {
  319. StoreInst *SI = cast<StoreInst>(GV->user_back());
  320. SI->eraseFromParent();
  321. MadeChanges = true;
  322. }
  323. M.getGlobalList().erase(GV);
  324. ++NumGlobalConst;
  325. }
  326. return MadeChanges;
  327. }
  328. PreservedAnalyses IPSCCPPass::run(Module &M, ModuleAnalysisManager &AM) {
  329. const DataLayout &DL = M.getDataLayout();
  330. auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
  331. auto GetTLI = [&FAM](Function &F) -> const TargetLibraryInfo & {
  332. return FAM.getResult<TargetLibraryAnalysis>(F);
  333. };
  334. auto GetTTI = [&FAM](Function &F) -> TargetTransformInfo & {
  335. return FAM.getResult<TargetIRAnalysis>(F);
  336. };
  337. auto GetAC = [&FAM](Function &F) -> AssumptionCache & {
  338. return FAM.getResult<AssumptionAnalysis>(F);
  339. };
  340. auto getAnalysis = [&FAM, this](Function &F) -> AnalysisResultsForFn {
  341. DominatorTree &DT = FAM.getResult<DominatorTreeAnalysis>(F);
  342. return {
  343. std::make_unique<PredicateInfo>(F, DT, FAM.getResult<AssumptionAnalysis>(F)),
  344. &DT, FAM.getCachedResult<PostDominatorTreeAnalysis>(F),
  345. isFuncSpecEnabled() ? &FAM.getResult<LoopAnalysis>(F) : nullptr };
  346. };
  347. if (!runIPSCCP(M, DL, &FAM, GetTLI, GetTTI, GetAC, getAnalysis,
  348. isFuncSpecEnabled()))
  349. return PreservedAnalyses::all();
  350. PreservedAnalyses PA;
  351. PA.preserve<DominatorTreeAnalysis>();
  352. PA.preserve<PostDominatorTreeAnalysis>();
  353. PA.preserve<FunctionAnalysisManagerModuleProxy>();
  354. return PA;
  355. }
  356. namespace {
  357. //===--------------------------------------------------------------------===//
  358. //
  359. /// IPSCCP Class - This class implements interprocedural Sparse Conditional
  360. /// Constant Propagation.
  361. ///
  362. class IPSCCPLegacyPass : public ModulePass {
  363. public:
  364. static char ID;
  365. IPSCCPLegacyPass() : ModulePass(ID) {
  366. initializeIPSCCPLegacyPassPass(*PassRegistry::getPassRegistry());
  367. }
  368. bool runOnModule(Module &M) override {
  369. if (skipModule(M))
  370. return false;
  371. const DataLayout &DL = M.getDataLayout();
  372. auto GetTLI = [this](Function &F) -> const TargetLibraryInfo & {
  373. return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
  374. };
  375. auto GetTTI = [this](Function &F) -> TargetTransformInfo & {
  376. return this->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
  377. };
  378. auto GetAC = [this](Function &F) -> AssumptionCache & {
  379. return this->getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
  380. };
  381. auto getAnalysis = [this](Function &F) -> AnalysisResultsForFn {
  382. DominatorTree &DT =
  383. this->getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
  384. return {
  385. std::make_unique<PredicateInfo>(
  386. F, DT,
  387. this->getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
  388. F)),
  389. nullptr, // We cannot preserve the LI, DT or PDT with the legacy pass
  390. nullptr, // manager, so set them to nullptr.
  391. nullptr};
  392. };
  393. return runIPSCCP(M, DL, nullptr, GetTLI, GetTTI, GetAC, getAnalysis, false);
  394. }
  395. void getAnalysisUsage(AnalysisUsage &AU) const override {
  396. AU.addRequired<AssumptionCacheTracker>();
  397. AU.addRequired<DominatorTreeWrapperPass>();
  398. AU.addRequired<TargetLibraryInfoWrapperPass>();
  399. AU.addRequired<TargetTransformInfoWrapperPass>();
  400. }
  401. };
  402. } // end anonymous namespace
  403. char IPSCCPLegacyPass::ID = 0;
  404. INITIALIZE_PASS_BEGIN(IPSCCPLegacyPass, "ipsccp",
  405. "Interprocedural Sparse Conditional Constant Propagation",
  406. false, false)
  407. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  408. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  409. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  410. INITIALIZE_PASS_END(IPSCCPLegacyPass, "ipsccp",
  411. "Interprocedural Sparse Conditional Constant Propagation",
  412. false, false)
  413. // createIPSCCPPass - This is the public interface to this file.
  414. ModulePass *llvm::createIPSCCPPass() { return new IPSCCPLegacyPass(); }