Revert r133435 and r133449 to appease buildbots.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@133499 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chad Rosier 2011-06-21 02:09:03 +00:00
parent 805569f54a
commit a88a0ca808
18 changed files with 156 additions and 211 deletions

View File

@ -110,7 +110,7 @@ public:
Function *getParent() { return Parent; } Function *getParent() { return Parent; }
/// use_back - Specialize the methods defined in Value, as we know that an /// use_back - Specialize the methods defined in Value, as we know that an
/// BasicBlock can only be used by Users (specifically terminators /// BasicBlock can only be used by Users (specifically PHI nodes, terminators,
/// and BlockAddress's). /// and BlockAddress's).
User *use_back() { return cast<User>(*use_begin());} User *use_back() { return cast<User>(*use_begin());}
const User *use_back() const { return cast<User>(*use_begin());} const User *use_back() const { return cast<User>(*use_begin());}
@ -248,10 +248,6 @@ public:
/// other than direct branches, switches, etc. to it. /// other than direct branches, switches, etc. to it.
bool hasAddressTaken() const { return getSubclassDataFromValue() != 0; } bool hasAddressTaken() const { return getSubclassDataFromValue() != 0; }
/// replaceSuccessorsPhiUsesWith - Update all phi nodes in all our successors
/// to refer to basic block New instead of to us.
void replaceSuccessorsPhiUsesWith(BasicBlock *New);
private: private:
/// AdjustBlockAddressRefCount - BasicBlock stores the number of BlockAddress /// AdjustBlockAddressRefCount - BasicBlock stores the number of BlockAddress
/// objects using it. This is almost always 0, sometimes one, possibly but /// objects using it. This is almost always 0, sometimes one, possibly but

View File

