<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>