summaryrefslogtreecommitdiff
path: root/lib/Transforms
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2002-06-25 16:12:52 +0000
committerChris Lattner <sabre@nondot.org>2002-06-25 16:12:52 +0000
commit18961504fc2b299578dba817900a0696cf3ccc4d (patch)
treec34853ffc064b841932d0897e25305c81c3a7338 /lib/Transforms
parenta2204e1ff25265a1da00ecbb3ebb22c05acf7194 (diff)
downloadllvm-18961504fc2b299578dba817900a0696cf3ccc4d.tar.gz
llvm-18961504fc2b299578dba817900a0696cf3ccc4d.tar.bz2
llvm-18961504fc2b299578dba817900a0696cf3ccc4d.tar.xz
*** empty log message ***
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2777 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms')
-rw-r--r--lib/Transforms/Utils/BasicBlockUtils.cpp43
-rw-r--r--lib/Transforms/Utils/CloneFunction.cpp36
-rw-r--r--lib/Transforms/Utils/Linker.cpp102
-rw-r--r--lib/Transforms/Utils/Local.cpp12
-rw-r--r--lib/Transforms/Utils/SimplifyCFG.cpp55
-rw-r--r--lib/Transforms/Utils/UnifyFunctionExitNodes.cpp16
6 files changed, 114 insertions, 150 deletions
diff --git a/lib/Transforms/Utils/BasicBlockUtils.cpp b/lib/Transforms/Utils/BasicBlockUtils.cpp
index 89078a12e9..aea0b94e10 100644
--- a/lib/Transforms/Utils/BasicBlockUtils.cpp
+++ b/lib/Transforms/Utils/BasicBlockUtils.cpp
@@ -15,19 +15,18 @@
//
void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
BasicBlock::iterator &BI, Value *V) {
- Instruction *I = *BI;
+ Instruction &I = *BI;
// Replaces all of the uses of the instruction with uses of the value
- I->replaceAllUsesWith(V);
+ I.replaceAllUsesWith(V);
- // Remove the unneccesary instruction now...
- BIL.remove(BI);
+ std::string OldName = I.getName();
+
+ // Delete the unneccesary instruction now...
+ BI = BIL.erase(BI);
// Make sure to propogate a name if there is one already...
- if (I->hasName() && !V->hasName())
- V->setName(I->getName(), BIL.getParent()->getSymbolTable());
-
- // Remove the dead instruction now...
- delete I;
+ if (OldName.size() && !V->hasName())
+ V->setName(OldName, BIL.getParent()->getSymbolTable());
}
@@ -41,13 +40,13 @@ void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
"ReplaceInstWithInst: Instruction already inserted into basic block!");
// Insert the new instruction into the basic block...
- BI = BIL.insert(BI, I)+1; // Increment BI to point to instruction to delete
+ BasicBlock::iterator New = BIL.insert(BI, I);
// Replace all uses of the old instruction, and delete it.
ReplaceInstWithValue(BIL, BI, I);
// Move BI back to point to the newly inserted instruction
- --BI;
+ BI = New;
}
// ReplaceInstWithInst - Replace the instruction specified by From with the
@@ -56,24 +55,6 @@ void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
// for the instruction.
//
void ReplaceInstWithInst(Instruction *From, Instruction *To) {
- BasicBlock *BB = From->getParent();
- BasicBlock::InstListType &BIL = BB->getInstList();
- BasicBlock::iterator BI = find(BIL.begin(), BIL.end(), From);
- assert(BI != BIL.end() && "Inst not in it's parents BB!");
- ReplaceInstWithInst(BIL, BI, To);
+ BasicBlock::iterator BI(From);
+ ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
}
-
-// InsertInstBeforeInst - Insert 'NewInst' into the basic block that 'Existing'
-// is already in, and put it right before 'Existing'. This instruction should
-// only be used when there is no iterator to Existing already around. The
-// returned iterator points to the new instruction.
-//
-BasicBlock::iterator InsertInstBeforeInst(Instruction *NewInst,
- Instruction *Existing) {
- BasicBlock *BB = Existing->getParent();
- BasicBlock::InstListType &BIL = BB->getInstList();
- BasicBlock::iterator BI = find(BIL.begin(), BIL.end(), Existing);
- assert(BI != BIL.end() && "Inst not in it's parents BB!");
- return BIL.insert(BI, NewInst);
-}
-
diff --git a/lib/Transforms/Utils/CloneFunction.cpp b/lib/Transforms/Utils/CloneFunction.cpp
index 6b1b24ac57..6bf7553e75 100644
--- a/lib/Transforms/Utils/CloneFunction.cpp
+++ b/lib/Transforms/Utils/CloneFunction.cpp
@@ -36,10 +36,9 @@ static inline void RemapInstruction(Instruction *I,
//
void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
const std::vector<Value*> &ArgMap) {
- assert(OldFunc->getArgumentList().empty() ||
- !NewFunc->getArgumentList().empty() &&
+ assert(OldFunc->aempty() || !NewFunc->aempty() &&
"Synthesization of arguments is not implemented yet!");
- assert(OldFunc->getArgumentList().size() == ArgMap.size() &&
+ assert(OldFunc->asize() == ArgMap.size() &&
"Improper number of argument values to map specified!");
// Keep a mapping between the original function's values and the new
@@ -49,8 +48,10 @@ void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
std::map<const Value *, Value*> ValueMap;
// Add all of the function arguments to the mapping...
- for (unsigned i = 0, e = ArgMap.size(); i != e; ++i)
- ValueMap[(Value*)OldFunc->getArgumentList()[i]] = ArgMap[i];
+ unsigned i = 0;
+ for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
+ I != E; ++I, ++i)
+ ValueMap[I] = ArgMap[i];
// Loop over all of the basic blocks in the function, cloning them as
@@ -58,33 +59,32 @@ void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
//
for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
BI != BE; ++BI) {
- const BasicBlock *BB = *BI;
- assert(BB->getTerminator() && "BasicBlock doesn't have terminator!?!?");
+ const BasicBlock &BB = *BI;
+ assert(BB.getTerminator() && "BasicBlock doesn't have terminator!?!?");
// Create a new basic block to copy instructions into!
- BasicBlock *CBB = new BasicBlock(BB->getName(), NewFunc);
- ValueMap[BB] = CBB; // Add basic block mapping.
+ BasicBlock *CBB = new BasicBlock(BB.getName(), NewFunc);
+ ValueMap[&BB] = CBB; // Add basic block mapping.
// Loop over all instructions copying them over...
- for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
+ for (BasicBlock::const_iterator II = BB.begin(), IE = BB.end();
II != IE; ++II) {
- Instruction *NewInst = (*II)->clone();
- NewInst->setName((*II)->getName()); // Name is not cloned...
+ Instruction *NewInst = II->clone();
+ NewInst->setName(II->getName()); // Name is not cloned...
CBB->getInstList().push_back(NewInst);
- ValueMap[*II] = NewInst; // Add instruction map to value.
+ ValueMap[II] = NewInst; // Add instruction map to value.
}
}
// Loop over all of the instructions in the function, fixing up operand
// references as we go. This uses ValueMap to do all the hard work.
//
- for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
- BI != BE; ++BI) {
- const BasicBlock *BB = *BI;
+ for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end();
+ BB != BE; ++BB) {
BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]);
// Loop over all instructions, fixing each one as we find it...
- for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); II++)
- RemapInstruction(*II, ValueMap);
+ for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II)
+ RemapInstruction(II, ValueMap);
}
}
diff --git a/lib/Transforms/Utils/Linker.cpp b/lib/Transforms/Utils/Linker.cpp
index 7d43047c0a..fb83e027b0 100644
--- a/lib/Transforms/Utils/Linker.cpp
+++ b/lib/Transforms/Utils/Linker.cpp
@@ -96,20 +96,20 @@ static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
}
// Check to see if it's a constant that we are interesting in transforming...
- if (Constant *CPV = dyn_cast<Constant>(In)) {
+ if (const Constant *CPV = dyn_cast<Constant>(In)) {
if (!isa<DerivedType>(CPV->getType()))
- return CPV; // Simple constants stay identical...
+ return const_cast<Constant*>(CPV); // Simple constants stay identical...
Constant *Result = 0;
- if (ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
+ if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
const std::vector<Use> &Ops = CPA->getValues();
std::vector<Constant*> Operands(Ops.size());
for (unsigned i = 0; i < Ops.size(); ++i)
Operands[i] =
cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
- } else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
+ } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
const std::vector<Use> &Ops = CPS->getValues();
std::vector<Constant*> Operands(Ops.size());
for (unsigned i = 0; i < Ops.size(); ++i)
@@ -117,8 +117,9 @@ static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
} else if (isa<ConstantPointerNull>(CPV)) {
- Result = CPV;
- } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
+ Result = const_cast<Constant*>(CPV);
+ } else if (const ConstantPointerRef *CPR =
+ dyn_cast<ConstantPointerRef>(CPV)) {
Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
Result = ConstantPointerRef::get(cast<GlobalValue>(V));
} else {
@@ -126,7 +127,7 @@ static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
}
// Cache the mapping in our local map structure...
- LocalMap.insert(std::make_pair(In, CPV));
+ LocalMap.insert(std::make_pair(In, const_cast<Constant*>(CPV)));
return Result;
}
@@ -158,7 +159,7 @@ static bool LinkGlobals(Module *Dest, const Module *Src,
// Loop over all of the globals in the src module, mapping them over as we go
//
for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
- const GlobalVariable *SGV = *I;
+ const GlobalVariable *SGV = I;
Value *V;
// If the global variable has a name, and that name is already in use in the
@@ -211,7 +212,7 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src,
// Loop over all of the globals in the src module, mapping them over as we go
//
for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
- const GlobalVariable *SGV = *I;
+ const GlobalVariable *SGV = I;
if (SGV->hasInitializer()) { // Only process initialized GV's
// Figure out what the initializer looks like in the dest module...
@@ -249,41 +250,41 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src,
// go
//
for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
- const Function *SM = *I; // SrcFunction
+ const Function *SF = I; // SrcFunction
Value *V;
// If the function has a name, and that name is already in use in the Dest
// module, make sure that the name is a compatible function...
//
- if (SM->hasExternalLinkage() && SM->hasName() &&
- (V = ST->lookup(SM->getType(), SM->getName())) &&
+ if (SF->hasExternalLinkage() && SF->hasName() &&
+ (V = ST->lookup(SF->getType(), SF->getName())) &&
cast<Function>(V)->hasExternalLinkage()) {
// The same named thing is a Function, because the only two things
// that may be in a module level symbol table are Global Vars and
// Functions, and they both have distinct, nonoverlapping, possible types.
//
- Function *DM = cast<Function>(V); // DestFunction
+ Function *DF = cast<Function>(V); // DestFunction
// Check to make sure the function is not defined in both modules...
- if (!SM->isExternal() && !DM->isExternal())
+ if (!SF->isExternal() && !DF->isExternal())
return Error(Err, "Function '" +
- SM->getFunctionType()->getDescription() + "':\"" +
- SM->getName() + "\" - Function is already defined!");
+ SF->getFunctionType()->getDescription() + "':\"" +
+ SF->getName() + "\" - Function is already defined!");
// Otherwise, just remember this mapping...
- ValueMap.insert(std::make_pair(SM, DM));
+ ValueMap.insert(std::make_pair(SF, DF));
} else {
// Function does not already exist, simply insert an external function
- // signature identical to SM into the dest module...
- Function *DM = new Function(SM->getFunctionType(),
- SM->hasInternalLinkage(),
- SM->getName());
+ // signature identical to SF into the dest module...
+ Function *DF = new Function(SF->getFunctionType(),
+ SF->hasInternalLinkage(),
+ SF->getName());
// Add the function signature to the dest module...
- Dest->getFunctionList().push_back(DM);
+ Dest->getFunctionList().push_back(DF);
// ... and remember this mapping...
- ValueMap.insert(std::make_pair(SM, DM));
+ ValueMap.insert(std::make_pair(SF, DF));
}
}
return false;
@@ -300,27 +301,22 @@ static bool LinkFunctionBody(Function *Dest, const Function *Src,
map<const Value*, Value*> LocalMap; // Map for function local values
// Go through and convert function arguments over...
- for (Function::ArgumentListType::const_iterator
- I = Src->getArgumentList().begin(),
- E = Src->getArgumentList().end(); I != E; ++I) {
- const Argument *SMA = *I;
-
+ for (Function::const_aiterator I = Src->abegin(), E = Src->aend();
+ I != E; ++I) {
// Create the new function argument and add to the dest function...
- Argument *DMA = new Argument(SMA->getType(), SMA->getName());
- Dest->getArgumentList().push_back(DMA);
+ Argument *DFA = new Argument(I->getType(), I->getName());
+ Dest->getArgumentList().push_back(DFA);
// Add a mapping to our local map
- LocalMap.insert(std::make_pair(SMA, DMA));
+ LocalMap.insert(std::make_pair(I, DFA));
}
// Loop over all of the basic blocks, copying the instructions over...
//
for (Function::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
- const BasicBlock *SBB = *I;
-
// Create new basic block and add to mapping and the Dest function...
- BasicBlock *DBB = new BasicBlock(SBB->getName(), Dest);
- LocalMap.insert(std::make_pair(SBB, DBB));
+ BasicBlock *DBB = new BasicBlock(I->getName(), Dest);
+ LocalMap.insert(std::make_pair(I, DBB));
// Loop over all of the instructions in the src basic block, copying them
// over. Note that this is broken in a strict sense because the cloned
@@ -328,13 +324,12 @@ static bool LinkFunctionBody(Function *Dest, const Function *Src,
// the remapped values. In our case, however, we will not get caught and
// so we can delay patching the values up until later...
//
- for (BasicBlock::const_iterator II = SBB->begin(), IE = SBB->end();
+ for (BasicBlock::const_iterator II = I->begin(), IE = I->end();
II != IE; ++II) {
- const Instruction *SI = *II;
- Instruction *DI = SI->clone();
- DI->setName(SI->getName());
+ Instruction *DI = II->clone();
+ DI->setName(II->getName());
DBB->getInstList().push_back(DI);
- LocalMap.insert(std::make_pair(SI, DI));
+ LocalMap.insert(std::make_pair(II, DI));
}
}
@@ -343,17 +338,11 @@ static bool LinkFunctionBody(Function *Dest, const Function *Src,
// the Source function as operands. Loop through all of the operands of the
// functions and patch them up to point to the local versions...
//
- for (Function::iterator BI = Dest->begin(), BE = Dest->end();
- BI != BE; ++BI) {
- BasicBlock *BB = *BI;
- for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
- Instruction *Inst = *I;
-
- for (Instruction::op_iterator OI = Inst->op_begin(), OE = Inst->op_end();
+ for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
+ for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
+ for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
OI != OE; ++OI)
*OI = RemapOperand(*OI, LocalMap, &GlobalMap);
- }
- }
return false;
}
@@ -370,20 +359,19 @@ static bool LinkFunctionBodies(Module *Dest, const Module *Src,
// Loop over all of the functions in the src module, mapping them over as we
// go
//
- for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
- const Function *SM = *I; // Source Function
- if (!SM->isExternal()) { // No body if function is external
- Function *DM = cast<Function>(ValueMap[SM]); // Destination function
+ for (Module::const_iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF){
+ if (!SF->isExternal()) { // No body if function is external
+ Function *DF = cast<Function>(ValueMap[SF]); // Destination function
- // DM not external SM external?
- if (!DM->isExternal()) {
+ // DF not external SF external?
+ if (!DF->isExternal()) {
if (Err)
- *Err = "Function '" + (SM->hasName() ? SM->getName() : string("")) +
+ *Err = "Function '" + (SF->hasName() ? SF->getName() : string("")) +
"' body multiply defined!";
return true;
}
- if (LinkFunctionBody(DM, SM, ValueMap, Err)) return true;
+ if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true;
}
}
return false;
diff --git a/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp
index cc152c6c09..10028bacec 100644
--- a/lib/Transforms/Utils/Local.cpp
+++ b/lib/Transforms/Utils/Local.cpp
@@ -17,13 +17,12 @@
// them together...
//
bool doConstantPropogation(BasicBlock::iterator &II) {
- Instruction *Inst = *II;
- if (Constant *C = ConstantFoldInstruction(Inst)) {
+ if (Constant *C = ConstantFoldInstruction(II)) {
// Replaces all of the uses of a variable with uses of the constant.
- Inst->replaceAllUsesWith(C);
+ II->replaceAllUsesWith(C);
// Remove the instruction from the basic block...
- delete Inst->getParent()->getInstList().remove(II);
+ II = II->getParent()->getInstList().erase(II);
return true;
}
@@ -102,9 +101,8 @@ bool isInstructionTriviallyDead(Instruction *I) {
//
bool dceInstruction(BasicBlock::iterator &BBI) {
// Look for un"used" definitions...
- Instruction *I = *BBI;
- if (isInstructionTriviallyDead(I)) {
- delete I->getParent()->getInstList().remove(BBI); // Bye bye
+ if (isInstructionTriviallyDead(BBI)) {
+ BBI = BBI->getParent()->getInstList().erase(BBI); // Bye bye
return true;
}
return false;
diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp
index 00a02dc61a..426d18ec9f 100644
--- a/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -46,7 +46,7 @@ static bool PropogatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
// Loop over all of the PHI nodes in the successor BB
for (BasicBlock::iterator I = Succ->begin();
- PHINode *PN = dyn_cast<PHINode>(*I); ++I) {
+ PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
Value *OldVal = PN->removeIncomingValue(BB);
assert(OldVal && "No entry in PHI for Pred BB!");
@@ -69,13 +69,12 @@ static bool PropogatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
//
// WARNING: The entry node of a function may not be simplified.
//
-bool SimplifyCFG(Function::iterator &BBIt) {
- BasicBlock *BB = *BBIt;
+bool SimplifyCFG(BasicBlock *BB) {
Function *M = BB->getParent();
assert(BB && BB->getParent() && "Block not embedded in function!");
assert(BB->getTerminator() && "Degenerate basic block encountered!");
- assert(BB->getParent()->front() != BB && "Can't Simplify entry block!");
+ assert(&BB->getParent()->front() != BB && "Can't Simplify entry block!");
// Remove basic blocks that have no predecessors... which are unreachable.
@@ -89,20 +88,20 @@ bool SimplifyCFG(Function::iterator &BBIt) {
std::bind2nd(std::mem_fun(&BasicBlock::removePredecessor), BB));
while (!BB->empty()) {
- Instruction *I = BB->back();
+ Instruction &I = BB->back();
// If this instruction is used, replace uses with an arbitrary
// constant value. Because control flow can't get here, we don't care
// what we replace the value with. Note that since this block is
// unreachable, and all values contained within it must dominate their
// uses, that all uses will eventually be removed.
- if (!I->use_empty())
+ if (!I.use_empty())
// Make all users of this instruction reference the constant instead
- I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
+ I.replaceAllUsesWith(Constant::getNullValue(I.getType()));
// Remove the instruction from the basic block
- delete BB->getInstList().pop_back();
+ BB->getInstList().pop_back();
}
- delete M->getBasicBlocks().remove(BBIt);
+ M->getBasicBlockList().erase(BB);
return true;
}
@@ -110,7 +109,7 @@ bool SimplifyCFG(Function::iterator &BBIt) {
// successor. If so, replace block references with successor.
succ_iterator SI(succ_begin(BB));
if (SI != succ_end(BB) && ++SI == succ_end(BB)) { // One succ?
- if (BB->front()->isTerminator()) { // Terminator is the only instruction!
+ if (BB->front().isTerminator()) { // Terminator is the only instruction!
BasicBlock *Succ = *succ_begin(BB); // There is exactly one successor
if (Succ != BB) { // Arg, don't hurt infinite loops!
@@ -125,11 +124,13 @@ bool SimplifyCFG(Function::iterator &BBIt) {
//cerr << "Killing Trivial BB: \n" << BB;
BB->replaceAllUsesWith(Succ);
- BB = M->getBasicBlocks().remove(BBIt);
+ std::string OldName = BB->getName();
+
+ // Delete the old basic block...
+ M->getBasicBlockList().erase(BB);
- if (BB->hasName() && !Succ->hasName()) // Transfer name if we can
- Succ->setName(BB->getName());
- delete BB; // Delete basic block
+ if (!OldName.empty() && !Succ->hasName()) // Transfer name if we can
+ Succ->setName(OldName);
//cerr << "Function after removal: \n" << M;
return true;
@@ -168,28 +169,24 @@ bool SimplifyCFG(Function::iterator &BBIt) {
TerminatorInst *Term = OnlyPred->getTerminator();
// Delete the unconditional branch from the predecessor...
- BasicBlock::iterator DI = OnlyPred->end();
- delete OnlyPred->getInstList().remove(--DI); // Destroy branch
+ OnlyPred->getInstList().pop_back();
// Move all definitions in the succecessor to the predecessor...
- std::vector<Instruction*> Insts(BB->begin(), BB->end());
- BB->getInstList().remove(BB->begin(), BB->end());
- OnlyPred->getInstList().insert(OnlyPred->end(),
- Insts.begin(), Insts.end());
-
- // Remove basic block from the function... and advance iterator to the
- // next valid block...
- M->getBasicBlocks().remove(BBIt);
-
+ OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
+
// Make all PHI nodes that refered to BB now refer to Pred as their
// source...
BB->replaceAllUsesWith(OnlyPred);
-
+
+ std::string OldName = BB->getName();
+
+ // Erase basic block from the function...
+ M->getBasicBlockList().erase(BB);
+
// Inherit predecessors name if it exists...
- if (BB->hasName() && !OnlyPred->hasName())
- OnlyPred->setName(BB->getName());
+ if (!OldName.empty() && !OnlyPred->hasName())
+ OnlyPred->setName(OldName);
- delete BB; // You ARE the weakest link... goodbye
return true;
}
}
diff --git a/lib/Transforms/Utils/UnifyFunctionExitNodes.cpp b/lib/Transforms/Utils/UnifyFunctionExitNodes.cpp
index 64ae2e3ed5..9a65fcecf4 100644
--- a/lib/Transforms/Utils/UnifyFunctionExitNodes.cpp
+++ b/lib/Transforms/Utils/UnifyFunctionExitNodes.cpp
@@ -24,14 +24,14 @@ AnalysisID UnifyFunctionExitNodes::ID(AnalysisID::create<UnifyFunctionExitNodes>
//
// If there are no return stmts in the Function, a null pointer is returned.
//
-bool UnifyFunctionExitNodes::runOnFunction(Function *M) {
+bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
// Loop over all of the blocks in a function, tracking all of the blocks that
// return.
//
vector<BasicBlock*> ReturningBlocks;
- for(Function::iterator I = M->begin(), E = M->end(); I != E; ++I)
- if (isa<ReturnInst>((*I)->getTerminator()))
- ReturningBlocks.push_back(*I);
+ for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
+ if (isa<ReturnInst>(I->getTerminator()))
+ ReturningBlocks.push_back(I);
if (ReturningBlocks.empty()) {
ExitNode = 0;
@@ -45,11 +45,11 @@ bool UnifyFunctionExitNodes::runOnFunction(Function *M) {
// node (if the function returns a value), and convert all of the return
// instructions into unconditional branches.
//
- BasicBlock *NewRetBlock = new BasicBlock("UnifiedExitNode", M);
+ BasicBlock *NewRetBlock = new BasicBlock("UnifiedExitNode", &F);
- if (M->getReturnType() != Type::VoidTy) {
+ if (F.getReturnType() != Type::VoidTy) {
// If the function doesn't return void... add a PHI node to the block...
- PHINode *PN = new PHINode(M->getReturnType(), "UnifiedRetVal");
+ PHINode *PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
NewRetBlock->getInstList().push_back(PN);
// Add an incoming element to the PHI node for every return instruction that
@@ -70,7 +70,7 @@ bool UnifyFunctionExitNodes::runOnFunction(Function *M) {
//
for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
E = ReturningBlocks.end(); I != E; ++I) {
- delete (*I)->getInstList().pop_back(); // Remove the return insn
+ (*I)->getInstList().pop_back(); // Remove the return insn
(*I)->getInstList().push_back(new BranchInst(NewRetBlock));
}
ExitNode = NewRetBlock;