48 while (!BB->
empty()) {
70 if (!isa<PHINode>(BB->
begin()))
return;
73 if (PN->getIncomingValue(0) != PN)
74 PN->replaceAllUsesWith(PN->getIncomingValue(0));
80 else if (AA && isa<PointerType>(PN->getType()))
83 PN->eraseFromParent();
100 bool Changed =
false;
101 for (
unsigned i = 0, e = PHIs.
size(); i != e; ++i)
102 if (
PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].
operator Value*()))
118 if (!PredBB)
return false;
121 if (PredBB == BB)
return false;
127 for (;
SI != SE; ++
SI)
128 if (*
SI != OnlySucc) {
134 if (!OnlySucc)
return false;
138 if (
PHINode *PN = dyn_cast<PHINode>(BI)) {
139 for (
Value *IncValue : PN->incoming_values())
147 if (isa<PHINode>(BB->
front()))
212 "ReplaceInstWithInst: Instruction already inserted into basic block!");
246 .setPreserveLCSSA()))
254 assert(
SP == BB &&
"CFG broken");
262 "Should have a single succ!");
269 unsigned NumBroken = 0;
288 while (isa<PHINode>(SplitIt) || isa<LandingPadInst>(SplitIt))
296 L->addBasicBlockToLoop(New, *LI);
301 std::vector<DomTreeNode *> Children;
304 Children.push_back(*
I);
307 for (std::vector<DomTreeNode *>::iterator
I = Children.begin(),
308 E = Children.end();
I != E; ++
I)
320 bool PreserveLCSSA,
bool &HasLoopExit) {
333 bool IsLoopEntry = !!L;
334 bool SplitMakesNewLoopHeader =
false;
343 if (!
PL->contains(OldBB))
353 SplitMakesNewLoopHeader =
true;
365 Loop *InnermostPredLoop =
nullptr;
367 i = Preds.
begin(), e = Preds.
end(); i != e; ++i) {
372 while (PredLoop && !PredLoop->contains(OldBB))
373 PredLoop = PredLoop->getParentLoop();
376 if (PredLoop && PredLoop->contains(OldBB) &&
377 (!InnermostPredLoop ||
378 InnermostPredLoop->
getLoopDepth() < PredLoop->getLoopDepth()))
379 InnermostPredLoop = PredLoop;
383 if (InnermostPredLoop)
387 if (SplitMakesNewLoopHeader)
404 Value *InVal =
nullptr;
450 if (PredSet.count(IncomingBB)) {
479 bool PreserveLCSSA) {
484 std::string NewName = std::string(Suffix) +
".split-lp";
487 NewBBs, AA, DT, LI, PreserveLCSSA);
500 for (
unsigned i = 0, e = Preds.
size(); i != e; ++i) {
504 assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
505 "Cannot split an edge from an IndirectBrInst");
506 Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB);
513 if (Preds.
size() == 0) {
521 bool HasLoopExit =
false;
545 const char *Suffix1,
const char *Suffix2,
549 assert(OrigBB->
isLandingPad() &&
"Trying to split a non-landing pad!");
563 for (
unsigned i = 0, e = Preds.
size(); i != e; ++i) {
567 assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
568 "Cannot split an edge from an IndirectBrInst");
569 Preds[i]->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1);
572 bool HasLoopExit =
false;
584 if (Pred == NewBB1)
continue;
586 "Cannot split an edge from an IndirectBrInst");
592 if (!NewBB2Preds.
empty()) {
605 i = NewBB2Preds.
begin(), e = NewBB2Preds.
end(); i != e; ++i)
606 (*i)->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2);
611 PreserveLCSSA, HasLoopExit);
614 UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, AA, HasLoopExit);
620 NewBB1->getInstList().insert(NewBB1->getFirstInsertionPt(), Clone1);
662 V = BCI->getOperand(0);
663 NewBC = BCI->
clone();
667 if (
PHINode *PN = dyn_cast<PHINode>(V)) {
668 if (PN->getParent() == BB) {
670 NewBC->
setOperand(0, PN->getIncomingValueForBlock(Pred));
672 *i = PN->getIncomingValueForBlock(Pred);
681 return cast<ReturnInst>(NewRet);
719 CheckTerm->setDebugLoc(SplitBefore->
getDebugLoc());
727 std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
730 for (
auto Child : Children)
766 (*ThenTerm)->setDebugLoc(SplitBefore->
getDebugLoc());
768 (*ElseTerm)->setDebugLoc(SplitBefore->
getDebugLoc());
812 if (!Pred1Br || !Pred2Br)
817 if (Pred2Br->isConditional()) {
864 if (!BI)
return nullptr;
866 assert(BI->
isConditional() &&
"Two successors but not conditional?");
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
addNewBlock - Add a new node to the dominator tree information.
ReturnInst - Return a value (possibly void), from a function.
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
void push_back(const T &Elt)
void ReplaceInstWithInst(BasicBlock::InstListType &BIL, BasicBlock::iterator &BI, Instruction *I)
ReplaceInstWithInst - Replace the instruction specified by BI with the instruction specified by I...
BasicBlock * getUniquePredecessor()
Return the predecessor of this block if it has a unique predecessor block.
void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs=false)
Notify the BasicBlock that the predecessor Pred is no longer able to reach it.
void addIncoming(Value *V, BasicBlock *BB)
addIncoming - Add an incoming value to the end of the PHI list
void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, TerminatorInst **ThenTerm, TerminatorInst **ElseTerm, MDNode *BranchWeights=nullptr)
SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, but also creates the ElseBlock...
void removeBlock(BlockT *BB)
removeBlock - This method completely removes BB from all data structures, including all of the Loop o...
const Instruction & back() const
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
SplitBlock - Split the specified block at the specified instruction - every thing before SplitPt stay...
void DeleteDeadBlock(BasicBlock *BB)
DeleteDeadBlock - Delete the specified block, which must have no predecessors.
static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB, ArrayRef< BasicBlock * > Preds, DominatorTree *DT, LoopInfo *LI, bool PreserveLCSSA, bool &HasLoopExit)
UpdateAnalysisInformation - Update DominatorTree, LoopInfo, and LCCSA analysis information.
TerminatorInst * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DominatorTree *DT=nullptr)
SplitBlockAndInsertIfThen - Split the containing block at the specified instruction - everything befo...
const Function * getParent() const
Return the enclosing method, or null if none.
const Instruction & front() const
unsigned GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ)
Search for the specified successor of basic block BB and return its position in the terminator instru...
unsigned SplitAllCriticalEdges(Function &F, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
LoopT * getLoopFor(const BlockT *BB) const
getLoopFor - Return the inner most loop that BB lives in.
StringRef getName() const
Return a constant reference to the value's name.
iterator begin()
Instruction iterator methods.
void push_back(NodeTy *val)
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
removeIncomingValue - Remove an incoming value.
Option class for critical edge splitting.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
std::vector< DomTreeNodeBase< NodeT > * >::iterator iterator
A Use represents the edge between a Value definition and its users.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Instruction * getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches, switches, etc.
MemoryDependenceAnalysis - This is an analysis that determines, for a given memory operation...
void setName(const Twine &Name)
Change the name of the value.
Instruction * clone() const
clone() - Create a copy of 'this' instruction that is identical in all ways except the following: ...
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
addBasicBlockToLoop - This method is used by other analyses to update loop information.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
bool MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, AliasAnalysis *AA=nullptr, MemoryDependenceAnalysis *MemDep=nullptr)
MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, if possible.
BasicBlock * getSuccessor(unsigned i) const
bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr)
DeleteDeadPHIs - Examine each PHI in the given block and delete it if it is dead. ...
This class represents a no-op cast from one type to another.
Base class for the actual dominator tree node.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
void ReplaceInstWithValue(BasicBlock::InstListType &BIL, BasicBlock::iterator &BI, Value *V)
ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) with a value...
void takeName(Value *V)
Transfer the name from V to this value.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
size_t size() const
size - Get the array size.
unsigned getNumIncomingValues() const
getNumIncomingValues - Return the number of incoming edges
Interval::succ_iterator succ_end(Interval *I)
void FoldSingleEntryPHINodes(BasicBlock *BB, AliasAnalysis *AA=nullptr, MemoryDependenceAnalysis *MemDep=nullptr)
FoldSingleEntryPHINodes - We know that BB has one predecessor.
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
BasicBlock * SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
SplitCriticalEdge - If this edge is a critical edge, insert a new node to split the critical edge...
LandingPadInst - The landingpad instruction holds all of the information necessary to generate correc...
Subclasses of this class are all able to terminate a basic block.
void setDebugLoc(DebugLoc Loc)
setDebugLoc - Set the debug location information for this instruction.
LLVM Basic Block Representation.
void eraseNode(NodeT *BB)
eraseNode - Removes a node from the dominator tree.
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
This is an important class for using LLVM in a threaded context.
BranchInst - Conditional or Unconditional Branch instruction.
UnreachableInst - This function has undefined behavior.
LandingPadInst * getLandingPadInst()
Return the landingpad instruction associated with the landing pad.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
BasicBlock * getIncomingBlock(unsigned i) const
getIncomingBlock - Return incoming basic block number i.
bool contains(const LoopT *L) const
contains - Return true if the specified loop is contained within in this loop.
const InstListType & getInstList() const
Return the underlying instruction list container.
iterator insert(iterator where, NodeTy *New)
Interval::pred_iterator pred_end(Interval *I)
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
static UndefValue * get(Type *T)
get() - Static factory methods - Return an 'undef' object of the specified type.
virtual void deleteValue(Value *V)
Methods that clients should call when they transform the program to allow alias analyses to update th...
iterator erase(iterator where)
void setMetadata(unsigned KindID, MDNode *Node)
setMetadata - Set the metadata of the specified kind to the specified node.
bool isConditional() const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Value * getIncomingValue(unsigned i) const
getIncomingValue - Return incoming value number x
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Type * getType() const
All values are typed, get the type of this value.
Provides information about what library functions are available for the current target.
bool RecursivelyDeleteDeadPHINode(PHINode *PN, const TargetLibraryInfo *TLI=nullptr)
RecursivelyDeleteDeadPHINode - If the specified value is an effectively dead PHI node, due to being a def-use chain of single-use nodes that either forms a cycle or is terminated by a trivially dead instruction, delete it.
Value * GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, BasicBlock *&IfFalse)
GetIfCondition - Check whether BB is the merge point of a if-region.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
void splice(iterator where, iplist &L2)
void setOperand(unsigned i, Value *Val)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
void SplitLandingPadPredecessors(BasicBlock *OrigBB, ArrayRef< BasicBlock * > Preds, const char *Suffix, const char *Suffix2, SmallVectorImpl< BasicBlock * > &NewBBs, AliasAnalysis *AA=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
SplitLandingPadPredecessors - This method transforms the landing pad, OrigBB, by introducing two new ...
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getSinglePredecessor()
Return the predecessor of this block if it has a single predecessor block.
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Value * getCondition() const
void moveToHeader(BlockT *BB)
moveToHeader - This method is used to move BB (which must be part of this loop) to be the loop header...
void invalidateCachedPredecessors()
invalidateCachedPredecessors - Clear the PredIteratorCache info.
iplist< BasicBlock >::iterator eraseFromParent()
Unlink 'this' from the containing function and delete it.
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
bool isLandingPad() const
Return true if this basic block is a landing pad.
ReturnInst * FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, BasicBlock *Pred)
FoldReturnIntoUncondBranch - This method duplicates the specified return instruction into a predecess...
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
LLVMContext & getContext() const
Get the context in which this basic block lives.
LLVM Value Representation.
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
SplitEdge - Split the edge connecting specified block.
C - The default llvm calling convention, compatible with C.
static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB, ArrayRef< BasicBlock * > Preds, BranchInst *BI, AliasAnalysis *AA, bool HasLoopExit)
UpdatePHINodes - Update the PHI nodes in OrigBB to include the values coming from NewBB...
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, AliasAnalysis *AA=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
SplitBlockPredecessors - This method introduces at least one new basic block into the function and mo...
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
DomTreeNodeBase< NodeT > * getNode(NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
unsigned getLoopDepth() const
getLoopDepth - Return the nesting level of this loop.
const BasicBlock * getParent() const
void removeInstruction(Instruction *InstToRemove)
removeInstruction - Remove an instruction from the dependence analysis, updating the dependence of in...
void splitBlock(NodeT *NewBB)
splitBlock - BB is split and now it has one successor.