blob: 4544975a4887b84c7b5f79b9e98dcf9beb0d6051 [file] [log] [blame]
Chris Lattnere15051d2008-05-14 20:38:44 +00001//===- SimplifyCFGPass.cpp - CFG Simplification Pass ----------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Misha Brukmanb1c93172005-04-21 23:48:37 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattnerfa7dad82002-05-21 20:49:37 +00008//
Chris Lattner19970e62007-12-03 19:43:18 +00009// This file implements dead code elimination and basic block merging, along
10// with a collection of other peephole control flow optimizations. For example:
Chris Lattnerfa7dad82002-05-21 20:49:37 +000011//
Gordon Henriksend5687672007-11-04 16:15:04 +000012// * Removes basic blocks with no predecessors.
13// * Merges a basic block into its predecessor if there is only one and the
Chris Lattnerfa7dad82002-05-21 20:49:37 +000014// predecessor only has one successor.
Gordon Henriksend5687672007-11-04 16:15:04 +000015// * Eliminates PHI nodes for basic blocks with a single predecessor.
16// * Eliminates a basic block that only contains an unconditional branch.
Chris Lattner19970e62007-12-03 19:43:18 +000017// * Changes invoke instructions to nounwind functions to be calls.
18// * Change things like "if (x) if (y)" into "if (x&y)".
19// * etc..
Chris Lattnerfa7dad82002-05-21 20:49:37 +000020//
21//===----------------------------------------------------------------------===//
22
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/ADT/SmallPtrSet.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/Statistic.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000026#include "llvm/Analysis/AssumptionCache.h"
Hyojin Sung4673f102016-03-29 04:08:57 +000027#include "llvm/Analysis/CFG.h"
Benjamin Kramer82de7d32016-05-27 14:27:24 +000028#include "llvm/Analysis/GlobalsModRef.h"
29#include "llvm/Analysis/TargetTransformInfo.h"
David Blaikie31b98d22018-06-04 21:23:21 +000030#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/Attributes.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000032#include "llvm/IR/CFG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/Constants.h"
34#include "llvm/IR/DataLayout.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/IntrinsicInst.h"
37#include "llvm/IR/Module.h"
Chris Lattnerfa7dad82002-05-21 20:49:37 +000038#include "llvm/Pass.h"
Jingyue Wufc029672014-09-30 22:23:38 +000039#include "llvm/Support/CommandLine.h"
Chandler Carruthfdffd872015-02-01 11:34:21 +000040#include "llvm/Transforms/Scalar.h"
Benjamin Kramer82de7d32016-05-27 14:27:24 +000041#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Benjamin Kramer82de7d32016-05-27 14:27:24 +000042#include <utility>
Chris Lattner49525f82004-01-09 06:02:20 +000043using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000044
Chandler Carruth964daaa2014-04-22 02:55:47 +000045#define DEBUG_TYPE "simplifycfg"
46
Sanjay Patelb0491732017-10-28 18:43:07 +000047static cl::opt<unsigned> UserBonusInstThreshold(
48 "bonus-inst-threshold", cl::Hidden, cl::init(1),
49 cl::desc("Control the number of bonus instructions (default = 1)"));
50
51static cl::opt<bool> UserKeepLoops(
52 "keep-loops", cl::Hidden, cl::init(true),
53 cl::desc("Preserve canonical loop structure (default = true)"));
54
55static cl::opt<bool> UserSwitchToLookup(
56 "switch-to-lookup", cl::Hidden, cl::init(false),
57 cl::desc("Convert switches to lookup tables (default = false)"));
58
59static cl::opt<bool> UserForwardSwitchCond(
60 "forward-switch-cond", cl::Hidden, cl::init(false),
61 cl::desc("Forward switch condition to phi ops (default = false)"));
Jingyue Wufc029672014-09-30 22:23:38 +000062
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +000063static cl::opt<bool> UserSinkCommonInsts(
64 "sink-common-insts", cl::Hidden, cl::init(false),
65 cl::desc("Sink common instructions (default = false)"));
66
67
Chris Lattner79a42ac2006-12-19 21:40:18 +000068STATISTIC(NumSimpl, "Number of blocks simplified");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000069
Sanjay Patel09159b8f2015-06-24 20:40:57 +000070/// If we have more than one empty (other than phi node) return blocks,
71/// merge them together to promote recursive block merging.
Jim Grosbach30c42822012-09-06 00:59:08 +000072static bool mergeEmptyReturnBlocks(Function &F) {
Chris Lattnerf21a2202009-12-22 06:07:30 +000073 bool Changed = false;
Nadav Rotem465834c2012-07-24 10:51:42 +000074
Craig Topperf40110f2014-04-25 05:29:35 +000075 BasicBlock *RetBlock = nullptr;
Nadav Rotem465834c2012-07-24 10:51:42 +000076
Chris Lattnerf21a2202009-12-22 06:07:30 +000077 // Scan all the blocks in the function, looking for empty return blocks.
78 for (Function::iterator BBI = F.begin(), E = F.end(); BBI != E; ) {
79 BasicBlock &BB = *BBI++;
Nadav Rotem465834c2012-07-24 10:51:42 +000080
Chris Lattnerf21a2202009-12-22 06:07:30 +000081 // Only look at return blocks.
82 ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator());
Craig Topperf40110f2014-04-25 05:29:35 +000083 if (!Ret) continue;
Nadav Rotem465834c2012-07-24 10:51:42 +000084
Chris Lattnerf21a2202009-12-22 06:07:30 +000085 // Only look at the block if it is empty or the only other thing in it is a
86 // single PHI node that is the operand to the return.
87 if (Ret != &BB.front()) {
88 // Check for something else in the block.
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +000089 BasicBlock::iterator I(Ret);
Chris Lattnerf21a2202009-12-22 06:07:30 +000090 --I;
Bill Wendling55e69d12010-03-14 10:40:55 +000091 // Skip over debug info.
92 while (isa<DbgInfoIntrinsic>(I) && I != BB.begin())
93 --I;
94 if (!isa<DbgInfoIntrinsic>(I) &&
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +000095 (!isa<PHINode>(I) || I != BB.begin() || Ret->getNumOperands() == 0 ||
96 Ret->getOperand(0) != &*I))
Chris Lattnerf21a2202009-12-22 06:07:30 +000097 continue;
98 }
Bill Wendling55e69d12010-03-14 10:40:55 +000099
Chris Lattnerf21a2202009-12-22 06:07:30 +0000100 // If this is the first returning block, remember it and keep going.
Craig Topperf40110f2014-04-25 05:29:35 +0000101 if (!RetBlock) {
Chris Lattnerf21a2202009-12-22 06:07:30 +0000102 RetBlock = &BB;
103 continue;
104 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000105
Chris Lattnerf21a2202009-12-22 06:07:30 +0000106 // Otherwise, we found a duplicate return block. Merge the two.
107 Changed = true;
Nadav Rotem465834c2012-07-24 10:51:42 +0000108
Chris Lattnerf21a2202009-12-22 06:07:30 +0000109 // Case when there is no input to the return or when the returned values
110 // agree is trivial. Note that they can't agree if there are phis in the
111 // blocks.
112 if (Ret->getNumOperands() == 0 ||
Nadav Rotem465834c2012-07-24 10:51:42 +0000113 Ret->getOperand(0) ==
Chris Lattnerf21a2202009-12-22 06:07:30 +0000114 cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0)) {
115 BB.replaceAllUsesWith(RetBlock);
116 BB.eraseFromParent();
117 continue;
118 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000119
Chris Lattnerf21a2202009-12-22 06:07:30 +0000120 // If the canonical return block has no PHI node, create one now.
121 PHINode *RetBlockPHI = dyn_cast<PHINode>(RetBlock->begin());
Craig Topperf40110f2014-04-25 05:29:35 +0000122 if (!RetBlockPHI) {
Devang Pateld3f41e82010-03-15 19:05:46 +0000123 Value *InVal = cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0);
Jay Foade0938d82011-03-30 11:19:20 +0000124 pred_iterator PB = pred_begin(RetBlock), PE = pred_end(RetBlock);
Jay Foad52131342011-03-30 11:28:46 +0000125 RetBlockPHI = PHINode::Create(Ret->getOperand(0)->getType(),
126 std::distance(PB, PE), "merge",
Chris Lattnerf21a2202009-12-22 06:07:30 +0000127 &RetBlock->front());
Nadav Rotem465834c2012-07-24 10:51:42 +0000128
Jay Foade0938d82011-03-30 11:19:20 +0000129 for (pred_iterator PI = PB; PI != PE; ++PI)
Chris Lattnerf21a2202009-12-22 06:07:30 +0000130 RetBlockPHI->addIncoming(InVal, *PI);
131 RetBlock->getTerminator()->setOperand(0, RetBlockPHI);
132 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000133
Chris Lattnerf21a2202009-12-22 06:07:30 +0000134 // Turn BB into a block that just unconditionally branches to the return
135 // block. This handles the case when the two return blocks have a common
136 // predecessor but that return different things.
137 RetBlockPHI->addIncoming(Ret->getOperand(0), &BB);
138 BB.getTerminator()->eraseFromParent();
139 BranchInst::Create(RetBlock, &BB);
140 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000141
Chris Lattnerf21a2202009-12-22 06:07:30 +0000142 return Changed;
143}
144
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000145/// Call SimplifyCFG on all the blocks in the function,
Chris Lattner61ce4df2007-11-13 07:32:38 +0000146/// iterating until no more changes are made.
Chandler Carruth0b4ef9c2013-01-07 03:53:25 +0000147static bool iterativelySimplifyCFG(Function &F, const TargetTransformInfo &TTI,
Sanjay Patel0f9b4772017-09-27 14:54:16 +0000148 const SimplifyCFGOptions &Options) {
Chris Lattner61ce4df2007-11-13 07:32:38 +0000149 bool Changed = false;
Reid Klecknerba857812016-03-28 18:07:40 +0000150 bool LocalChange = true;
Hyojin Sung4673f102016-03-29 04:08:57 +0000151
152 SmallVector<std::pair<const BasicBlock *, const BasicBlock *>, 32> Edges;
153 FindFunctionBackedges(F, Edges);
154 SmallPtrSet<BasicBlock *, 16> LoopHeaders;
155 for (unsigned i = 0, e = Edges.size(); i != e; ++i)
156 LoopHeaders.insert(const_cast<BasicBlock *>(Edges[i].second));
157
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000158 while (LocalChange) {
159 LocalChange = false;
Nadav Rotem465834c2012-07-24 10:51:42 +0000160
Sanjay Patel64ea2072015-06-24 20:42:33 +0000161 // Loop over all of the basic blocks and remove them if they are unneeded.
Dan Gohman4a63fad2010-08-14 00:29:42 +0000162 for (Function::iterator BBIt = F.begin(); BBIt != F.end(); ) {
Sanjay Patel4c33d522017-10-04 20:26:25 +0000163 if (simplifyCFG(&*BBIt++, TTI, Options, &LoopHeaders)) {
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000164 LocalChange = true;
165 ++NumSimpl;
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000166 }
167 }
168 Changed |= LocalChange;
169 }
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000170 return Changed;
171}
Chris Lattner61ce4df2007-11-13 07:32:38 +0000172
Chandler Carruthfdffd872015-02-01 11:34:21 +0000173static bool simplifyFunctionCFG(Function &F, const TargetTransformInfo &TTI,
Sanjay Patel0f9b4772017-09-27 14:54:16 +0000174 const SimplifyCFGOptions &Options) {
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000175 bool EverChanged = removeUnreachableBlocks(F);
Jim Grosbach30c42822012-09-06 00:59:08 +0000176 EverChanged |= mergeEmptyReturnBlocks(F);
Sanjay Patel4c33d522017-10-04 20:26:25 +0000177 EverChanged |= iterativelySimplifyCFG(F, TTI, Options);
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000178
Chris Lattner61ce4df2007-11-13 07:32:38 +0000179 // If neither pass changed anything, we're done.
180 if (!EverChanged) return false;
181
Jim Grosbach30c42822012-09-06 00:59:08 +0000182 // iterativelySimplifyCFG can (rarely) make some loops dead. If this happens,
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000183 // removeUnreachableBlocks is needed to nuke them, which means we should
Chris Lattner61ce4df2007-11-13 07:32:38 +0000184 // iterate between the two optimizations. We structure the code like this to
Sanjay Patel64ea2072015-06-24 20:42:33 +0000185 // avoid rerunning iterativelySimplifyCFG if the second pass of
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000186 // removeUnreachableBlocks doesn't do anything.
187 if (!removeUnreachableBlocks(F))
Chris Lattner61ce4df2007-11-13 07:32:38 +0000188 return true;
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000189
Chris Lattner61ce4df2007-11-13 07:32:38 +0000190 do {
Sanjay Patel4c33d522017-10-04 20:26:25 +0000191 EverChanged = iterativelySimplifyCFG(F, TTI, Options);
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000192 EverChanged |= removeUnreachableBlocks(F);
Chris Lattner61ce4df2007-11-13 07:32:38 +0000193 } while (EverChanged);
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000194
Chris Lattner61ce4df2007-11-13 07:32:38 +0000195 return true;
196}
Chandler Carruthfdffd872015-02-01 11:34:21 +0000197
Sanjay Patelb0491732017-10-28 18:43:07 +0000198// Command-line settings override compile-time settings.
199SimplifyCFGPass::SimplifyCFGPass(const SimplifyCFGOptions &Opts) {
200 Options.BonusInstThreshold = UserBonusInstThreshold.getNumOccurrences()
201 ? UserBonusInstThreshold
202 : Opts.BonusInstThreshold;
203 Options.ForwardSwitchCondToPhi = UserForwardSwitchCond.getNumOccurrences()
204 ? UserForwardSwitchCond
205 : Opts.ForwardSwitchCondToPhi;
206 Options.ConvertSwitchToLookupTable = UserSwitchToLookup.getNumOccurrences()
207 ? UserSwitchToLookup
208 : Opts.ConvertSwitchToLookupTable;
209 Options.NeedCanonicalLoop = UserKeepLoops.getNumOccurrences()
210 ? UserKeepLoops
211 : Opts.NeedCanonicalLoop;
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000212 Options.SinkCommonInsts = UserSinkCommonInsts.getNumOccurrences()
213 ? UserSinkCommonInsts
214 : Opts.SinkCommonInsts;
Sanjay Patelb0491732017-10-28 18:43:07 +0000215}
Chandler Carruthfdffd872015-02-01 11:34:21 +0000216
217PreservedAnalyses SimplifyCFGPass::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000218 FunctionAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +0000219 auto &TTI = AM.getResult<TargetIRAnalysis>(F);
Sanjay Patel4c33d522017-10-04 20:26:25 +0000220 Options.AC = &AM.getResult<AssumptionAnalysis>(F);
221 if (!simplifyFunctionCFG(F, TTI, Options))
Davide Italianod8d83f42016-06-08 13:32:23 +0000222 return PreservedAnalyses::all();
223 PreservedAnalyses PA;
224 PA.preserve<GlobalsAA>();
225 return PA;
Chandler Carruthfdffd872015-02-01 11:34:21 +0000226}
227
Dehao Chenf50c67c2016-05-05 20:47:53 +0000228namespace {
Sanjay Patelb0491732017-10-28 18:43:07 +0000229struct CFGSimplifyPass : public FunctionPass {
230 static char ID;
231 SimplifyCFGOptions Options;
Dehao Chenf50c67c2016-05-05 20:47:53 +0000232 std::function<bool(const Function &)> PredicateFtor;
Akira Hatanaka4a616192015-06-08 18:50:43 +0000233
Sanjay Patelb0491732017-10-28 18:43:07 +0000234 CFGSimplifyPass(unsigned Threshold = 1, bool ForwardSwitchCond = false,
235 bool ConvertSwitch = false, bool KeepLoops = true,
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000236 bool SinkCommon = false,
Sanjay Patelb0491732017-10-28 18:43:07 +0000237 std::function<bool(const Function &)> Ftor = nullptr)
238 : FunctionPass(ID), PredicateFtor(std::move(Ftor)) {
239
240 initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry());
241
242 // Check for command-line overrides of options for debug/customization.
243 Options.BonusInstThreshold = UserBonusInstThreshold.getNumOccurrences()
244 ? UserBonusInstThreshold
245 : Threshold;
246
247 Options.ForwardSwitchCondToPhi = UserForwardSwitchCond.getNumOccurrences()
248 ? UserForwardSwitchCond
249 : ForwardSwitchCond;
250
251 Options.ConvertSwitchToLookupTable = UserSwitchToLookup.getNumOccurrences()
252 ? UserSwitchToLookup
253 : ConvertSwitch;
254
255 Options.NeedCanonicalLoop =
256 UserKeepLoops.getNumOccurrences() ? UserKeepLoops : KeepLoops;
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000257
258 Options.SinkCommonInsts = UserSinkCommonInsts.getNumOccurrences()
259 ? UserSinkCommonInsts
260 : SinkCommon;
Dehao Chenf50c67c2016-05-05 20:47:53 +0000261 }
Sanjay Patelb0491732017-10-28 18:43:07 +0000262
Dehao Chenf50c67c2016-05-05 20:47:53 +0000263 bool runOnFunction(Function &F) override {
264 if (skipFunction(F) || (PredicateFtor && !PredicateFtor(F)))
265 return false;
Chandler Carruthfdffd872015-02-01 11:34:21 +0000266
Sanjay Patelb0491732017-10-28 18:43:07 +0000267 Options.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
268 auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
269 return simplifyFunctionCFG(F, TTI, Options);
Dehao Chenf50c67c2016-05-05 20:47:53 +0000270 }
Dehao Chenf50c67c2016-05-05 20:47:53 +0000271 void getAnalysisUsage(AnalysisUsage &AU) const override {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000272 AU.addRequired<AssumptionCacheTracker>();
Dehao Chenf50c67c2016-05-05 20:47:53 +0000273 AU.addRequired<TargetTransformInfoWrapperPass>();
274 AU.addPreserved<GlobalsAAWrapperPass>();
275 }
276};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000277}
Chandler Carruthfdffd872015-02-01 11:34:21 +0000278
279char CFGSimplifyPass::ID = 0;
280INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
281 false)
282INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000283INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruthfdffd872015-02-01 11:34:21 +0000284INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
285 false)
286
287// Public interface to the CFGSimplification pass
Akira Hatanaka4a616192015-06-08 18:50:43 +0000288FunctionPass *
Sanjay Patelb0491732017-10-28 18:43:07 +0000289llvm::createCFGSimplificationPass(unsigned Threshold, bool ForwardSwitchCond,
290 bool ConvertSwitch, bool KeepLoops,
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000291 bool SinkCommon,
Joerg Sonnenbergerfa736742017-03-26 06:44:08 +0000292 std::function<bool(const Function &)> Ftor) {
Sanjay Patelb0491732017-10-28 18:43:07 +0000293 return new CFGSimplifyPass(Threshold, ForwardSwitchCond, ConvertSwitch,
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000294 KeepLoops, SinkCommon, std::move(Ftor));
Joerg Sonnenbergerfa736742017-03-26 06:44:08 +0000295}