blob: b7b1db76b492379589b1880672aee22d0af1dbaa [file] [log] [blame]
Chris Lattnere15051d2008-05-14 20:38:44 +00001//===- SimplifyCFGPass.cpp - CFG Simplification Pass ----------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerfa7dad82002-05-21 20:49:37 +00009//
Chris Lattner19970e62007-12-03 19:43:18 +000010// This file implements dead code elimination and basic block merging, along
11// with a collection of other peephole control flow optimizations. For example:
Chris Lattnerfa7dad82002-05-21 20:49:37 +000012//
Gordon Henriksend5687672007-11-04 16:15:04 +000013// * Removes basic blocks with no predecessors.
14// * Merges a basic block into its predecessor if there is only one and the
Chris Lattnerfa7dad82002-05-21 20:49:37 +000015// predecessor only has one successor.
Gordon Henriksend5687672007-11-04 16:15:04 +000016// * Eliminates PHI nodes for basic blocks with a single predecessor.
17// * Eliminates a basic block that only contains an unconditional branch.
Chris Lattner19970e62007-12-03 19:43:18 +000018// * Changes invoke instructions to nounwind functions to be calls.
19// * Change things like "if (x) if (y)" into "if (x&y)".
20// * etc..
Chris Lattnerfa7dad82002-05-21 20:49:37 +000021//
22//===----------------------------------------------------------------------===//
23
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/ADT/SmallPtrSet.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/ADT/Statistic.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000027#include "llvm/Analysis/AssumptionCache.h"
Hyojin Sung4673f102016-03-29 04:08:57 +000028#include "llvm/Analysis/CFG.h"
Benjamin Kramer82de7d32016-05-27 14:27:24 +000029#include "llvm/Analysis/GlobalsModRef.h"
30#include "llvm/Analysis/TargetTransformInfo.h"
David Blaikie31b98d22018-06-04 21:23:21 +000031#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Attributes.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000033#include "llvm/IR/CFG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/Constants.h"
35#include "llvm/IR/DataLayout.h"
36#include "llvm/IR/Instructions.h"
37#include "llvm/IR/IntrinsicInst.h"
38#include "llvm/IR/Module.h"
Chris Lattnerfa7dad82002-05-21 20:49:37 +000039#include "llvm/Pass.h"
Jingyue Wufc029672014-09-30 22:23:38 +000040#include "llvm/Support/CommandLine.h"
Chandler Carruthfdffd872015-02-01 11:34:21 +000041#include "llvm/Transforms/Scalar.h"
Benjamin Kramer82de7d32016-05-27 14:27:24 +000042#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Benjamin Kramer82de7d32016-05-27 14:27:24 +000043#include <utility>
Chris Lattner49525f82004-01-09 06:02:20 +000044using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000045
Chandler Carruth964daaa2014-04-22 02:55:47 +000046#define DEBUG_TYPE "simplifycfg"
47
Sanjay Patelb0491732017-10-28 18:43:07 +000048static cl::opt<unsigned> UserBonusInstThreshold(
49 "bonus-inst-threshold", cl::Hidden, cl::init(1),
50 cl::desc("Control the number of bonus instructions (default = 1)"));
51
52static cl::opt<bool> UserKeepLoops(
53 "keep-loops", cl::Hidden, cl::init(true),
54 cl::desc("Preserve canonical loop structure (default = true)"));
55
56static cl::opt<bool> UserSwitchToLookup(
57 "switch-to-lookup", cl::Hidden, cl::init(false),
58 cl::desc("Convert switches to lookup tables (default = false)"));
59
60static cl::opt<bool> UserForwardSwitchCond(
61 "forward-switch-cond", cl::Hidden, cl::init(false),
62 cl::desc("Forward switch condition to phi ops (default = false)"));
Jingyue Wufc029672014-09-30 22:23:38 +000063
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +000064static cl::opt<bool> UserSinkCommonInsts(
65 "sink-common-insts", cl::Hidden, cl::init(false),
66 cl::desc("Sink common instructions (default = false)"));
67
68
Chris Lattner79a42ac2006-12-19 21:40:18 +000069STATISTIC(NumSimpl, "Number of blocks simplified");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000070
Sanjay Patel09159b8f2015-06-24 20:40:57 +000071/// If we have more than one empty (other than phi node) return blocks,
72/// merge them together to promote recursive block merging.
Jim Grosbach30c42822012-09-06 00:59:08 +000073static bool mergeEmptyReturnBlocks(Function &F) {
Chris Lattnerf21a2202009-12-22 06:07:30 +000074 bool Changed = false;
Nadav Rotem465834c2012-07-24 10:51:42 +000075
Craig Topperf40110f2014-04-25 05:29:35 +000076 BasicBlock *RetBlock = nullptr;
Nadav Rotem465834c2012-07-24 10:51:42 +000077
Chris Lattnerf21a2202009-12-22 06:07:30 +000078 // Scan all the blocks in the function, looking for empty return blocks.
79 for (Function::iterator BBI = F.begin(), E = F.end(); BBI != E; ) {
80 BasicBlock &BB = *BBI++;
Nadav Rotem465834c2012-07-24 10:51:42 +000081
Chris Lattnerf21a2202009-12-22 06:07:30 +000082 // Only look at return blocks.
83 ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator());
Craig Topperf40110f2014-04-25 05:29:35 +000084 if (!Ret) continue;
Nadav Rotem465834c2012-07-24 10:51:42 +000085
Chris Lattnerf21a2202009-12-22 06:07:30 +000086 // Only look at the block if it is empty or the only other thing in it is a
87 // single PHI node that is the operand to the return.
88 if (Ret != &BB.front()) {
89 // Check for something else in the block.
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +000090 BasicBlock::iterator I(Ret);
Chris Lattnerf21a2202009-12-22 06:07:30 +000091 --I;
Bill Wendling55e69d12010-03-14 10:40:55 +000092 // Skip over debug info.
93 while (isa<DbgInfoIntrinsic>(I) && I != BB.begin())
94 --I;
95 if (!isa<DbgInfoIntrinsic>(I) &&
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +000096 (!isa<PHINode>(I) || I != BB.begin() || Ret->getNumOperands() == 0 ||
97 Ret->getOperand(0) != &*I))
Chris Lattnerf21a2202009-12-22 06:07:30 +000098 continue;
99 }
Bill Wendling55e69d12010-03-14 10:40:55 +0000100
Chris Lattnerf21a2202009-12-22 06:07:30 +0000101 // If this is the first returning block, remember it and keep going.
Craig Topperf40110f2014-04-25 05:29:35 +0000102 if (!RetBlock) {
Chris Lattnerf21a2202009-12-22 06:07:30 +0000103 RetBlock = &BB;
104 continue;
105 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000106
Chris Lattnerf21a2202009-12-22 06:07:30 +0000107 // Otherwise, we found a duplicate return block. Merge the two.
108 Changed = true;
Nadav Rotem465834c2012-07-24 10:51:42 +0000109
Chris Lattnerf21a2202009-12-22 06:07:30 +0000110 // Case when there is no input to the return or when the returned values
111 // agree is trivial. Note that they can't agree if there are phis in the
112 // blocks.
113 if (Ret->getNumOperands() == 0 ||
Nadav Rotem465834c2012-07-24 10:51:42 +0000114 Ret->getOperand(0) ==
Chris Lattnerf21a2202009-12-22 06:07:30 +0000115 cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0)) {
116 BB.replaceAllUsesWith(RetBlock);
117 BB.eraseFromParent();
118 continue;
119 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000120
Chris Lattnerf21a2202009-12-22 06:07:30 +0000121 // If the canonical return block has no PHI node, create one now.
122 PHINode *RetBlockPHI = dyn_cast<PHINode>(RetBlock->begin());
Craig Topperf40110f2014-04-25 05:29:35 +0000123 if (!RetBlockPHI) {
Devang Pateld3f41e82010-03-15 19:05:46 +0000124 Value *InVal = cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0);
Jay Foade0938d82011-03-30 11:19:20 +0000125 pred_iterator PB = pred_begin(RetBlock), PE = pred_end(RetBlock);
Jay Foad52131342011-03-30 11:28:46 +0000126 RetBlockPHI = PHINode::Create(Ret->getOperand(0)->getType(),
127 std::distance(PB, PE), "merge",
Chris Lattnerf21a2202009-12-22 06:07:30 +0000128 &RetBlock->front());
Nadav Rotem465834c2012-07-24 10:51:42 +0000129
Jay Foade0938d82011-03-30 11:19:20 +0000130 for (pred_iterator PI = PB; PI != PE; ++PI)
Chris Lattnerf21a2202009-12-22 06:07:30 +0000131 RetBlockPHI->addIncoming(InVal, *PI);
132 RetBlock->getTerminator()->setOperand(0, RetBlockPHI);
133 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000134
Chris Lattnerf21a2202009-12-22 06:07:30 +0000135 // Turn BB into a block that just unconditionally branches to the return
136 // block. This handles the case when the two return blocks have a common
137 // predecessor but that return different things.
138 RetBlockPHI->addIncoming(Ret->getOperand(0), &BB);
139 BB.getTerminator()->eraseFromParent();
140 BranchInst::Create(RetBlock, &BB);
141 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000142
Chris Lattnerf21a2202009-12-22 06:07:30 +0000143 return Changed;
144}
145
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000146/// Call SimplifyCFG on all the blocks in the function,
Chris Lattner61ce4df2007-11-13 07:32:38 +0000147/// iterating until no more changes are made.
Chandler Carruth0b4ef9c2013-01-07 03:53:25 +0000148static bool iterativelySimplifyCFG(Function &F, const TargetTransformInfo &TTI,
Sanjay Patel0f9b4772017-09-27 14:54:16 +0000149 const SimplifyCFGOptions &Options) {
Chris Lattner61ce4df2007-11-13 07:32:38 +0000150 bool Changed = false;
Reid Klecknerba857812016-03-28 18:07:40 +0000151 bool LocalChange = true;
Hyojin Sung4673f102016-03-29 04:08:57 +0000152
153 SmallVector<std::pair<const BasicBlock *, const BasicBlock *>, 32> Edges;
154 FindFunctionBackedges(F, Edges);
155 SmallPtrSet<BasicBlock *, 16> LoopHeaders;
156 for (unsigned i = 0, e = Edges.size(); i != e; ++i)
157 LoopHeaders.insert(const_cast<BasicBlock *>(Edges[i].second));
158
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000159 while (LocalChange) {
160 LocalChange = false;
Nadav Rotem465834c2012-07-24 10:51:42 +0000161
Sanjay Patel64ea2072015-06-24 20:42:33 +0000162 // Loop over all of the basic blocks and remove them if they are unneeded.
Dan Gohman4a63fad2010-08-14 00:29:42 +0000163 for (Function::iterator BBIt = F.begin(); BBIt != F.end(); ) {
Sanjay Patel4c33d522017-10-04 20:26:25 +0000164 if (simplifyCFG(&*BBIt++, TTI, Options, &LoopHeaders)) {
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000165 LocalChange = true;
166 ++NumSimpl;
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000167 }
168 }
169 Changed |= LocalChange;
170 }
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000171 return Changed;
172}
Chris Lattner61ce4df2007-11-13 07:32:38 +0000173
Chandler Carruthfdffd872015-02-01 11:34:21 +0000174static bool simplifyFunctionCFG(Function &F, const TargetTransformInfo &TTI,
Sanjay Patel0f9b4772017-09-27 14:54:16 +0000175 const SimplifyCFGOptions &Options) {
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000176 bool EverChanged = removeUnreachableBlocks(F);
Jim Grosbach30c42822012-09-06 00:59:08 +0000177 EverChanged |= mergeEmptyReturnBlocks(F);
Sanjay Patel4c33d522017-10-04 20:26:25 +0000178 EverChanged |= iterativelySimplifyCFG(F, TTI, Options);
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000179
Chris Lattner61ce4df2007-11-13 07:32:38 +0000180 // If neither pass changed anything, we're done.
181 if (!EverChanged) return false;
182
Jim Grosbach30c42822012-09-06 00:59:08 +0000183 // iterativelySimplifyCFG can (rarely) make some loops dead. If this happens,
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000184 // removeUnreachableBlocks is needed to nuke them, which means we should
Chris Lattner61ce4df2007-11-13 07:32:38 +0000185 // iterate between the two optimizations. We structure the code like this to
Sanjay Patel64ea2072015-06-24 20:42:33 +0000186 // avoid rerunning iterativelySimplifyCFG if the second pass of
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000187 // removeUnreachableBlocks doesn't do anything.
188 if (!removeUnreachableBlocks(F))
Chris Lattner61ce4df2007-11-13 07:32:38 +0000189 return true;
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000190
Chris Lattner61ce4df2007-11-13 07:32:38 +0000191 do {
Sanjay Patel4c33d522017-10-04 20:26:25 +0000192 EverChanged = iterativelySimplifyCFG(F, TTI, Options);
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000193 EverChanged |= removeUnreachableBlocks(F);
Chris Lattner61ce4df2007-11-13 07:32:38 +0000194 } while (EverChanged);
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000195
Chris Lattner61ce4df2007-11-13 07:32:38 +0000196 return true;
197}
Chandler Carruthfdffd872015-02-01 11:34:21 +0000198
Sanjay Patelb0491732017-10-28 18:43:07 +0000199// Command-line settings override compile-time settings.
200SimplifyCFGPass::SimplifyCFGPass(const SimplifyCFGOptions &Opts) {
201 Options.BonusInstThreshold = UserBonusInstThreshold.getNumOccurrences()
202 ? UserBonusInstThreshold
203 : Opts.BonusInstThreshold;
204 Options.ForwardSwitchCondToPhi = UserForwardSwitchCond.getNumOccurrences()
205 ? UserForwardSwitchCond
206 : Opts.ForwardSwitchCondToPhi;
207 Options.ConvertSwitchToLookupTable = UserSwitchToLookup.getNumOccurrences()
208 ? UserSwitchToLookup
209 : Opts.ConvertSwitchToLookupTable;
210 Options.NeedCanonicalLoop = UserKeepLoops.getNumOccurrences()
211 ? UserKeepLoops
212 : Opts.NeedCanonicalLoop;
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000213 Options.SinkCommonInsts = UserSinkCommonInsts.getNumOccurrences()
214 ? UserSinkCommonInsts
215 : Opts.SinkCommonInsts;
Sanjay Patelb0491732017-10-28 18:43:07 +0000216}
Chandler Carruthfdffd872015-02-01 11:34:21 +0000217
218PreservedAnalyses SimplifyCFGPass::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000219 FunctionAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +0000220 auto &TTI = AM.getResult<TargetIRAnalysis>(F);
Sanjay Patel4c33d522017-10-04 20:26:25 +0000221 Options.AC = &AM.getResult<AssumptionAnalysis>(F);
222 if (!simplifyFunctionCFG(F, TTI, Options))
Davide Italianod8d83f42016-06-08 13:32:23 +0000223 return PreservedAnalyses::all();
224 PreservedAnalyses PA;
225 PA.preserve<GlobalsAA>();
226 return PA;
Chandler Carruthfdffd872015-02-01 11:34:21 +0000227}
228
Dehao Chenf50c67c2016-05-05 20:47:53 +0000229namespace {
Sanjay Patelb0491732017-10-28 18:43:07 +0000230struct CFGSimplifyPass : public FunctionPass {
231 static char ID;
232 SimplifyCFGOptions Options;
Dehao Chenf50c67c2016-05-05 20:47:53 +0000233 std::function<bool(const Function &)> PredicateFtor;
Akira Hatanaka4a616192015-06-08 18:50:43 +0000234
Sanjay Patelb0491732017-10-28 18:43:07 +0000235 CFGSimplifyPass(unsigned Threshold = 1, bool ForwardSwitchCond = false,
236 bool ConvertSwitch = false, bool KeepLoops = true,
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000237 bool SinkCommon = false,
Sanjay Patelb0491732017-10-28 18:43:07 +0000238 std::function<bool(const Function &)> Ftor = nullptr)
239 : FunctionPass(ID), PredicateFtor(std::move(Ftor)) {
240
241 initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry());
242
243 // Check for command-line overrides of options for debug/customization.
244 Options.BonusInstThreshold = UserBonusInstThreshold.getNumOccurrences()
245 ? UserBonusInstThreshold
246 : Threshold;
247
248 Options.ForwardSwitchCondToPhi = UserForwardSwitchCond.getNumOccurrences()
249 ? UserForwardSwitchCond
250 : ForwardSwitchCond;
251
252 Options.ConvertSwitchToLookupTable = UserSwitchToLookup.getNumOccurrences()
253 ? UserSwitchToLookup
254 : ConvertSwitch;
255
256 Options.NeedCanonicalLoop =
257 UserKeepLoops.getNumOccurrences() ? UserKeepLoops : KeepLoops;
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000258
259 Options.SinkCommonInsts = UserSinkCommonInsts.getNumOccurrences()
260 ? UserSinkCommonInsts
261 : SinkCommon;
Dehao Chenf50c67c2016-05-05 20:47:53 +0000262 }
Sanjay Patelb0491732017-10-28 18:43:07 +0000263
Dehao Chenf50c67c2016-05-05 20:47:53 +0000264 bool runOnFunction(Function &F) override {
265 if (skipFunction(F) || (PredicateFtor && !PredicateFtor(F)))
266 return false;
Chandler Carruthfdffd872015-02-01 11:34:21 +0000267
Sanjay Patelb0491732017-10-28 18:43:07 +0000268 Options.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
269 auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
270 return simplifyFunctionCFG(F, TTI, Options);
Dehao Chenf50c67c2016-05-05 20:47:53 +0000271 }
Dehao Chenf50c67c2016-05-05 20:47:53 +0000272 void getAnalysisUsage(AnalysisUsage &AU) const override {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000273 AU.addRequired<AssumptionCacheTracker>();
Dehao Chenf50c67c2016-05-05 20:47:53 +0000274 AU.addRequired<TargetTransformInfoWrapperPass>();
275 AU.addPreserved<GlobalsAAWrapperPass>();
276 }
277};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000278}
Chandler Carruthfdffd872015-02-01 11:34:21 +0000279
280char CFGSimplifyPass::ID = 0;
281INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
282 false)
283INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000284INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruthfdffd872015-02-01 11:34:21 +0000285INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
286 false)
287
288// Public interface to the CFGSimplification pass
Akira Hatanaka4a616192015-06-08 18:50:43 +0000289FunctionPass *
Sanjay Patelb0491732017-10-28 18:43:07 +0000290llvm::createCFGSimplificationPass(unsigned Threshold, bool ForwardSwitchCond,
291 bool ConvertSwitch, bool KeepLoops,
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000292 bool SinkCommon,
Joerg Sonnenbergerfa736742017-03-26 06:44:08 +0000293 std::function<bool(const Function &)> Ftor) {
Sanjay Patelb0491732017-10-28 18:43:07 +0000294 return new CFGSimplifyPass(Threshold, ForwardSwitchCond, ConvertSwitch,
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000295 KeepLoops, SinkCommon, std::move(Ftor));
Joerg Sonnenbergerfa736742017-03-26 06:44:08 +0000296}