<div dir="ltr">OK, now I see what confuses you. PointerUnion relies on the fact that memory coming from the allocator is aligned. So you allocate a byte and get an address X, then you allocate another byte and get the address Y. There's no guarantee you'll get consecutive addresses but lets ignore that for simplicity. The relative difference between the two addresses isn't one, it's whatever the size of the word is. That means that lower bytes are always empty and that's where PointerUnion stores the flag that tells it what type is stored inside. The pointers that are stored inside all take the same space, the size of a pointer, it's just that you have to clear the lower bits to get to the actual value.</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Sep 2, 2015 at 10:50 AM, Feng Lu <span dir="ltr"><<a href="mailto:lufeng1204@gmail.com" target="_blank">lufeng1204@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi, Nikola,<div> Thanks for your help. </div><div>  Is it assumed that pointer types are all have the same number of bits in pointer parts?</div><div>  e.g. 30 bits in pointer part and 2 bits in flag part.</div><div>  Otherwise I cannot understand why point types does not matter.</div><div>  For example, if PT1 has 30 bit in pointer part, and PT2 has 31 bit in pointer part. It would be different for them to be NULL.</div><div><br></div><div>Thanks</div><span class="HOEnZb"><font color="#888888"><div>Feng Lu</div></font></span></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Sep 1, 2015 at 2:20 PM, Nikola Smiljanic <span dir="ltr"><<a href="mailto:popizdeh@gmail.com" target="_blank">popizdeh@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">You can't get a different result because it's not really important what type you're getting. The type isn't exposed in the interface of this method. It's just checking that pointer is null. The type doesn't matter which is why the comment also says "one of the pointer types".</div><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Wed, Sep 2, 2015 at 1:57 AM, Feng Lu via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr"><div>Hi, All,</div><div> I am very interested in LLVM, and starting to learn from LLVM source code.</div><div> I encountered one problem that I cannot understand.</div><div><br></div><div> The part of the code is from  llvm/include/llvm/ADT/PointerUnion.h</div><div><br></div><div> class PointerUnion {PointerUnion.h<br>  public:<br>    typedef PointerIntPair<void*, 1, bool,<br>                           PointerUnionUIntTraits<PT1,PT2> > ValTy;<br>  private:<br>    ValTy Val;</div><div><br></div><div>    struct IsPT1 {<br>      static const int Num = 0;<br>    };<br>    struct IsPT2 {<br>      static const int Num = 1;<br>    };<br>    template <typename T><br>    struct UNION_DOESNT_CONTAIN_TYPE { };</div><div><br></div><div>  public:<br>    PointerUnion() {}</div><div><br></div><div>    PointerUnion(PT1 V) : Val(<br>      const_cast<void *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(V))) {<br>    }<br>    PointerUnion(PT2 V) : Val(<br>      const_cast<void *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(V)), 1) {<br>    }</div><div><br></div><div>    /// isNull - Return true if the pointer held in the union is null,<br>    /// regardless of which type it is.<br>    <font color="#ff0000">bool isNull() const {</font><br>      // Convert from the void* to one of the pointer types, to make sure that<br>      // we recursively strip off low bits if we have a nested PointerUnion.<br>      <font color="#ff0000">return !PointerLikeTypeTraits<PT1>::getFromVoidPointer(Val.getPointer());</font><br>    }<br></div><div> </div><div>What confusing me is the <font color="#ff0000"> isNull() </font><font color="#000000">function.</font></div><div><font color="#000000">Would it be possible to get different result if I use </font></div><div><font color="#000000">  </font><font color="#000000" style="background-color:rgb(255,255,255)">return !PointerLikeTypeTraits<<font color="#ff0000">PT2</font>>::getFromVoidPointer(Val.getPointer());</font></div><div><font color="#000000">instead of </font></div><font color="#000000"><div><font color="#000000">  </font><font color="#000000" style="background-color:rgb(255,255,255)">return !PointerLikeTypeTraits<<font color="#ff0000">PT1</font>>::getFromVoidPointer(Val.getPointer());</font></div></font><div><font color="#000000">  </font></div><div><font color="#000000">Thanks for your help.</font></div><span><font color="#888888"><div><font color="#000000">Feng Lu</font></div><div><font color="#000000"></font><br></div></font></span></div>
<br></div></div>_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>