[llvm] [InferAddressSpaces] apply InferAddressSpaces to inttoptr-zext-ptrtoint address expression. (PR #79108)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 23 17:41:02 PST 2024


================
@@ -300,6 +300,34 @@ static bool isNoopPtrIntCastPair(const Operator *I2P, const DataLayout &DL,
          (P2IOp0AS == I2PAS || TTI->isNoopAddrSpaceCast(P2IOp0AS, I2PAS));
 }
 
+// Check whether that's pointer bitcast using `ptrtoint`-`zext`-`inttoptr`
+static bool isZExtPtrIntCastPair(const Operator *I2P, const DataLayout &DL) {
+  assert(I2P->getOpcode() == Instruction::IntToPtr);
+  auto *ZExt = dyn_cast<Operator>(I2P->getOperand(0));
+  if (!ZExt || ZExt->getOpcode() != Instruction::ZExt)
+    return false;
+  auto *P2I = dyn_cast<Operator>(ZExt->getOperand(0));
+  if (!P2I || P2I->getOpcode() != Instruction::PtrToInt)
+    return false;
+  unsigned P2IOp0AS = P2I->getOperand(0)->getType()->getPointerAddressSpace();
+  unsigned I2PAS = I2P->getType()->getPointerAddressSpace();
+  unsigned P2IOp0SizeInBits =
+      DL.getIntPtrType(P2I->getOperand(0)->getType())->getScalarSizeInBits();
+  unsigned I2PSizeInBits =
+      DL.getIntPtrType(I2P->getType())->getScalarSizeInBits();
+  // Check:
+  // 1. `inttoptr` and `ptrtoint` are no-op casts
+  // 2. src address pointer and dst address pointer should be different address
+  // space and different size
+  return CastInst::isNoopCast(Instruction::CastOps(I2P->getOpcode()),
+                              I2P->getOperand(0)->getType(), I2P->getType(),
+                              DL) &&
+         CastInst::isNoopCast(Instruction::CastOps(P2I->getOpcode()),
+                              P2I->getOperand(0)->getType(), P2I->getType(),
+                              DL) &&
+         (P2IOp0AS != I2PAS) && (P2IOp0SizeInBits < I2PSizeInBits);
----------------
arsenm wrote:

Check these conditions first?

https://github.com/llvm/llvm-project/pull/79108


More information about the llvm-commits mailing list