IR: Remove implicit iterator conversions from lib/IR, NFC

Stop converting implicitly between iterators and pointers/references in
lib/IR.  For convenience, I've added a `getIterator()` accessor to
`ilist_node` so that callers don't need to know how to spell the
iterator class (i.e., they can use `X.getIterator()` instead of
`Function::iterator(X)`).

I'll eventually disallow these implicit conversions entirely, but
there's a lot of code, so it doesn't make sense to do it all in one
patch.  One library or so at a time.

Why?  To root out cases of `getNextNode()` and `getPrevNode()` being
used in iterator logic.  The design of `ilist` makes that invalid when
the current node could be at the back of the list, but it happens to
"work" right now because of a bug where those functions never return
`nullptr` if you're using a half-node sentinel.  Before I can fix the
function, I have to remove uses of it that rely on it misbehaving.
(Maybe the function should just be deleted anyway?  But I don't want
deleting it -- potentially a huge project -- to block fixing
ilist/iplist.)

llvm-svn: 249782
This commit is contained in:
Duncan P. N. Exon Smith 2015-10-08 23:49:46 +00:00
parent e20eda5e6e
commit 7045cca7d5
16 changed files with 95 additions and 77 deletions

View File

@ -238,6 +238,8 @@ public:
return *this;
}
void reset(pointer NP) { NodePtr = NP; }
// Accessors...
operator pointer() const {
return NodePtr;
@ -469,7 +471,7 @@ public:
this->setPrev(CurNode, New);
this->addNodeToList(New); // Notify traits that we added a node...
return New;
return iterator(New);
}
iterator insertAfter(iterator where, NodeTy *New) {
@ -490,7 +492,7 @@ public:
else
Head = NextNode;
this->setPrev(NextNode, PrevNode);
IT = NextNode;
IT.reset(NextNode);
this->removeNodeFromList(Node); // Notify traits that we removed a node...
// Set the next/prev pointers of the current node to null. This isn't
@ -569,7 +571,7 @@ private:
this->setNext(Last, PosNext);
this->setPrev(PosNext, Last);
this->transferNodesFromList(L2, First, PosNext);
this->transferNodesFromList(L2, iterator(First), iterator(PosNext));
// Now that everything is set, restore the pointers to the list sentinels.
L2.setTail(L2Sentinel);

View File

@ -39,6 +39,8 @@ protected:
template<typename NodeTy>
struct ilist_nextprev_traits;
template <typename NodeTy> class ilist_iterator;
/// ilist_node - Base class that provides next/prev services for nodes
/// that use ilist_nextprev_traits or ilist_default_traits.
///
@ -56,6 +58,15 @@ protected:
ilist_node() : Next(nullptr) {}
public:
ilist_iterator<NodeTy> getIterator() {
// FIXME: Stop downcasting to create the iterator (potential UB).
return ilist_iterator<NodeTy>(static_cast<NodeTy *>(this));
}
ilist_iterator<const NodeTy> getIterator() const {
// FIXME: Stop downcasting to create the iterator (potential UB).
return ilist_iterator<const NodeTy>(static_cast<const NodeTy *>(this));
}
/// @name Adjacent Node Accessors
/// @{

View File

@ -75,7 +75,7 @@ public:
/// inserted into a block.
void ClearInsertionPoint() {
BB = nullptr;
InsertPt = nullptr;
InsertPt.reset(nullptr);
}
BasicBlock *GetInsertBlock() const { return BB; }
@ -93,8 +93,8 @@ public:
/// the specified instruction.
void SetInsertPoint(Instruction *I) {
BB = I->getParent();
InsertPt = I;
assert(I != BB->end() && "Can't read debug loc from end()");
InsertPt = I->getIterator();
assert(InsertPt != BB->end() && "Can't read debug loc from end()");
SetCurrentDebugLocation(I->getDebugLoc());
}

View File

@ -734,13 +734,13 @@ public:
for (typename TraitsTy::nodes_iterator I = TraitsTy::nodes_begin(&F),
E = TraitsTy::nodes_end(&F);
I != E; ++I) {
if (TraitsTy::child_begin(I) == TraitsTy::child_end(I))
addRoot(I);
if (TraitsTy::child_begin(&*I) == TraitsTy::child_end(&*I))
addRoot(&*I);
// Prepopulate maps so that we don't get iterator invalidation issues
// later.
this->IDoms[I] = nullptr;
this->DomTreeNodes[I] = nullptr;
this->IDoms[&*I] = nullptr;
this->DomTreeNodes[&*I] = nullptr;
}
Calculate<FT, Inverse<NodeT *>>(*this, F);

View File

@ -817,7 +817,7 @@ void SlotTracker::processFunction() {
for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
AE = TheFunction->arg_end(); AI != AE; ++AI)
if (!AI->hasName())
CreateFunctionSlot(AI);
CreateFunctionSlot(&*AI);
ST_DEBUG("Inserting Instructions:\n");
@ -2635,7 +2635,7 @@ void AssemblyWriter::printFunction(const Function *F) {
Out << " {";
// Output all of the function's basic blocks.
for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
printBasicBlock(I);
printBasicBlock(&*I);
// Output the function's use-lists.
printUseLists(F);

View File

@ -362,7 +362,7 @@ void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Function *F = CI->getCalledFunction();
LLVMContext &C = CI->getContext();
IRBuilder<> Builder(C);
Builder.SetInsertPoint(CI->getParent(), CI);
Builder.SetInsertPoint(CI->getParent(), CI->getIterator());
assert(F && "Intrinsic call is not direct?");
@ -388,7 +388,7 @@ void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Name == "llvm.x86.avx.movnt.ps.256" ||
Name == "llvm.x86.avx.movnt.pd.256") {
IRBuilder<> Builder(C);
Builder.SetInsertPoint(CI->getParent(), CI);
Builder.SetInsertPoint(CI->getParent(), CI->getIterator());
Module *M = F->getParent();
SmallVector<Metadata *, 1> Elts;

View File

@ -56,7 +56,7 @@ void BasicBlock::insertInto(Function *NewParent, BasicBlock *InsertBefore) {
assert(!Parent && "Already has a parent");
if (InsertBefore)
NewParent->getBasicBlockList().insert(InsertBefore, this);
NewParent->getBasicBlockList().insert(InsertBefore->getIterator(), this);
else
NewParent->getBasicBlockList().push_back(this);
}
@ -91,26 +91,26 @@ void BasicBlock::setParent(Function *parent) {
}
void BasicBlock::removeFromParent() {
getParent()->getBasicBlockList().remove(this);
getParent()->getBasicBlockList().remove(getIterator());
}
iplist<BasicBlock>::iterator BasicBlock::eraseFromParent() {
return getParent()->getBasicBlockList().erase(this);
return getParent()->getBasicBlockList().erase(getIterator());
}
/// Unlink this basic block from its current function and
/// insert it into the function that MovePos lives in, right before MovePos.
void BasicBlock::moveBefore(BasicBlock *MovePos) {
MovePos->getParent()->getBasicBlockList().splice(MovePos,
getParent()->getBasicBlockList(), this);
MovePos->getParent()->getBasicBlockList().splice(
MovePos->getIterator(), getParent()->getBasicBlockList(), getIterator());
}
/// Unlink this basic block from its current function and
/// insert it into the function that MovePos lives in, right after MovePos.
void BasicBlock::moveAfter(BasicBlock *MovePos) {
Function::iterator I = MovePos;
MovePos->getParent()->getBasicBlockList().splice(++I,
getParent()->getBasicBlockList(), this);
MovePos->getParent()->getBasicBlockList().splice(
++MovePos->getIterator(), getParent()->getBasicBlockList(),
getIterator());
}
const Module *BasicBlock::getModule() const {
@ -196,7 +196,7 @@ BasicBlock::iterator BasicBlock::getFirstInsertionPt() {
if (!FirstNonPHI)
return end();
iterator InsertPt = FirstNonPHI;
iterator InsertPt = FirstNonPHI->getIterator();
if (InsertPt->isEHPad()) ++InsertPt;
return InsertPt;
}

View File

@ -1533,7 +1533,7 @@ LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
Module::global_iterator I = Mod->global_begin();
if (I == Mod->global_end())
return nullptr;
return wrap(I);
return wrap(&*I);
}
LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
@ -1541,23 +1541,23 @@ LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
Module::global_iterator I = Mod->global_end();
if (I == Mod->global_begin())
return nullptr;
return wrap(--I);
return wrap(&*--I);
}
LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
Module::global_iterator I = GV;
Module::global_iterator I(GV);
if (++I == GV->getParent()->global_end())
return nullptr;
return wrap(I);
return wrap(&*I);
}
LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
Module::global_iterator I = GV;
Module::global_iterator I(GV);
if (I == GV->getParent()->global_begin())
return nullptr;
return wrap(--I);
return wrap(&*--I);
}
void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
@ -1666,7 +1666,7 @@ LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
Module::iterator I = Mod->begin();
if (I == Mod->end())
return nullptr;
return wrap(I);
return wrap(&*I);
}
LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
@ -1674,23 +1674,23 @@ LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
Module::iterator I = Mod->end();
if (I == Mod->begin())
return nullptr;
return wrap(--I);
return wrap(&*--I);
}
LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
Function *Func = unwrap<Function>(Fn);
Module::iterator I = Func;
Module::iterator I(Func);
if (++I == Func->getParent()->end())
return nullptr;
return wrap(I);
return wrap(&*I);
}
LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
Function *Func = unwrap<Function>(Fn);
Module::iterator I = Func;
Module::iterator I(Func);
if (I == Func->getParent()->begin())
return nullptr;
return wrap(--I);
return wrap(&*--I);
}
void LLVMDeleteFunction(LLVMValueRef Fn) {
@ -1785,14 +1785,14 @@ void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
Function *Fn = unwrap<Function>(FnRef);
for (Function::arg_iterator I = Fn->arg_begin(),
E = Fn->arg_end(); I != E; I++)
*ParamRefs++ = wrap(I);
*ParamRefs++ = wrap(&*I);
}
LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
while (index --> 0)
AI++;
return wrap(AI);
return wrap(&*AI);
}
LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
@ -1804,7 +1804,7 @@ LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
Function::arg_iterator I = Func->arg_begin();
if (I == Func->arg_end())
return nullptr;
return wrap(I);
return wrap(&*I);
}
LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
@ -1812,23 +1812,23 @@ LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
Function::arg_iterator I = Func->arg_end();
if (I == Func->arg_begin())
return nullptr;
return wrap(--I);
return wrap(&*--I);
}
LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
Argument *A = unwrap<Argument>(Arg);
Function::arg_iterator I = A;
Function::arg_iterator I(A);
if (++I == A->getParent()->arg_end())
return nullptr;
return wrap(I);
return wrap(&*I);
}
LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
Argument *A = unwrap<Argument>(Arg);
Function::arg_iterator I = A;
Function::arg_iterator I(A);
if (I == A->getParent()->arg_begin())
return nullptr;
return wrap(--I);
return wrap(&*--I);
}
void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
@ -1886,7 +1886,7 @@ unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
Function *Fn = unwrap<Function>(FnRef);
for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
*BasicBlocksRefs++ = wrap(I);
*BasicBlocksRefs++ = wrap(&*I);
}
LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
@ -1898,7 +1898,7 @@ LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
Function::iterator I = Func->begin();
if (I == Func->end())
return nullptr;
return wrap(I);
return wrap(&*I);
}
LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
@ -1906,23 +1906,23 @@ LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
Function::iterator I = Func->end();
if (I == Func->begin())
return nullptr;
return wrap(--I);
return wrap(&*--I);
}
LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
BasicBlock *Block = unwrap(BB);
Function::iterator I = Block;
Function::iterator I(Block);
if (++I == Block->getParent()->end())
return nullptr;
return wrap(I);
return wrap(&*I);
}
LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
BasicBlock *Block = unwrap(BB);
Function::iterator I = Block;
Function::iterator I(Block);
if (I == Block->getParent()->begin())
return nullptr;
return wrap(--I);
return wrap(&*--I);
}
LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
@ -1974,7 +1974,7 @@ LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
BasicBlock::iterator I = Block->begin();
if (I == Block->end())
return nullptr;
return wrap(I);
return wrap(&*I);
}
LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
@ -1982,23 +1982,23 @@ LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
BasicBlock::iterator I = Block->end();
if (I == Block->begin())
return nullptr;
return wrap(--I);
return wrap(&*--I);
}
LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
Instruction *Instr = unwrap<Instruction>(Inst);
BasicBlock::iterator I = Instr;
BasicBlock::iterator I(Instr);
if (++I == Instr->getParent()->end())
return nullptr;
return wrap(I);
return wrap(&*I);
}
LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
Instruction *Instr = unwrap<Instruction>(Inst);
BasicBlock::iterator I = Instr;
BasicBlock::iterator I(Instr);
if (I == Instr->getParent()->begin())
return nullptr;
return wrap(--I);
return wrap(&*--I);
}
void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
@ -2166,12 +2166,12 @@ void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
LLVMValueRef Instr) {
BasicBlock *BB = unwrap(Block);
Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
unwrap(Builder)->SetInsertPoint(BB, I);
unwrap(Builder)->SetInsertPoint(BB, I->getIterator());
}
void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
Instruction *I = unwrap<Instruction>(Instr);
unwrap(Builder)->SetInsertPoint(I->getParent(), I);
unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
}
void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {

View File

@ -336,7 +336,7 @@ bool llvm::StripDebugInfo(Module &M) {
for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
NME = M.named_metadata_end(); NMI != NME;) {
NamedMDNode *NMD = NMI;
NamedMDNode *NMD = &*NMI;
++NMI;
if (NMD->getName().startswith("llvm.dbg.")) {
NMD->eraseFromParent();

View File

@ -235,11 +235,11 @@ Type *Function::getReturnType() const {
}
void Function::removeFromParent() {
getParent()->getFunctionList().remove(this);
getParent()->getFunctionList().remove(getIterator());
}
void Function::eraseFromParent() {
getParent()->getFunctionList().erase(this);
getParent()->getFunctionList().erase(getIterator());
}
//===----------------------------------------------------------------------===//

View File

@ -178,7 +178,7 @@ GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
}
if (Before)
Before->getParent()->getGlobalList().insert(Before, this);
Before->getParent()->getGlobalList().insert(Before->getIterator(), this);
else
M.getGlobalList().push_back(this);
}
@ -188,11 +188,11 @@ void GlobalVariable::setParent(Module *parent) {
}
void GlobalVariable::removeFromParent() {
getParent()->getGlobalList().remove(this);
getParent()->getGlobalList().remove(getIterator());
}
void GlobalVariable::eraseFromParent() {
getParent()->getGlobalList().erase(this);
getParent()->getGlobalList().erase(getIterator());
}
void GlobalVariable::setInitializer(Constant *InitVal) {
@ -276,11 +276,11 @@ void GlobalAlias::setParent(Module *parent) {
}
void GlobalAlias::removeFromParent() {
getParent()->getAliasList().remove(this);
getParent()->getAliasList().remove(getIterator());
}
void GlobalAlias::eraseFromParent() {
getParent()->getAliasList().erase(this);
getParent()->getAliasList().erase(getIterator());
}
void GlobalAlias::setAliasee(Constant *Aliasee) {

View File

@ -28,7 +28,7 @@ Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
if (InsertBefore) {
BasicBlock *BB = InsertBefore->getParent();
assert(BB && "Instruction to insert before is not in a basic block!");
BB->getInstList().insert(InsertBefore, this);
BB->getInstList().insert(InsertBefore->getIterator(), this);
}
}
@ -64,31 +64,32 @@ Module *Instruction::getModule() {
void Instruction::removeFromParent() {
getParent()->getInstList().remove(this);
getParent()->getInstList().remove(getIterator());
}
iplist<Instruction>::iterator Instruction::eraseFromParent() {
return getParent()->getInstList().erase(this);
return getParent()->getInstList().erase(getIterator());
}
/// insertBefore - Insert an unlinked instructions into a basic block
/// immediately before the specified instruction.
void Instruction::insertBefore(Instruction *InsertPos) {
InsertPos->getParent()->getInstList().insert(InsertPos, this);
InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this);
}
/// insertAfter - Insert an unlinked instructions into a basic block
/// immediately after the specified instruction.
void Instruction::insertAfter(Instruction *InsertPos) {
InsertPos->getParent()->getInstList().insertAfter(InsertPos, this);
InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(),
this);
}
/// moveBefore - Unlink this instruction from its current basic block and
/// insert it into the basic block that MovePos lives in, right before
/// MovePos.
void Instruction::moveBefore(Instruction *MovePos) {
MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
this);
MovePos->getParent()->getInstList().splice(
MovePos->getIterator(), getParent()->getInstList(), getIterator());
}
/// Set or clear the unsafe-algebra flag on this instruction, which must be an