@ -1814,7 +1814,7 @@ class PHINode : public Instruction {
explicit PHINode(const Type *Ty, unsigned NumReservedValues, explicit PHINode(const Type *Ty, unsigned NumReservedValues,
const Twine &NameStr = "", Instruction *InsertBefore = 0) const Twine &NameStr = "", Instruction *InsertBefore = 0)
: Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore), : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
ReservedSpace(NumReservedValues) { ReservedSpace(NumReservedValues * 2) {
setName(NameStr); setName(NameStr);
OperandList = allocHungoffUses(ReservedSpace); OperandList = allocHungoffUses(ReservedSpace);
} }
@ -1822,16 +1822,11 @@ class PHINode : public Instruction {
PHINode(const Type *Ty, unsigned NumReservedValues, const Twine &NameStr, PHINode(const Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
BasicBlock *InsertAtEnd) BasicBlock *InsertAtEnd)
: Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd), : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
ReservedSpace(NumReservedValues) { ReservedSpace(NumReservedValues * 2) {
setName(NameStr); setName(NameStr);
OperandList = allocHungoffUses(ReservedSpace); OperandList = allocHungoffUses(ReservedSpace);
} }
protected: protected:
// allocHungoffUses - this is more complicated than the generic
// User::allocHungoffUses, because we have to allocate Uses for the incoming
// values and pointers to the incoming blocks, all in one allocation.
Use *allocHungoffUses(unsigned) const;
virtual PHINode *clone_impl() const; virtual PHINode *clone_impl() const;
public: public:
/// Constructors - NumReservedValues is a hint for the number of incoming /// Constructors - NumReservedValues is a hint for the number of incoming
@ -1850,55 +1845,32 @@ public:
/// Provide fast operand accessors /// Provide fast operand accessors
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
// Block iterator interface. This provides access to the list of incoming
// basic blocks, which parallels the list of incoming values.
typedef BasicBlock **block_iterator;
typedef BasicBlock * const *const_block_iterator;
block_iterator block_begin() {
Use::UserRef *ref =
reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
return reinterpret_cast<block_iterator>(ref + 1);
}
const_block_iterator block_begin() const {
const Use::UserRef *ref =
reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
return reinterpret_cast<const_block_iterator>(ref + 1);
}
block_iterator block_end() {
return block_begin() + getNumOperands();
}
const_block_iterator block_end() const {
return block_begin() + getNumOperands();
}
/// getNumIncomingValues - Return the number of incoming edges /// getNumIncomingValues - Return the number of incoming edges
/// ///
unsigned getNumIncomingValues() const { return getNumOperands(); } unsigned getNumIncomingValues() const { return getNumOperands()/2; }
/// getIncomingValue - Return incoming value number x /// getIncomingValue - Return incoming value number x
/// ///
Value *getIncomingValue(unsigned i) const { Value *getIncomingValue(unsigned i) const {
return getOperand(i); assert(i*2 < getNumOperands() && "Invalid value number!");
return getOperand(i*2);
} }
void setIncomingValue(unsigned i, Value *V) { void setIncomingValue(unsigned i, Value *V) {
setOperand(i, V); assert(i*2 < getNumOperands() && "Invalid value number!");
setOperand(i*2, V);
} }
static unsigned getOperandNumForIncomingValue(unsigned i) { static unsigned getOperandNumForIncomingValue(unsigned i) {
return i; return i*2;
} }
static unsigned getIncomingValueNumForOperand(unsigned i) { static unsigned getIncomingValueNumForOperand(unsigned i) {
return i; assert(i % 2 == 0 && "Invalid incoming-value operand index!");
return i/2;
} }
/// getIncomingBlock - Return incoming basic block number @p i. /// getIncomingBlock - Return incoming basic block number @p i.
/// ///
BasicBlock *getIncomingBlock(unsigned i) const { BasicBlock *getIncomingBlock(unsigned i) const {
return block_begin()[i]; return cast<BasicBlock>(getOperand(i*2+1));
} }
/// getIncomingBlock - Return incoming basic block corresponding /// getIncomingBlock - Return incoming basic block corresponding
@ -1906,7 +1878,7 @@ public:
/// ///
BasicBlock *getIncomingBlock(const Use &U) const { BasicBlock *getIncomingBlock(const Use &U) const {
assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?"); assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
return getIncomingBlock(&U - op_begin()); return cast<BasicBlock>((&U + 1)->get());
} }
/// getIncomingBlock - Return incoming basic block corresponding /// getIncomingBlock - Return incoming basic block corresponding
@ -1917,8 +1889,16 @@ public:
return getIncomingBlock(I.getUse()); return getIncomingBlock(I.getUse());
} }
void setIncomingBlock(unsigned i, BasicBlock *BB) { void setIncomingBlock(unsigned i, BasicBlock *BB) {
block_begin()[i] = BB; setOperand(i*2+1, (Value*)BB);
}
static unsigned getOperandNumForIncomingBlock(unsigned i) {
return i*2+1;
}
static unsigned getIncomingBlockNumForOperand(unsigned i) {
assert(i % 2 == 1 && "Invalid incoming-block operand index!");
return i/2;
} }
/// addIncoming - Add an incoming value to the end of the PHI list /// addIncoming - Add an incoming value to the end of the PHI list
@ -1928,12 +1908,13 @@ public:
assert(BB && "PHI node got a null basic block!"); assert(BB && "PHI node got a null basic block!");
assert(getType() == V->getType() && assert(getType() == V->getType() &&
"All operands to PHI node must be the same type as the PHI node!"); "All operands to PHI node must be the same type as the PHI node!");
if (NumOperands == ReservedSpace) unsigned OpNo = NumOperands;
if (OpNo+2 > ReservedSpace)
growOperands(); // Get more space! growOperands(); // Get more space!
// Initialize some new operands. // Initialize some new operands.
++NumOperands; NumOperands = OpNo+2;
setIncomingValue(NumOperands - 1, V); OperandList[OpNo] = V;
setIncomingBlock(NumOperands - 1, BB); OperandList[OpNo+1] = (Value*)BB;
} }
/// removeIncomingValue - Remove an incoming value. This is useful if a /// removeIncomingValue - Remove an incoming value. This is useful if a
@ -1956,16 +1937,14 @@ public:
/// block in the value list for this PHI. Returns -1 if no instance. /// block in the value list for this PHI. Returns -1 if no instance.
/// ///
int getBasicBlockIndex(const BasicBlock *BB) const { int getBasicBlockIndex(const BasicBlock *BB) const {
for (unsigned i = 0, e = getNumOperands(); i != e; ++i) Use *OL = OperandList;
if (block_begin()[i] == BB) for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
return i; if (OL[i+1].get() == (const Value*)BB) return i/2;
return -1; return -1;
} }
Value *getIncomingValueForBlock(const BasicBlock *BB) const { Value *getIncomingValueForBlock(const BasicBlock *BB) const {
int Idx = getBasicBlockIndex(BB); return getIncomingValue(getBasicBlockIndex(BB));
assert(Idx >= 0 && "Invalid basic block argument!");
return getIncomingValue(Idx);
} }
/// hasConstantValue - If the specified PHI node always merges together the /// hasConstantValue - If the specified PHI node always merges together the

