2003-09-30 20:37:50 +02:00
|
|
|
//===-- Transform/Utils/BasicBlockUtils.h - BasicBlock Utils ----*- C++ -*-===//
|
2005-04-21 22:59:05 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:59:05 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-05-07 20:52:48 +02:00
|
|
|
//
|
|
|
|
// This family of functions perform manipulations on basic blocks, and
|
|
|
|
// instructions contained within basic blocks.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_TRANSFORMS_UTILS_BASICBLOCK_H
|
|
|
|
#define LLVM_TRANSFORMS_UTILS_BASICBLOCK_H
|
|
|
|
|
|
|
|
// FIXME: Move to this file: BasicBlock::removePredecessor, BB::splitBasicBlock
|
|
|
|
|
|
|
|
#include "llvm/BasicBlock.h"
|
2003-11-10 05:42:13 +01:00
|
|
|
#include "llvm/Support/CFG.h"
|
2003-11-11 23:41:34 +01:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2002-05-07 20:52:48 +02:00
|
|
|
class Instruction;
|
2003-11-10 05:10:27 +01:00
|
|
|
class Pass;
|
2008-11-27 09:10:05 +01:00
|
|
|
class AliasAnalysis;
|
2002-05-07 20:52:48 +02:00
|
|
|
|
2008-12-03 07:40:52 +01:00
|
|
|
/// DeleteDeadBlock - Delete the specified block, which must have no
|
|
|
|
/// predecessors.
|
|
|
|
void DeleteDeadBlock(BasicBlock *BB);
|
2008-12-03 07:37:44 +01:00
|
|
|
|
2008-12-03 20:44:02 +01:00
|
|
|
|
|
|
|
/// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are
|
|
|
|
/// any single-entry PHI nodes in it, fold them away. This handles the case
|
|
|
|
/// when all entries to the PHI nodes in a block are guaranteed equal, such as
|
|
|
|
/// when the block has exactly one predecessor.
|
|
|
|
void FoldSingleEntryPHINodes(BasicBlock *BB);
|
2009-05-02 20:29:22 +02:00
|
|
|
|
|
|
|
/// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
|
|
|
|
/// is dead. Also recursively delete any operands that become dead as
|
|
|
|
/// a result. This includes tracing the def-use list from the PHI to see if
|
2010-01-05 16:45:31 +01:00
|
|
|
/// it is ultimately unused or if it reaches an unused cycle. Return true
|
|
|
|
/// if any PHIs were deleted.
|
|
|
|
bool DeleteDeadPHIs(BasicBlock *BB);
|
2009-05-02 20:29:22 +02:00
|
|
|
|
2009-10-31 18:33:01 +01:00
|
|
|
/// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
|
|
|
|
/// if possible. The return value indicates success or failure.
|
|
|
|
bool MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P = 0);
|
2008-07-17 02:01:40 +02:00
|
|
|
|
2002-05-07 20:52:48 +02:00
|
|
|
// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
|
|
|
|
// with a value, then remove and delete the original instruction.
|
|
|
|
//
|
|
|
|
void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
|
|
|
|
BasicBlock::iterator &BI, Value *V);
|
|
|
|
|
|
|
|
// ReplaceInstWithInst - Replace the instruction specified by BI with the
|
|
|
|
// instruction specified by I. The original instruction is deleted and BI is
|
|
|
|
// updated to point to the new instruction.
|
|
|
|
//
|
|
|
|
void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
|
|
|
|
BasicBlock::iterator &BI, Instruction *I);
|
|
|
|
|
|
|
|
// ReplaceInstWithInst - Replace the instruction specified by From with the
|
2002-07-30 00:32:06 +02:00
|
|
|
// instruction specified by To.
|
2002-05-07 20:52:48 +02:00
|
|
|
//
|
|
|
|
void ReplaceInstWithInst(Instruction *From, Instruction *To);
|
|
|
|
|
2009-05-04 04:25:58 +02:00
|
|
|
/// FindFunctionBackedges - Analyze the specified function to find all of the
|
|
|
|
/// loop backedges in the function and return them. This is a relatively cheap
|
|
|
|
/// (compared to computing dominators and loop info) analysis.
|
|
|
|
///
|
|
|
|
/// The output is added to Result, as pairs of <from,to> edge info.
|
|
|
|
void FindFunctionBackedges(const Function &F,
|
|
|
|
SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result);
|
2008-11-27 09:10:05 +01:00
|
|
|
|
2002-07-30 00:32:06 +02:00
|
|
|
|
2010-02-16 22:06:42 +01:00
|
|
|
/// GetSuccessorNumber - Search for the specified successor of basic block BB
|
|
|
|
/// and return its position in the terminator instruction's list of
|
|
|
|
/// successors. It is an error to call this with a block that is not a
|
|
|
|
/// successor.
|
|
|
|
unsigned GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ);
|
2010-02-16 20:49:17 +01:00
|
|
|
|
2003-11-10 05:10:27 +01:00
|
|
|
/// isCriticalEdge - Return true if the specified edge is a critical edge.
|
|
|
|
/// Critical edges are edges from a block with multiple successors to a block
|
|
|
|
/// with multiple predecessors.
|
|
|
|
///
|
2006-10-28 08:58:17 +02:00
|
|
|
bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum,
|
|
|
|
bool AllowIdenticalEdges = false);
|
2003-11-10 05:10:27 +01:00
|
|
|
|
|
|
|
/// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
|
2008-02-25 20:21:38 +01:00
|
|
|
/// split the critical edge. This will update DominatorTree and
|
|
|
|
/// DominatorFrontier information if it is available, thus calling this pass
|
2009-10-31 22:51:10 +01:00
|
|
|
/// will not invalidate either of them. This returns the new block if the edge
|
|
|
|
/// was split, null otherwise.
|
2008-02-25 20:21:38 +01:00
|
|
|
///
|
2008-12-03 20:18:54 +01:00
|
|
|
/// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the
|
2007-06-08 02:02:08 +02:00
|
|
|
/// specified successor will be merged into the same critical edge block.
|
|
|
|
/// This is most commonly interesting with switch instructions, which may
|
2008-12-03 20:18:54 +01:00
|
|
|
/// have many edges to any one destination. This ensures that all edges to that
|
2007-06-08 02:02:08 +02:00
|
|
|
/// dest go to one block instead of each going to a different block, but isn't
|
|
|
|
/// the standard definition of a "critical edge".
|
2003-11-10 05:10:27 +01:00
|
|
|
///
|
2009-10-31 22:51:10 +01:00
|
|
|
/// It is invalid to call this function on a critical edge that starts at an
|
|
|
|
/// IndirectBrInst. Splitting these edges will almost always create an invalid
|
2009-11-01 20:16:21 +01:00
|
|
|
/// program because the address of the new block won't be the one that is jumped
|
2009-10-31 22:51:10 +01:00
|
|
|
/// to.
|
|
|
|
///
|
2009-09-08 17:45:00 +02:00
|
|
|
BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
|
|
|
|
Pass *P = 0, bool MergeIdenticalEdges = false);
|
2003-11-10 05:10:27 +01:00
|
|
|
|
2009-10-31 22:51:10 +01:00
|
|
|
inline BasicBlock *SplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
|
|
|
|
Pass *P = 0) {
|
2003-11-10 05:42:13 +01:00
|
|
|
return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
|
|
|
|
/// false. Otherwise, split all edges between the two blocks and return true.
|
|
|
|
/// This updates all of the same analyses as the other SplitCriticalEdge
|
2006-05-14 04:01:22 +02:00
|
|
|
/// function. If P is specified, it updates the analyses
|
|
|
|
/// described above.
|
2003-11-10 05:42:13 +01:00
|
|
|
inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, Pass *P = 0) {
|
|
|
|
bool MadeChange = false;
|
2006-02-10 22:32:11 +01:00
|
|
|
TerminatorInst *TI = (*PI)->getTerminator();
|
|
|
|
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
|
|
|
|
if (TI->getSuccessor(i) == Succ)
|
2009-09-08 17:45:00 +02:00
|
|
|
MadeChange |= !!SplitCriticalEdge(TI, i, P);
|
2003-11-10 05:42:13 +01:00
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2006-05-14 04:01:22 +02:00
|
|
|
/// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
|
|
|
|
/// and return true, otherwise return false. This method requires that there be
|
|
|
|
/// an edge between the two blocks. If P is specified, it updates the analyses
|
|
|
|
/// described above.
|
2009-09-08 17:45:00 +02:00
|
|
|
inline BasicBlock *SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
|
|
|
|
Pass *P = 0,
|
|
|
|
bool MergeIdenticalEdges = false) {
|
2006-02-10 22:32:11 +01:00
|
|
|
TerminatorInst *TI = Src->getTerminator();
|
2006-05-14 04:01:22 +02:00
|
|
|
unsigned i = 0;
|
|
|
|
while (1) {
|
|
|
|
assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
|
2006-02-10 22:32:11 +01:00
|
|
|
if (TI->getSuccessor(i) == Dst)
|
2006-10-28 08:44:56 +02:00
|
|
|
return SplitCriticalEdge(TI, i, P, MergeIdenticalEdges);
|
2006-05-14 04:01:22 +02:00
|
|
|
++i;
|
|
|
|
}
|
2005-08-17 08:34:37 +02:00
|
|
|
}
|
2007-07-06 23:39:20 +02:00
|
|
|
|
|
|
|
/// SplitEdge - Split the edge connecting specified block. Pass P must
|
|
|
|
/// not be NULL.
|
|
|
|
BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P);
|
|
|
|
|
|
|
|
/// SplitBlock - Split the specified block at the specified instruction - every
|
|
|
|
/// thing before SplitPt stays in Old and everything starting with SplitPt moves
|
|
|
|
/// to a new block. The two blocks are joined by an unconditional branch and
|
|
|
|
/// the loop info is updated.
|
|
|
|
///
|
|
|
|
BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P);
|
2008-04-21 03:28:02 +02:00
|
|
|
|
|
|
|
/// SplitBlockPredecessors - This method transforms BB by introducing a new
|
|
|
|
/// basic block into the function, and moving some of the predecessors of BB to
|
|
|
|
/// be predecessors of the new block. The new predecessors are indicated by the
|
|
|
|
/// Preds array, which has NumPreds elements in it. The new block is given a
|
|
|
|
/// suffix of 'Suffix'. This function returns the new block.
|
|
|
|
///
|
2009-09-08 17:45:00 +02:00
|
|
|
/// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
|
|
|
|
/// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
|
|
|
|
/// In particular, it does not preserve LoopSimplify (because it's
|
|
|
|
/// complicated to handle the case where one of the edges being split
|
|
|
|
/// is an exit of a loop with other exits).
|
|
|
|
///
|
2008-04-21 03:28:02 +02:00
|
|
|
BasicBlock *SplitBlockPredecessors(BasicBlock *BB, BasicBlock *const *Preds,
|
|
|
|
unsigned NumPreds, const char *Suffix,
|
|
|
|
Pass *P = 0);
|
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
} // End llvm namespace
|
2003-11-10 05:42:13 +01:00
|
|
|
|
2002-05-07 20:52:48 +02:00
|
|
|
#endif
|