blob: 8371f6d35279ebd05879b4b0f2688266090e311d [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
Chris Lattner79a42ac2006-12-19 21:40:18 +000024#define DEBUG_TYPE "simplifycfg"
Chris Lattnerfa7dad82002-05-21 20:49:37 +000025#include "llvm/Transforms/Scalar.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/ADT/SmallPtrSet.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/Statistic.h"
Chandler Carruthd3e73552013-01-07 03:08:10 +000029#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/Attributes.h"
31#include "llvm/IR/Constants.h"
32#include "llvm/IR/DataLayout.h"
33#include "llvm/IR/Instructions.h"
34#include "llvm/IR/IntrinsicInst.h"
35#include "llvm/IR/Module.h"
Chris Lattnerfa7dad82002-05-21 20:49:37 +000036#include "llvm/Pass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000037#include "llvm/Support/CFG.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000038#include "llvm/Transforms/Utils/Local.h"
Chris Lattner49525f82004-01-09 06:02:20 +000039using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000040
Chris Lattner79a42ac2006-12-19 21:40:18 +000041STATISTIC(NumSimpl, "Number of blocks simplified");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000042
Chris Lattner79a42ac2006-12-19 21:40:18 +000043namespace {
Tom Stellard8b1e0212013-07-27 00:01:07 +000044struct CFGSimplifyPass : public FunctionPass {
Tom Stellardaa664d92013-08-06 02:43:45 +000045 static char ID; // Pass identification, replacement for typeid
46 CFGSimplifyPass() : FunctionPass(ID) {
47 initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry());
48 }
Tom Stellard8b1e0212013-07-27 00:01:07 +000049 virtual bool runOnFunction(Function &F);
Devang Patel09f162c2007-05-01 21:15:47 +000050
Tom Stellard8b1e0212013-07-27 00:01:07 +000051 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.addRequired<TargetTransformInfo>();
53 }
Tom Stellard8b1e0212013-07-27 00:01:07 +000054};
Chris Lattnerfa7dad82002-05-21 20:49:37 +000055}
56
Tom Stellardaa664d92013-08-06 02:43:45 +000057char CFGSimplifyPass::ID = 0;
58INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
Tom Stellard8b1e0212013-07-27 00:01:07 +000059 false)
Chandler Carruth0b4ef9c2013-01-07 03:53:25 +000060INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
Tom Stellardaa664d92013-08-06 02:43:45 +000061INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
Tom Stellard8b1e0212013-07-27 00:01:07 +000062 false)
Dan Gohmand78c4002008-05-13 00:00:25 +000063
Brian Gaeke960707c2003-11-11 22:41:34 +000064// Public interface to the CFGSimplification pass
Tom Stellardaa664d92013-08-06 02:43:45 +000065FunctionPass *llvm::createCFGSimplificationPass() {
66 return new CFGSimplifyPass();
Chris Lattnerfa7dad82002-05-21 20:49:37 +000067}
68
Jim Grosbach30c42822012-09-06 00:59:08 +000069/// mergeEmptyReturnBlocks - If we have more than one empty (other than phi
Chris Lattnerf21a2202009-12-22 06:07:30 +000070/// node) return blocks, merge them together to promote recursive block merging.
Jim Grosbach30c42822012-09-06 00:59:08 +000071static bool mergeEmptyReturnBlocks(Function &F) {
Chris Lattnerf21a2202009-12-22 06:07:30 +000072 bool Changed = false;
Nadav Rotem465834c2012-07-24 10:51:42 +000073
Chris Lattnerf21a2202009-12-22 06:07:30 +000074 BasicBlock *RetBlock = 0;
Nadav Rotem465834c2012-07-24 10:51:42 +000075
Chris Lattnerf21a2202009-12-22 06:07:30 +000076 // Scan all the blocks in the function, looking for empty return blocks.
77 for (Function::iterator BBI = F.begin(), E = F.end(); BBI != E; ) {
78 BasicBlock &BB = *BBI++;
Nadav Rotem465834c2012-07-24 10:51:42 +000079
Chris Lattnerf21a2202009-12-22 06:07:30 +000080 // Only look at return blocks.
81 ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator());
82 if (Ret == 0) continue;
Nadav Rotem465834c2012-07-24 10:51:42 +000083
Chris Lattnerf21a2202009-12-22 06:07:30 +000084 // Only look at the block if it is empty or the only other thing in it is a
85 // single PHI node that is the operand to the return.
86 if (Ret != &BB.front()) {
87 // Check for something else in the block.
88 BasicBlock::iterator I = Ret;
89 --I;
Bill Wendling55e69d12010-03-14 10:40:55 +000090 // Skip over debug info.
91 while (isa<DbgInfoIntrinsic>(I) && I != BB.begin())
92 --I;
93 if (!isa<DbgInfoIntrinsic>(I) &&
94 (!isa<PHINode>(I) || I != BB.begin() ||
95 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.
101 if (RetBlock == 0) {
102 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());
122 if (RetBlockPHI == 0) {
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
Jim Grosbach30c42822012-09-06 00:59:08 +0000145/// iterativelySimplifyCFG - 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,
Tom Stellardaa664d92013-08-06 02:43:45 +0000148 const DataLayout *TD) {
Chris Lattner61ce4df2007-11-13 07:32:38 +0000149 bool Changed = false;
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000150 bool LocalChange = true;
151 while (LocalChange) {
152 LocalChange = false;
Nadav Rotem465834c2012-07-24 10:51:42 +0000153
Dan Gohman4a63fad2010-08-14 00:29:42 +0000154 // Loop over all of the basic blocks and remove them if they are unneeded...
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000155 //
Dan Gohman4a63fad2010-08-14 00:29:42 +0000156 for (Function::iterator BBIt = F.begin(); BBIt != F.end(); ) {
Tom Stellardaa664d92013-08-06 02:43:45 +0000157 if (SimplifyCFG(BBIt++, TTI, TD)) {
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000158 LocalChange = true;
159 ++NumSimpl;
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000160 }
161 }
162 Changed |= LocalChange;
163 }
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000164 return Changed;
165}
Chris Lattner61ce4df2007-11-13 07:32:38 +0000166
167// It is possible that we may require multiple passes over the code to fully
168// simplify the CFG.
169//
170bool CFGSimplifyPass::runOnFunction(Function &F) {
Chandler Carruth0b4ef9c2013-01-07 03:53:25 +0000171 const TargetTransformInfo &TTI = getAnalysis<TargetTransformInfo>();
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000172 const DataLayout *TD = getAnalysisIfAvailable<DataLayout>();
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000173 bool EverChanged = removeUnreachableBlocks(F);
Jim Grosbach30c42822012-09-06 00:59:08 +0000174 EverChanged |= mergeEmptyReturnBlocks(F);
Tom Stellardaa664d92013-08-06 02:43:45 +0000175 EverChanged |= iterativelySimplifyCFG(F, TTI, TD);
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000176
Chris Lattner61ce4df2007-11-13 07:32:38 +0000177 // If neither pass changed anything, we're done.
178 if (!EverChanged) return false;
179
Jim Grosbach30c42822012-09-06 00:59:08 +0000180 // iterativelySimplifyCFG can (rarely) make some loops dead. If this happens,
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000181 // removeUnreachableBlocks is needed to nuke them, which means we should
Chris Lattner61ce4df2007-11-13 07:32:38 +0000182 // iterate between the two optimizations. We structure the code like this to
Jim Grosbach30c42822012-09-06 00:59:08 +0000183 // avoid reruning iterativelySimplifyCFG if the second pass of
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000184 // removeUnreachableBlocks doesn't do anything.
185 if (!removeUnreachableBlocks(F))
Chris Lattner61ce4df2007-11-13 07:32:38 +0000186 return true;
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000187
Chris Lattner61ce4df2007-11-13 07:32:38 +0000188 do {
Tom Stellardaa664d92013-08-06 02:43:45 +0000189 EverChanged = iterativelySimplifyCFG(F, TTI, TD);
Peter Collingbourne8d642de2013-08-12 22:38:43 +0000190 EverChanged |= removeUnreachableBlocks(F);
Chris Lattner61ce4df2007-11-13 07:32:38 +0000191 } while (EverChanged);
Jakob Stoklund Olesen916f48a2010-02-05 22:03:18 +0000192
Chris Lattner61ce4df2007-11-13 07:32:38 +0000193 return true;
194}