View File

@ -33,7 +33,7 @@ class PredIterator : public std::iterator<std::forward_iterator_tag,
USE_iterator It; USE_iterator It;
inline void advancePastNonTerminators() { inline void advancePastNonTerminators() {
// Loop to ignore non terminator uses (for example BlockAddresses). // Loop to ignore non terminator uses (for example PHI nodes).
while (!It.atEnd() && !isa<TerminatorInst>(*It)) while (!It.atEnd() && !isa<TerminatorInst>(*It))
++It; ++It;
} }

View File

@ -112,16 +112,13 @@ public:
Use *getNext() const { return Next; } Use *getNext() const { return Next; }
/// initTags - initialize the waymarking tags on an array of Uses, so that
/// getUser() can find the User from any of those Uses.
static Use *initTags(Use *Start, Use *Stop);
/// zap - This is used to destroy Use operands when the number of operands of /// zap - This is used to destroy Use operands when the number of operands of
/// a User changes. /// a User changes.
static void zap(Use *Start, const Use *Stop, bool del = false); static void zap(Use *Start, const Use *Stop, bool del = false);
private: private:
const Use* getImpliedUser() const; const Use* getImpliedUser() const;
static Use *initTags(Use *Start, Use *Stop);
Value *Val; Value *Val;
Use *Next; Use *Next;
@ -143,6 +140,7 @@ private:
} }
friend class Value; friend class Value;
friend class User;
}; };
// simplify_type - Allow clients to treat uses just like values when using // simplify_type - Allow clients to treat uses just like values when using

View File

