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