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