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/Transforms/Utils/Local.h"
00039 using namespace llvm;
00040 
00041 #define DEBUG_TYPE "simplifycfg"
00042 
00043 STATISTIC(NumSimpl, "Number of blocks simplified");
00044 
00045 namespace {
00046 struct CFGSimplifyPass : public FunctionPass {
00047   static char ID; // Pass identification, replacement for typeid
00048   CFGSimplifyPass() : FunctionPass(ID) {
00049     initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry());
00050   }
00051   bool runOnFunction(Function &F) override;
00052 
00053   void getAnalysisUsage(AnalysisUsage &AU) const override {
00054     AU.addRequired<AssumptionTracker>();
00055     AU.addRequired<TargetTransformInfo>();
00056   }
00057 };
00058 }
00059 
00060 char CFGSimplifyPass::ID = 0;
00061 INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
00062                       false)
00063 INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
00064 INITIALIZE_PASS_DEPENDENCY(AssumptionTracker)
00065 INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
00066                     false)
00067 
00068 // Public interface to the CFGSimplification pass
00069 FunctionPass *llvm::createCFGSimplificationPass() {
00070   return new CFGSimplifyPass();
00071 }
00072 
00073 /// mergeEmptyReturnBlocks - If we have more than one empty (other than phi
00074 /// node) return blocks, merge them together to promote recursive block merging.
00075 static bool mergeEmptyReturnBlocks(Function &F) {
00076   bool Changed = false;
00077 
00078   BasicBlock *RetBlock = nullptr;
00079 
00080   // Scan all the blocks in the function, looking for empty return blocks.
00081   for (Function::iterator BBI = F.begin(), E = F.end(); BBI != E; ) {
00082     BasicBlock &BB = *BBI++;
00083 
00084     // Only look at return blocks.
00085     ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator());
00086     if (!Ret) continue;
00087 
00088     // Only look at the block if it is empty or the only other thing in it is a
00089     // single PHI node that is the operand to the return.
00090     if (Ret != &BB.front()) {
00091       // Check for something else in the block.
00092       BasicBlock::iterator I = Ret;
00093       --I;
00094       // Skip over debug info.
00095       while (isa<DbgInfoIntrinsic>(I) && I != BB.begin())
00096         --I;
00097       if (!isa<DbgInfoIntrinsic>(I) &&
00098           (!isa<PHINode>(I) || I != BB.begin() ||
00099            Ret->getNumOperands() == 0 ||
00100            Ret->getOperand(0) != I))
00101         continue;
00102     }
00103 
00104     // If this is the first returning block, remember it and keep going.
00105     if (!RetBlock) {
00106       RetBlock = &BB;
00107       continue;
00108     }
00109 
00110     // Otherwise, we found a duplicate return block.  Merge the two.
00111     Changed = true;
00112 
00113     // Case when there is no input to the return or when the returned values
00114     // agree is trivial.  Note that they can't agree if there are phis in the
00115     // blocks.
00116     if (Ret->getNumOperands() == 0 ||
00117         Ret->getOperand(0) ==
00118           cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0)) {
00119       BB.replaceAllUsesWith(RetBlock);
00120       BB.eraseFromParent();
00121       continue;
00122     }
00123 
00124     // If the canonical return block has no PHI node, create one now.
00125     PHINode *RetBlockPHI = dyn_cast<PHINode>(RetBlock->begin());
00126     if (!RetBlockPHI) {
00127       Value *InVal = cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0);
00128       pred_iterator PB = pred_begin(RetBlock), PE = pred_end(RetBlock);
00129       RetBlockPHI = PHINode::Create(Ret->getOperand(0)->getType(),
00130                                     std::distance(PB, PE), "merge",
00131                                     &RetBlock->front());
00132 
00133       for (pred_iterator PI = PB; PI != PE; ++PI)
00134         RetBlockPHI->addIncoming(InVal, *PI);
00135       RetBlock->getTerminator()->setOperand(0, RetBlockPHI);
00136     }
00137 
00138     // Turn BB into a block that just unconditionally branches to the return
00139     // block.  This handles the case when the two return blocks have a common
00140     // predecessor but that return different things.
00141     RetBlockPHI->addIncoming(Ret->getOperand(0), &BB);
00142     BB.getTerminator()->eraseFromParent();
00143     BranchInst::Create(RetBlock, &BB);
00144   }
00145 
00146   return Changed;
00147 }
00148 
00149 /// iterativelySimplifyCFG - Call SimplifyCFG on all the blocks in the function,
00150 /// iterating until no more changes are made.
00151 static bool iterativelySimplifyCFG(Function &F, const TargetTransformInfo &TTI,
00152                                    const DataLayout *DL,
00153                                    AssumptionTracker *AT) {
00154   bool Changed = false;
00155   bool LocalChange = true;
00156   while (LocalChange) {
00157     LocalChange = false;
00158 
00159     // Loop over all of the basic blocks and remove them if they are unneeded...
00160     //
00161     for (Function::iterator BBIt = F.begin(); BBIt != F.end(); ) {
00162       if (SimplifyCFG(BBIt++, TTI, DL, AT)) {
00163         LocalChange = true;
00164         ++NumSimpl;
00165       }
00166     }
00167     Changed |= LocalChange;
00168   }
00169   return Changed;
00170 }
00171 
00172 // It is possible that we may require multiple passes over the code to fully
00173 // simplify the CFG.
00174 //
00175 bool CFGSimplifyPass::runOnFunction(Function &F) {
00176   if (skipOptnoneFunction(F))
00177     return false;
00178 
00179   AssumptionTracker *AT = &getAnalysis<AssumptionTracker>();
00180   const TargetTransformInfo &TTI = getAnalysis<TargetTransformInfo>();
00181   DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
00182   const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr;
00183   bool EverChanged = removeUnreachableBlocks(F);
00184   EverChanged |= mergeEmptyReturnBlocks(F);
00185   EverChanged |= iterativelySimplifyCFG(F, TTI, DL, AT);
00186 
00187   // If neither pass changed anything, we're done.
00188   if (!EverChanged) return false;
00189 
00190   // iterativelySimplifyCFG can (rarely) make some loops dead.  If this happens,
00191   // removeUnreachableBlocks is needed to nuke them, which means we should
00192   // iterate between the two optimizations.  We structure the code like this to
00193   // avoid reruning iterativelySimplifyCFG if the second pass of
00194   // removeUnreachableBlocks doesn't do anything.
00195   if (!removeUnreachableBlocks(F))
00196     return true;
00197 
00198   do {
00199     EverChanged = iterativelySimplifyCFG(F, TTI, DL, AT);
00200     EverChanged |= removeUnreachableBlocks(F);
00201   } while (EverChanged);
00202 
00203   return true;
00204 }