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