@ -1356,7 +1356,7 @@ void CppWriter::printInstruction(const Instruction *I,
for (unsigned i = 0; i < phi->getNumIncomingValues(); ++i) { for (unsigned i = 0; i < phi->getNumIncomingValues(); ++i) {
Out << iName << "->addIncoming(" Out << iName << "->addIncoming("
<< opNames[PHINode::getOperandNumForIncomingValue(i)] << ", " << opNames[PHINode::getOperandNumForIncomingValue(i)] << ", "
<< getOpName(phi->getIncomingBlock(i)) << ");"; << opNames[PHINode::getOperandNumForIncomingBlock(i)] << ");";
nl(Out); nl(Out);
} }
break; break;

View File

@ -1021,10 +1021,6 @@ void LoopUnswitch::SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L) {
while (PHINode *PN = dyn_cast<PHINode>(Succ->begin())) while (PHINode *PN = dyn_cast<PHINode>(Succ->begin()))
ReplaceUsesOfWith(PN, PN->getIncomingValue(0), Worklist, L, LPM); ReplaceUsesOfWith(PN, PN->getIncomingValue(0), Worklist, L, LPM);
// If Succ has any successors with PHI nodes, update them to have
// entries coming from Pred instead of Succ.
Succ->replaceAllUsesWith(Pred);
// Move all of the successor contents from Succ to Pred. // Move all of the successor contents from Succ to Pred.
Pred->getInstList().splice(BI, Succ->getInstList(), Succ->begin(), Pred->getInstList().splice(BI, Succ->getInstList(), Succ->begin(),
Succ->end()); Succ->end());
@ -1032,6 +1028,10 @@ void LoopUnswitch::SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L) {
BI->eraseFromParent(); BI->eraseFromParent();
RemoveFromWorklist(BI, Worklist); RemoveFromWorklist(BI, Worklist);
// If Succ has any successors with PHI nodes, update them to have
// entries coming from Pred instead of Succ.
Succ->replaceAllUsesWith(Pred);
// Remove Succ from the loop tree. // Remove Succ from the loop tree.
LI->removeBlock(Succ); LI->removeBlock(Succ);
LPM->deleteSimpleAnalysisValue(Succ, L); LPM->deleteSimpleAnalysisValue(Succ, L);

View File

@ -153,13 +153,13 @@ bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, Pass *P) {
// Delete the unconditional branch from the predecessor... // Delete the unconditional branch from the predecessor...
PredBB->getInstList().pop_back(); PredBB->getInstList().pop_back();
// Move all definitions in the successor to the predecessor...
PredBB->getInstList().splice(PredBB->end(), BB->getInstList());
// Make all PHI nodes that referred to BB now refer to Pred as their // Make all PHI nodes that referred to BB now refer to Pred as their
// source... // source...
BB->replaceAllUsesWith(PredBB); BB->replaceAllUsesWith(PredBB);
// Move all definitions in the successor to the predecessor...
PredBB->getInstList().splice(PredBB->end(), BB->getInstList());
// Inherit predecessors name if it exists. // Inherit predecessors name if it exists.
if (!PredBB->hasName()) if (!PredBB->hasName())
PredBB->takeName(BB); PredBB->takeName(BB);

View File

@ -193,22 +193,44 @@ BasicBlock *llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
// If there are any PHI nodes in DestBB, we need to update them so that they // If there are any PHI nodes in DestBB, we need to update them so that they
// merge incoming values from NewBB instead of from TIBB. // merge incoming values from NewBB instead of from TIBB.
{ if (PHINode *APHI = dyn_cast<PHINode>(DestBB->begin())) {
unsigned BBIdx = 0; // This conceptually does:
for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) { // foreach (PHINode *PN in DestBB)
// We no longer enter through TIBB, now we come in through NewBB. // PN->setIncomingBlock(PN->getIncomingBlock(TIBB), NewBB);
// Revector exactly one entry in the PHI node that used to come from // but is optimized for two cases.
// TIBB to come from NewBB.
PHINode *PN = cast<PHINode>(I); if (APHI->getNumIncomingValues() <= 8) { // Small # preds case.
unsigned BBIdx = 0;
// Reuse the previous value of BBIdx if it lines up. In cases where we for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) {
// have multiple phi nodes with *lots* of predecessors, this is a speed // We no longer enter through TIBB, now we come in through NewBB.
// win because we don't have to scan the PHI looking for TIBB. This // Revector exactly one entry in the PHI node that used to come from
// happens because the BB list of PHI nodes are usually in the same // TIBB to come from NewBB.
// order. PHINode *PN = cast<PHINode>(I);
if (PN->getIncomingBlock(BBIdx) != TIBB)
BBIdx = PN->getBasicBlockIndex(TIBB); // Reuse the previous value of BBIdx if it lines up. In cases where we
PN->setIncomingBlock(BBIdx, NewBB); // have multiple phi nodes with *lots* of predecessors, this is a speed
// win because we don't have to scan the PHI looking for TIBB. This
// happens because the BB list of PHI nodes are usually in the same
// order.
if (PN->getIncomingBlock(BBIdx) != TIBB)
BBIdx = PN->getBasicBlockIndex(TIBB);
PN->setIncomingBlock(BBIdx, NewBB);
}
} else {
// However, the foreach loop is slow for blocks with lots of predecessors
// because PHINode::getIncomingBlock is O(n) in # preds. Instead, walk
// the user list of TIBB to find the PHI nodes.
SmallPtrSet<PHINode*, 16> UpdatedPHIs;
for (Value::use_iterator UI = TIBB->use_begin(), E = TIBB->use_end();
UI != E; ) {
Value::use_iterator Use = UI++;
if (PHINode *PN = dyn_cast<PHINode>(*Use)) {
// Remove one entry from each PHI.
if (PN->getParent() == DestBB && UpdatedPHIs.insert(PN))
PN->setOperand(Use.getOperandNo(), NewBB);
}
}
} }
} }

View File

@ -572,12 +572,12 @@ void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
// removed, so we just need to splice the blocks. // removed, so we just need to splice the blocks.
BI->eraseFromParent(); BI->eraseFromParent();
// Make all PHI nodes that referred to Dest now refer to I as their source.
Dest->replaceAllUsesWith(I);
// Move all the instructions in the succ to the pred. // Move all the instructions in the succ to the pred.
I->getInstList().splice(I->end(), Dest->getInstList()); I->getInstList().splice(I->end(), Dest->getInstList());
// Make all PHI nodes that referred to Dest now refer to I as their source.
Dest->replaceAllUsesWith(I);
// Remove the dest block. // Remove the dest block.
Dest->eraseFromParent(); Dest->eraseFromParent();

View File

