[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:26:13 PDT 2021


arichardson added inline comments.


================
Comment at: llvm/lib/Analysis/ConstantFolding.cpp:1365
+        APInt BaseOffset(BitWidth, 0);
+        auto *Base = cast<Constant>(
+            GEP->stripAndAccumulateConstantOffsets(DL, BaseOffset, true));
----------------
arichardson wrote:
> lebedev.ri wrote:
> > How do you know the result is a constant?
> Don't all inputs need to be constants if the GEP is a `ConstantExpr`? I couldn't think of a counter-example:
> 
> Even for globals this cast works fine:
> ```
> @a = external addrspace(1) global i16
> 
> define i64 @nofold_ptrtoint_gep_global(i32 %x) {
>   %ptr = getelementptr i16, i16 addrspace(1)* @a, i32 1
>   %ret = ptrtoint i16 addrspace(1)* %ptr to i64
>   ret i64 %ret
> }
> ```
If this is not true, I'm obviously happy to change it to dyn_cast and add the check.


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