<div dir="ltr"><div dir="ltr"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><br></div></div></div></div></div></div></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>
<p>The semantics of addrspacecast
(<a href="https://llvm.org/docs/LangRef.html#addrspacecast-to-instruction" target="_blank">https://llvm.org/docs/LangRef.html#addrspacecast-to-instruction</a>)
are:</p>
<blockquote>
<p>The ‘<code class="gmail-notranslate"><span>addrspacecast</span></code>’ instruction
converts the pointer value
<code class="gmail-notranslate"><span>ptrval</span></code>
to type <code class="gmail-notranslate"><span>pty2</span></code>. <u>It can be a </u><u><em>no-op
cast</em></u><u> or a complex
value modification, depending on the target and the address
space
pair.</u> Pointer conversions within the same address space
must be
performed with the <code class="gmail-notranslate"><span>bitcast</span></code> instruction. Note that if
the address space
conversion is legal then both result and operand refer to the
same memory
location.</p>
</blockquote>
<p>So whether it is valid for instcombine to optimise your example
depends on the target. It would be wrong for instcombine to
unconditionally optimise this to i64 3098316506530080114. It might
be possible to extend it to optimise this when the optimisation is
valid by getting extra information from the target, there is a
isNoopAddrSpaceCast function already that can be used to determine
whether it is valid, but the function you are looking at is
target-independent, it cannot get target info. It will require
some thought on the design.</p></div></blockquote><div>Thank you, Your explanation helps me to understand this. So it is wrong to look at the <b>visitPtrToInt </b>method? </div><div><a href="https://github.com/llvm/llvm-project/blob/main/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp#L2046">https://github.com/llvm/llvm-project/blob/main/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp#L2046</a><br></div><div>After going through that method I found that it calls <a href="https://github.com/llvm/llvm-project/blob/main/llvm/lib/IR/Instructions.cpp#L2797">https://github.com/llvm/llvm-project/blob/main/llvm/lib/IR/Instructions.cpp#L2797</a>, which disallow ptrtoint -> addrspacecast.</div></div></div>