@ -1097,15 +1097,15 @@ bool llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI) {
TheCall->replaceAllUsesWith(Returns[0]->getReturnValue()); TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
} }
// Update PHI nodes that use the ReturnBB to use the AfterCallBB.
BasicBlock *ReturnBB = Returns[0]->getParent();
ReturnBB->replaceAllUsesWith(AfterCallBB);
// Splice the code from the return block into the block that it will return // Splice the code from the return block into the block that it will return
// to, which contains the code that was after the call. // to, which contains the code that was after the call.
BasicBlock *ReturnBB = Returns[0]->getParent();
AfterCallBB->getInstList().splice(AfterCallBB->begin(), AfterCallBB->getInstList().splice(AfterCallBB->begin(),
ReturnBB->getInstList()); ReturnBB->getInstList());
// Update PHI nodes that use the ReturnBB to use the AfterCallBB.
ReturnBB->replaceAllUsesWith(AfterCallBB);
// Delete the return instruction now and empty ReturnBB now. // Delete the return instruction now and empty ReturnBB now.
Returns[0]->eraseFromParent(); Returns[0]->eraseFromParent();
ReturnBB->eraseFromParent(); ReturnBB->eraseFromParent();
@ -1125,8 +1125,8 @@ bool llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI) {
// Splice the code entry block into calling block, right before the // Splice the code entry block into calling block, right before the
// unconditional branch. // unconditional branch.
CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes
OrigBB->getInstList().splice(Br, CalleeEntry->getInstList()); OrigBB->getInstList().splice(Br, CalleeEntry->getInstList());
CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes
// Remove the unconditional branch. // Remove the unconditional branch.
OrigBB->getInstList().erase(Br); OrigBB->getInstList().erase(Br);

View File

@ -427,6 +427,10 @@ void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, Pass *P) {
BasicBlock *PredBB = DestBB->getSinglePredecessor(); BasicBlock *PredBB = DestBB->getSinglePredecessor();
assert(PredBB && "Block doesn't have a single predecessor!"); assert(PredBB && "Block doesn't have a single predecessor!");
// Splice all the instructions from PredBB to DestBB.
PredBB->getTerminator()->eraseFromParent();
DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList());
// Zap anything that took the address of DestBB. Not doing this will give the // Zap anything that took the address of DestBB. Not doing this will give the
// address an invalid value. // address an invalid value.
if (DestBB->hasAddressTaken()) { if (DestBB->hasAddressTaken()) {
@ -441,10 +445,6 @@ void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, Pass *P) {
// Anything that branched to PredBB now branches to DestBB. // Anything that branched to PredBB now branches to DestBB.
PredBB->replaceAllUsesWith(DestBB); PredBB->replaceAllUsesWith(DestBB);
// Splice all the instructions from PredBB to DestBB.
PredBB->getTerminator()->eraseFromParent();
DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList());
if (P) { if (P) {
DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>(); DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>();
if (DT) { if (DT) {
@ -660,17 +660,12 @@ bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) {
// them, which helps expose duplicates, but we have to check all the // them, which helps expose duplicates, but we have to check all the
// operands to be safe in case instcombine hasn't run. // operands to be safe in case instcombine hasn't run.
uintptr_t Hash = 0; uintptr_t Hash = 0;
// This hash algorithm is quite weak as hash functions go, but it seems
// to do a good enough job for this particular purpose, and is very quick.
for (User::op_iterator I = PN->op_begin(), E = PN->op_end(); I != E; ++I) { for (User::op_iterator I = PN->op_begin(), E = PN->op_end(); I != E; ++I) {
// This hash algorithm is quite weak as hash functions go, but it seems
// to do a good enough job for this particular purpose, and is very quick.
Hash ^= reinterpret_cast<uintptr_t>(static_cast<Value *>(*I)); Hash ^= reinterpret_cast<uintptr_t>(static_cast<Value *>(*I));
Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7)); Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7));
} }
for (PHINode::block_iterator I = PN->block_begin(), E = PN->block_end();
I != E; ++I) {
Hash ^= reinterpret_cast<uintptr_t>(static_cast<BasicBlock *>(*I));
Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7));
}
// Avoid colliding with the DenseMap sentinels ~0 and ~0-1. // Avoid colliding with the DenseMap sentinels ~0 and ~0-1.
Hash >>= 1; Hash >>= 1;
// If we've never seen this hash value before, it's a unique PHI. // If we've never seen this hash value before, it's a unique PHI.

View File

