<div dir="ltr">This kind of change can really make it difficult to migrate a large LLVM based compiler from an older version to the latest when you're not aware of it and its a silent error.</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 10, 2015 at 10:14 AM, Daniel Berlin <span dir="ltr"><<a href="mailto:dberlin@dberlin.org" target="_blank">dberlin@dberlin.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I stand corrected :)<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
On Wed, Jun 10, 2015 at 10:09 AM, Jonathan Roelofs<br>
<<a href="mailto:jonathan@codesourcery.com">jonathan@codesourcery.com</a>> wrote:<br>
><br>
><br>
> On 6/10/15 11:01 AM, Zack Waters wrote:<br>
>><br>
>> It appears dereferencing the use iterator returned the "user" in older<br>
>> LLVM versions. See the following commit to Use.h. You can see that the<br>
>> intention is to return the "user". So this is not the behavior anymore.<br>
>><br>
>><br>
>> <a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_llvm-2Dmirror_llvm_commit_d1fe495464e4abc384565813cbf1cb8b130e5a6d&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Mfk2qtn1LTDThVkh6-oGglNfMADXfJdty4_bhmuhMHA&m=bYUVQeK4cDFw6-YGt6bH8L-r_xR3MZX4ySwp7I_0Uek&s=cpOzZRrMiNu5DedfjKYBoyH2tc4Icqe7CULyYpRueUk&e=" target="_blank">https://github.com/llvm-mirror/llvm/commit/d1fe495464e4abc384565813cbf1cb8b130e5a6d</a><br>
>><br>
>> - // Retrieve a pointer to the current User.<br>
>>                 - UserTy *operator*() const {<br>
>>                 - assert(U && "Cannot dereference end iterator!");<br>
>>                 - return U->getUser();<br>
>>                 - }<br>
><br>
><br>
> Looks like the change actually happened in:<br>
> <a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_llvm-2Dmirror_llvm_commit_36b699f2b139a30a2dfa4448223d6985b55daa8a&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Mfk2qtn1LTDThVkh6-oGglNfMADXfJdty4_bhmuhMHA&m=bYUVQeK4cDFw6-YGt6bH8L-r_xR3MZX4ySwp7I_0Uek&s=ugMauSgVBs0_H9hmrIg4XBdyVwWXTGMRn1GTiMqbzAs&e=" target="_blank">https://github.com/llvm-mirror/llvm/commit/36b699f2b139a30a2dfa4448223d6985b55daa8a</a><br>
> (r203364), and from the commit message, it was deliberate. Whether or not<br>
> that part of it was a "good" change is a different question.<br>
><br>
><br>
> Jon<br>
><br>
>><br>
>><br>
>> Zack<br>
>><br>
>> On Wed, Jun 10, 2015 at 7:17 AM, Daniel Berlin <<a href="mailto:dberlin@dberlin.org">dberlin@dberlin.org</a><br>
>> <mailto:<a href="mailto:dberlin@dberlin.org">dberlin@dberlin.org</a>>> wrote:<br>
>><br>
>>     It has, AFAIK, always been this way.<br>
>>     It is also a common source of bugs, as I believe you have discovered.<br>
>><br>
>>     If we had  llvm-specific "clang warnings", this would be one i added<br>
>>     (IE "Dereferencing use iterator of x gives x") :)<br>
>><br>
>>     In the past 6 months, i did this twice by accident in passes and then<br>
>>     spent hours tracking it down.<br>
>><br>
>>     It actually makes me wonder whether use_iterator should even have an<br>
>>     operator * for the use_iterator_impl<Use> case. ISTM to basically<br>
>>     always be a bug (though this is an idle thought, i haven't thought<br>
>>     through the implications at all).<br>
>><br>
>><br>
>><br>
>>     On Tue, Jun 9, 2015 at 7:54 PM, Zack Waters <<a href="mailto:zswaters@gmail.com">zswaters@gmail.com</a><br>
>>     <mailto:<a href="mailto:zswaters@gmail.com">zswaters@gmail.com</a>>> wrote:<br>
>>      > Thanks Dan and Jon. I made an incorrect assumption that the "use"<br>
>>     iterator<br>
>>      > was actually giving me the "user" when de-referencing it.<br>
>>      ><br>
>>      > Did it always have this behavior in previous LLVM versions? I've<br>
>>     seen lots<br>
>>      > of examples of the "use" iterator being dereferenced and resulting<br>
>>      > Instruction pointer being treated as the "user"?<br>
>>      ><br>
>>      > Thanks,<br>
>>      > Zack<br>
>>      ><br>
>>      ><br>
>>      > On Tue, Jun 9, 2015 at 7:25 PM, Jonathan Roelofs<br>
>>     <<a href="mailto:jonathan@codesourcery.com">jonathan@codesourcery.com</a> <mailto:<a href="mailto:jonathan@codesourcery.com">jonathan@codesourcery.com</a>>><br>
>><br>
>>      > wrote:<br>
>>      >><br>
>>      >><br>
>>      >><br>
>>      >> On 6/9/15 8:02 PM, Zack Waters wrote:<br>
>>      >>><br>
>>      >>> Hi,<br>
>>      >>><br>
>>      >>> I'm having a problem with the use iterator. Each "use" that I<br>
>>     see, when<br>
>>      >>> using the use_iterator, is the same as the "def". Meaning, in<br>
>>     the code<br>
>>      >>> below the pDef is always equal to pUse pointer for every<br>
>>     instruction in<br>
>>      >>> all basic blocks (except terminators).<br>
>>      >>><br>
>>      >>>              for (auto i = inst_begin(f), ie = inst_end(f); i<br>
>>     != ie; ++i)<br>
>>      >>>                  Instruction* pDef = &(*i);<br>
>>      >>>                  errs() << "Def: " << *pDef << "\n";<br>
>>      >>><br>
>>      >>>                  for (auto ui = pDef->use_begin(), uie =<br>
>>      >>> pDef->use_end(); ui != uie; ++ui)<br>
>>      >>>                  {<br>
>>      >><br>
>>      >><br>
>>      >> 'user' != 'use'.<br>
>>      >><br>
>>      >> Think of llvm::Use as the edge between the place where a value is<br>
>>      >> produced, and the place where that value is consumed. The<br>
>>     consumer is the<br>
>>      >> 'User', and the Use points at it.<br>
>>      >><br>
>>      >> <a href="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=bYUVQeK4cDFw6-YGt6bH8L-r_xR3MZX4ySwp7I_0Uek&s=ft5w3oOHkLf9tLzw_YiOoPoGdwb1pun7U4p7Clc68tQ&e=" target="_blank">http://llvm.org/docs/doxygen/html/classllvm_1_1Use.html</a><br>
>>      >><br>
>>      >> The confusing thing that's happening below is that the llvm::Use<br>
>> is<br>
>>      >> implicitly converted via `llvm::Use::operator Value *() const` to<br>
>> a<br>
>>      >> `Value*`, and that `Value*` is `pDef`.<br>
>>      >><br>
>>      >><br>
>>      >> HTH,<br>
>>      >><br>
>>      >> Jon<br>
>>      >><br>
>>      >>>                      Instruction* pUse =<br>
>>     dyn_cast<Instruction>(*ui);<br>
>>      >>>                      errs() << "  Use: \t" << *pUse << "\n";<br>
>>      >>>                  }<br>
>>      >>>              }<br>
>>      >>><br>
>>      >>> However, everything works as expected when using the<br>
>>     range-based use<br>
>>      >>> iterator with the following code.<br>
>>      >>><br>
>>      >>>              for (auto i = inst_begin(f), ie = inst_end(f); i<br>
>>     != ie; ++i)<br>
>>      >>>              {<br>
>>      >>>                  Instruction* pDef = &(*i);<br>
>>      >>>                  errs() << "Def: " << *pDef << "\n";<br>
>>      >>><br>
>>      >>>                  for (User* pUser : pDef->users())<br>
>>      >>>                  {<br>
>>      >>>                      Instruction* pUse =<br>
>>     dyn_cast<Instruction>(pUser);<br>
>>      >>>                      errs() << "  Use: \t" << *pUse << "\n";<br>
>>      >>>                  }<br>
>>      >>>              }<br>
>>      >>><br>
>>      >>> Also, the code is executed inside a function pass. So was<br>
>> initially<br>
>>      >>> thinking I somehow screwed up the use information in a previous<br>
>>     pass.<br>
>>      >>> However, I would assume the range-based iterator would not work<br>
>>     as well<br>
>>      >>> but it does.<br>
>>      >>><br>
>>      >>> Finally, I'm currently using LLVM 3.5.1 built for Windows.<br>
>>     Google hasn't<br>
>>      >>> been much help. Anybody have any suggestions as to why the<br>
>>     first example<br>
>>      >>> above doesn't work?<br>
>>      >>><br>
>>      >>> Thanks,<br>
>>      >>> Zack<br>
>>      >>><br>
>>      >>><br>
>>      >>> _______________________________________________<br>
>>      >>> LLVM Developers mailing list<br>
>>      >>> <a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a> <mailto:<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>><br>
>>     <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
>>      >>> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
>>      >>><br>
>>      >><br>
>>      >> --<br>
>>      >> Jon Roelofs<br>
>>      >> <a href="mailto:jonathan@codesourcery.com">jonathan@codesourcery.com</a> <mailto:<a href="mailto:jonathan@codesourcery.com">jonathan@codesourcery.com</a>><br>
>>      >> CodeSourcery / Mentor Embedded<br>
>>      ><br>
>>      ><br>
>>      ><br>
>>      > _______________________________________________<br>
>>      > LLVM Developers mailing list<br>
>>      > <a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a> <mailto:<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>><br>
>>     <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
>>      > <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
>>      ><br>
>><br>
>><br>
><br>
> --<br>
> Jon Roelofs<br>
> <a href="mailto:jonathan@codesourcery.com">jonathan@codesourcery.com</a><br>
> CodeSourcery / Mentor Embedded<br>
</div></div></blockquote></div><br></div>