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