blob: 789e0a477932852ae5e579d9871266e520b077b1 [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"
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"
42#include "llvm/Transforms/Utils/Local.h"
43#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
Chris Lattner79a42ac2006-12-19 21:40:18 +000064STATISTIC(NumSimpl, "Number of blocks simplified");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000065
Sanjay Patel09159b8f2015-06-24 20:40:57 +000066/// If we have more than one empty (other than phi node) return blocks,
67/// merge them together to promote recursive block merging.
Jim Grosbach30c42822012-09-06 00:59:08 +000068static bool mergeEmptyReturnBlocks(Function &F) {
Chris Lattnerf21a2202009-12-22 06:07:30 +000069 bool Changed = false;
Nadav Rotem465834c2012-07-24 10:51:42 +000070
Craig Topperf40110f2014-04-25 05:29:35 +000071 BasicBlock *RetBlock = nullptr;
Nadav Rotem465834c2012-07-24 10:51:42 +000072
Chris Lattnerf21a2202009-12-22 06:07:30 +000073 // Scan all the blocks in the function, looking for empty return blocks.
74 for (Function::iterator BBI = F.begin(), E = F.end(); BBI != E; ) {
75 BasicBlock &BB = *BBI++;
Nadav Rotem465834c2012-07-24 10:51:42 +000076
Chris Lattnerf21a2202009-12-22 06:07:30 +000077 // Only look at return blocks.
78 ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator());
Craig Topperf40110f2014-04-25 05:29:35 +000079 if (!Ret) continue;
Nadav Rotem465834c2012-07-24 10:51:42 +000080
Chris Lattnerf21a2202009-12-22 06:07:30 +000081 // Only look at the block if it is empty or the only other thing in it is a
82 // single PHI node that is the operand to the return.
83 if (Ret != &BB.front()) {
84 // Check for something else in the block.
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +000085 BasicBlock::iterator I(Ret);
Chris Lattnerf21a2202009-12-22 06:07:30 +000086 --I;
Bill Wendling55e69d12010-03-14 10:40:55 +000087 // Skip over debug info.
88 while (isa<DbgInfoIntrinsic>(I) && I != BB.begin())
89 --I;
90 if (!isa<DbgInfoIntrinsic>(I) &&
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +000091 (!isa<PHINode>(I) || I != BB.begin() || Ret->getNumOperands() == 0 ||
92 Ret->getOperand(0) != &*I))
Chris Lattnerf21a2202009-12-22 06:07:30 +000093 continue;
94 }
Bill Wendling55e69d12010-03-14 10:40:55 +000095
Chris Lattnerf21a2202009-12-22 06:07:30 +000096 // If this is the first returning block, remember it and keep going.
Craig Topperf40110f2014-04-25 05:29:35 +000097 if (!RetBlock) {
Chris Lattnerf21a2202009-12-22 06:07:30 +000098 RetBlock = &BB;
99 continue;
100 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000101
Chris Lattnerf21a2202009-12-22 06:07:30 +0000102 // Otherwise, we found a duplicate return block. Merge the two.
103 Changed = true;
Nadav Rotem465834c2012-07-24 10:51:42 +0000104
Chris Lattnerf21a2202009-12-22 06:07:30 +0000105 // Case when there is no input to the return or when the returned values
106 // agree is trivial. Note that they can't agree if there are phis in the
107 // blocks.
108 if (Ret->getNumOperands() == 0 ||
Nadav Rotem465834c2012-07-24 10:51:42 +0000109 Ret->getOperand(0) ==
Chris Lattnerf21a2202009-12-22 06:07:30 +0000110 cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0)) {
111 BB.replaceAllUsesWith(RetBlock);
112 BB.eraseFromParent();
113 continue;
114 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000115
Chris Lattnerf21a2202009-12-22 06:07:30 +0000116 // If the canonical return block has no PHI node, create one now.
117 PHINode *RetBlockPHI = dyn_cast<PHINode>(RetBlock->begin());
Craig Topperf40110f2014-04-25 05:29:35 +0000118 if (!RetBlockPHI) {
Devang Pateld3f41e82010-03-15 19:05:46 +0000119 Value *InVal = cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0);
Jay Foade0938d82011-03-30 11:19:20 +0000120 pred_iterator PB = pred_begin(RetBlock), PE = pred_end(RetBlock);
Jay Foad52131342011-03-30 11:28:46 +0000121 RetBlockPHI = PHINode::Create(Ret->getOperand(0)->getType(),
122 std::distance(PB, PE), "merge",
Chris Lattnerf21a2202009-12-22 06:07:30 +0000123 &RetBlock->front());
Nadav Rotem465834c2012-07-24 10:51:42 +0000124
Jay Foade0938d82011-03-30 11:19:20 +0000125 for (pred_iterator PI = PB; PI != PE; ++PI)
Chris Lattnerf21a2202009-12-22 06:07:30 +0000126 RetBlockPHI->addIncoming(InVal, *PI);
127 RetBlock->getTerminator()->setOperand(0, RetBlockPHI);
128 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000129
Chris Lattnerf21a2202009-12-22 06:07:30 +0000130 // Turn BB into a block that just unconditionally branches to the return
131 // block. This handles the case when the two return blocks have a common
132 // predecessor but that return different things.
133 RetBlockPHI->addIncoming(Ret->getOperand(0), &BB);
134 BB.getTerminator()->eraseFromParent();
135 BranchInst::Create(RetBlock, &BB);
136 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000137
Chris Lattnerf21a2202009-12-22 06:07:30 +0000138 return Changed;
139}
140
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000141/// Call SimplifyCFG on all the blocks in the function,
Chris Lattner61ce4df2007-11-13 07:32:38 +0000142/// iterating until no more changes are made.
Chandler Carruth0b4ef9c2013-01-07 03:53:25 +0000143static bool iterativelySimplifyCFG(Function &F, const TargetTransformInfo &TTI,
Sanjay Patel0f9b4772017-09-27 14:54:16 +0000144 const SimplifyCFGOptions &Options) {
Chris Lattner61ce4df2007-11-13 07:32:38 +0000145 bool Changed = false;
Reid Klecknerba857812016-03-28 18:07:40 +0000146 bool LocalChange = true;
Hyojin Sung4673f102016-03-29 04:08:57 +0000147
148 SmallVector<std::pair<const BasicBlock *, const BasicBlock *>, 32> Edges;
149 FindFunctionBackedges(F, Edges);
150 SmallPtrSet<BasicBlock *, 16> LoopHeaders;
151 for (unsigned i = 0, e = Edges.size(); i != e; ++i)
152 LoopHeaders.insert(const_cast<BasicBlock *>(Edges[i].second));
153
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000154 while (LocalChange) {
155 LocalChange = false;
Nadav Rotem465834c2012-07-24 10:51:42 +0000156
Sanjay Patel64ea2072015-06-24 20:42:33 +0000157 // Loop over all of the basic blocks and remove them if they are unneeded.
Dan Gohman4a63fad2010-08-14 00:29:42 +0000158 for (Function::iterator BBIt = F.begin(); BBIt != F.end(); ) {
Sanjay Patel4c33d522017-10-04 20:26:25 +0000159 if (simplifyCFG(&*BBIt++, TTI, Options, &LoopHeaders)) {
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000160 LocalChange = true;
161 ++NumSimpl;
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000162 }
163 }
164 Changed |= LocalChange;
165 }
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000166 return Changed;
167}
Chris Lattner61ce4df2007-11-13 07:32:38 +0000168
Chandler Carruthfdffd872015-02-01 11:34:21 +0000169static bool simplifyFunctionCFG(Function &F, const TargetTransformInfo &TTI,
Sanjay Patel0f9b4772017-09-27 14:54:16 +0000170 const SimplifyCFGOptions &Options) {
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000171 bool EverChanged = removeUnreachableBlocks(F);
Jim Grosbach30c42822012-09-06 00:59:08 +0000172 EverChanged |= mergeEmptyReturnBlocks(F);
Sanjay Patel4c33d522017-10-04 20:26:25 +0000173 EverChanged |= iterativelySimplifyCFG(F, TTI, Options);
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000174
Chris Lattner61ce4df2007-11-13 07:32:38 +0000175 // If neither pass changed anything, we're done.
176 if (!EverChanged) return false;
177
Jim Grosbach30c42822012-09-06 00:59:08 +0000178 // iterativelySimplifyCFG can (rarely) make some loops dead. If this happens,
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000179 // removeUnreachableBlocks is needed to nuke them, which means we should
Chris Lattner61ce4df2007-11-13 07:32:38 +0000180 // iterate between the two optimizations. We structure the code like this to
Sanjay Patel64ea2072015-06-24 20:42:33 +0000181 // avoid rerunning iterativelySimplifyCFG if the second pass of
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000182 // removeUnreachableBlocks doesn't do anything.
183 if (!removeUnreachableBlocks(F))
Chris Lattner61ce4df2007-11-13 07:32:38 +0000184 return true;
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000185
Chris Lattner61ce4df2007-11-13 07:32:38 +0000186 do {
Sanjay Patel4c33d522017-10-04 20:26:25 +0000187 EverChanged = iterativelySimplifyCFG(F, TTI, Options);
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000188 EverChanged |= removeUnreachableBlocks(F);
Chris Lattner61ce4df2007-11-13 07:32:38 +0000189 } while (EverChanged);
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000190
Chris Lattner61ce4df2007-11-13 07:32:38 +0000191 return true;
192}
Chandler Carruthfdffd872015-02-01 11:34:21 +0000193
Sanjay Patelb0491732017-10-28 18:43:07 +0000194// Command-line settings override compile-time settings.
195SimplifyCFGPass::SimplifyCFGPass(const SimplifyCFGOptions &Opts) {
196 Options.BonusInstThreshold = UserBonusInstThreshold.getNumOccurrences()
197 ? UserBonusInstThreshold
198 : Opts.BonusInstThreshold;
199 Options.ForwardSwitchCondToPhi = UserForwardSwitchCond.getNumOccurrences()
200 ? UserForwardSwitchCond
201 : Opts.ForwardSwitchCondToPhi;
202 Options.ConvertSwitchToLookupTable = UserSwitchToLookup.getNumOccurrences()
203 ? UserSwitchToLookup
204 : Opts.ConvertSwitchToLookupTable;
205 Options.NeedCanonicalLoop = UserKeepLoops.getNumOccurrences()
206 ? UserKeepLoops
207 : Opts.NeedCanonicalLoop;
208}
Chandler Carruthfdffd872015-02-01 11:34:21 +0000209
210PreservedAnalyses SimplifyCFGPass::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000211 FunctionAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +0000212 auto &TTI = AM.getResult<TargetIRAnalysis>(F);
Sanjay Patel4c33d522017-10-04 20:26:25 +0000213 Options.AC = &AM.getResult<AssumptionAnalysis>(F);
214 if (!simplifyFunctionCFG(F, TTI, Options))
Davide Italianod8d83f42016-06-08 13:32:23 +0000215 return PreservedAnalyses::all();
216 PreservedAnalyses PA;
217 PA.preserve<GlobalsAA>();
218 return PA;
Chandler Carruthfdffd872015-02-01 11:34:21 +0000219}
220
Dehao Chenf50c67c2016-05-05 20:47:53 +0000221namespace {
Sanjay Patelb0491732017-10-28 18:43:07 +0000222struct CFGSimplifyPass : public FunctionPass {
223 static char ID;
224 SimplifyCFGOptions Options;
Dehao Chenf50c67c2016-05-05 20:47:53 +0000225 std::function<bool(const Function &)> PredicateFtor;
Akira Hatanaka4a616192015-06-08 18:50:43 +0000226
Sanjay Patelb0491732017-10-28 18:43:07 +0000227 CFGSimplifyPass(unsigned Threshold = 1, bool ForwardSwitchCond = false,
228 bool ConvertSwitch = false, bool KeepLoops = true,
229 std::function<bool(const Function &)> Ftor = nullptr)
230 : FunctionPass(ID), PredicateFtor(std::move(Ftor)) {
231
232 initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry());
233
234 // Check for command-line overrides of options for debug/customization.
235 Options.BonusInstThreshold = UserBonusInstThreshold.getNumOccurrences()
236 ? UserBonusInstThreshold
237 : Threshold;
238
239 Options.ForwardSwitchCondToPhi = UserForwardSwitchCond.getNumOccurrences()
240 ? UserForwardSwitchCond
241 : ForwardSwitchCond;
242
243 Options.ConvertSwitchToLookupTable = UserSwitchToLookup.getNumOccurrences()
244 ? UserSwitchToLookup
245 : ConvertSwitch;
246
247 Options.NeedCanonicalLoop =
248 UserKeepLoops.getNumOccurrences() ? UserKeepLoops : KeepLoops;
Dehao Chenf50c67c2016-05-05 20:47:53 +0000249 }
Sanjay Patelb0491732017-10-28 18:43:07 +0000250
Dehao Chenf50c67c2016-05-05 20:47:53 +0000251 bool runOnFunction(Function &F) override {
252 if (skipFunction(F) || (PredicateFtor && !PredicateFtor(F)))
253 return false;
Chandler Carruthfdffd872015-02-01 11:34:21 +0000254
Sanjay Patelb0491732017-10-28 18:43:07 +0000255 Options.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
256 auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
257 return simplifyFunctionCFG(F, TTI, Options);
Dehao Chenf50c67c2016-05-05 20:47:53 +0000258 }
Dehao Chenf50c67c2016-05-05 20:47:53 +0000259 void getAnalysisUsage(AnalysisUsage &AU) const override {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000260 AU.addRequired<AssumptionCacheTracker>();
Dehao Chenf50c67c2016-05-05 20:47:53 +0000261 AU.addRequired<TargetTransformInfoWrapperPass>();
262 AU.addPreserved<GlobalsAAWrapperPass>();
263 }
264};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000265}
Chandler Carruthfdffd872015-02-01 11:34:21 +0000266
267char CFGSimplifyPass::ID = 0;
268INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
269 false)
270INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000271INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruthfdffd872015-02-01 11:34:21 +0000272INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
273 false)
274
275// Public interface to the CFGSimplification pass
Akira Hatanaka4a616192015-06-08 18:50:43 +0000276FunctionPass *
Sanjay Patelb0491732017-10-28 18:43:07 +0000277llvm::createCFGSimplificationPass(unsigned Threshold, bool ForwardSwitchCond,
278 bool ConvertSwitch, bool KeepLoops,
Joerg Sonnenbergerfa736742017-03-26 06:44:08 +0000279 std::function<bool(const Function &)> Ftor) {
Sanjay Patelb0491732017-10-28 18:43:07 +0000280 return new CFGSimplifyPass(Threshold, ForwardSwitchCond, ConvertSwitch,
281 KeepLoops, std::move(Ftor));
Joerg Sonnenbergerfa736742017-03-26 06:44:08 +0000282}