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