blob: 5d5606ba47b0d5dec7ca2f523f646ebd10a79aee [file] [log] [blame]
Chris Lattner4186ad52008-05-14 20:38:44 +00001//===- SimplifyCFGPass.cpp - CFG Simplification Pass ----------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner573527b2002-05-21 20:49:37 +00009//
Chris Lattner4cf4b692007-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 Lattner573527b2002-05-21 20:49:37 +000012//
Gordon Henriksenc86b6772007-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 Lattner573527b2002-05-21 20:49:37 +000015// predecessor only has one successor.
Gordon Henriksenc86b6772007-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 Lattner4cf4b692007-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 Lattner573527b2002-05-21 20:49:37 +000021//
22//===----------------------------------------------------------------------===//
23
24#include "llvm/Transforms/Scalar.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/SmallVector.h"
27#include "llvm/ADT/Statistic.h"
Chandler Carruthbe049292013-01-07 03:08:10 +000028#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000029#include "llvm/IR/Attributes.h"
Stephen Hines36b56882014-04-23 16:57:46 -070030#include "llvm/IR/CFG.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000031#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 Lattner573527b2002-05-21 20:49:37 +000036#include "llvm/Pass.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000037#include "llvm/Transforms/Utils/Local.h"
Chris Lattnerd7456022004-01-09 06:02:20 +000038using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000039
Stephen Hinesdce4a402014-05-29 02:49:00 -070040#define DEBUG_TYPE "simplifycfg"
41
Chris Lattner0e5f4992006-12-19 21:40:18 +000042STATISTIC(NumSimpl, "Number of blocks simplified");
Chris Lattnera92f6962002-10-01 22:38:41 +000043
Chris Lattner0e5f4992006-12-19 21:40:18 +000044namespace {
Tom Stellard57e6b2d2013-07-27 00:01:07 +000045struct CFGSimplifyPass : public FunctionPass {
Tom Stellard01d72032013-08-06 02:43:45 +000046 static char ID; // Pass identification, replacement for typeid
47 CFGSimplifyPass() : FunctionPass(ID) {
48 initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry());
49 }
Stephen Hines36b56882014-04-23 16:57:46 -070050 bool runOnFunction(Function &F) override;
Devang Patel794fd752007-05-01 21:15:47 +000051
Stephen Hines36b56882014-04-23 16:57:46 -070052 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tom Stellard57e6b2d2013-07-27 00:01:07 +000053 AU.addRequired<TargetTransformInfo>();
54 }
Tom Stellard57e6b2d2013-07-27 00:01:07 +000055};
Chris Lattner573527b2002-05-21 20:49:37 +000056}
57
Tom Stellard01d72032013-08-06 02:43:45 +000058char CFGSimplifyPass::ID = 0;
59INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
Tom Stellard57e6b2d2013-07-27 00:01:07 +000060 false)
Chandler Carruth5f46c3c2013-01-07 03:53:25 +000061INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
Tom Stellard01d72032013-08-06 02:43:45 +000062INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
Tom Stellard57e6b2d2013-07-27 00:01:07 +000063 false)
Dan Gohman844731a2008-05-13 00:00:25 +000064
Brian Gaeked0fde302003-11-11 22:41:34 +000065// Public interface to the CFGSimplification pass
Tom Stellard01d72032013-08-06 02:43:45 +000066FunctionPass *llvm::createCFGSimplificationPass() {
67 return new CFGSimplifyPass();
Chris Lattner573527b2002-05-21 20:49:37 +000068}
69
Jim Grosbach557a20a2012-09-06 00:59:08 +000070/// mergeEmptyReturnBlocks - If we have more than one empty (other than phi
Chris Lattner1a0e7082009-12-22 06:07:30 +000071/// node) return blocks, merge them together to promote recursive block merging.
Jim Grosbach557a20a2012-09-06 00:59:08 +000072static bool mergeEmptyReturnBlocks(Function &F) {
Chris Lattner1a0e7082009-12-22 06:07:30 +000073 bool Changed = false;
Nadav Rotema94d6e82012-07-24 10:51:42 +000074
Stephen Hinesdce4a402014-05-29 02:49:00 -070075 BasicBlock *RetBlock = nullptr;
Nadav Rotema94d6e82012-07-24 10:51:42 +000076
Chris Lattner1a0e7082009-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 Rotema94d6e82012-07-24 10:51:42 +000080
Chris Lattner1a0e7082009-12-22 06:07:30 +000081 // Only look at return blocks.
82 ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator());
Stephen Hinesdce4a402014-05-29 02:49:00 -070083 if (!Ret) continue;
Nadav Rotema94d6e82012-07-24 10:51:42 +000084
Chris Lattner1a0e7082009-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.
89 BasicBlock::iterator I = Ret;
90 --I;
Bill Wendlingead138b2010-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) &&
95 (!isa<PHINode>(I) || I != BB.begin() ||
96 Ret->getNumOperands() == 0 ||
97 Ret->getOperand(0) != I))
Chris Lattner1a0e7082009-12-22 06:07:30 +000098 continue;
99 }
Bill Wendlingead138b2010-03-14 10:40:55 +0000100
Chris Lattner1a0e7082009-12-22 06:07:30 +0000101 // If this is the first returning block, remember it and keep going.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700102 if (!RetBlock) {
Chris Lattner1a0e7082009-12-22 06:07:30 +0000103 RetBlock = &BB;
104 continue;
105 }
Nadav Rotema94d6e82012-07-24 10:51:42 +0000106
Chris Lattner1a0e7082009-12-22 06:07:30 +0000107 // Otherwise, we found a duplicate return block. Merge the two.
108 Changed = true;
Nadav Rotema94d6e82012-07-24 10:51:42 +0000109
Chris Lattner1a0e7082009-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 Rotema94d6e82012-07-24 10:51:42 +0000114 Ret->getOperand(0) ==
Chris Lattner1a0e7082009-12-22 06:07:30 +0000115 cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0)) {
116 BB.replaceAllUsesWith(RetBlock);
117 BB.eraseFromParent();
118 continue;
119 }
Nadav Rotema94d6e82012-07-24 10:51:42 +0000120
Chris Lattner1a0e7082009-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());
Stephen Hinesdce4a402014-05-29 02:49:00 -0700123 if (!RetBlockPHI) {
Devang Patelca704952010-03-15 19:05:46 +0000124 Value *InVal = cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0);
Jay Foadd8b4fb42011-03-30 11:19:20 +0000125 pred_iterator PB = pred_begin(RetBlock), PE = pred_end(RetBlock);
Jay Foad3ecfc862011-03-30 11:28:46 +0000126 RetBlockPHI = PHINode::Create(Ret->getOperand(0)->getType(),
127 std::distance(PB, PE), "merge",
Chris Lattner1a0e7082009-12-22 06:07:30 +0000128 &RetBlock->front());
Nadav Rotema94d6e82012-07-24 10:51:42 +0000129
Jay Foadd8b4fb42011-03-30 11:19:20 +0000130 for (pred_iterator PI = PB; PI != PE; ++PI)
Chris Lattner1a0e7082009-12-22 06:07:30 +0000131 RetBlockPHI->addIncoming(InVal, *PI);
132 RetBlock->getTerminator()->setOperand(0, RetBlockPHI);
133 }
Nadav Rotema94d6e82012-07-24 10:51:42 +0000134
Chris Lattner1a0e7082009-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 Rotema94d6e82012-07-24 10:51:42 +0000142
Chris Lattner1a0e7082009-12-22 06:07:30 +0000143 return Changed;
144}
145
Jim Grosbach557a20a2012-09-06 00:59:08 +0000146/// iterativelySimplifyCFG - Call SimplifyCFG on all the blocks in the function,
Chris Lattnerb42c8f72007-11-13 07:32:38 +0000147/// iterating until no more changes are made.
Chandler Carruth5f46c3c2013-01-07 03:53:25 +0000148static bool iterativelySimplifyCFG(Function &F, const TargetTransformInfo &TTI,
Stephen Hines36b56882014-04-23 16:57:46 -0700149 const DataLayout *DL) {
Chris Lattnerb42c8f72007-11-13 07:32:38 +0000150 bool Changed = false;
Chris Lattner573527b2002-05-21 20:49:37 +0000151 bool LocalChange = true;
152 while (LocalChange) {
153 LocalChange = false;
Nadav Rotema94d6e82012-07-24 10:51:42 +0000154
Dan Gohmane2c6d132010-08-14 00:29:42 +0000155 // Loop over all of the basic blocks and remove them if they are unneeded...
Chris Lattner573527b2002-05-21 20:49:37 +0000156 //
Dan Gohmane2c6d132010-08-14 00:29:42 +0000157 for (Function::iterator BBIt = F.begin(); BBIt != F.end(); ) {
Stephen Hines36b56882014-04-23 16:57:46 -0700158 if (SimplifyCFG(BBIt++, TTI, DL)) {
Chris Lattner573527b2002-05-21 20:49:37 +0000159 LocalChange = true;
160 ++NumSimpl;
Chris Lattner573527b2002-05-21 20:49:37 +0000161 }
162 }
163 Changed |= LocalChange;
164 }
Chris Lattner573527b2002-05-21 20:49:37 +0000165 return Changed;
166}
Chris Lattnerb42c8f72007-11-13 07:32:38 +0000167
168// It is possible that we may require multiple passes over the code to fully
169// simplify the CFG.
170//
171bool CFGSimplifyPass::runOnFunction(Function &F) {
Stephen Hines36b56882014-04-23 16:57:46 -0700172 if (skipOptnoneFunction(F))
173 return false;
174
Chandler Carruth5f46c3c2013-01-07 03:53:25 +0000175 const TargetTransformInfo &TTI = getAnalysis<TargetTransformInfo>();
Stephen Hines36b56882014-04-23 16:57:46 -0700176 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700177 const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr;
Peter Collingbourne4f96b7e2013-08-12 22:38:43 +0000178 bool EverChanged = removeUnreachableBlocks(F);
Jim Grosbach557a20a2012-09-06 00:59:08 +0000179 EverChanged |= mergeEmptyReturnBlocks(F);
Stephen Hines36b56882014-04-23 16:57:46 -0700180 EverChanged |= iterativelySimplifyCFG(F, TTI, DL);
Jakob Stoklund Olesen58e9ee82010-02-05 22:03:18 +0000181
Chris Lattnerb42c8f72007-11-13 07:32:38 +0000182 // If neither pass changed anything, we're done.
183 if (!EverChanged) return false;
184
Jim Grosbach557a20a2012-09-06 00:59:08 +0000185 // iterativelySimplifyCFG can (rarely) make some loops dead. If this happens,
Peter Collingbourne4f96b7e2013-08-12 22:38:43 +0000186 // removeUnreachableBlocks is needed to nuke them, which means we should
Chris Lattnerb42c8f72007-11-13 07:32:38 +0000187 // iterate between the two optimizations. We structure the code like this to
Jim Grosbach557a20a2012-09-06 00:59:08 +0000188 // avoid reruning iterativelySimplifyCFG if the second pass of
Peter Collingbourne4f96b7e2013-08-12 22:38:43 +0000189 // removeUnreachableBlocks doesn't do anything.
190 if (!removeUnreachableBlocks(F))
Chris Lattnerb42c8f72007-11-13 07:32:38 +0000191 return true;
Jakob Stoklund Olesen58e9ee82010-02-05 22:03:18 +0000192
Chris Lattnerb42c8f72007-11-13 07:32:38 +0000193 do {
Stephen Hines36b56882014-04-23 16:57:46 -0700194 EverChanged = iterativelySimplifyCFG(F, TTI, DL);
Peter Collingbourne4f96b7e2013-08-12 22:38:43 +0000195 EverChanged |= removeUnreachableBlocks(F);
Chris Lattnerb42c8f72007-11-13 07:32:38 +0000196 } while (EverChanged);
Jakob Stoklund Olesen58e9ee82010-02-05 22:03:18 +0000197
Chris Lattnerb42c8f72007-11-13 07:32:38 +0000198 return true;
199}