<div dir="ltr">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.<div><br></div><div><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=_MyRsnM2BfxlNe2_sRPB9ObEiKNYLPnbHsAKGMRdUHA&s=TG6Y4S5CX1SuMhWoc0T5l2iqSoTUA2zLEbgBAlsP_O8&e=">https://github.com/llvm-mirror/llvm/commit/d1fe495464e4abc384565813cbf1cb8b130e5a6d</a><br><div><br></div><div><table class="" style="border-spacing:0px;width:918px;color:rgb(51,51,51);font-family:Helvetica,arial,nimbussansl,liberationsans,freesans,clean,sans-serif,'Segoe UI Emoji','Segoe UI Symbol';font-size:13px;line-height:9.10000038146973px"><tbody><tr><td class="" style="padding:0px 10px;vertical-align:top;background-color:rgb(255,236,236)"><span class="" style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;white-space:pre;overflow:visible;word-wrap:normal">-  <span class="" style="color:rgb(150,152,150)">// Retrieve a pointer to the current User.</span></span></td></tr><tr><td id="diff-d8fb93fe1ed5ba376830fbab932af8d1L204" class="" style="padding:0px 10px;width:50px;min-width:50px;white-space:nowrap;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18px;color:rgba(0,0,0,0.298039);vertical-align:top;text-align:right;border-style:solid;border-color:rgb(241,192,192);border-width:0px 1px 0px 0px;background-color:rgb(255,221,221)"></td><td class="" style="padding:0px 10px;width:50px;min-width:50px;white-space:nowrap;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18px;color:rgba(0,0,0,0.298039);vertical-align:top;text-align:right;border-style:solid;border-color:rgb(241,192,192);border-width:0px 1px 0px 0px;background-color:rgb(255,221,221)"></td><td class="" style="padding:0px 10px;vertical-align:top;background-color:rgb(255,236,236)"><span class="" style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;white-space:pre;overflow:visible;word-wrap:normal">-  UserTy *<span class="" style="color:rgb(167,29,93)">operator</span>*() <span class="" style="color:rgb(167,29,93)">const</span> {</span></td></tr><tr><td id="diff-d8fb93fe1ed5ba376830fbab932af8d1L205" class="" style="padding:0px 10px;width:50px;min-width:50px;white-space:nowrap;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18px;color:rgba(0,0,0,0.298039);vertical-align:top;text-align:right;border-style:solid;border-color:rgb(241,192,192);border-width:0px 1px 0px 0px;background-color:rgb(255,221,221)"></td><td class="" style="padding:0px 10px;width:50px;min-width:50px;white-space:nowrap;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18px;color:rgba(0,0,0,0.298039);vertical-align:top;text-align:right;border-style:solid;border-color:rgb(241,192,192);border-width:0px 1px 0px 0px;background-color:rgb(255,221,221)"></td><td class="" style="padding:0px 10px;vertical-align:top;background-color:rgb(255,236,236)"><span class="" style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;white-space:pre;overflow:visible;word-wrap:normal">-    <span class="" style="color:rgb(0,134,179)">assert</span>(U && <span class="" style="color:rgb(24,54,145)"><span class="">"</span>Cannot dereference end iterator!<span class="">"</span></span>);</span></td></tr><tr><td id="diff-d8fb93fe1ed5ba376830fbab932af8d1L206" class="" style="padding:0px 10px;width:50px;min-width:50px;white-space:nowrap;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18px;color:rgba(0,0,0,0.298039);vertical-align:top;text-align:right;border-style:solid;border-color:rgb(241,192,192);border-width:0px 1px 0px 0px;background-color:rgb(255,221,221)"></td><td class="" style="padding:0px 10px;width:50px;min-width:50px;white-space:nowrap;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18px;color:rgba(0,0,0,0.298039);vertical-align:top;text-align:right;border-style:solid;border-color:rgb(241,192,192);border-width:0px 1px 0px 0px;background-color:rgb(255,221,221)"></td><td class="" style="padding:0px 10px;vertical-align:top;background-color:rgb(255,236,236)"><span class="" style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;white-space:pre;overflow:visible;word-wrap:normal">-    <span class="" style="color:rgb(167,29,93)">return</span> U-><span class="" style="color:rgb(0,134,179)">getUser</span>();</span></td></tr><tr><td id="diff-d8fb93fe1ed5ba376830fbab932af8d1L207" class="" style="padding:0px 10px;width:50px;min-width:50px;white-space:nowrap;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18px;color:rgba(0,0,0,0.298039);vertical-align:top;text-align:right;border-style:solid;border-color:rgb(241,192,192);border-width:0px 1px 0px 0px;background-color:rgb(255,221,221)"></td><td class="" style="padding:0px 10px;width:50px;min-width:50px;white-space:nowrap;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18px;color:rgba(0,0,0,0.298039);vertical-align:top;text-align:right;border-style:solid;border-color:rgb(241,192,192);border-width:0px 1px 0px 0px;background-color:rgb(255,221,221)"></td><td class="" style="padding:0px 10px;vertical-align:top;background-color:rgb(255,236,236)"><span class="" style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;white-space:pre;overflow:visible;word-wrap:normal">-  }

