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

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 23 02:18:12 PDT 2021


lebedev.ri added inline comments.


================
Comment at: llvm/lib/Analysis/ConstantFolding.cpp:1355
+        FoldedValue = ConstantExpr::getIntegerCast(
+            CE->getOperand(0), DL.getIntPtrType(CE->getType()), false);
+      } else if (auto *GEP = dyn_cast<GEPOperator>(CE)) {
----------------



================
Comment at: llvm/lib/Analysis/ConstantFolding.cpp:1365
+        APInt BaseOffset(BitWidth, 0);
+        auto *Base = cast<Constant>(
+            GEP->stripAndAccumulateConstantOffsets(DL, BaseOffset, true));
----------------
How do you know the result is a constant?


================
Comment at: llvm/lib/Analysis/ConstantFolding.cpp:1366
+        auto *Base = cast<Constant>(
+            GEP->stripAndAccumulateConstantOffsets(DL, BaseOffset, true));
+        if (Base->isNullValue()) {
----------------



================
Comment at: llvm/lib/Analysis/ConstantFolding.cpp:1367
+            GEP->stripAndAccumulateConstantOffsets(DL, BaseOffset, true));
+        if (Base->isNullValue()) {
+          // Note: we can ignore inbounds here. An inbounds GEP with base of
----------------



================
Comment at: llvm/lib/Analysis/ConstantFolding.cpp:1378
+        // Do a zext or trunc to get to the ptrtoint dest size.
+        return ConstantExpr::getIntegerCast(FoldedValue, DestTy, false);
       }
----------------



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