@ -47,14 +47,6 @@ static inline void RemapInstruction(Instruction *I,
if (It != VMap.end()) if (It != VMap.end())
I->setOperand(op, It->second); I->setOperand(op, It->second);
} }
if (PHINode *PN = dyn_cast<PHINode>(I)) {
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
ValueToValueMapTy::iterator It = VMap.find(PN->getIncomingBlock(i));
if (It != VMap.end())
PN->setIncomingBlock(i, cast<BasicBlock>(It->second));
}
}
} }
/// FoldBlockIntoPredecessor - Folds a basic block into its predecessor if it /// FoldBlockIntoPredecessor - Folds a basic block into its predecessor if it
@ -83,13 +75,13 @@ static BasicBlock *FoldBlockIntoPredecessor(BasicBlock *BB, LoopInfo* LI) {
// Delete the unconditional branch from the predecessor... // Delete the unconditional branch from the predecessor...
OnlyPred->getInstList().pop_back(); OnlyPred->getInstList().pop_back();
// Move all definitions in the successor to the predecessor...
OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
// Make all PHI nodes that referred to BB now refer to Pred as their // Make all PHI nodes that referred to BB now refer to Pred as their
// source... // source...
BB->replaceAllUsesWith(OnlyPred); BB->replaceAllUsesWith(OnlyPred);
// Move all definitions in the successor to the predecessor...
OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
std::string OldName = BB->getName(); std::string OldName = BB->getName();
// Erase basic block from the function... // Erase basic block from the function...
@ -255,14 +247,16 @@ bool llvm::UnrollLoop(Loop *L, unsigned Count,
// the successor of the latch block. The successor of the exit block will // the successor of the latch block. The successor of the exit block will
// be updated specially after unrolling all the way. // be updated specially after unrolling all the way.
if (*BB != LatchBlock) if (*BB != LatchBlock)
for (succ_iterator SI = succ_begin(*BB), SE = succ_end(*BB); SI != SE; for (Value::use_iterator UI = (*BB)->use_begin(), UE = (*BB)->use_end();
++SI) UI != UE;) {
if (!L->contains(*SI)) Instruction *UseInst = cast<Instruction>(*UI);
for (BasicBlock::iterator BBI = (*SI)->begin(), BBE = (*SI)->end(); ++UI;
PHINode *phi = dyn_cast<PHINode>(BBI); ++BBI) { if (isa<PHINode>(UseInst) && !L->contains(UseInst)) {
Value *Incoming = phi->getIncomingValueForBlock(*BB); PHINode *phi = cast<PHINode>(UseInst);
phi->addIncoming(Incoming, New); Value *Incoming = phi->getIncomingValueForBlock(*BB);
} phi->addIncoming(Incoming, New);
}
}
// Keep track of new headers and latches as we create them, so that // Keep track of new headers and latches as we create them, so that
// we can insert the proper branches later. // we can insert the proper branches later.
@ -294,20 +288,24 @@ bool llvm::UnrollLoop(Loop *L, unsigned Count,
// successor blocks, update them to use the appropriate values computed as the // successor blocks, update them to use the appropriate values computed as the
// last iteration of the loop. // last iteration of the loop.
if (Count != 1) { if (Count != 1) {
SmallPtrSet<PHINode*, 8> Users;
for (Value::use_iterator UI = LatchBlock->use_begin(),
UE = LatchBlock->use_end(); UI != UE; ++UI)
if (PHINode *phi = dyn_cast<PHINode>(*UI))
Users.insert(phi);
BasicBlock *LastIterationBB = cast<BasicBlock>(LastValueMap[LatchBlock]); BasicBlock *LastIterationBB = cast<BasicBlock>(LastValueMap[LatchBlock]);
for (succ_iterator SI = succ_begin(LatchBlock), SE = succ_end(LatchBlock); for (SmallPtrSet<PHINode*,8>::iterator SI = Users.begin(), SE = Users.end();
SI != SE; ++SI) { SI != SE; ++SI) {
for (BasicBlock::iterator BBI = (*SI)->begin(), BBE = (*SI)->end(); PHINode *PN = *SI;
PHINode *PN = dyn_cast<PHINode>(BBI); ++BBI) { Value *InVal = PN->removeIncomingValue(LatchBlock, false);
Value *InVal = PN->removeIncomingValue(LatchBlock, false); // If this value was defined in the loop, take the value defined by the
// If this value was defined in the loop, take the value defined by the // last iteration of the loop.
// last iteration of the loop. if (Instruction *InValI = dyn_cast<Instruction>(InVal)) {
if (Instruction *InValI = dyn_cast<Instruction>(InVal)) { if (L->contains(InValI))
if (L->contains(InValI)) InVal = LastValueMap[InVal];
InVal = LastValueMap[InVal];
}
PN->addIncoming(InVal, LastIterationBB);
} }
PN->addIncoming(InVal, LastIterationBB);
} }
} }
@ -354,16 +352,11 @@ bool llvm::UnrollLoop(Loop *L, unsigned Count,
// Replace the conditional branch with an unconditional one. // Replace the conditional branch with an unconditional one.
BranchInst::Create(Dest, Term); BranchInst::Create(Dest, Term);
Term->eraseFromParent(); Term->eraseFromParent();
} // Merge adjacent basic blocks, if possible.
} if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI)) {
// Merge adjacent basic blocks, if possible.
for (unsigned i = 0, e = Latches.size(); i != e; ++i) {
BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator());
if (Term->isUnconditional()) {
BasicBlock *Dest = Term->getSuccessor(0);
if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI))
std::replace(Latches.begin(), Latches.end(), Dest, Fold); std::replace(Latches.begin(), Latches.end(), Dest, Fold);
std::replace(Headers.begin(), Headers.end(), Dest, Fold);
}
} }
} }

