SimplifyCFGPass.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. //===- SimplifyCFGPass.cpp - CFG Simplification Pass ----------------------===//
  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 dead code elimination and basic block merging, along
  10. // with a collection of other peephole control flow optimizations. For example:
  11. //
  12. // * Removes basic blocks with no predecessors.
  13. // * Merges a basic block into its predecessor if there is only one and the
  14. // predecessor only has one successor.
  15. // * Eliminates PHI nodes for basic blocks with a single predecessor.
  16. // * Eliminates a basic block that only contains an unconditional branch.
  17. // * Changes invoke instructions to nounwind functions to be calls.
  18. // * Change things like "if (x) if (y)" into "if (x&y)".
  19. // * etc..
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #include "llvm/ADT/MapVector.h"
  23. #include "llvm/ADT/SmallPtrSet.h"
  24. #include "llvm/ADT/SmallVector.h"
  25. #include "llvm/ADT/Statistic.h"
  26. #include "llvm/Analysis/AssumptionCache.h"
  27. #include "llvm/Analysis/CFG.h"
  28. #include "llvm/Analysis/DomTreeUpdater.h"
  29. #include "llvm/Analysis/GlobalsModRef.h"
  30. #include "llvm/Analysis/TargetTransformInfo.h"
  31. #include "llvm/IR/Attributes.h"
  32. #include "llvm/IR/CFG.h"
  33. #include "llvm/IR/Constants.h"
  34. #include "llvm/IR/DataLayout.h"
  35. #include "llvm/IR/Dominators.h"
  36. #include "llvm/IR/Instructions.h"
  37. #include "llvm/IR/IntrinsicInst.h"
  38. #include "llvm/IR/Module.h"
  39. #include "llvm/IR/ValueHandle.h"
  40. #include "llvm/InitializePasses.h"
  41. #include "llvm/Pass.h"
  42. #include "llvm/Support/CommandLine.h"
  43. #include "llvm/Transforms/Scalar.h"
  44. #include "llvm/Transforms/Scalar/SimplifyCFG.h"
  45. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  46. #include "llvm/Transforms/Utils/Local.h"
  47. #include "llvm/Transforms/Utils/SimplifyCFGOptions.h"
  48. #include <utility>
  49. using namespace llvm;
  50. #define DEBUG_TYPE "simplifycfg"
  51. static cl::opt<unsigned> UserBonusInstThreshold(
  52. "bonus-inst-threshold", cl::Hidden, cl::init(1),
  53. cl::desc("Control the number of bonus instructions (default = 1)"));
  54. static cl::opt<bool> UserKeepLoops(
  55. "keep-loops", cl::Hidden, cl::init(true),
  56. cl::desc("Preserve canonical loop structure (default = true)"));
  57. static cl::opt<bool> UserSwitchRangeToICmp(
  58. "switch-range-to-icmp", cl::Hidden, cl::init(false),
  59. cl::desc(
  60. "Convert switches into an integer range comparison (default = false)"));
  61. static cl::opt<bool> UserSwitchToLookup(
  62. "switch-to-lookup", cl::Hidden, cl::init(false),
  63. cl::desc("Convert switches to lookup tables (default = false)"));
  64. static cl::opt<bool> UserForwardSwitchCond(
  65. "forward-switch-cond", cl::Hidden, cl::init(false),
  66. cl::desc("Forward switch condition to phi ops (default = false)"));
  67. static cl::opt<bool> UserHoistCommonInsts(
  68. "hoist-common-insts", cl::Hidden, cl::init(false),
  69. cl::desc("hoist common instructions (default = false)"));
  70. static cl::opt<bool> UserSinkCommonInsts(
  71. "sink-common-insts", cl::Hidden, cl::init(false),
  72. cl::desc("Sink common instructions (default = false)"));
  73. STATISTIC(NumSimpl, "Number of blocks simplified");
  74. static bool
  75. performBlockTailMerging(Function &F, ArrayRef<BasicBlock *> BBs,
  76. std::vector<DominatorTree::UpdateType> *Updates) {
  77. SmallVector<PHINode *, 1> NewOps;
  78. // We don't want to change IR just because we can.
  79. // Only do that if there are at least two blocks we'll tail-merge.
  80. if (BBs.size() < 2)
  81. return false;
  82. if (Updates)
  83. Updates->reserve(Updates->size() + BBs.size());
  84. BasicBlock *CanonicalBB;
  85. Instruction *CanonicalTerm;
  86. {
  87. auto *Term = BBs[0]->getTerminator();
  88. // Create a canonical block for this function terminator type now,
  89. // placing it *before* the first block that will branch to it.
  90. CanonicalBB = BasicBlock::Create(
  91. F.getContext(), Twine("common.") + Term->getOpcodeName(), &F, BBs[0]);
  92. // We'll also need a PHI node per each operand of the terminator.
  93. NewOps.resize(Term->getNumOperands());
  94. for (auto I : zip(Term->operands(), NewOps)) {
  95. std::get<1>(I) = PHINode::Create(std::get<0>(I)->getType(),
  96. /*NumReservedValues=*/BBs.size(),
  97. CanonicalBB->getName() + ".op");
  98. CanonicalBB->getInstList().push_back(std::get<1>(I));
  99. }
  100. // Make it so that this canonical block actually has the right
  101. // terminator.
  102. CanonicalTerm = Term->clone();
  103. CanonicalBB->getInstList().push_back(CanonicalTerm);
  104. // If the canonical terminator has operands, rewrite it to take PHI's.
  105. for (auto I : zip(NewOps, CanonicalTerm->operands()))
  106. std::get<1>(I) = std::get<0>(I);
  107. }
  108. // Now, go through each block (with the current terminator type)
  109. // we've recorded, and rewrite it to branch to the new common block.
  110. const DILocation *CommonDebugLoc = nullptr;
  111. for (BasicBlock *BB : BBs) {
  112. auto *Term = BB->getTerminator();
  113. assert(Term->getOpcode() == CanonicalTerm->getOpcode() &&
  114. "All blocks to be tail-merged must be the same "
  115. "(function-terminating) terminator type.");
  116. // Aha, found a new non-canonical function terminator. If it has operands,
  117. // forward them to the PHI nodes in the canonical block.
  118. for (auto I : zip(Term->operands(), NewOps))
  119. std::get<1>(I)->addIncoming(std::get<0>(I), BB);
  120. // Compute the debug location common to all the original terminators.
  121. if (!CommonDebugLoc)
  122. CommonDebugLoc = Term->getDebugLoc();
  123. else
  124. CommonDebugLoc =
  125. DILocation::getMergedLocation(CommonDebugLoc, Term->getDebugLoc());
  126. // And turn BB into a block that just unconditionally branches
  127. // to the canonical block.
  128. Term->eraseFromParent();
  129. BranchInst::Create(CanonicalBB, BB);
  130. if (Updates)
  131. Updates->push_back({DominatorTree::Insert, BB, CanonicalBB});
  132. }
  133. CanonicalTerm->setDebugLoc(CommonDebugLoc);
  134. return true;
  135. }
  136. static bool tailMergeBlocksWithSimilarFunctionTerminators(Function &F,
  137. DomTreeUpdater *DTU) {
  138. SmallMapVector<unsigned /*TerminatorOpcode*/, SmallVector<BasicBlock *, 2>, 4>
  139. Structure;
  140. // Scan all the blocks in the function, record the interesting-ones.
  141. for (BasicBlock &BB : F) {
  142. if (DTU && DTU->isBBPendingDeletion(&BB))
  143. continue;
  144. // We are only interested in function-terminating blocks.
  145. if (!succ_empty(&BB))
  146. continue;
  147. auto *Term = BB.getTerminator();
  148. // Fow now only support `ret`/`resume` function terminators.
  149. // FIXME: lift this restriction.
  150. switch (Term->getOpcode()) {
  151. case Instruction::Ret:
  152. case Instruction::Resume:
  153. break;
  154. default:
  155. continue;
  156. }
  157. // We can't tail-merge block that contains a musttail call.
  158. if (BB.getTerminatingMustTailCall())
  159. continue;
  160. // Calls to experimental_deoptimize must be followed by a return
  161. // of the value computed by experimental_deoptimize.
  162. // I.e., we can not change `ret` to `br` for this block.
  163. if (auto *CI =
  164. dyn_cast_or_null<CallInst>(Term->getPrevNonDebugInstruction())) {
  165. if (Function *F = CI->getCalledFunction())
  166. if (Intrinsic::ID ID = F->getIntrinsicID())
  167. if (ID == Intrinsic::experimental_deoptimize)
  168. continue;
  169. }
  170. // PHI nodes cannot have token type, so if the terminator has an operand
  171. // with token type, we can not tail-merge this kind of function terminators.
  172. if (any_of(Term->operands(),
  173. [](Value *Op) { return Op->getType()->isTokenTy(); }))
  174. continue;
  175. // Canonical blocks are uniqued based on the terminator type (opcode).
  176. Structure[Term->getOpcode()].emplace_back(&BB);
  177. }
  178. bool Changed = false;
  179. std::vector<DominatorTree::UpdateType> Updates;
  180. for (ArrayRef<BasicBlock *> BBs : make_second_range(Structure))
  181. Changed |= performBlockTailMerging(F, BBs, DTU ? &Updates : nullptr);
  182. if (DTU)
  183. DTU->applyUpdates(Updates);
  184. return Changed;
  185. }
  186. /// Call SimplifyCFG on all the blocks in the function,
  187. /// iterating until no more changes are made.
  188. static bool iterativelySimplifyCFG(Function &F, const TargetTransformInfo &TTI,
  189. DomTreeUpdater *DTU,
  190. const SimplifyCFGOptions &Options) {
  191. bool Changed = false;
  192. bool LocalChange = true;
  193. SmallVector<std::pair<const BasicBlock *, const BasicBlock *>, 32> Edges;
  194. FindFunctionBackedges(F, Edges);
  195. SmallPtrSet<BasicBlock *, 16> UniqueLoopHeaders;
  196. for (unsigned i = 0, e = Edges.size(); i != e; ++i)
  197. UniqueLoopHeaders.insert(const_cast<BasicBlock *>(Edges[i].second));
  198. SmallVector<WeakVH, 16> LoopHeaders(UniqueLoopHeaders.begin(),
  199. UniqueLoopHeaders.end());
  200. unsigned IterCnt = 0;
  201. (void)IterCnt;
  202. while (LocalChange) {
  203. assert(IterCnt++ < 1000 && "Iterative simplification didn't converge!");
  204. LocalChange = false;
  205. // Loop over all of the basic blocks and remove them if they are unneeded.
  206. for (Function::iterator BBIt = F.begin(); BBIt != F.end(); ) {
  207. BasicBlock &BB = *BBIt++;
  208. if (DTU) {
  209. assert(
  210. !DTU->isBBPendingDeletion(&BB) &&
  211. "Should not end up trying to simplify blocks marked for removal.");
  212. // Make sure that the advanced iterator does not point at the blocks
  213. // that are marked for removal, skip over all such blocks.
  214. while (BBIt != F.end() && DTU->isBBPendingDeletion(&*BBIt))
  215. ++BBIt;
  216. }
  217. if (simplifyCFG(&BB, TTI, DTU, Options, LoopHeaders)) {
  218. LocalChange = true;
  219. ++NumSimpl;
  220. }
  221. }
  222. Changed |= LocalChange;
  223. }
  224. return Changed;
  225. }
  226. static bool simplifyFunctionCFGImpl(Function &F, const TargetTransformInfo &TTI,
  227. DominatorTree *DT,
  228. const SimplifyCFGOptions &Options) {
  229. DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
  230. bool EverChanged = removeUnreachableBlocks(F, DT ? &DTU : nullptr);
  231. EverChanged |=
  232. tailMergeBlocksWithSimilarFunctionTerminators(F, DT ? &DTU : nullptr);
  233. EverChanged |= iterativelySimplifyCFG(F, TTI, DT ? &DTU : nullptr, Options);
  234. // If neither pass changed anything, we're done.
  235. if (!EverChanged) return false;
  236. // iterativelySimplifyCFG can (rarely) make some loops dead. If this happens,
  237. // removeUnreachableBlocks is needed to nuke them, which means we should
  238. // iterate between the two optimizations. We structure the code like this to
  239. // avoid rerunning iterativelySimplifyCFG if the second pass of
  240. // removeUnreachableBlocks doesn't do anything.
  241. if (!removeUnreachableBlocks(F, DT ? &DTU : nullptr))
  242. return true;
  243. do {
  244. EverChanged = iterativelySimplifyCFG(F, TTI, DT ? &DTU : nullptr, Options);
  245. EverChanged |= removeUnreachableBlocks(F, DT ? &DTU : nullptr);
  246. } while (EverChanged);
  247. return true;
  248. }
  249. static bool simplifyFunctionCFG(Function &F, const TargetTransformInfo &TTI,
  250. DominatorTree *DT,
  251. const SimplifyCFGOptions &Options) {
  252. assert((!RequireAndPreserveDomTree ||
  253. (DT && DT->verify(DominatorTree::VerificationLevel::Full))) &&
  254. "Original domtree is invalid?");
  255. bool Changed = simplifyFunctionCFGImpl(F, TTI, DT, Options);
  256. assert((!RequireAndPreserveDomTree ||
  257. (DT && DT->verify(DominatorTree::VerificationLevel::Full))) &&
  258. "Failed to maintain validity of domtree!");
  259. return Changed;
  260. }
  261. // Command-line settings override compile-time settings.
  262. static void applyCommandLineOverridesToOptions(SimplifyCFGOptions &Options) {
  263. if (UserBonusInstThreshold.getNumOccurrences())
  264. Options.BonusInstThreshold = UserBonusInstThreshold;
  265. if (UserForwardSwitchCond.getNumOccurrences())
  266. Options.ForwardSwitchCondToPhi = UserForwardSwitchCond;
  267. if (UserSwitchRangeToICmp.getNumOccurrences())
  268. Options.ConvertSwitchRangeToICmp = UserSwitchRangeToICmp;
  269. if (UserSwitchToLookup.getNumOccurrences())
  270. Options.ConvertSwitchToLookupTable = UserSwitchToLookup;
  271. if (UserKeepLoops.getNumOccurrences())
  272. Options.NeedCanonicalLoop = UserKeepLoops;
  273. if (UserHoistCommonInsts.getNumOccurrences())
  274. Options.HoistCommonInsts = UserHoistCommonInsts;
  275. if (UserSinkCommonInsts.getNumOccurrences())
  276. Options.SinkCommonInsts = UserSinkCommonInsts;
  277. }
  278. SimplifyCFGPass::SimplifyCFGPass() {
  279. applyCommandLineOverridesToOptions(Options);
  280. }
  281. SimplifyCFGPass::SimplifyCFGPass(const SimplifyCFGOptions &Opts)
  282. : Options(Opts) {
  283. applyCommandLineOverridesToOptions(Options);
  284. }
  285. void SimplifyCFGPass::printPipeline(
  286. raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
  287. static_cast<PassInfoMixin<SimplifyCFGPass> *>(this)->printPipeline(
  288. OS, MapClassName2PassName);
  289. OS << "<";
  290. OS << "bonus-inst-threshold=" << Options.BonusInstThreshold << ";";
  291. OS << (Options.ForwardSwitchCondToPhi ? "" : "no-") << "forward-switch-cond;";
  292. OS << (Options.ConvertSwitchRangeToICmp ? "" : "no-")
  293. << "switch-range-to-icmp;";
  294. OS << (Options.ConvertSwitchToLookupTable ? "" : "no-")
  295. << "switch-to-lookup;";
  296. OS << (Options.NeedCanonicalLoop ? "" : "no-") << "keep-loops;";
  297. OS << (Options.HoistCommonInsts ? "" : "no-") << "hoist-common-insts;";
  298. OS << (Options.SinkCommonInsts ? "" : "no-") << "sink-common-insts";
  299. OS << ">";
  300. }
  301. PreservedAnalyses SimplifyCFGPass::run(Function &F,
  302. FunctionAnalysisManager &AM) {
  303. auto &TTI = AM.getResult<TargetIRAnalysis>(F);
  304. Options.AC = &AM.getResult<AssumptionAnalysis>(F);
  305. DominatorTree *DT = nullptr;
  306. if (RequireAndPreserveDomTree)
  307. DT = &AM.getResult<DominatorTreeAnalysis>(F);
  308. if (F.hasFnAttribute(Attribute::OptForFuzzing)) {
  309. Options.setSimplifyCondBranch(false).setFoldTwoEntryPHINode(false);
  310. } else {
  311. Options.setSimplifyCondBranch(true).setFoldTwoEntryPHINode(true);
  312. }
  313. if (!simplifyFunctionCFG(F, TTI, DT, Options))
  314. return PreservedAnalyses::all();
  315. PreservedAnalyses PA;
  316. if (RequireAndPreserveDomTree)
  317. PA.preserve<DominatorTreeAnalysis>();
  318. return PA;
  319. }
  320. namespace {
  321. struct CFGSimplifyPass : public FunctionPass {
  322. static char ID;
  323. SimplifyCFGOptions Options;
  324. std::function<bool(const Function &)> PredicateFtor;
  325. CFGSimplifyPass(SimplifyCFGOptions Options_ = SimplifyCFGOptions(),
  326. std::function<bool(const Function &)> Ftor = nullptr)
  327. : FunctionPass(ID), Options(Options_), PredicateFtor(std::move(Ftor)) {
  328. initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry());
  329. // Check for command-line overrides of options for debug/customization.
  330. applyCommandLineOverridesToOptions(Options);
  331. }
  332. bool runOnFunction(Function &F) override {
  333. if (skipFunction(F) || (PredicateFtor && !PredicateFtor(F)))
  334. return false;
  335. Options.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
  336. DominatorTree *DT = nullptr;
  337. if (RequireAndPreserveDomTree)
  338. DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  339. if (F.hasFnAttribute(Attribute::OptForFuzzing)) {
  340. Options.setSimplifyCondBranch(false)
  341. .setFoldTwoEntryPHINode(false);
  342. } else {
  343. Options.setSimplifyCondBranch(true)
  344. .setFoldTwoEntryPHINode(true);
  345. }
  346. auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
  347. return simplifyFunctionCFG(F, TTI, DT, Options);
  348. }
  349. void getAnalysisUsage(AnalysisUsage &AU) const override {
  350. AU.addRequired<AssumptionCacheTracker>();
  351. if (RequireAndPreserveDomTree)
  352. AU.addRequired<DominatorTreeWrapperPass>();
  353. AU.addRequired<TargetTransformInfoWrapperPass>();
  354. if (RequireAndPreserveDomTree)
  355. AU.addPreserved<DominatorTreeWrapperPass>();
  356. AU.addPreserved<GlobalsAAWrapperPass>();
  357. }
  358. };
  359. }
  360. char CFGSimplifyPass::ID = 0;
  361. INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
  362. false)
  363. INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
  364. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  365. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  366. INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
  367. false)
  368. // Public interface to the CFGSimplification pass
  369. FunctionPass *
  370. llvm::createCFGSimplificationPass(SimplifyCFGOptions Options,
  371. std::function<bool(const Function &)> Ftor) {
  372. return new CFGSimplifyPass(Options, std::move(Ftor));
  373. }