[PATCH] D110245: [ConstantFolding] Fold ptrtoint(gep i8 null, x) -> x

Alexander Richardson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 23 02:48:20 PDT 2021


arichardson marked an inline comment as not done.
arichardson added a comment.

In D110245#3017539 <https://reviews.llvm.org/D110245#3017539>, @lebedev.ri wrote:

> If there isn't a fold `gep inbounds null, ???  -->  bitcast(null)` it might be interesting to add one though.

I tried adding a `(gep inbounds null, nonzero) -> poison` fold, but that broke lots of tests. `(gep inbounds null, ..) -> null` is a bit safer but will break `&((TYPE *)0)->MEMBER)` __builtin_offsetof fallbacks since they are constant-folded to null before UBSAN can handle it.



================
Comment at: llvm/lib/Analysis/ConstantFolding.cpp:1361
+        // (ptrtoint (gep (gep null, x), y) -> x + y, etc.
+        // FIXME: is this also valid for non-integral pointers? I would assume
+        // so since this is a constant expression so it should evaluate to the
----------------
The only open question I have is whether this is valid for non-integral pointers? It is definitely valid for CHERI capabilities which are non-integral but with a well defined pointer->int mapping (only int->pointer isn't).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110245/new/

https://reviews.llvm.org/D110245



More information about the llvm-commits mailing list