Thanks Dan and Jon. I made an incorrect assumption that the "use" iterator was actually giving me the "user" when de-referencing it. Did it always have this behavior in previous LLVM versions? I've seen lots of examples of the "use" iterator being dereferenced and resulting Instruction pointer being treated as the "user"? Thanks, Zack On Tue, Jun 9, 2015 at 7:25 PM, Jonathan Roelofs <jonathan at codesourcery.com> wrote:> > > On 6/9/15 8:02 PM, Zack Waters wrote: > >> Hi, >> >> I'm having a problem with the use iterator. Each "use" that I see, when >> using the use_iterator, is the same as the "def". Meaning, in the code >> below the pDef is always equal to pUse pointer for every instruction in >> all basic blocks (except terminators). >> >> for (auto i = inst_begin(f), ie = inst_end(f); i != ie; ++i) >> Instruction* pDef = &(*i); >> errs() << "Def: " << *pDef << "\n"; >> >> for (auto ui = pDef->use_begin(), uie >> pDef->use_end(); ui != uie; ++ui) >> { >> > > 'user' != 'use'. > > Think of llvm::Use as the edge between the place where a value is > produced, and the place where that value is consumed. The consumer is the > 'User', and the Use points at it. > > http://llvm.org/docs/doxygen/html/classllvm_1_1Use.html > > The confusing thing that's happening below is that the llvm::Use is > implicitly converted via `llvm::Use::operator Value *() const` to a > `Value*`, and that `Value*` is `pDef`. > > > HTH, > > Jon > > Instruction* pUse = dyn_cast<Instruction>(*ui); >> errs() << " Use: \t" << *pUse << "\n"; >> } >> } >> >> However, everything works as expected when using the range-based use >> iterator with the following code. >> >> for (auto i = inst_begin(f), ie = inst_end(f); i != ie; ++i) >> { >> Instruction* pDef = &(*i); >> errs() << "Def: " << *pDef << "\n"; >> >> for (User* pUser : pDef->users()) >> { >> Instruction* pUse = dyn_cast<Instruction>(pUser); >> errs() << " Use: \t" << *pUse << "\n"; >> } >> } >> >> Also, the code is executed inside a function pass. So was initially >> thinking I somehow screwed up the use information in a previous pass. >> However, I would assume the range-based iterator would not work as well >> but it does. >> >> Finally, I'm currently using LLVM 3.5.1 built for Windows. Google hasn't >> been much help. Anybody have any suggestions as to why the first example >> above doesn't work? >> >> Thanks, >> Zack >> >> >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >> >> > -- > Jon Roelofs > jonathan at codesourcery.com > CodeSourcery / Mentor Embedded >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150609/66e1331e/attachment.html>
On 6/9/15 8:54 PM, Zack Waters wrote:> Thanks Dan and Jon. I made an incorrect assumption that the "use" > iterator was actually giving me the "user" when de-referencing it. > > Did it always have this behavior in previous LLVM versions?Not sure.... I don't know the history of it. Best guess is: "probably".> I've seen > lots of examples of the "use" iterator being dereferenced and resulting > Instruction pointer being treated as the "user"?Can you point out specific examples? Jon> > Thanks, > Zack > > > On Tue, Jun 9, 2015 at 7:25 PM, Jonathan Roelofs > <jonathan at codesourcery.com <mailto:jonathan at codesourcery.com>> wrote: > > > > On 6/9/15 8:02 PM, Zack Waters wrote: > > Hi, > > I'm having a problem with the use iterator. Each "use" that I > see, when > using the use_iterator, is the same as the "def". Meaning, in > the code > below the pDef is always equal to pUse pointer for every > instruction in > all basic blocks (except terminators). > > for (auto i = inst_begin(f), ie = inst_end(f); i > != ie; ++i) > Instruction* pDef = &(*i); > errs() << "Def: " << *pDef << "\n"; > > for (auto ui = pDef->use_begin(), uie > pDef->use_end(); ui != uie; ++ui) > { > > > 'user' != 'use'. > > Think of llvm::Use as the edge between the place where a value is > produced, and the place where that value is consumed. The consumer > is the 'User', and the Use points at it. > > http://llvm.org/docs/doxygen/html/classllvm_1_1Use.html > <https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_docs_doxygen_html_classllvm-5F1-5F1Use.html&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Mfk2qtn1LTDThVkh6-oGglNfMADXfJdty4_bhmuhMHA&m=P9nO53gRzaeWOd6JcPss24m-SNOOVI-ki0Gmt8jG2a4&s=vHntZspdmEIervA_wRJRemP18BYspEqdQnW_J99QC28&e=> > > The confusing thing that's happening below is that the llvm::Use is > implicitly converted via `llvm::Use::operator Value *() const` to a > `Value*`, and that `Value*` is `pDef`. > > > HTH, > > Jon > > Instruction* pUse > dyn_cast<Instruction>(*ui); > errs() << " Use: \t" << *pUse << "\n"; > } > } > > However, everything works as expected when using the range-based use > iterator with the following code. > > for (auto i = inst_begin(f), ie = inst_end(f); i > != ie; ++i) > { > Instruction* pDef = &(*i); > errs() << "Def: " << *pDef << "\n"; > > for (User* pUser : pDef->users()) > { > Instruction* pUse > dyn_cast<Instruction>(pUser); > errs() << " Use: \t" << *pUse << "\n"; > } > } > > Also, the code is executed inside a function pass. So was initially > thinking I somehow screwed up the use information in a previous > pass. > However, I would assume the range-based iterator would not work > as well > but it does. > > Finally, I'm currently using LLVM 3.5.1 built for Windows. > Google hasn't > been much help. Anybody have any suggestions as to why the first > example > above doesn't work? > > Thanks, > Zack > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu> > http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > > > -- > Jon Roelofs > jonathan at codesourcery.com <mailto:jonathan at codesourcery.com> > CodeSourcery / Mentor Embedded > > > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >-- Jon Roelofs jonathan at codesourcery.com CodeSourcery / Mentor Embedded
Here are some specific examples: http://www.cs.cmu.edu/afs/cs/academic/class/15745-f09/www/lectures/lect3-llvm.pdf - Slide 30 http://aviral.lab.asu.edu/?p=1699 - LLVM Def-Use & Use-Def Chains. http://stackoverflow.com/questions/6807743/def-use-chain-in-llvm https://github.com/codelion/pa.llvm/blob/master/CVA/CVA.cpp - RunAnalysis starting line 234 http://lists.cs.uiuc.edu/pipermail/llvmdev/2010-September/034909.html https://github.com/karrenberg/wfv/blob/master/src/functionPasses/loopLiveValueAnalysis.hpp - See findUseOutsideLoop function at line 227. I've seen other projects using LLVM 3.3 that have similar code. Zack On Tue, Jun 9, 2015 at 8:19 PM, Jonathan Roelofs <jonathan at codesourcery.com> wrote:> > > On 6/9/15 8:54 PM, Zack Waters wrote: > >> Thanks Dan and Jon. I made an incorrect assumption that the "use" >> iterator was actually giving me the "user" when de-referencing it. >> >> Did it always have this behavior in previous LLVM versions? >> > > Not sure.... I don't know the history of it. Best guess is: "probably". > > I've seen >> lots of examples of the "use" iterator being dereferenced and resulting >> Instruction pointer being treated as the "user"? >> > > Can you point out specific examples? > > > Jon > > >> Thanks, >> Zack >> >> >> On Tue, Jun 9, 2015 at 7:25 PM, Jonathan Roelofs >> <jonathan at codesourcery.com <mailto:jonathan at codesourcery.com>> wrote: >> >> >> >> On 6/9/15 8:02 PM, Zack Waters wrote: >> >> Hi, >> >> I'm having a problem with the use iterator. Each "use" that I >> see, when >> using the use_iterator, is the same as the "def". Meaning, in >> the code >> below the pDef is always equal to pUse pointer for every >> instruction in >> all basic blocks (except terminators). >> >> for (auto i = inst_begin(f), ie = inst_end(f); i >> != ie; ++i) >> Instruction* pDef = &(*i); >> errs() << "Def: " << *pDef << "\n"; >> >> for (auto ui = pDef->use_begin(), uie >> pDef->use_end(); ui != uie; ++ui) >> { >> >> >> 'user' != 'use'. >> >> Think of llvm::Use as the edge between the place where a value is >> produced, and the place where that value is consumed. The consumer >> is the 'User', and the Use points at it. >> >> http://llvm.org/docs/doxygen/html/classllvm_1_1Use.html >> < >> https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_docs_doxygen_html_classllvm-5F1-5F1Use.html&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Mfk2qtn1LTDThVkh6-oGglNfMADXfJdty4_bhmuhMHA&m=P9nO53gRzaeWOd6JcPss24m-SNOOVI-ki0Gmt8jG2a4&s=vHntZspdmEIervA_wRJRemP18BYspEqdQnW_J99QC28&e>> > >> >> >> The confusing thing that's happening below is that the llvm::Use is >> implicitly converted via `llvm::Use::operator Value *() const` to a >> `Value*`, and that `Value*` is `pDef`. >> >> >> HTH, >> >> Jon >> >> Instruction* pUse >> dyn_cast<Instruction>(*ui); >> errs() << " Use: \t" << *pUse << "\n"; >> } >> } >> >> However, everything works as expected when using the range-based >> use >> iterator with the following code. >> >> for (auto i = inst_begin(f), ie = inst_end(f); i >> != ie; ++i) >> { >> Instruction* pDef = &(*i); >> errs() << "Def: " << *pDef << "\n"; >> >> for (User* pUser : pDef->users()) >> { >> Instruction* pUse >> dyn_cast<Instruction>(pUser); >> errs() << " Use: \t" << *pUse << "\n"; >> } >> } >> >> Also, the code is executed inside a function pass. So was >> initially >> thinking I somehow screwed up the use information in a previous >> pass. >> However, I would assume the range-based iterator would not work >> as well >> but it does. >> >> Finally, I'm currently using LLVM 3.5.1 built for Windows. >> Google hasn't >> been much help. Anybody have any suggestions as to why the first >> example >> above doesn't work? >> >> Thanks, >> Zack >> >> >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu> >> http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >> >> >> -- >> Jon Roelofs >> jonathan at codesourcery.com <mailto:jonathan at codesourcery.com> >> CodeSourcery / Mentor Embedded >> >> >> >> >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >> >> > -- > Jon Roelofs > jonathan at codesourcery.com > CodeSourcery / Mentor Embedded >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150609/fa5d0bd1/attachment.html>
It has, AFAIK, always been this way. It is also a common source of bugs, as I believe you have discovered. If we had llvm-specific "clang warnings", this would be one i added (IE "Dereferencing use iterator of x gives x") :) In the past 6 months, i did this twice by accident in passes and then spent hours tracking it down. It actually makes me wonder whether use_iterator should even have an operator * for the use_iterator_impl<Use> case. ISTM to basically always be a bug (though this is an idle thought, i haven't thought through the implications at all). On Tue, Jun 9, 2015 at 7:54 PM, Zack Waters <zswaters at gmail.com> wrote:> Thanks Dan and Jon. I made an incorrect assumption that the "use" iterator > was actually giving me the "user" when de-referencing it. > > Did it always have this behavior in previous LLVM versions? I've seen lots > of examples of the "use" iterator being dereferenced and resulting > Instruction pointer being treated as the "user"? > > Thanks, > Zack > > > On Tue, Jun 9, 2015 at 7:25 PM, Jonathan Roelofs <jonathan at codesourcery.com> > wrote: >> >> >> >> On 6/9/15 8:02 PM, Zack Waters wrote: >>> >>> Hi, >>> >>> I'm having a problem with the use iterator. Each "use" that I see, when >>> using the use_iterator, is the same as the "def". Meaning, in the code >>> below the pDef is always equal to pUse pointer for every instruction in >>> all basic blocks (except terminators). >>> >>> for (auto i = inst_begin(f), ie = inst_end(f); i != ie; ++i) >>> Instruction* pDef = &(*i); >>> errs() << "Def: " << *pDef << "\n"; >>> >>> for (auto ui = pDef->use_begin(), uie >>> pDef->use_end(); ui != uie; ++ui) >>> { >> >> >> 'user' != 'use'. >> >> Think of llvm::Use as the edge between the place where a value is >> produced, and the place where that value is consumed. The consumer is the >> 'User', and the Use points at it. >> >> http://llvm.org/docs/doxygen/html/classllvm_1_1Use.html >> >> The confusing thing that's happening below is that the llvm::Use is >> implicitly converted via `llvm::Use::operator Value *() const` to a >> `Value*`, and that `Value*` is `pDef`. >> >> >> HTH, >> >> Jon >> >>> Instruction* pUse = dyn_cast<Instruction>(*ui); >>> errs() << " Use: \t" << *pUse << "\n"; >>> } >>> } >>> >>> However, everything works as expected when using the range-based use >>> iterator with the following code. >>> >>> for (auto i = inst_begin(f), ie = inst_end(f); i != ie; ++i) >>> { >>> Instruction* pDef = &(*i); >>> errs() << "Def: " << *pDef << "\n"; >>> >>> for (User* pUser : pDef->users()) >>> { >>> Instruction* pUse = dyn_cast<Instruction>(pUser); >>> errs() << " Use: \t" << *pUse << "\n"; >>> } >>> } >>> >>> Also, the code is executed inside a function pass. So was initially >>> thinking I somehow screwed up the use information in a previous pass. >>> However, I would assume the range-based iterator would not work as well >>> but it does. >>> >>> Finally, I'm currently using LLVM 3.5.1 built for Windows. Google hasn't >>> been much help. Anybody have any suggestions as to why the first example >>> above doesn't work? >>> >>> Thanks, >>> Zack >>> >>> >>> _______________________________________________ >>> LLVM Developers mailing list >>> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >>> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >>> >> >> -- >> Jon Roelofs >> jonathan at codesourcery.com >> CodeSourcery / Mentor Embedded > > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >
It appears dereferencing the use iterator returned the "user" in older LLVM versions. See the following commit to Use.h. You can see that the intention is to return the "user". So this is not the behavior anymore. https://github.com/llvm-mirror/llvm/commit/d1fe495464e4abc384565813cbf1cb8b130e5a6d - // Retrieve a pointer to the current User.- UserTy *operator*() const {- assert(U && "Cannot dereference end iterator!");- return U->getUser();- } Zack On Wed, Jun 10, 2015 at 7:17 AM, Daniel Berlin <dberlin at dberlin.org> wrote:> It has, AFAIK, always been this way. > It is also a common source of bugs, as I believe you have discovered. > > If we had llvm-specific "clang warnings", this would be one i added > (IE "Dereferencing use iterator of x gives x") :) > > In the past 6 months, i did this twice by accident in passes and then > spent hours tracking it down. > > It actually makes me wonder whether use_iterator should even have an > operator * for the use_iterator_impl<Use> case. ISTM to basically > always be a bug (though this is an idle thought, i haven't thought > through the implications at all). > > > > On Tue, Jun 9, 2015 at 7:54 PM, Zack Waters <zswaters at gmail.com> wrote: > > Thanks Dan and Jon. I made an incorrect assumption that the "use" > iterator > > was actually giving me the "user" when de-referencing it. > > > > Did it always have this behavior in previous LLVM versions? I've seen > lots > > of examples of the "use" iterator being dereferenced and resulting > > Instruction pointer being treated as the "user"? > > > > Thanks, > > Zack > > > > > > On Tue, Jun 9, 2015 at 7:25 PM, Jonathan Roelofs < > jonathan at codesourcery.com> > > wrote: > >> > >> > >> > >> On 6/9/15 8:02 PM, Zack Waters wrote: > >>> > >>> Hi, > >>> > >>> I'm having a problem with the use iterator. Each "use" that I see, when > >>> using the use_iterator, is the same as the "def". Meaning, in the code > >>> below the pDef is always equal to pUse pointer for every instruction in > >>> all basic blocks (except terminators). > >>> > >>> for (auto i = inst_begin(f), ie = inst_end(f); i != ie; > ++i) > >>> Instruction* pDef = &(*i); > >>> errs() << "Def: " << *pDef << "\n"; > >>> > >>> for (auto ui = pDef->use_begin(), uie > >>> pDef->use_end(); ui != uie; ++ui) > >>> { > >> > >> > >> 'user' != 'use'. > >> > >> Think of llvm::Use as the edge between the place where a value is > >> produced, and the place where that value is consumed. The consumer is > the > >> 'User', and the Use points at it. > >> > >> http://llvm.org/docs/doxygen/html/classllvm_1_1Use.html > >> > >> The confusing thing that's happening below is that the llvm::Use is > >> implicitly converted via `llvm::Use::operator Value *() const` to a > >> `Value*`, and that `Value*` is `pDef`. > >> > >> > >> HTH, > >> > >> Jon > >> > >>> Instruction* pUse = dyn_cast<Instruction>(*ui); > >>> errs() << " Use: \t" << *pUse << "\n"; > >>> } > >>> } > >>> > >>> However, everything works as expected when using the range-based use > >>> iterator with the following code. > >>> > >>> for (auto i = inst_begin(f), ie = inst_end(f); i != ie; > ++i) > >>> { > >>> Instruction* pDef = &(*i); > >>> errs() << "Def: " << *pDef << "\n"; > >>> > >>> for (User* pUser : pDef->users()) > >>> { > >>> Instruction* pUse = dyn_cast<Instruction>(pUser); > >>> errs() << " Use: \t" << *pUse << "\n"; > >>> } > >>> } > >>> > >>> Also, the code is executed inside a function pass. So was initially > >>> thinking I somehow screwed up the use information in a previous pass. > >>> However, I would assume the range-based iterator would not work as well > >>> but it does. > >>> > >>> Finally, I'm currently using LLVM 3.5.1 built for Windows. Google > hasn't > >>> been much help. Anybody have any suggestions as to why the first > example > >>> above doesn't work? > >>> > >>> Thanks, > >>> Zack > >>> > >>> > >>> _______________________________________________ > >>> LLVM Developers mailing list > >>> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > >>> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > >>> > >> > >> -- > >> Jon Roelofs > >> jonathan at codesourcery.com > >> CodeSourcery / Mentor Embedded > > > > > > > > _______________________________________________ > > LLVM Developers mailing list > > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150610/c4e684dc/attachment.html>