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