LLVM API Documentation

SimplifyCFGPass.cpp
Go to the documentation of this file.
00001 //===- SimplifyCFGPass.cpp - CFG Simplification Pass ----------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements dead code elimination and basic block merging, along
00011 // with a collection of other peephole control flow optimizations.  For example:
00012 //
00013 //   * Removes basic blocks with no predecessors.
00014 //   * Merges a basic block into its predecessor if there is only one and the
00015 //     predecessor only has one successor.
00016 //   * Eliminates PHI nodes for basic blocks with a single predecessor.
00017 //   * Eliminates a basic block that only contains an unconditional branch.
00018 //   * Changes invoke instructions to nounwind functions to be calls.
00019 //   * Change things like "if (x) if (y)" into "if (x&y)".
00020 //   * etc..
00021 //
00022 //===----------------------------------------------------------------------===//
00023 
00024 #include "llvm/Transforms/Scalar.h"
00025 #include "llvm/ADT/SmallPtrSet.h"
00026 #include "llvm/ADT/SmallVector.h"
00027 #include "llvm/ADT/Statistic.h"
00028 #include "llvm/Analysis/AssumptionTracker.h"
00029 #include "llvm/Analysis/TargetTransformInfo.h"
00030 #include "llvm/IR/Attributes.h"
00031 #include "llvm/IR/CFG.h"
00032 #include "llvm/IR/Constants.h"
00033 #include "llvm/IR/DataLayout.h"
00034 #include "llvm/IR/Instructions.h"
00035 #include "llvm/IR/IntrinsicInst.h"
00036 #include "llvm/IR/Module.h"
00037 #include "llvm/Pass.h"
00038 #include "llvm/Support/CommandLine.h"
00039 #include "llvm/Transforms/Utils/Local.h"
00040 using namespace llvm;
00041 
00042 #define DEBUG_TYPE "simplifycfg"
00043 
00044 static cl::opt<unsigned>
00045 UserBonusInstThreshold("bonus-inst-threshold", cl::Hidden, cl::init(1),
00046    cl::desc("Control the number of bonus instructions (default = 1)"));
00047 
00048 STATISTIC(NumSimpl, "Number of blocks simplified");
00049 
00050 namespace {
00051 struct CFGSimplifyPass : public FunctionPass {
00052   static char ID; // Pass identification, replacement for typeid
00053   unsigned BonusInstThreshold;
00054   CFGSimplifyPass(int T = -1) : FunctionPass(ID) {
00055     BonusInstThreshold = (T == -1) ? UserBonusInstThreshold : unsigned(T);
00056     initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry());
00057   }
00058   bool runOnFunction(Function &F) override;
00059 
00060   void getAnalysisUsage(AnalysisUsage &AU) const override {
00061     AU.addRequired<AssumptionTracker>();
00062     AU.addRequired<TargetTransformInfo>();
00063   }
00064 };
00065 }
00066 
00067 char CFGSimplifyPass::ID = 0;
00068 INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
00069                       false)
00070 INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
00071 INITIALIZE_PASS_DEPENDENCY(AssumptionTracker)
00072 INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
00073                     false)
00074 
00075 // Public interface to the CFGSimplification pass
00076 FunctionPass *llvm::createCFGSimplificationPass(int Threshold) {
00077   return new CFGSimplifyPass(Threshold);
00078 }
00079 
00080 /// mergeEmptyReturnBlocks - If we have more than one empty (other than phi
00081 /// node) return blocks, merge them together to promote recursive block merging.
00082 static bool mergeEmptyReturnBlocks(Function &F) {
00083   bool Changed = false;
00084 
00085   BasicBlock *RetBlock = nullptr;
00086 
00087   // Scan all the blocks in the function, looking for empty return blocks.
00088   for (Function::iterator BBI = F.begin(), E = F.end(); BBI != E; ) {
00089     BasicBlock &BB = *BBI++;
00090 
00091     // Only look at return blocks.
00092     ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator());
00093     if (!Ret) continue;
00094 
00095     // Only look at the block if it is empty or the only other thing in it is a
00096     // single PHI node that is the operand to the return.
00097     if (Ret != &BB.front()) {
00098       // Check for something else in the block.
00099       BasicBlock::iterator I = Ret;
00100       --I;
00101       // Skip over debug info.
00102       while (isa<DbgInfoIntrinsic>(I) && I != BB.begin())
00103         --I;
00104       if (!isa<DbgInfoIntrinsic>(I) &&
00105           (!isa<PHINode>(I) || I != BB.begin() ||
00106            Ret->getNumOperands() == 0 ||
00107            Ret->getOperand(0) != I))
00108         continue;
00109     }
00110 
00111     // If this is the first returning block, remember it and keep going.
00112     if (!RetBlock) {
00113       RetBlock = &BB;
00114       continue;
00115     }
00116 
00117     // Otherwise, we found a duplicate return block.  Merge the two.
00118     Changed = true;
00119 
00120     // Case when there is no input to the return or when the returned values
00121     // agree is trivial.  Note that they can't agree if there are phis in the
00122     // blocks.
00123     if (Ret->getNumOperands() == 0 ||
00124         Ret->getOperand(0) ==
00125           cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0)) {
00126       BB.replaceAllUsesWith(RetBlock);
00127       BB.eraseFromParent();
00128       continue;
00129     }
00130 
00131     // If the canonical return block has no PHI node, create one now.
00132     PHINode *RetBlockPHI = dyn_cast<PHINode>(RetBlock->begin());
00133     if (!RetBlockPHI) {
00134       Value *InVal = cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0);
00135       pred_iterator PB = pred_begin(RetBlock), PE = pred_end(RetBlock);
00136       RetBlockPHI = PHINode::Create(Ret->getOperand(0)->getType(),
00137                                     std::distance(PB, PE), "merge",
00138                                     &RetBlock->front());
00139 
00140       for (pred_iterator PI = PB; PI != PE; ++PI)
00141         RetBlockPHI->addIncoming(InVal, *PI);
00142       RetBlock->getTerminator()->setOperand(0, RetBlockPHI);
00143     }
00144 
00145     // Turn BB into a block that just unconditionally branches to the return
00146     // block.  This handles the case when the two return blocks have a common
00147     // predecessor but that return different things.
00148     RetBlockPHI->addIncoming(Ret->getOperand(0), &BB);
00149     BB.getTerminator()->eraseFromParent();
00150     BranchInst::Create(RetBlock, &BB);
00151   }
00152 
00153   return Changed;
00154 }
00155 
00156 /// iterativelySimplifyCFG - Call SimplifyCFG on all the blocks in the function,
00157 /// iterating until no more changes are made.
00158 static bool iterativelySimplifyCFG(Function &F, const TargetTransformInfo &TTI,
00159                                    const DataLayout *DL,
00160                                    AssumptionTracker *AT,
00161                                    unsigned BonusInstThreshold) {
00162   bool Changed = false;
00163   bool LocalChange = true;
00164   while (LocalChange) {
00165     LocalChange = false;
00166 
00167     // Loop over all of the basic blocks and remove them if they are unneeded...
00168     //
00169     for (Function::iterator BBIt = F.begin(); BBIt != F.end(); ) {
00170       if (SimplifyCFG(BBIt++, TTI, BonusInstThreshold, DL, AT)) {
00171         LocalChange = true;
00172         ++NumSimpl;
00173       }
00174     }
00175     Changed |= LocalChange;
00176   }
00177   return Changed;
00178 }
00179 
00180 // It is possible that we may require multiple passes over the code to fully
00181 // simplify the CFG.
00182 //
00183 bool CFGSimplifyPass::runOnFunction(Function &F) {
00184   if (skipOptnoneFunction(F))
00185     return false;
00186 
00187   AssumptionTracker *AT = &getAnalysis<AssumptionTracker>();
00188   const TargetTransformInfo &TTI = getAnalysis<TargetTransformInfo>();
00189   DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
00190   const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr;
00191   bool EverChanged = removeUnreachableBlocks(F);
00192   EverChanged |= mergeEmptyReturnBlocks(F);
00193   EverChanged |= iterativelySimplifyCFG(F, TTI, DL, AT, BonusInstThreshold);
00194 
00195   // If neither pass changed anything, we're done.
00196   if (!EverChanged) return false;
00197 
00198   // iterativelySimplifyCFG can (rarely) make some loops dead.  If this happens,
00199   // removeUnreachableBlocks is needed to nuke them, which means we should
00200   // iterate between the two optimizations.  We structure the code like this to
00201   // avoid reruning iterativelySimplifyCFG if the second pass of
00202   // removeUnreachableBlocks doesn't do anything.
00203   if (!removeUnreachableBlocks(F))
00204     return true;
00205 
00206   do {
00207     EverChanged = iterativelySimplifyCFG(F, TTI, DL, AT, BonusInstThreshold);
00208     EverChanged |= removeUnreachableBlocks(F);
00209   } while (EverChanged);
00210 
00211   return true;
00212 }