48 while (!BB->
empty()) {
69 if (!isa<PHINode>(BB->
begin()))
return;
79 if (PN->getIncomingValue(0) != PN)
80 PN->replaceAllUsesWith(PN->getIncomingValue(0));
86 else if (AA && isa<PointerType>(PN->getType()))
89 PN->eraseFromParent();
106 bool Changed =
false;
107 for (
unsigned i = 0, e = PHIs.
size(); i != e; ++i)
108 if (
PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].
operator Value*()))
122 if (!PredBB)
return false;
125 if (PredBB == BB)
return false;
131 for (; SI != SE; ++SI)
132 if (*SI != OnlySucc) {
138 if (!OnlySucc)
return false;
142 if (
PHINode *PN = dyn_cast<PHINode>(BI)) {
143 for (
unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
144 if (PN->getIncomingValue(i) == PN)
151 if (isa<PHINode>(BB->
front()))
175 DE = Children.end(); DI !=
DE; ++DI)
176 DT->changeImmediateDominator(*DI, PredDTN);
186 MD->invalidateCachedPredecessors();
219 "ReplaceInstWithInst: Instruction already inserted into basic block!");
254 assert(SP == BB &&
"CFG broken");
262 "Should have a single succ!");
273 while (isa<PHINode>(SplitIt) || isa<LandingPadInst>(SplitIt))
280 if (
Loop *L =
LI->getLoopFor(Old))
281 L->addBasicBlockToLoop(New,
LI->getBase());
286 std::vector<DomTreeNode *> Children;
289 Children.push_back(*
I);
292 for (std::vector<DomTreeNode *>::iterator
I = Children.begin(),
293 E = Children.end();
I != E; ++
I)
294 DT->changeImmediateDominator(*
I, NewNode);
305 Pass *
P,
bool &HasLoopExit) {
313 bool IsLoopEntry = !!L;
314 bool SplitMakesNewLoopHeader =
false;
318 i = Preds.
begin(), e = Preds.
end(); i != e; ++i) {
325 if (!
PL->contains(OldBB))
331 if (L->contains(Pred))
334 SplitMakesNewLoopHeader =
true;
350 Loop *InnermostPredLoop = 0;
352 i = Preds.
begin(), e = Preds.
end(); i != e; ++i) {
357 while (PredLoop && !PredLoop->contains(OldBB))
358 PredLoop = PredLoop->getParentLoop();
361 if (PredLoop && PredLoop->contains(OldBB) &&
362 (!InnermostPredLoop ||
363 InnermostPredLoop->
getLoopDepth() < PredLoop->getLoopDepth()))
364 InnermostPredLoop = PredLoop;
368 if (InnermostPredLoop)
371 L->addBasicBlockToLoop(NewBB, LI->
getBase());
372 if (SplitMakesNewLoopHeader)
373 L->moveToHeader(NewBB);
381 Pass *
P,
bool HasLoopExit) {
392 for (
unsigned i = 1, e = Preds.
size(); i != e; ++i)
403 for (
unsigned i = 0, e = Preds.
size(); i != e; ++i) {
417 for (
unsigned i = 0, e = Preds.
size(); i != e; ++i) {
444 const char *Suffix,
Pass *
P) {
453 for (
unsigned i = 0, e = Preds.
size(); i != e; ++i) {
457 assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
458 "Cannot split an edge from an IndirectBrInst");
459 Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB);
466 if (Preds.
size() == 0) {
474 bool HasLoopExit =
false;
497 const char *Suffix1,
const char *Suffix2,
500 assert(OrigBB->
isLandingPad() &&
"Trying to split a non-landing pad!");
513 for (
unsigned i = 0, e = Preds.
size(); i != e; ++i) {
517 assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
518 "Cannot split an edge from an IndirectBrInst");
519 Preds[i]->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1);
523 bool HasLoopExit =
false;
534 if (Pred == NewBB1)
continue;
536 "Cannot split an edge from an IndirectBrInst");
542 if (!NewBB2Preds.
empty()) {
554 i = NewBB2Preds.
begin(), e = NewBB2Preds.
end(); i != e; ++i)
555 (*i)->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2);
568 NewBB1->getInstList().insert(NewBB1->getFirstInsertionPt(), Clone1);
610 V = BCI->getOperand(0);
611 NewBC = BCI->
clone();
615 if (
PHINode *PN = dyn_cast<PHINode>(V)) {
616 if (PN->getParent() == BB) {
618 NewBC->
setOperand(0, PN->getIncomingValueForBlock(Pred));
620 *i = PN->getIncomingValueForBlock(Pred);
629 return cast<ReturnInst>(NewRet);
653 bool Unreachable,
MDNode *BranchWeights) {
708 if (Pred1Br == 0 || Pred2Br == 0)
713 if (Pred2Br->isConditional()) {
760 if (BI == 0)
return 0;
762 assert(BI->
isConditional() &&
"Two successors but not conditional?");
void push_back(const T &Elt)
void FoldSingleEntryPHINodes(BasicBlock *BB, Pass *P=0)
void ReplaceInstWithInst(BasicBlock::InstListType &BIL, BasicBlock::iterator &BI, Instruction *I)
BasicBlock * getUniquePredecessor()
Return this block if it has a unique predecessor block. Otherwise return a null pointer.
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P)
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)
const Instruction & back() const
BasicBlock * SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P=0, bool MergeIdenticalEdges=false, bool DontDeleteUselessPHIs=false, bool SplitLandingPads=false)
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void DeleteDeadBlock(BasicBlock *BB)
void splitBlock(BasicBlock *NewBB)
const Function * getParent() const
Return the enclosing method, or null if none.
MDNode - a tuple of other values.
const Instruction & front() const
unsigned GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ)
LoopInfoBase< BlockT, LoopT > * LI
StringRef getName() const
void push_back(NodeTy *val)
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
NodeTy * getNextNode()
Get the next node, or 0 for the list tail.
AnalysisType * getAnalysisIfAvailable() const
static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB, ArrayRef< BasicBlock * > Preds, BranchInst *BI, Pass *P, bool HasLoopExit)
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=0)
bool mustPreserveAnalysisID(char &AID) const
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches, switches, etc. to it.
bool MergeBlockIntoPredecessor(BasicBlock *BB, Pass *P=0)
void setName(const Twine &Name)
Instruction * clone() const
Interval::succ_iterator succ_begin(Interval *I)
virtual void copyValue(Value *From, Value *To)
bool RecursivelyDeleteDeadPHINode(PHINode *PN, const TargetLibraryInfo *TLI=0)
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, Pass *P=0)
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
BasicBlock * getSuccessor(unsigned i) const
This class represents a no-op cast from one type to another.
Loop * getLoopFor(const BasicBlock *BB) const
void replaceAllUsesWith(Value *V)
void ReplaceInstWithValue(BasicBlock::InstListType &BIL, BasicBlock::iterator &BI, Value *V)
size_t size() const
size - Get the array size.
unsigned getNumIncomingValues() const
Interval::succ_iterator succ_end(Interval *I)
unsigned getNumSuccessors() const
LLVM Basic Block Representation.
TerminatorInst * SplitBlockAndInsertIfThen(Instruction *Cmp, bool Unreachable, MDNode *BranchWeights=0)
BasicBlock * getSuccessor(unsigned idx) const
LandingPadInst * getLandingPadInst()
Return the landingpad instruction associated with the landing pad.
Interval::pred_iterator pred_begin(Interval *I)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=0)
BasicBlock * getIncomingBlock(unsigned i) const
const InstListType & getInstList() const
Return the underlying instruction list container.
iterator insert(iterator where, NodeTy *New)
Interval::pred_iterator pred_end(Interval *I)
static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB, ArrayRef< BasicBlock * > Preds, Pass *P, bool &HasLoopExit)
static UndefValue * get(Type *T)
virtual void deleteValue(Value *V)
iterator erase(iterator where)
void setMetadata(unsigned KindID, MDNode *Node)
bool isConditional() const
void eraseFromParent()
Unlink 'this' from the containing function and delete it.
Value * GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, BasicBlock *&IfFalse)
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.
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P)
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getSinglePredecessor()
Return this block if it has a single predecessor block. Otherwise return a null pointer.
Value * getCondition() const
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.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=0, BasicBlock *InsertBefore=0)
Creates a new BasicBlock.
ReturnInst * FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, BasicBlock *Pred)
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
std::vector< DomTreeNodeBase< NodeT > * >::iterator iterator
LLVMContext & getContext() const
Get the context in which this basic block lives.
LLVM Value Representation.
bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI=0)
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
void SplitLandingPadPredecessors(BasicBlock *OrigBB, ArrayRef< BasicBlock * > Preds, const char *Suffix, const char *Suffix2, Pass *P, SmallVectorImpl< BasicBlock * > &NewBBs)
unsigned getLoopDepth() const
int getBasicBlockIndex(const BasicBlock *BB) const
LoopInfoBase< BasicBlock, Loop > & getBase()
const BasicBlock * getParent() const
void removeInstruction(Instruction *InstToRemove)