View File

@ -16,7 +16,6 @@
#include "llvm/Type.h" #include "llvm/Type.h"
#include "llvm/Constants.h" #include "llvm/Constants.h"
#include "llvm/Function.h" #include "llvm/Function.h"
#include "llvm/Instructions.h"
#include "llvm/Metadata.h" #include "llvm/Metadata.h"
#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/SmallVector.h"
using namespace llvm; using namespace llvm;
@ -129,19 +128,6 @@ void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
"Referenced value not in value map!"); "Referenced value not in value map!");
} }
// Remap phi nodes' incoming blocks.
if (PHINode *PN = dyn_cast<PHINode>(I)) {
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
// If we aren't ignoring missing entries, assert that something happened.
if (V != 0)
PN->setIncomingBlock(i, cast<BasicBlock>(V));
else
assert((Flags & RF_IgnoreMissingEntries) &&
"Referenced block not in value map!");
}
}
// Remap attached metadata. // Remap attached metadata.
SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
I->getAllMetadata(MDs); I->getAllMetadata(MDs);

View File

@ -308,19 +308,3 @@ BasicBlock *BasicBlock::splitBasicBlock(iterator I, const Twine &BBName) {
return New; return New;
} }
void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) {
TerminatorInst *TI = getTerminator();
if (!TI)
// Cope with being called on a BasicBlock that doesn't have a terminator
// yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this.
return;
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
BasicBlock *Succ = TI->getSuccessor(i);
for (iterator II = Succ->begin(); PHINode *PN = dyn_cast<PHINode>(II);
++II) {
int i;
while ((i = PN->getBasicBlockIndex(this)) >= 0)
PN->setIncomingBlock(i, New);
}
}
}

View File

