@@ -1497,6 +1497,19 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
14971497 llvm_unreachable (" Missing case" );
14981498 case Instruction::PtrToAddr:
14991499 case Instruction::PtrToInt:
1500+ // If the input is a nullptr, we can fold it to the corresponding nullptr
1501+ // value.
1502+ if (Opcode == Instruction::PtrToInt && C->isNullValue ()) {
1503+ if (std::optional<APInt> NullPtrValue = DL.getNullPtrValue (
1504+ C->getType ()->getScalarType ()->getPointerAddressSpace ())) {
1505+ if (NullPtrValue->isZero ())
1506+ return Constant::getZeroValue (DestTy);
1507+ else if (NullPtrValue->isAllOnes ())
1508+ return Constant::getAllOnesValue (DestTy);
1509+ else
1510+ llvm_unreachable (" invalid nullptr value" );
1511+ }
1512+ }
15001513 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
15011514 Constant *FoldedValue = nullptr ;
15021515 // If the input is an inttoptr, eliminate the pair. This requires knowing
@@ -1543,6 +1556,13 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
15431556 }
15441557 break ;
15451558 case Instruction::IntToPtr:
1559+ // We can fold it to a null pointer if the input is the nullptr value.
1560+ if (std::optional<APInt> NullPtrValue = DL.getNullPtrValue (
1561+ DestTy->getScalarType ()->getPointerAddressSpace ())) {
1562+ if ((NullPtrValue->isZero () && C->isZeroValue ()) ||
1563+ (NullPtrValue->isAllOnes () && C->isAllOnesValue ()))
1564+ return Constant::getNullValue (DestTy);
1565+ }
15461566 // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
15471567 // the int size is >= the ptr size and the address spaces are the same.
15481568 // This requires knowing the width of a pointer, so it can't be done in
@@ -1561,6 +1581,24 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
15611581 }
15621582 }
15631583 break ;
1584+ case Instruction::AddrSpaceCast:
1585+ // A null pointer (`ptr addrspace(N) null` in IR presentation,
1586+ // `ConstantPointerNull` in LLVM class, not `nullptr` in C/C++) used to
1587+ // represent a zero-value pointer in the corresponding address space.
1588+ // Therefore, we can't simply fold an address space cast of a null pointer
1589+ // from one address space to another, because on some targets, the nullptr
1590+ // of an address space could be non-zero.
1591+ //
1592+ // Recently, the semantic of `ptr addrspace(N) null` is changed to represent
1593+ // the actual nullptr in the corresponding address space. It can be zero or
1594+ // non-zero, depending on the target. Therefore, we can fold an address
1595+ // space cast of a nullptr from one address space to another.
1596+
1597+ // If the input is a nullptr, we can fold it to the corresponding
1598+ // nullptr in the destination address space.
1599+ if (C->isNullValue ())
1600+ return Constant::getNullValue (DestTy);
1601+ [[fallthrough]];
15641602 case Instruction::Trunc:
15651603 case Instruction::ZExt:
15661604 case Instruction::SExt:
@@ -1570,7 +1608,6 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
15701608 case Instruction::SIToFP:
15711609 case Instruction::FPToUI:
15721610 case Instruction::FPToSI:
1573- case Instruction::AddrSpaceCast:
15741611 break ;
15751612 case Instruction::BitCast:
15761613 return FoldBitCast (C, DestTy, DL);
0 commit comments