diff options
author | Nikita Popov <npopov@redhat.com> | 2023-11-03 10:46:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-03 10:46:07 +0100 |
commit | e4a4122eb6768b69640173b4c32fd88de4547227 (patch) | |
tree | e0c6343e6a4f10225168345da68dc93877a78e16 /llvm/lib/IR/Constants.cpp | |
parent | e9db60c05e2fb96ff40cbb1f78790abc5de9237e (diff) | |
download | llvm-e4a4122eb6768b69640173b4c32fd88de4547227.zip llvm-e4a4122eb6768b69640173b4c32fd88de4547227.tar.gz llvm-e4a4122eb6768b69640173b4c32fd88de4547227.tar.bz2 |
[IR] Remove zext and sext constant expressions (#71040)
Remove support for zext and sext constant expressions. All places
creating them have been removed beforehand, so this just removes the
APIs and uses of these constant expressions in tests.
There is some additional cleanup that can be done on top of this, e.g.
we can remove the ZExtInst vs ZExtOperator footgun.
This is part of
https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179.
Diffstat (limited to 'llvm/lib/IR/Constants.cpp')
-rw-r--r-- | llvm/lib/IR/Constants.cpp | 91 |
1 files changed, 24 insertions, 67 deletions
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp index 58cbde1..cca4811 100644 --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -1958,6 +1958,8 @@ Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty, bool OnlyIfReduced) { Instruction::CastOps opc = Instruction::CastOps(oc); assert(Instruction::isCast(opc) && "opcode out of range"); + assert(isSupportedCastOp(opc) && + "Cast opcode not supported as constant expression"); assert(C && Ty && "Null arguments to getCast"); assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!"); @@ -1966,10 +1968,6 @@ Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty, llvm_unreachable("Invalid cast opcode"); case Instruction::Trunc: return getTrunc(C, Ty, OnlyIfReduced); - case Instruction::ZExt: - return getZExt(C, Ty, OnlyIfReduced); - case Instruction::SExt: - return getSExt(C, Ty, OnlyIfReduced); case Instruction::FPTrunc: return getFPTrunc(C, Ty, OnlyIfReduced); case Instruction::FPExt: @@ -1993,35 +1991,12 @@ Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty, } } -Constant *ConstantExpr::getZExtOrBitCast(Constant *C, Type *Ty) { - if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) - return getBitCast(C, Ty); - return getZExt(C, Ty); -} - -Constant *ConstantExpr::getSExtOrBitCast(Constant *C, Type *Ty) { - if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) - return getBitCast(C, Ty); - return getSExt(C, Ty); -} - Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) { if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) return getBitCast(C, Ty); return getTrunc(C, Ty); } -Constant *ConstantExpr::getSExtOrTrunc(Constant *C, Type *Ty) { - assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && - "Can only sign extend/truncate integers!"); - Type *CTy = C->getType(); - if (CTy->getScalarSizeInBits() < Ty->getScalarSizeInBits()) - return getSExt(C, Ty); - if (CTy->getScalarSizeInBits() > Ty->getScalarSizeInBits()) - return getTrunc(C, Ty); - return C; -} - Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) { assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && @@ -2048,18 +2023,6 @@ Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S, return getBitCast(S, Ty); } -Constant *ConstantExpr::getIntegerCast(Constant *C, Type *Ty, bool isSigned) { - assert(C->getType()->isIntOrIntVectorTy() && - Ty->isIntOrIntVectorTy() && "Invalid cast"); - unsigned SrcBits = C->getType()->getScalarSizeInBits(); - unsigned DstBits = Ty->getScalarSizeInBits(); - Instruction::CastOps opcode = - (SrcBits == DstBits ? Instruction::BitCast : - (SrcBits > DstBits ? Instruction::Trunc : - (isSigned ? Instruction::SExt : Instruction::ZExt))); - return getCast(opcode, C, Ty); -} - Constant *ConstantExpr::getFPCast(Constant *C, Type *Ty) { assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && "Invalid cast"); @@ -2086,34 +2049,6 @@ Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) { return getFoldedCast(Instruction::Trunc, C, Ty, OnlyIfReduced); } -Constant *ConstantExpr::getSExt(Constant *C, Type *Ty, bool OnlyIfReduced) { -#ifndef NDEBUG - bool fromVec = isa<VectorType>(C->getType()); - bool toVec = isa<VectorType>(Ty); -#endif - assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); - assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral"); - assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer"); - assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& - "SrcTy must be smaller than DestTy for SExt!"); - - return getFoldedCast(Instruction::SExt, C, Ty, OnlyIfReduced); -} - -Constant *ConstantExpr::getZExt(Constant *C, Type *Ty, bool OnlyIfReduced) { -#ifndef NDEBUG - bool fromVec = isa<VectorType>(C->getType()); - bool toVec = isa<VectorType>(Ty); -#endif - assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); - assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral"); - assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer"); - assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& - "SrcTy must be smaller than DestTy for ZExt!"); - - return getFoldedCast(Instruction::ZExt, C, Ty, OnlyIfReduced); -} - Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) { #ifndef NDEBUG bool fromVec = isa<VectorType>(C->getType()); @@ -2353,6 +2288,28 @@ bool ConstantExpr::isDesirableCastOp(unsigned Opcode) { } } +bool ConstantExpr::isSupportedCastOp(unsigned Opcode) { + switch (Opcode) { + case Instruction::ZExt: + case Instruction::SExt: + return false; + case Instruction::Trunc: + case Instruction::FPTrunc: + case Instruction::FPExt: + case Instruction::UIToFP: + case Instruction::SIToFP: + case Instruction::FPToUI: + case Instruction::FPToSI: + case Instruction::PtrToInt: + case Instruction::IntToPtr: + case Instruction::BitCast: + case Instruction::AddrSpaceCast: + return true; + default: + llvm_unreachable("Argument must be cast opcode"); + } +} + Constant *ConstantExpr::getSizeOf(Type* Ty) { // sizeof is implemented as: (i64) gep (Ty*)null, 1 // Note that a non-inbounds gep is used, as null isn't within any object. |