@ -87,8 +87,11 @@ PHINode::PHINode(const PHINode &PN)
: Instruction(PN.getType(), Instruction::PHI, : Instruction(PN.getType(), Instruction::PHI,
allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()), allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
ReservedSpace(PN.getNumOperands()) { ReservedSpace(PN.getNumOperands()) {
std::copy(PN.op_begin(), PN.op_end(), op_begin()); Use *OL = OperandList;
std::copy(PN.block_begin(), PN.block_end(), block_begin()); for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
OL[i] = PN.getOperand(i);
OL[i+1] = PN.getOperand(i+1);
}
SubclassOptionalData = PN.SubclassOptionalData; SubclassOptionalData = PN.SubclassOptionalData;
} }
@ -96,37 +99,31 @@ PHINode::~PHINode() {
dropHungoffUses(); dropHungoffUses();
} }
Use *PHINode::allocHungoffUses(unsigned N) const {
// Allocate the array of Uses of the incoming values, followed by a pointer
// (with bottom bit set) to the User, followed by the array of pointers to
// the incoming basic blocks.
size_t size = N * sizeof(Use) + sizeof(Use::UserRef)
+ N * sizeof(BasicBlock*);
Use *Begin = static_cast<Use*>(::operator new(size));
Use *End = Begin + N;
(void) new(End) Use::UserRef(const_cast<PHINode*>(this), 1);
return Use::initTags(Begin, End);
}
// removeIncomingValue - Remove an incoming value. This is useful if a // removeIncomingValue - Remove an incoming value. This is useful if a
// predecessor basic block is deleted. // predecessor basic block is deleted.
Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Value *Removed = getIncomingValue(Idx); unsigned NumOps = getNumOperands();
Use *OL = OperandList;
assert(Idx*2 < NumOps && "BB not in PHI node!");
Value *Removed = OL[Idx*2];
// Move everything after this operand down. // Move everything after this operand down.
// //
// FIXME: we could just swap with the end of the list, then erase. However, // FIXME: we could just swap with the end of the list, then erase. However,
// clients might not expect this to happen. The code as it is thrashes the // client might not expect this to happen. The code as it is thrashes the
// use/def lists, which is kinda lame. // use/def lists, which is kinda lame.
std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx); for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx); OL[i-2] = OL[i];
OL[i-2+1] = OL[i+1];
}
// Nuke the last value. // Nuke the last value.
Op<-1>().set(0); OL[NumOps-2].set(0);
--NumOperands; OL[NumOps-2+1].set(0);
NumOperands = NumOps-2;
// If the PHI node is dead, because it has zero entries, nuke it now. // If the PHI node is dead, because it has zero entries, nuke it now.
if (getNumOperands() == 0 && DeletePHIIfEmpty) { if (NumOps == 2 && DeletePHIIfEmpty) {
// If anyone is using this PHI, make them use a dummy value instead... // If anyone is using this PHI, make them use a dummy value instead...
replaceAllUsesWith(UndefValue::get(getType())); replaceAllUsesWith(UndefValue::get(getType()));
eraseFromParent(); eraseFromParent();
@ -140,18 +137,15 @@ Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
/// ///
void PHINode::growOperands() { void PHINode::growOperands() {
unsigned e = getNumOperands(); unsigned e = getNumOperands();
unsigned NumOps = e + e / 2; // Multiply by 1.5 and round down so the result is still even.
if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common. unsigned NumOps = e + e / 4 * 2;
if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
Use *OldOps = op_begin();
BasicBlock **OldBlocks = block_begin();
ReservedSpace = NumOps; ReservedSpace = NumOps;
OperandList = allocHungoffUses(ReservedSpace); Use *OldOps = OperandList;
Use *NewOps = allocHungoffUses(NumOps);
std::copy(OldOps, OldOps + e, op_begin()); std::copy(OldOps, OldOps + e, NewOps);
std::copy(OldBlocks, OldBlocks + e, block_begin()); OperandList = NewOps;
Use::zap(OldOps, OldOps + e, true); Use::zap(OldOps, OldOps + e, true);
} }

View File

@ -40,10 +40,8 @@ void User::replaceUsesOfWith(Value *From, Value *To) {
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
Use *User::allocHungoffUses(unsigned N) const { Use *User::allocHungoffUses(unsigned N) const {
// Allocate the array of Uses, followed by a pointer (with bottom bit set) to Use *Begin = static_cast<Use*>(::operator new(sizeof(Use) * N
// the User. + sizeof(Use::UserRef)));
size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
Use *Begin = static_cast<Use*>(::operator new(size));
Use *End = Begin + N; Use *End = Begin + N;
(void) new(End) Use::UserRef(const_cast<User*>(this), 1); (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
return Use::initTags(Begin, End); return Use::initTags(Begin, End);

View File

@ -305,9 +305,6 @@ void Value::uncheckedReplaceAllUsesWith(Value *New) {
U.set(New); U.set(New);
} }
if (BasicBlock *BB = dyn_cast<BasicBlock>(this))
BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
} }
void Value::replaceAllUsesWith(Value *New) { void Value::replaceAllUsesWith(Value *New) {

View File

@ -1139,6 +1139,9 @@ void Verifier::visitPHINode(PHINode &PN) {
for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Assert1(PN.getType() == PN.getIncomingValue(i)->getType(), Assert1(PN.getType() == PN.getIncomingValue(i)->getType(),
"PHI node operands are not the same type as the result!", &PN); "PHI node operands are not the same type as the result!", &PN);
Assert1(isa<BasicBlock>(PN.getOperand(
PHINode::getOperandNumForIncomingBlock(i))),
"PHI node incoming block is not a BasicBlock!", &PN);
} }
// All other PHI node constraints are checked in the visitBasicBlock method. // All other PHI node constraints are checked in the visitBasicBlock method.