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