[RFC] opaque pointer ideas

Hi,

I am pleased to see all the wonderful progress with opaque pointers.
With LLVM-15 I have been playing with them.
Some things I have learnt:

  1. opaque pointers essentially turn/reduce GEP into a simple pointer addition instruction.
    But, it is a restricted ADD instruction, in the sense that one can only ADD a pointer to an INT, and not add two pointers together.

Some ideas going forward:
Now that GEP is essentially a pointer addition instruction, why don’t we introduce other pointer arithmetic instructions?

  1. Pointer AND instruction.
    The AND instruction would be useful when doing address alignment and testing address alignment without having to resort of ptrtoint and inttoptr instructions.
  2. Pointer SUB instruction.
    One can sub one pointer from another and create an INT (checking that both pointers are in the same address space), but one cannot ADD two pointers together. So, it can be constrained more than the integer SUB instruction.

It has a few benefits:

  1. fewer cases where one would need to use ptrtoint and inttoptr.
  2. Simplify alias analysis.

What do people think?

Kind Regards

James

We already have this in a sense, it’s called @llvm.ptrmask, just heavily underused and likely not understood by a lot of optimisation passes as a result.

We would love to see this upstream. We have an intrinsic downstream in CHERI LLVM for this, but only used for CHERI capabilities.

We already have this in a sense, it’s called @llvm.ptrmask , just heavily underused and likely not understood by a lot of optimisation passes as a result.

Yeah, the question here is how to best expose it. Maybe a builtin would be a good start.

Well we have __builtin_align_up/down and __bultin_is_aligned (contributed by us), they just don’t use that intrinsic currently because it doesn’t perform well.