Need Help to Understand LLVM Source Code

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
typedef PointerIntPair<void*, 1, bool,
PointerUnionUIntTraits<PT1,PT2> > ValTy;
ValTy Val;

struct IsPT1 {
static const int Num = 0;
struct IsPT2 {
static const int Num = 1;

PointerUnion() {}

PointerUnion(PT1 V) : Val(
const_cast<void *>(PointerLikeTypeTraits::getAsVoidPointer(V))) {
PointerUnion(PT2 V) : Val(
const_cast<void *>(PointerLikeTypeTraits::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::getFromVoidPointer(Val.getPointer());

What confusing me is the isNull() function.
Would it be possible to get different result if I use
return !PointerLikeTypeTraits::getFromVoidPointer(Val.getPointer());
instead of

return !PointerLikeTypeTraits::getFromVoidPointer(Val.getPointer());

Thanks for your help.
Feng Lu

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

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.

Feng Lu

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.