</span></td></tr></tbody></table></div></div><div class="gmail_extra"><br><div class="gmail_quote">Zack</div><div class="gmail_quote"><br></div><div class="gmail_quote">On Wed, Jun 10, 2015 at 7:17 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">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>
<div class="HOEnZb"><div class="h5"><br>
<br>
<br>
On Tue, Jun 9, 2015 at 7:54 PM, Zack Waters <<a href="mailto:zswaters@gmail.com">zswaters@gmail.com</a>> wrote:<br>
> Thanks Dan and Jon. I made an incorrect assumption that the "use" 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 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 <<a href="mailto:jonathan@codesourcery.com">jonathan@codesourcery.com</a>><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 see, when<br>
>>> using the use_iterator, is the same as the "def". Meaning, in the code<br>
>>> below the pDef is always equal to pUse pointer for every instruction in<br>
>>> all basic blocks (except terminators).<br>
>>><br>
>>>              for (auto i = inst_begin(f), ie = inst_end(f); i != 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 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=_MyRsnM2BfxlNe2_sRPB9ObEiKNYLPnbHsAKGMRdUHA&s=GgnY58sNTZOXspFygmTHiXiPX_rhSE8Fuj6reYoadFU&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 is<br>
>> implicitly converted via `llvm::Use::operator Value *() const` to a<br>
>> `Value*`, and that `Value*` is `pDef`.<br>
>><br>
>><br>
>> HTH,<br>
>><br>
>> Jon<br>
>><br>
>>>                      Instruction* pUse = dyn_cast<Instruction>(*ui);<br>
>>>                      errs() << "  Use: \t" << *pUse << "\n";<br>
>>>                  }<br>
>>>              }<br>
>>><br>
>>> However, everything works as expected when using the range-based use<br>
>>> iterator with the following code.<br>
>>><br>
>>>              for (auto i = inst_begin(f), ie = inst_end(f); i != ie; ++i)<br>
>>>              {<br>
>>>                  Instruction* pDef = &(*i);<br>
>>>                  errs() << "Def: " << *pDef << "\n";<br>
>>><br>
>>>                  for (User* pUser : pDef->users())<br>
>>>                  {<br>
>>>                      Instruction* pUse = 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 initially<br>
>>> thinking I somehow screwed up the use information in a previous pass.<br>
>>> However, I would assume the range-based iterator would not work as well<br>
>>> but it does.<br>
>>><br>
>>> Finally, I'm currently using LLVM 3.5.1 built for Windows. Google hasn't<br>
>>> been much help. Anybody have any suggestions as to why the 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>         <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><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>         <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>
</div></div></blockquote></div><br></div></div>