View File

@ -277,7 +277,7 @@ NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
/// delete it.
void Module::eraseNamedMetadata(NamedMDNode *NMD) {
static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
NamedMDList.erase(NMD);
NamedMDList.erase(NMD->getIterator());
}
bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) {

View File

@ -55,7 +55,7 @@ void SymbolTableListTraits<ValueSubClass>::setSymTabObject(TPtr *Dest,
// Add all of the items to the new symtab.
for (auto I = ItemList.begin(); I != ItemList.end(); ++I)
if (I->hasName())
NewST->reinsertValue(I);
NewST->reinsertValue(&*I);
}
}

View File

@ -56,7 +56,7 @@ void TypeFinder::run(const Module &M, bool onlyNamed) {
// First incorporate the arguments.
for (Function::const_arg_iterator AI = FI->arg_begin(),
AE = FI->arg_end(); AI != AE; ++AI)
incorporateValue(AI);
incorporateValue(&*AI);
for (Function::const_iterator BB = FI->begin(), E = FI->end();
BB != E;++BB)
@ -85,7 +85,7 @@ void TypeFinder::run(const Module &M, bool onlyNamed) {
for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
E = M.named_metadata_end(); I != E; ++I) {
const NamedMDNode *NMD = I;
const NamedMDNode *NMD = &*I;
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
incorporateMDNode(NMD->getOperand(i));
}

View File

@ -92,6 +92,10 @@ struct VerifierSupport {
: OS(OS), M(nullptr), Broken(false) {}
private:
template <class NodeTy> void Write(const ilist_iterator<NodeTy> &I) {
Write(&*I);
}
void Write(const Value *V) {
if (!V)
return;