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 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/Local.h" 46 #include "llvm/Transforms/Utils/SimplifyCFGOptions.h" 47 #include <utility> 48 using namespace llvm; 49 50 #define DEBUG_TYPE "simplifycfg" 51 52 static cl::opt<unsigned> UserBonusInstThreshold( 53 "bonus-inst-threshold", cl::Hidden, cl::init(1), 54 cl::desc("Control the number of bonus instructions (default = 1)")); 55 56 static cl::opt<bool> UserKeepLoops( 57 "keep-loops", cl::Hidden, cl::init(true), 58 cl::desc("Preserve canonical loop structure (default = true)")); 59 60 static cl::opt<bool> UserSwitchToLookup( 61 "switch-to-lookup", cl::Hidden, cl::init(false), 62 cl::desc("Convert switches to lookup tables (default = false)")); 63 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 68 static cl::opt<bool> UserHoistCommonInsts( 69 "hoist-common-insts", cl::Hidden, cl::init(false), 70 cl::desc("hoist common instructions (default = false)")); 71 72 static cl::opt<bool> UserSinkCommonInsts( 73 "sink-common-insts", cl::Hidden, cl::init(false), 74 cl::desc("Sink common instructions (default = false)")); 75 76 77 STATISTIC(NumSimpl, "Number of blocks simplified"); 78 79 /// If we have more than one empty (other than phi node) return blocks, 80 /// merge them together to promote recursive block merging. 81 static bool mergeEmptyReturnBlocks(Function &F, DomTreeUpdater *DTU) { 82 bool Changed = false; 83 84 std::vector<DominatorTree::UpdateType> Updates; 85 SmallVector<BasicBlock *, 8> DeadBlocks; 86 87 BasicBlock *RetBlock = nullptr; 88 89 // Scan all the blocks in the function, looking for empty return blocks. 90 for (BasicBlock &BB : make_early_inc_range(F)) { 91 if (DTU && DTU->isBBPendingDeletion(&BB)) 92 continue; 93 94 // Only look at return blocks. 95 ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()); 96 if (!Ret) continue; 97 98 // Only look at the block if it is empty or the only other thing in it is a 99 // single PHI node that is the operand to the return. 100 if (Ret != &BB.front()) { 101 // Check for something else in the block. 102 BasicBlock::iterator I(Ret); 103 --I; 104 // Skip over debug info. 105 while (isa<DbgInfoIntrinsic>(I) && I != BB.begin()) 106 --I; 107 if (!isa<DbgInfoIntrinsic>(I) && 108 (!isa<PHINode>(I) || I != BB.begin() || Ret->getNumOperands() == 0 || 109 Ret->getOperand(0) != &*I)) 110 continue; 111 } 112 113 // If this is the first returning block, remember it and keep going. 114 if (!RetBlock) { 115 RetBlock = &BB; 116 continue; 117 } 118 119 // Skip merging if this would result in a CallBr instruction with a 120 // duplicate destination. FIXME: See note in CodeGenPrepare.cpp. 121 bool SkipCallBr = false; 122 for (pred_iterator PI = pred_begin(&BB), E = pred_end(&BB); 123 PI != E && !SkipCallBr; ++PI) { 124 if (auto *CBI = dyn_cast<CallBrInst>((*PI)->getTerminator())) 125 for (unsigned i = 0, e = CBI->getNumSuccessors(); i != e; ++i) 126 if (RetBlock == CBI->getSuccessor(i)) { 127 SkipCallBr = true; 128 break; 129 } 130 } 131 if (SkipCallBr) 132 continue; 133 134 // Otherwise, we found a duplicate return block. Merge the two. 135 Changed = true; 136 137 // Case when there is no input to the return or when the returned values 138 // agree is trivial. Note that they can't agree if there are phis in the 139 // blocks. 140 if (Ret->getNumOperands() == 0 || 141 Ret->getOperand(0) == 142 cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0)) { 143 // All predecessors of BB should now branch to RetBlock instead. 144 if (DTU) { 145 SmallPtrSet<BasicBlock *, 2> PredsOfBB(pred_begin(&BB), pred_end(&BB)); 146 SmallPtrSet<BasicBlock *, 2> PredsOfRetBlock(pred_begin(RetBlock), 147 pred_end(RetBlock)); 148 Updates.reserve(Updates.size() + 2 * PredsOfBB.size()); 149 for (auto *Predecessor : PredsOfBB) 150 // But, iff Predecessor already branches to RetBlock, 151 // don't (re-)add DomTree edge, because it already exists. 152 if (!PredsOfRetBlock.contains(Predecessor)) 153 Updates.push_back({DominatorTree::Insert, Predecessor, RetBlock}); 154 for (auto *Predecessor : PredsOfBB) 155 Updates.push_back({DominatorTree::Delete, Predecessor, &BB}); 156 } 157 BB.replaceAllUsesWith(RetBlock); 158 DeadBlocks.emplace_back(&BB); 159 continue; 160 } 161 162 // If the canonical return block has no PHI node, create one now. 163 PHINode *RetBlockPHI = dyn_cast<PHINode>(RetBlock->begin()); 164 if (!RetBlockPHI) { 165 Value *InVal = cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0); 166 pred_iterator PB = pred_begin(RetBlock), PE = pred_end(RetBlock); 167 RetBlockPHI = PHINode::Create(Ret->getOperand(0)->getType(), 168 std::distance(PB, PE), "merge", 169 &RetBlock->front()); 170 171 for (pred_iterator PI = PB; PI != PE; ++PI) 172 RetBlockPHI->addIncoming(InVal, *PI); 173 RetBlock->getTerminator()->setOperand(0, RetBlockPHI); 174 } 175 176 // Turn BB into a block that just unconditionally branches to the return 177 // block. This handles the case when the two return blocks have a common 178 // predecessor but that return different things. 179 RetBlockPHI->addIncoming(Ret->getOperand(0), &BB); 180 BB.getTerminator()->eraseFromParent(); 181 BranchInst::Create(RetBlock, &BB); 182 if (DTU) 183 Updates.push_back({DominatorTree::Insert, &BB, RetBlock}); 184 } 185 186 if (DTU) { 187 DTU->applyUpdates(Updates); 188 for (auto *BB : DeadBlocks) 189 DTU->deleteBB(BB); 190 } else { 191 for (auto *BB : DeadBlocks) 192 BB->eraseFromParent(); 193 } 194 195 return Changed; 196 } 197 198 /// Call SimplifyCFG on all the blocks in the function, 199 /// iterating until no more changes are made. 200 static bool iterativelySimplifyCFG(Function &F, const TargetTransformInfo &TTI, 201 DomTreeUpdater *DTU, 202 const SimplifyCFGOptions &Options) { 203 bool Changed = false; 204 bool LocalChange = true; 205 206 SmallVector<std::pair<const BasicBlock *, const BasicBlock *>, 32> Edges; 207 FindFunctionBackedges(F, Edges); 208 SmallPtrSet<BasicBlock *, 16> UniqueLoopHeaders; 209 for (unsigned i = 0, e = Edges.size(); i != e; ++i) 210 UniqueLoopHeaders.insert(const_cast<BasicBlock *>(Edges[i].second)); 211 212 SmallVector<WeakVH, 16> LoopHeaders(UniqueLoopHeaders.begin(), 213 UniqueLoopHeaders.end()); 214 215 while (LocalChange) { 216 LocalChange = false; 217 218 // Loop over all of the basic blocks and remove them if they are unneeded. 219 for (Function::iterator BBIt = F.begin(); BBIt != F.end(); ) { 220 BasicBlock &BB = *BBIt++; 221 if (DTU) { 222 assert( 223 !DTU->isBBPendingDeletion(&BB) && 224 "Should not end up trying to simplify blocks marked for removal."); 225 // Make sure that the advanced iterator does not point at the blocks 226 // that are marked for removal, skip over all such blocks. 227 while (BBIt != F.end() && DTU->isBBPendingDeletion(&*BBIt)) 228 ++BBIt; 229 } 230 if (simplifyCFG(&BB, TTI, DTU, Options, LoopHeaders)) { 231 LocalChange = true; 232 ++NumSimpl; 233 } 234 } 235 Changed |= LocalChange; 236 } 237 return Changed; 238 } 239 240 static bool simplifyFunctionCFGImpl(Function &F, const TargetTransformInfo &TTI, 241 DominatorTree *DT, 242 const SimplifyCFGOptions &Options) { 243 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); 244 245 bool EverChanged = removeUnreachableBlocks(F, DT ? &DTU : nullptr); 246 EverChanged |= mergeEmptyReturnBlocks(F, DT ? &DTU : nullptr); 247 EverChanged |= iterativelySimplifyCFG(F, TTI, DT ? &DTU : nullptr, Options); 248 249 // If neither pass changed anything, we're done. 250 if (!EverChanged) return false; 251 252 // iterativelySimplifyCFG can (rarely) make some loops dead. If this happens, 253 // removeUnreachableBlocks is needed to nuke them, which means we should 254 // iterate between the two optimizations. We structure the code like this to 255 // avoid rerunning iterativelySimplifyCFG if the second pass of 256 // removeUnreachableBlocks doesn't do anything. 257 if (!removeUnreachableBlocks(F, DT ? &DTU : nullptr)) 258 return true; 259 260 do { 261 EverChanged = iterativelySimplifyCFG(F, TTI, DT ? &DTU : nullptr, Options); 262 EverChanged |= removeUnreachableBlocks(F, DT ? &DTU : nullptr); 263 } while (EverChanged); 264 265 return true; 266 } 267 268 static bool simplifyFunctionCFG(Function &F, const TargetTransformInfo &TTI, 269 DominatorTree *DT, 270 const SimplifyCFGOptions &Options) { 271 assert((!RequireAndPreserveDomTree || 272 (DT && DT->verify(DominatorTree::VerificationLevel::Full))) && 273 "Original domtree is invalid?"); 274 275 bool Changed = simplifyFunctionCFGImpl(F, TTI, DT, Options); 276 277 assert((!RequireAndPreserveDomTree || 278 (DT && DT->verify(DominatorTree::VerificationLevel::Full))) && 279 "Failed to maintain validity of domtree!"); 280 281 return Changed; 282 } 283 284 // Command-line settings override compile-time settings. 285 static void applyCommandLineOverridesToOptions(SimplifyCFGOptions &Options) { 286 if (UserBonusInstThreshold.getNumOccurrences()) 287 Options.BonusInstThreshold = UserBonusInstThreshold; 288 if (UserForwardSwitchCond.getNumOccurrences()) 289 Options.ForwardSwitchCondToPhi = UserForwardSwitchCond; 290 if (UserSwitchToLookup.getNumOccurrences()) 291 Options.ConvertSwitchToLookupTable = UserSwitchToLookup; 292 if (UserKeepLoops.getNumOccurrences()) 293 Options.NeedCanonicalLoop = UserKeepLoops; 294 if (UserHoistCommonInsts.getNumOccurrences()) 295 Options.HoistCommonInsts = UserHoistCommonInsts; 296 if (UserSinkCommonInsts.getNumOccurrences()) 297 Options.SinkCommonInsts = UserSinkCommonInsts; 298 } 299 300 SimplifyCFGPass::SimplifyCFGPass() : Options() { 301 applyCommandLineOverridesToOptions(Options); 302 } 303 304 SimplifyCFGPass::SimplifyCFGPass(const SimplifyCFGOptions &Opts) 305 : Options(Opts) { 306 applyCommandLineOverridesToOptions(Options); 307 } 308 309 PreservedAnalyses SimplifyCFGPass::run(Function &F, 310 FunctionAnalysisManager &AM) { 311 auto &TTI = AM.getResult<TargetIRAnalysis>(F); 312 Options.AC = &AM.getResult<AssumptionAnalysis>(F); 313 DominatorTree *DT = nullptr; 314 if (RequireAndPreserveDomTree) 315 DT = &AM.getResult<DominatorTreeAnalysis>(F); 316 if (F.hasFnAttribute(Attribute::OptForFuzzing)) { 317 Options.setSimplifyCondBranch(false).setFoldTwoEntryPHINode(false); 318 } else { 319 Options.setSimplifyCondBranch(true).setFoldTwoEntryPHINode(true); 320 } 321 if (!simplifyFunctionCFG(F, TTI, DT, Options)) 322 return PreservedAnalyses::all(); 323 PreservedAnalyses PA; 324 if (RequireAndPreserveDomTree) 325 PA.preserve<DominatorTreeAnalysis>(); 326 PA.preserve<GlobalsAA>(); 327 return PA; 328 } 329 330 namespace { 331 struct CFGSimplifyPass : public FunctionPass { 332 static char ID; 333 SimplifyCFGOptions Options; 334 std::function<bool(const Function &)> PredicateFtor; 335 336 CFGSimplifyPass(SimplifyCFGOptions Options_ = SimplifyCFGOptions(), 337 std::function<bool(const Function &)> Ftor = nullptr) 338 : FunctionPass(ID), Options(Options_), PredicateFtor(std::move(Ftor)) { 339 340 initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry()); 341 342 // Check for command-line overrides of options for debug/customization. 343 applyCommandLineOverridesToOptions(Options); 344 } 345 346 bool runOnFunction(Function &F) override { 347 if (skipFunction(F) || (PredicateFtor && !PredicateFtor(F))) 348 return false; 349 350 Options.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); 351 DominatorTree *DT = nullptr; 352 if (RequireAndPreserveDomTree) 353 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 354 if (F.hasFnAttribute(Attribute::OptForFuzzing)) { 355 Options.setSimplifyCondBranch(false) 356 .setFoldTwoEntryPHINode(false); 357 } else { 358 Options.setSimplifyCondBranch(true) 359 .setFoldTwoEntryPHINode(true); 360 } 361 362 auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); 363 return simplifyFunctionCFG(F, TTI, DT, Options); 364 } 365 void getAnalysisUsage(AnalysisUsage &AU) const override { 366 AU.addRequired<AssumptionCacheTracker>(); 367 if (RequireAndPreserveDomTree) 368 AU.addRequired<DominatorTreeWrapperPass>(); 369 AU.addRequired<TargetTransformInfoWrapperPass>(); 370 if (RequireAndPreserveDomTree) 371 AU.addPreserved<DominatorTreeWrapperPass>(); 372 AU.addPreserved<GlobalsAAWrapperPass>(); 373 } 374 }; 375 } 376 377 char CFGSimplifyPass::ID = 0; 378 INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false, 379 false) 380 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) 381 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 382 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 383 INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false, 384 false) 385 386 // Public interface to the CFGSimplification pass 387 FunctionPass * 388 llvm::createCFGSimplificationPass(SimplifyCFGOptions Options, 389 std::function<bool(const Function &)> Ftor) { 390 return new CFGSimplifyPass(Options, std::move(Ftor)); 391 } 392