LLVM API Documentation

BasicBlockUtils.h
Go to the documentation of this file.
00001 //===-- Transform/Utils/BasicBlockUtils.h - BasicBlock Utils ----*- C++ -*-===//
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 family of functions perform manipulations on basic blocks, and
00011 // instructions contained within basic blocks.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H
00016 #define LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H
00017 
00018 // FIXME: Move to this file: BasicBlock::removePredecessor, BB::splitBasicBlock
00019 
00020 #include "llvm/IR/BasicBlock.h"
00021 #include "llvm/IR/CFG.h"
00022 
00023 namespace llvm {
00024 
00025 class AliasAnalysis;
00026 class MemoryDependenceAnalysis;
00027 class DominatorTree;
00028 class LoopInfo;
00029 class Instruction;
00030 class MDNode;
00031 class ReturnInst;
00032 class TargetLibraryInfo;
00033 class TerminatorInst;
00034 
00035 /// DeleteDeadBlock - Delete the specified block, which must have no
00036 /// predecessors.
00037 void DeleteDeadBlock(BasicBlock *BB);
00038 
00039 /// FoldSingleEntryPHINodes - We know that BB has one predecessor.  If there are
00040 /// any single-entry PHI nodes in it, fold them away.  This handles the case
00041 /// when all entries to the PHI nodes in a block are guaranteed equal, such as
00042 /// when the block has exactly one predecessor.
00043 void FoldSingleEntryPHINodes(BasicBlock *BB, AliasAnalysis *AA = nullptr,
00044                              MemoryDependenceAnalysis *MemDep = nullptr);
00045 
00046 /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
00047 /// is dead. Also recursively delete any operands that become dead as
00048 /// a result. This includes tracing the def-use list from the PHI to see if
00049 /// it is ultimately unused or if it reaches an unused cycle. Return true
00050 /// if any PHIs were deleted.
00051 bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI = nullptr);
00052 
00053 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
00054 /// if possible.  The return value indicates success or failure.
00055 bool MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT = nullptr,
00056                                LoopInfo *LI = nullptr,
00057                                AliasAnalysis *AA = nullptr,
00058                                MemoryDependenceAnalysis *MemDep = nullptr);
00059 
00060 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
00061 // with a value, then remove and delete the original instruction.
00062 //
00063 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
00064                           BasicBlock::iterator &BI, Value *V);
00065 
00066 // ReplaceInstWithInst - Replace the instruction specified by BI with the
00067 // instruction specified by I.  The original instruction is deleted and BI is
00068 // updated to point to the new instruction.
00069 //
00070 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
00071                          BasicBlock::iterator &BI, Instruction *I);
00072 
00073 // ReplaceInstWithInst - Replace the instruction specified by From with the
00074 // instruction specified by To.
00075 //
00076 void ReplaceInstWithInst(Instruction *From, Instruction *To);
00077 
00078 /// \brief Option class for critical edge splitting.
00079 ///
00080 /// This provides a builder interface for overriding the default options used
00081 /// during critical edge splitting.
00082 struct CriticalEdgeSplittingOptions {
00083   AliasAnalysis *AA;
00084   DominatorTree *DT;
00085   LoopInfo *LI;
00086   bool MergeIdenticalEdges;
00087   bool DontDeleteUselessPHIs;
00088   bool PreserveLCSSA;
00089 
00090   CriticalEdgeSplittingOptions()
00091       : AA(nullptr), DT(nullptr), LI(nullptr), MergeIdenticalEdges(false),
00092         DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
00093 
00094   /// \brief Basic case of setting up all the analysis.
00095   CriticalEdgeSplittingOptions(AliasAnalysis *AA, DominatorTree *DT = nullptr,
00096                                LoopInfo *LI = nullptr)
00097       : AA(AA), DT(DT), LI(LI), MergeIdenticalEdges(false),
00098         DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
00099 
00100   /// \brief A common pattern is to preserve the dominator tree and loop
00101   /// info but not care about AA.
00102   CriticalEdgeSplittingOptions(DominatorTree *DT, LoopInfo *LI)
00103       : AA(nullptr), DT(DT), LI(LI), MergeIdenticalEdges(false),
00104         DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
00105 
00106   CriticalEdgeSplittingOptions &setMergeIdenticalEdges() {
00107     MergeIdenticalEdges = true;
00108     return *this;
00109   }
00110 
00111   CriticalEdgeSplittingOptions &setDontDeleteUselessPHIs() {
00112     DontDeleteUselessPHIs = true;
00113     return *this;
00114   }
00115 
00116   CriticalEdgeSplittingOptions &setPreserveLCSSA() {
00117     PreserveLCSSA = true;
00118     return *this;
00119   }
00120 };
00121 
00122 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
00123 /// split the critical edge.  This will update the analyses passed in through
00124 /// the option struct. This returns the new block if the edge was split, null
00125 /// otherwise.
00126 ///
00127 /// If MergeIdenticalEdges in the options struct is true (not the default),
00128 /// *all* edges from TI to the specified successor will be merged into the same
00129 /// critical edge block. This is most commonly interesting with switch
00130 /// instructions, which may have many edges to any one destination.  This
00131 /// ensures that all edges to that dest go to one block instead of each going
00132 /// to a different block, but isn't the standard definition of a "critical
00133 /// edge".
00134 ///
00135 /// It is invalid to call this function on a critical edge that starts at an
00136 /// IndirectBrInst.  Splitting these edges will almost always create an invalid
00137 /// program because the address of the new block won't be the one that is jumped
00138 /// to.
00139 ///
00140 BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
00141                               const CriticalEdgeSplittingOptions &Options =
00142                                   CriticalEdgeSplittingOptions());
00143 
00144 inline BasicBlock *
00145 SplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
00146                   const CriticalEdgeSplittingOptions &Options =
00147                       CriticalEdgeSplittingOptions()) {
00148   return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(),
00149                            Options);
00150 }
00151 
00152 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
00153 /// false.  Otherwise, split all edges between the two blocks and return true.
00154 /// This updates all of the same analyses as the other SplitCriticalEdge
00155 /// function.  If P is specified, it updates the analyses
00156 /// described above.
00157 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI,
00158                               const CriticalEdgeSplittingOptions &Options =
00159                                   CriticalEdgeSplittingOptions()) {
00160   bool MadeChange = false;
00161   TerminatorInst *TI = (*PI)->getTerminator();
00162   for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
00163     if (TI->getSuccessor(i) == Succ)
00164       MadeChange |= !!SplitCriticalEdge(TI, i, Options);
00165   return MadeChange;
00166 }
00167 
00168 /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
00169 /// and return true, otherwise return false.  This method requires that there be
00170 /// an edge between the two blocks.  It updates the analyses
00171 /// passed in the options struct
00172 inline BasicBlock *
00173 SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
00174                   const CriticalEdgeSplittingOptions &Options =
00175                       CriticalEdgeSplittingOptions()) {
00176   TerminatorInst *TI = Src->getTerminator();
00177   unsigned i = 0;
00178   while (1) {
00179     assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
00180     if (TI->getSuccessor(i) == Dst)
00181       return SplitCriticalEdge(TI, i, Options);
00182     ++i;
00183   }
00184 }
00185 
00186 // SplitAllCriticalEdges - Loop over all of the edges in the CFG,
00187 // breaking critical edges as they are found.
00188 // Returns the number of broken edges.
00189 unsigned SplitAllCriticalEdges(Function &F,
00190                                const CriticalEdgeSplittingOptions &Options =
00191                                    CriticalEdgeSplittingOptions());
00192 
00193 /// SplitEdge -  Split the edge connecting specified block.
00194 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To,
00195                       DominatorTree *DT = nullptr, LoopInfo *LI = nullptr);
00196 
00197 /// SplitBlock - Split the specified block at the specified instruction - every
00198 /// thing before SplitPt stays in Old and everything starting with SplitPt moves
00199 /// to a new block.  The two blocks are joined by an unconditional branch and
00200 /// the loop info is updated.
00201 ///
00202 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt,
00203                        DominatorTree *DT = nullptr, LoopInfo *LI = nullptr);
00204 
00205 /// SplitBlockPredecessors - This method transforms BB by introducing a new
00206 /// basic block into the function, and moving some of the predecessors of BB to
00207 /// be predecessors of the new block.  The new predecessors are indicated by the
00208 /// Preds array, which has NumPreds elements in it.  The new block is given a
00209 /// suffix of 'Suffix'.  This function returns the new block.
00210 ///
00211 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
00212 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
00213 /// In particular, it does not preserve LoopSimplify (because it's
00214 /// complicated to handle the case where one of the edges being split
00215 /// is an exit of a loop with other exits).
00216 ///
00217 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
00218                                    const char *Suffix,
00219                                    AliasAnalysis *AA = nullptr,
00220                                    DominatorTree *DT = nullptr,
00221                                    LoopInfo *LI = nullptr,
00222                                    bool PreserveLCSSA = false);
00223 
00224 /// SplitLandingPadPredecessors - This method transforms the landing pad,
00225 /// OrigBB, by introducing two new basic blocks into the function. One of those
00226 /// new basic blocks gets the predecessors listed in Preds. The other basic
00227 /// block gets the remaining predecessors of OrigBB. The landingpad instruction
00228 /// OrigBB is clone into both of the new basic blocks. The new blocks are given
00229 /// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector.
00230 ///
00231 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
00232 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular,
00233 /// it does not preserve LoopSimplify (because it's complicated to handle the
00234 /// case where one of the edges being split is an exit of a loop with other
00235 /// exits).
00236 ///
00237 void SplitLandingPadPredecessors(BasicBlock *OrigBB,
00238                                  ArrayRef<BasicBlock *> Preds,
00239                                  const char *Suffix, const char *Suffix2,
00240                                  SmallVectorImpl<BasicBlock *> &NewBBs,
00241                                  AliasAnalysis *AA = nullptr,
00242                                  DominatorTree *DT = nullptr,
00243                                  LoopInfo *LI = nullptr,
00244                                  bool PreserveLCSSA = false);
00245 
00246 /// FoldReturnIntoUncondBranch - This method duplicates the specified return
00247 /// instruction into a predecessor which ends in an unconditional branch. If
00248 /// the return instruction returns a value defined by a PHI, propagate the
00249 /// right value into the return. It returns the new return instruction in the
00250 /// predecessor.
00251 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
00252                                        BasicBlock *Pred);
00253 
00254 /// SplitBlockAndInsertIfThen - Split the containing block at the
00255 /// specified instruction - everything before and including SplitBefore stays
00256 /// in the old basic block, and everything after SplitBefore is moved to a
00257 /// new block. The two blocks are connected by a conditional branch
00258 /// (with value of Cmp being the condition).
00259 /// Before:
00260 ///   Head
00261 ///   SplitBefore
00262 ///   Tail
00263 /// After:
00264 ///   Head
00265 ///   if (Cond)
00266 ///     ThenBlock
00267 ///   SplitBefore
00268 ///   Tail
00269 ///
00270 /// If Unreachable is true, then ThenBlock ends with
00271 /// UnreachableInst, otherwise it branches to Tail.
00272 /// Returns the NewBasicBlock's terminator.
00273 ///
00274 /// Updates DT if given.
00275 TerminatorInst *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
00276                                           bool Unreachable,
00277                                           MDNode *BranchWeights = nullptr,
00278                                           DominatorTree *DT = nullptr);
00279 
00280 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
00281 /// but also creates the ElseBlock.
00282 /// Before:
00283 ///   Head
00284 ///   SplitBefore
00285 ///   Tail
00286 /// After:
00287 ///   Head
00288 ///   if (Cond)
00289 ///     ThenBlock
00290 ///   else
00291 ///     ElseBlock
00292 ///   SplitBefore
00293 ///   Tail
00294 void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
00295                                    TerminatorInst **ThenTerm,
00296                                    TerminatorInst **ElseTerm,
00297                                    MDNode *BranchWeights = nullptr);
00298 
00299 ///
00300 /// GetIfCondition - Check whether BB is the merge point of a if-region.
00301 /// If so, return the boolean condition that determines which entry into
00302 /// BB will be taken.  Also, return by references the block that will be
00303 /// entered from if the condition is true, and the block that will be
00304 /// entered if the condition is false.
00305 Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
00306                       BasicBlock *&IfFalse);
00307 } // End llvm namespace
00308 
00309 #endif