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