[cfe-dev] Need Help to Understand LLVM Source Code

Feng Lu via cfe-dev cfe-dev at lists.llvm.org
Tue Sep 1 17:50:14 PDT 2015


Hi, Nikola,
 Thanks for your help.
  Is it assumed that pointer types are all have the same number of bits in
pointer parts?
  e.g. 30 bits in pointer part and 2 bits in flag part.
  Otherwise I cannot understand why point types does not matter.
  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.

Thanks
Feng Lu

On Tue, Sep 1, 2015 at 2:20 PM, Nikola Smiljanic <popizdeh at gmail.com> wrote:

> 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".
>
> On Wed, Sep 2, 2015 at 1:57 AM, Feng Lu via cfe-dev <
> cfe-dev at lists.llvm.org> wrote:
>
>> Hi, All,
>>  I am very interested in LLVM, and starting to learn from LLVM source
>> code.
>>  I encountered one problem that I cannot understand.
>>
>>  The part of the code is from  llvm/include/llvm/ADT/PointerUnion.h
>>
>>  class PointerUnion {PointerUnion.h
>>   public:
>>     typedef PointerIntPair<void*, 1, bool,
>>                            PointerUnionUIntTraits<PT1,PT2> > ValTy;
>>   private:
>>     ValTy Val;
>>
>>     struct IsPT1 {
>>       static const int Num = 0;
>>     };
>>     struct IsPT2 {
>>       static const int Num = 1;
>>     };
>>     template <typename T>
>>     struct UNION_DOESNT_CONTAIN_TYPE { };
>>
>>   public:
>>     PointerUnion() {}
>>
>>     PointerUnion(PT1 V) : Val(
>>       const_cast<void
>> *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(V))) {
>>     }
>>     PointerUnion(PT2 V) : Val(
>>       const_cast<void
>> *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(V)), 1) {
>>     }
>>
>>     /// isNull - Return true if the pointer held in the union is null,
>>     /// regardless of which type it is.
>>     bool isNull() const {
>>       // Convert from the void* to one of the pointer types, to make sure
>> that
>>       // we recursively strip off low bits if we have a nested
>> PointerUnion.
>>       return
>> !PointerLikeTypeTraits<PT1>::getFromVoidPointer(Val.getPointer());
>>     }
>>
>> What confusing me is the  isNull() function.
>> Would it be possible to get different result if I use
>>   return !PointerLikeTypeTraits<PT2
>> >::getFromVoidPointer(Val.getPointer());
>> instead of
>>   return !PointerLikeTypeTraits<PT1
>> >::getFromVoidPointer(Val.getPointer());
>>
>> Thanks for your help.
>> Feng Lu
>>
>>
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150901/49eca07b/attachment.html>


More information about the cfe-dev mailing list