llvm.org GIT mirror llvm / eac3095
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.) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@249782 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 4 years ago
16 changed file(s) with 95 addition(s) and 77 deletion(s). Raw diff Collapse all Expand all
236236 NodePtr = RHS.getNodePtrUnchecked();
237237 return *this;
238238 }
239
240 void reset(pointer NP) { NodePtr = NP; }
239241
240242 // Accessors...
241243 operator pointer() const {
468470 this->setPrev(CurNode, New);
469471
470472 this->addNodeToList(New); // Notify traits that we added a node...
471 return New;
473 return iterator(New);
472474 }
473475
474476 iterator insertAfter(iterator where, NodeTy *New) {
489491 else
490492 Head = NextNode;
491493 this->setPrev(NextNode, PrevNode);
492 IT = NextNode;
494 IT.reset(NextNode);
493495 this->removeNodeFromList(Node); // Notify traits that we removed a node...
494496
495497 // Set the next/prev pointers of the current node to null. This isn't
568570 this->setNext(Last, PosNext);
569571 this->setPrev(PosNext, Last);
570572
571 this->transferNodesFromList(L2, First, PosNext);
573 this->transferNodesFromList(L2, iterator(First), iterator(PosNext));
572574
573575 // Now that everything is set, restore the pointers to the list sentinels.
574576 L2.setTail(L2Sentinel);
3838 template
3939 struct ilist_nextprev_traits;
4040
41 template class ilist_iterator;
42
4143 /// ilist_node - Base class that provides next/prev services for nodes
4244 /// that use ilist_nextprev_traits or ilist_default_traits.
4345 ///
5557 ilist_node() : Next(nullptr) {}
5658
5759 public:
60 ilist_iterator getIterator() {
61 // FIXME: Stop downcasting to create the iterator (potential UB).
62 return ilist_iterator(static_cast(this));
63 }
64 ilist_iterator getIterator() const {
65 // FIXME: Stop downcasting to create the iterator (potential UB).
66 return ilist_iterator(static_cast(this));
67 }
68
5869 /// @name Adjacent Node Accessors
5970 /// @{
6071
7474 /// inserted into a block.
7575 void ClearInsertionPoint() {
7676 BB = nullptr;
77 InsertPt = nullptr;
77 InsertPt.reset(nullptr);
7878 }
7979
8080 BasicBlock *GetInsertBlock() const { return BB; }
9292 /// the specified instruction.
9393 void SetInsertPoint(Instruction *I) {
9494 BB = I->getParent();
95 InsertPt = I;
96 assert(I != BB->end() && "Can't read debug loc from end()");
95 InsertPt = I->getIterator();
96 assert(InsertPt != BB->end() && "Can't read debug loc from end()");
9797 SetCurrentDebugLocation(I->getDebugLoc());
9898 }
9999
733733 for (typename TraitsTy::nodes_iterator I = TraitsTy::nodes_begin(&F),
734734 E = TraitsTy::nodes_end(&F);
735735 I != E; ++I) {
736 if (TraitsTy::child_begin(I) == TraitsTy::child_end(I))
737 addRoot(I);
736 if (TraitsTy::child_begin(&*I) == TraitsTy::child_end(&*I))
737 addRoot(&*I);
738738
739739 // Prepopulate maps so that we don't get iterator invalidation issues
740740 // later.
741 this->IDoms[I] = nullptr;
742 this->DomTreeNodes[I] = nullptr;
741 this->IDoms[&*I] = nullptr;
742 this->DomTreeNodes[&*I] = nullptr;
743743 }
744744
745745 Calculate>(*this, F);
816816 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
817817 AE = TheFunction->arg_end(); AI != AE; ++AI)
818818 if (!AI->hasName())
819 CreateFunctionSlot(AI);
819 CreateFunctionSlot(&*AI);
820820
821821 ST_DEBUG("Inserting Instructions:\n");
822822
26342634 Out << " {";
26352635 // Output all of the function's basic blocks.
26362636 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
2637 printBasicBlock(I);
2637 printBasicBlock(&*I);
26382638
26392639 // Output the function's use-lists.
26402640 printUseLists(F);
361361 Function *F = CI->getCalledFunction();
362362 LLVMContext &C = CI->getContext();
363363 IRBuilder<> Builder(C);
364 Builder.SetInsertPoint(CI->getParent(), CI);
364 Builder.SetInsertPoint(CI->getParent(), CI->getIterator());
365365
366366 assert(F && "Intrinsic call is not direct?");
367367
387387 Name == "llvm.x86.avx.movnt.ps.256" ||
388388 Name == "llvm.x86.avx.movnt.pd.256") {
389389 IRBuilder<> Builder(C);
390 Builder.SetInsertPoint(CI->getParent(), CI);
390 Builder.SetInsertPoint(CI->getParent(), CI->getIterator());
391391
392392 Module *M = F->getParent();
393393 SmallVector Elts;
5555 assert(!Parent && "Already has a parent");
5656
5757 if (InsertBefore)
58 NewParent->getBasicBlockList().insert(InsertBefore, this);
58 NewParent->getBasicBlockList().insert(InsertBefore->getIterator(), this);
5959 else
6060 NewParent->getBasicBlockList().push_back(this);
6161 }
9090 }
9191
9292 void BasicBlock::removeFromParent() {
93 getParent()->getBasicBlockList().remove(this);
93 getParent()->getBasicBlockList().remove(getIterator());
9494 }
9595
9696 iplist::iterator BasicBlock::eraseFromParent() {
97 return getParent()->getBasicBlockList().erase(this);
97 return getParent()->getBasicBlockList().erase(getIterator());
9898 }
9999
100100 /// Unlink this basic block from its current function and
101101 /// insert it into the function that MovePos lives in, right before MovePos.
102102 void BasicBlock::moveBefore(BasicBlock *MovePos) {
103 MovePos->getParent()->getBasicBlockList().splice(MovePos,
104 getParent()->getBasicBlockList(), this);
103 MovePos->getParent()->getBasicBlockList().splice(
104 MovePos->getIterator(), getParent()->getBasicBlockList(), getIterator());
105105 }
106106
107107 /// Unlink this basic block from its current function and
108108 /// insert it into the function that MovePos lives in, right after MovePos.
109109 void BasicBlock::moveAfter(BasicBlock *MovePos) {
110 Function::iterator I = MovePos;
111 MovePos->getParent()->getBasicBlockList().splice(++I,
112 getParent()->getBasicBlockList(), this);
110 MovePos->getParent()->getBasicBlockList().splice(
111 ++MovePos->getIterator(), getParent()->getBasicBlockList(),
112 getIterator());
113113 }
114114
115115 const Module *BasicBlock::getModule() const {
195195 if (!FirstNonPHI)
196196 return end();
197197
198 iterator InsertPt = FirstNonPHI;
198 iterator InsertPt = FirstNonPHI->getIterator();
199199 if (InsertPt->isEHPad()) ++InsertPt;
200200 return InsertPt;
201201 }
15321532 Module::global_iterator I = Mod->global_begin();
15331533 if (I == Mod->global_end())
15341534 return nullptr;
1535 return wrap(I);
1535 return wrap(&*I);
15361536 }
15371537
15381538 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
15401540 Module::global_iterator I = Mod->global_end();
15411541 if (I == Mod->global_begin())
15421542 return nullptr;
1543 return wrap(--I);
1543 return wrap(&*--I);
15441544 }
15451545
15461546 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
15471547 GlobalVariable *GV = unwrap(GlobalVar);
1548 Module::global_iterator I = GV;
1548 Module::global_iterator I(GV);
15491549 if (++I == GV->getParent()->global_end())
15501550 return nullptr;
1551 return wrap(I);
1551 return wrap(&*I);
15521552 }
15531553
15541554 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
15551555 GlobalVariable *GV = unwrap(GlobalVar);
1556 Module::global_iterator I = GV;
1556 Module::global_iterator I(GV);
15571557 if (I == GV->getParent()->global_begin())
15581558 return nullptr;
1559 return wrap(--I);
1559 return wrap(&*--I);
15601560 }
15611561
15621562 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
16651665 Module::iterator I = Mod->begin();
16661666 if (I == Mod->end())
16671667 return nullptr;
1668 return wrap(I);
1668 return wrap(&*I);
16691669 }
16701670
16711671 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
16731673 Module::iterator I = Mod->end();
16741674 if (I == Mod->begin())
16751675 return nullptr;
1676 return wrap(--I);
1676 return wrap(&*--I);
16771677 }
16781678
16791679 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
16801680 Function *Func = unwrap(Fn);
1681 Module::iterator I = Func;
1681 Module::iterator I(Func);
16821682 if (++I == Func->getParent()->end())
16831683 return nullptr;
1684 return wrap(I);
1684 return wrap(&*I);
16851685 }
16861686
16871687 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
16881688 Function *Func = unwrap(Fn);
1689 Module::iterator I = Func;
1689 Module::iterator I(Func);
16901690 if (I == Func->getParent()->begin())
16911691 return nullptr;
1692 return wrap(--I);
1692 return wrap(&*--I);
16931693 }
16941694
16951695 void LLVMDeleteFunction(LLVMValueRef Fn) {
17841784 Function *Fn = unwrap(FnRef);
17851785 for (Function::arg_iterator I = Fn->arg_begin(),
17861786 E = Fn->arg_end(); I != E; I++)
1787 *ParamRefs++ = wrap(I);
1787 *ParamRefs++ = wrap(&*I);
17881788 }
17891789
17901790 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
17911791 Function::arg_iterator AI = unwrap(FnRef)->arg_begin();
17921792 while (index --> 0)
17931793 AI++;
1794 return wrap(AI);
1794 return wrap(&*AI);
17951795 }
17961796
17971797 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
18031803 Function::arg_iterator I = Func->arg_begin();
18041804 if (I == Func->arg_end())
18051805 return nullptr;
1806 return wrap(I);
1806 return wrap(&*I);
18071807 }
18081808
18091809 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
18111811 Function::arg_iterator I = Func->arg_end();
18121812 if (I == Func->arg_begin())
18131813 return nullptr;
1814 return wrap(--I);
1814 return wrap(&*--I);
18151815 }
18161816
18171817 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
18181818 Argument *A = unwrap(Arg);
1819 Function::arg_iterator I = A;
1819 Function::arg_iterator I(A);
18201820 if (++I == A->getParent()->arg_end())
18211821 return nullptr;
1822 return wrap(I);
1822 return wrap(&*I);
18231823 }
18241824
18251825 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
18261826 Argument *A = unwrap(Arg);
1827 Function::arg_iterator I = A;
1827 Function::arg_iterator I(A);
18281828 if (I == A->getParent()->arg_begin())
18291829 return nullptr;
1830 return wrap(--I);
1830 return wrap(&*--I);
18311831 }
18321832
18331833 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
18851885 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
18861886 Function *Fn = unwrap(FnRef);
18871887 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1888 *BasicBlocksRefs++ = wrap(I);
1888 *BasicBlocksRefs++ = wrap(&*I);
18891889 }
18901890
18911891 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
18971897 Function::iterator I = Func->begin();
18981898 if (I == Func->end())
18991899 return nullptr;
1900 return wrap(I);
1900 return wrap(&*I);
19011901 }
19021902
19031903 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
19051905 Function::iterator I = Func->end();
19061906 if (I == Func->begin())
19071907 return nullptr;
1908 return wrap(--I);
1908 return wrap(&*--I);
19091909 }
19101910
19111911 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
19121912 BasicBlock *Block = unwrap(BB);
1913 Function::iterator I = Block;
1913 Function::iterator I(Block);
19141914 if (++I == Block->getParent()->end())
19151915 return nullptr;
1916 return wrap(I);
1916 return wrap(&*I);
19171917 }
19181918
19191919 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
19201920 BasicBlock *Block = unwrap(BB);
1921 Function::iterator I = Block;
1921 Function::iterator I(Block);
19221922 if (I == Block->getParent()->begin())
19231923 return nullptr;
1924 return wrap(--I);
1924 return wrap(&*--I);
19251925 }
19261926
19271927 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
19731973 BasicBlock::iterator I = Block->begin();
19741974 if (I == Block->end())
19751975 return nullptr;
1976 return wrap(I);
1976 return wrap(&*I);
19771977 }
19781978
19791979 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
19811981 BasicBlock::iterator I = Block->end();
19821982 if (I == Block->begin())
19831983 return nullptr;
1984 return wrap(--I);
1984 return wrap(&*--I);
19851985 }
19861986
19871987 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
19881988 Instruction *Instr = unwrap(Inst);
1989 BasicBlock::iterator I = Instr;
1989 BasicBlock::iterator I(Instr);
19901990 if (++I == Instr->getParent()->end())
19911991 return nullptr;
1992 return wrap(I);
1992 return wrap(&*I);
19931993 }
19941994
19951995 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
19961996 Instruction *Instr = unwrap(Inst);
1997 BasicBlock::iterator I = Instr;
1997 BasicBlock::iterator I(Instr);
19981998 if (I == Instr->getParent()->begin())
19991999 return nullptr;
2000 return wrap(--I);
2000 return wrap(&*--I);
20012001 }
20022002
20032003 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
21652165 LLVMValueRef Instr) {
21662166 BasicBlock *BB = unwrap(Block);
21672167 Instruction *I = Instr? unwrap(Instr) : (Instruction*) BB->end();
2168 unwrap(Builder)->SetInsertPoint(BB, I);
2168 unwrap(Builder)->SetInsertPoint(BB, I->getIterator());
21692169 }
21702170
21712171 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
21722172 Instruction *I = unwrap(Instr);
2173 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
2173 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
21742174 }
21752175
21762176 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
335335
336336 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
337337 NME = M.named_metadata_end(); NMI != NME;) {
338 NamedMDNode *NMD = NMI;
338 NamedMDNode *NMD = &*NMI;
339339 ++NMI;
340340 if (NMD->getName().startswith("llvm.dbg.")) {
341341 NMD->eraseFromParent();
234234 }
235235
236236 void Function::removeFromParent() {
237 getParent()->getFunctionList().remove(this);
237 getParent()->getFunctionList().remove(getIterator());
238238 }
239239
240240 void Function::eraseFromParent() {
241 getParent()->getFunctionList().erase(this);
241 getParent()->getFunctionList().erase(getIterator());
242242 }
243243
244244 //===----------------------------------------------------------------------===//
177177 }
178178
179179 if (Before)
180 Before->getParent()->getGlobalList().insert(Before, this);
180 Before->getParent()->getGlobalList().insert(Before->getIterator(), this);
181181 else
182182 M.getGlobalList().push_back(this);
183183 }
187187 }
188188
189189 void GlobalVariable::removeFromParent() {
190 getParent()->getGlobalList().remove(this);
190 getParent()->getGlobalList().remove(getIterator());
191191 }
192192
193193 void GlobalVariable::eraseFromParent() {
194 getParent()->getGlobalList().erase(this);
194 getParent()->getGlobalList().erase(getIterator());
195195 }
196196
197197 void GlobalVariable::setInitializer(Constant *InitVal) {
275275 }
276276
277277 void GlobalAlias::removeFromParent() {
278 getParent()->getAliasList().remove(this);
278 getParent()->getAliasList().remove(getIterator());
279279 }
280280
281281 void GlobalAlias::eraseFromParent() {
282 getParent()->getAliasList().erase(this);
282 getParent()->getAliasList().erase(getIterator());
283283 }
284284
285285 void GlobalAlias::setAliasee(Constant *Aliasee) {
2727 if (InsertBefore) {
2828 BasicBlock *BB = InsertBefore->getParent();
2929 assert(BB && "Instruction to insert before is not in a basic block!");
30 BB->getInstList().insert(InsertBefore, this);
30 BB->getInstList().insert(InsertBefore->getIterator(), this);
3131 }
3232 }
3333
6363
6464
6565 void Instruction::removeFromParent() {
66 getParent()->getInstList().remove(this);
66 getParent()->getInstList().remove(getIterator());
6767 }
6868
6969 iplist::iterator Instruction::eraseFromParent() {
70 return getParent()->getInstList().erase(this);
70 return getParent()->getInstList().erase(getIterator());
7171 }
7272
7373 /// insertBefore - Insert an unlinked instructions into a basic block
7474 /// immediately before the specified instruction.
7575 void Instruction::insertBefore(Instruction *InsertPos) {
76 InsertPos->getParent()->getInstList().insert(InsertPos, this);
76 InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this);
7777 }
7878
7979 /// insertAfter - Insert an unlinked instructions into a basic block
8080 /// immediately after the specified instruction.
8181 void Instruction::insertAfter(Instruction *InsertPos) {
82 InsertPos->getParent()->getInstList().insertAfter(InsertPos, this);
82 InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(),
83 this);
8384 }
8485
8586 /// moveBefore - Unlink this instruction from its current basic block and
8687 /// insert it into the basic block that MovePos lives in, right before
8788 /// MovePos.
8889 void Instruction::moveBefore(Instruction *MovePos) {
89 MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
90 this);
90 MovePos->getParent()->getInstList().splice(
91 MovePos->getIterator(), getParent()->getInstList(), getIterator());
9192 }
9293
9394 /// Set or clear the unsafe-algebra flag on this instruction, which must be an
276276 /// delete it.
277277 void Module::eraseNamedMetadata(NamedMDNode *NMD) {
278278 static_cast *>(NamedMDSymTab)->erase(NMD->getName());
279 NamedMDList.erase(NMD);
279 NamedMDList.erase(NMD->getIterator());
280280 }
281281
282282 bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) {
5454 // Add all of the items to the new symtab.
5555 for (auto I = ItemList.begin(); I != ItemList.end(); ++I)
5656 if (I->hasName())
57 NewST->reinsertValue(I);
57 NewST->reinsertValue(&*I);
5858 }
5959
6060 }
5555 // First incorporate the arguments.
5656 for (Function::const_arg_iterator AI = FI->arg_begin(),
5757 AE = FI->arg_end(); AI != AE; ++AI)
58 incorporateValue(AI);
58 incorporateValue(&*AI);
5959
6060 for (Function::const_iterator BB = FI->begin(), E = FI->end();
6161 BB != E;++BB)
8484
8585 for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
8686 E = M.named_metadata_end(); I != E; ++I) {
87 const NamedMDNode *NMD = I;
87 const NamedMDNode *NMD = &*I;
8888 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
8989 incorporateMDNode(NMD->getOperand(i));
9090 }
9191 : OS(OS), M(nullptr), Broken(false) {}
9292
9393 private:
94 template void Write(const ilist_iterator &I) {
95 Write(&*I);
96 }
97
9498 void Write(const Value *V) {
9599 if (!V)
96100 return;