[llvm-dev] Is pointer tagging defined behavior?

Sanjoy Das via llvm-dev llvm-dev at lists.llvm.org
Sat Mar 26 11:57:24 PDT 2016


On Sat, Mar 26, 2016 at 5:32 AM, Bruce Hoult via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> On Sat, Mar 26, 2016 at 2:58 PM, Russell Wallace via llvm-dev
> <llvm-dev at lists.llvm.org> wrote:
>>
>> Dynamic languages commonly use an implementation technique where you take
>> a pointer to an object (aligned on eight bytes so the lower three bits are
>> zero), cast to intptr_t, change the lower three bits to a tag value
>> indicating the type of the object, then later test the tag value, remove the
>> tag, cast back to a pointer and dereference the pointer.
>
>
> That doesn't sound exactly right.
>
> In the implementations I've seen, pointers always have tags with all 0 bits.
> So if the thing is actually a pointer you AND with 0x7 and find the result
> is zero then you just go ahead and use the original value as a pointer.

V8 does (or used to do?) the opposite -- pointers are tagged in their
low bits, and integers are not.  Since most of the time you're
accessing an offset within the pointer anyway, you didn't have to
unmask the low bits out, but could instead change the offset instead
(i.e. load from Ptr+15 instead of Ptr+16, since pointers have their
lowest bit set (say)).

Not tagging integers then makes most integer math cheaper
e.g. addition of two unpacked integers can just be an add instruction,
multiplication needs only one shift instead of two etc..

-- Sanjoy


More information about the llvm-dev mailing list