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