blob: 344a89f36044d428dd445a47981d169c17a2e9e8 [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"
Chandler Carruth66b31302015-01-04 12:03:27 +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
Jingyue Wufc029672014-09-30 22:23:38 +000048static cl::opt<unsigned>
49UserBonusInstThreshold("bonus-inst-threshold", cl::Hidden, cl::init(1),
50 cl::desc("Control the number of bonus instructions (default = 1)"));
51
Chris Lattner79a42ac2006-12-19 21:40:18 +000052STATISTIC(NumSimpl, "Number of blocks simplified");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000053
Sanjay Patel09159b8f2015-06-24 20:40:57 +000054/// If we have more than one empty (other than phi node) return blocks,
55/// merge them together to promote recursive block merging.
Jim Grosbach30c42822012-09-06 00:59:08 +000056static bool mergeEmptyReturnBlocks(Function &F) {
Chris Lattnerf21a2202009-12-22 06:07:30 +000057 bool Changed = false;
Nadav Rotem465834c2012-07-24 10:51:42 +000058
Craig Topperf40110f2014-04-25 05:29:35 +000059 BasicBlock *RetBlock = nullptr;
Nadav Rotem465834c2012-07-24 10:51:42 +000060
Chris Lattnerf21a2202009-12-22 06:07:30 +000061 // Scan all the blocks in the function, looking for empty return blocks.
62 for (Function::iterator BBI = F.begin(), E = F.end(); BBI != E; ) {
63 BasicBlock &BB = *BBI++;
Nadav Rotem465834c2012-07-24 10:51:42 +000064
Chris Lattnerf21a2202009-12-22 06:07:30 +000065 // Only look at return blocks.
66 ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator());
Craig Topperf40110f2014-04-25 05:29:35 +000067 if (!Ret) continue;
Nadav Rotem465834c2012-07-24 10:51:42 +000068
Chris Lattnerf21a2202009-12-22 06:07:30 +000069 // Only look at the block if it is empty or the only other thing in it is a
70 // single PHI node that is the operand to the return.
71 if (Ret != &BB.front()) {
72 // Check for something else in the block.
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +000073 BasicBlock::iterator I(Ret);
Chris Lattnerf21a2202009-12-22 06:07:30 +000074 --I;
Bill Wendling55e69d12010-03-14 10:40:55 +000075 // Skip over debug info.
76 while (isa<DbgInfoIntrinsic>(I) && I != BB.begin())
77 --I;
78 if (!isa<DbgInfoIntrinsic>(I) &&
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +000079 (!isa<PHINode>(I) || I != BB.begin() || Ret->getNumOperands() == 0 ||
80 Ret->getOperand(0) != &*I))
Chris Lattnerf21a2202009-12-22 06:07:30 +000081 continue;
82 }
Bill Wendling55e69d12010-03-14 10:40:55 +000083
Chris Lattnerf21a2202009-12-22 06:07:30 +000084 // If this is the first returning block, remember it and keep going.
Craig Topperf40110f2014-04-25 05:29:35 +000085 if (!RetBlock) {
Chris Lattnerf21a2202009-12-22 06:07:30 +000086 RetBlock = &BB;
87 continue;
88 }
Nadav Rotem465834c2012-07-24 10:51:42 +000089
Chris Lattnerf21a2202009-12-22 06:07:30 +000090 // Otherwise, we found a duplicate return block. Merge the two.
91 Changed = true;
Nadav Rotem465834c2012-07-24 10:51:42 +000092
Chris Lattnerf21a2202009-12-22 06:07:30 +000093 // Case when there is no input to the return or when the returned values
94 // agree is trivial. Note that they can't agree if there are phis in the
95 // blocks.
96 if (Ret->getNumOperands() == 0 ||
Nadav Rotem465834c2012-07-24 10:51:42 +000097 Ret->getOperand(0) ==
Chris Lattnerf21a2202009-12-22 06:07:30 +000098 cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0)) {
99 BB.replaceAllUsesWith(RetBlock);
100 BB.eraseFromParent();
101 continue;
102 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000103
Chris Lattnerf21a2202009-12-22 06:07:30 +0000104 // If the canonical return block has no PHI node, create one now.
105 PHINode *RetBlockPHI = dyn_cast<PHINode>(RetBlock->begin());
Craig Topperf40110f2014-04-25 05:29:35 +0000106 if (!RetBlockPHI) {
Devang Pateld3f41e82010-03-15 19:05:46 +0000107 Value *InVal = cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0);
Jay Foade0938d82011-03-30 11:19:20 +0000108 pred_iterator PB = pred_begin(RetBlock), PE = pred_end(RetBlock);
Jay Foad52131342011-03-30 11:28:46 +0000109 RetBlockPHI = PHINode::Create(Ret->getOperand(0)->getType(),
110 std::distance(PB, PE), "merge",
Chris Lattnerf21a2202009-12-22 06:07:30 +0000111 &RetBlock->front());
Nadav Rotem465834c2012-07-24 10:51:42 +0000112
Jay Foade0938d82011-03-30 11:19:20 +0000113 for (pred_iterator PI = PB; PI != PE; ++PI)
Chris Lattnerf21a2202009-12-22 06:07:30 +0000114 RetBlockPHI->addIncoming(InVal, *PI);
115 RetBlock->getTerminator()->setOperand(0, RetBlockPHI);
116 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000117
Chris Lattnerf21a2202009-12-22 06:07:30 +0000118 // Turn BB into a block that just unconditionally branches to the return
119 // block. This handles the case when the two return blocks have a common
120 // predecessor but that return different things.
121 RetBlockPHI->addIncoming(Ret->getOperand(0), &BB);
122 BB.getTerminator()->eraseFromParent();
123 BranchInst::Create(RetBlock, &BB);
124 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000125
Chris Lattnerf21a2202009-12-22 06:07:30 +0000126 return Changed;
127}
128
Sanjay Patel09159b8f2015-06-24 20:40:57 +0000129/// Call SimplifyCFG on all the blocks in the function,
Chris Lattner61ce4df2007-11-13 07:32:38 +0000130/// iterating until no more changes are made.
Chandler Carruth0b4ef9c2013-01-07 03:53:25 +0000131static bool iterativelySimplifyCFG(Function &F, const TargetTransformInfo &TTI,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000132 AssumptionCache *AC,
Jingyue Wufc029672014-09-30 22:23:38 +0000133 unsigned BonusInstThreshold) {
Chris Lattner61ce4df2007-11-13 07:32:38 +0000134 bool Changed = false;
Reid Klecknerba857812016-03-28 18:07:40 +0000135 bool LocalChange = true;
Hyojin Sung4673f102016-03-29 04:08:57 +0000136
137 SmallVector<std::pair<const BasicBlock *, const BasicBlock *>, 32> Edges;
138 FindFunctionBackedges(F, Edges);
139 SmallPtrSet<BasicBlock *, 16> LoopHeaders;
140 for (unsigned i = 0, e = Edges.size(); i != e; ++i)
141 LoopHeaders.insert(const_cast<BasicBlock *>(Edges[i].second));
142
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000143 while (LocalChange) {
144 LocalChange = false;
Nadav Rotem465834c2012-07-24 10:51:42 +0000145
Sanjay Patel64ea2072015-06-24 20:42:33 +0000146 // Loop over all of the basic blocks and remove them if they are unneeded.
Dan Gohman4a63fad2010-08-14 00:29:42 +0000147 for (Function::iterator BBIt = F.begin(); BBIt != F.end(); ) {
Hyojin Sung4673f102016-03-29 04:08:57 +0000148 if (SimplifyCFG(&*BBIt++, TTI, BonusInstThreshold, AC, &LoopHeaders)) {
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000149 LocalChange = true;
150 ++NumSimpl;
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000151 }
152 }
153 Changed |= LocalChange;
154 }
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000155 return Changed;
156}
Chris Lattner61ce4df2007-11-13 07:32:38 +0000157
Chandler Carruthfdffd872015-02-01 11:34:21 +0000158static bool simplifyFunctionCFG(Function &F, const TargetTransformInfo &TTI,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000159 AssumptionCache *AC, int BonusInstThreshold) {
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000160 bool EverChanged = removeUnreachableBlocks(F);
Jim Grosbach30c42822012-09-06 00:59:08 +0000161 EverChanged |= mergeEmptyReturnBlocks(F);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000162 EverChanged |= iterativelySimplifyCFG(F, TTI, AC, BonusInstThreshold);
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000163
Chris Lattner61ce4df2007-11-13 07:32:38 +0000164 // If neither pass changed anything, we're done.
165 if (!EverChanged) return false;
166
Jim Grosbach30c42822012-09-06 00:59:08 +0000167 // iterativelySimplifyCFG can (rarely) make some loops dead. If this happens,
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000168 // removeUnreachableBlocks is needed to nuke them, which means we should
Chris Lattner61ce4df2007-11-13 07:32:38 +0000169 // iterate between the two optimizations. We structure the code like this to
Sanjay Patel64ea2072015-06-24 20:42:33 +0000170 // avoid rerunning iterativelySimplifyCFG if the second pass of
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000171 // removeUnreachableBlocks doesn't do anything.
172 if (!removeUnreachableBlocks(F))
Chris Lattner61ce4df2007-11-13 07:32:38 +0000173 return true;
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000174
Chris Lattner61ce4df2007-11-13 07:32:38 +0000175 do {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000176 EverChanged = iterativelySimplifyCFG(F, TTI, AC, BonusInstThreshold);
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000177 EverChanged |= removeUnreachableBlocks(F);
Chris Lattner61ce4df2007-11-13 07:32:38 +0000178 } while (EverChanged);
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000179
Chris Lattner61ce4df2007-11-13 07:32:38 +0000180 return true;
181}
Chandler Carruthfdffd872015-02-01 11:34:21 +0000182
183SimplifyCFGPass::SimplifyCFGPass()
184 : BonusInstThreshold(UserBonusInstThreshold) {}
185
186SimplifyCFGPass::SimplifyCFGPass(int BonusInstThreshold)
187 : BonusInstThreshold(BonusInstThreshold) {}
188
189PreservedAnalyses SimplifyCFGPass::run(Function &F,
Chandler Carruthb47f8012016-03-11 11:05:24 +0000190 AnalysisManager<Function> &AM) {
191 auto &TTI = AM.getResult<TargetIRAnalysis>(F);
192 auto &AC = AM.getResult<AssumptionAnalysis>(F);
Chandler Carruthfdffd872015-02-01 11:34:21 +0000193
Justin Bognerfd757642016-01-15 21:21:39 +0000194 if (simplifyFunctionCFG(F, TTI, &AC, BonusInstThreshold))
Chandler Carruthfdffd872015-02-01 11:34:21 +0000195 return PreservedAnalyses::none();
196
197 return PreservedAnalyses::all();
198}
199
Dehao Chenf50c67c2016-05-05 20:47:53 +0000200namespace {
201struct CFGSimplifyPass : public FunctionPass {
202 static char ID; // Pass identification, replacement for typeid
203 unsigned BonusInstThreshold;
204 std::function<bool(const Function &)> PredicateFtor;
Akira Hatanaka4a616192015-06-08 18:50:43 +0000205
Dehao Chenf50c67c2016-05-05 20:47:53 +0000206 CFGSimplifyPass(int T = -1,
207 std::function<bool(const Function &)> Ftor = nullptr)
Benjamin Kramer82de7d32016-05-27 14:27:24 +0000208 : FunctionPass(ID), PredicateFtor(std::move(Ftor)) {
Dehao Chenf50c67c2016-05-05 20:47:53 +0000209 BonusInstThreshold = (T == -1) ? UserBonusInstThreshold : unsigned(T);
210 initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry());
211 }
212 bool runOnFunction(Function &F) override {
213 if (skipFunction(F) || (PredicateFtor && !PredicateFtor(F)))
214 return false;
Chandler Carruthfdffd872015-02-01 11:34:21 +0000215
Dehao Chenf50c67c2016-05-05 20:47:53 +0000216 AssumptionCache *AC =
217 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
218 const TargetTransformInfo &TTI =
219 getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
220 return simplifyFunctionCFG(F, TTI, AC, BonusInstThreshold);
221 }
Chandler Carruthfdffd872015-02-01 11:34:21 +0000222
Dehao Chenf50c67c2016-05-05 20:47:53 +0000223 void getAnalysisUsage(AnalysisUsage &AU) const override {
224 AU.addRequired<AssumptionCacheTracker>();
225 AU.addRequired<TargetTransformInfoWrapperPass>();
226 AU.addPreserved<GlobalsAAWrapperPass>();
227 }
228};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000229}
Chandler Carruthfdffd872015-02-01 11:34:21 +0000230
231char CFGSimplifyPass::ID = 0;
232INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
233 false)
234INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
235INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
236INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
237 false)
238
239// Public interface to the CFGSimplification pass
Akira Hatanaka4a616192015-06-08 18:50:43 +0000240FunctionPass *
241llvm::createCFGSimplificationPass(int Threshold,
242 std::function<bool(const Function &)> Ftor) {
243 return new CFGSimplifyPass(Threshold, Ftor);
Chandler Carruthfdffd872015-02-01 11:34:21 +0000244}