aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/llvm/lib/IR/Constants.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/llvm-project/llvm/lib/IR/Constants.cpp')
-rw-r--r--contrib/llvm-project/llvm/lib/IR/Constants.cpp269
1 files changed, 82 insertions, 187 deletions
diff --git a/contrib/llvm-project/llvm/lib/IR/Constants.cpp b/contrib/llvm-project/llvm/lib/IR/Constants.cpp
index c69c7c095f78..a38b912164b1 100644
--- a/contrib/llvm-project/llvm/lib/IR/Constants.cpp
+++ b/contrib/llvm-project/llvm/lib/IR/Constants.cpp
@@ -1770,7 +1770,7 @@ BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
}
BlockAddress::BlockAddress(Function *F, BasicBlock *BB)
- : Constant(Type::getInt8PtrTy(F->getContext(), F->getAddressSpace()),
+ : Constant(PointerType::get(F->getContext(), F->getAddressSpace()),
Value::BlockAddressVal, &Op<0>(), 2) {
setOperand(0, F);
setOperand(1, BB);
@@ -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,22 +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:
- return getFPExtend(C, Ty, OnlyIfReduced);
- case Instruction::UIToFP:
- return getUIToFP(C, Ty, OnlyIfReduced);
- case Instruction::SIToFP:
- return getSIToFP(C, Ty, OnlyIfReduced);
- case Instruction::FPToUI:
- return getFPToUI(C, Ty, OnlyIfReduced);
- case Instruction::FPToSI:
- return getFPToSI(C, Ty, OnlyIfReduced);
case Instruction::PtrToInt:
return getPtrToInt(C, Ty, OnlyIfReduced);
case Instruction::IntToPtr:
@@ -1993,35 +1979,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,30 +2011,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");
- unsigned SrcBits = C->getType()->getScalarSizeInBits();
- unsigned DstBits = Ty->getScalarSizeInBits();
- if (SrcBits == DstBits)
- return C; // Avoid a useless cast
- Instruction::CastOps opcode =
- (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
- return getCast(opcode, C, Ty);
-}
-
Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
#ifndef NDEBUG
bool fromVec = isa<VectorType>(C->getType());
@@ -2086,102 +2025,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());
- bool toVec = isa<VectorType>(Ty);
-#endif
- assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
- assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
- C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
- "This is an illegal floating point truncation!");
- return getFoldedCast(Instruction::FPTrunc, C, Ty, OnlyIfReduced);
-}
-
-Constant *ConstantExpr::getFPExtend(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()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
- C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
- "This is an illegal floating point extension!");
- return getFoldedCast(Instruction::FPExt, C, Ty, OnlyIfReduced);
-}
-
-Constant *ConstantExpr::getUIToFP(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() && Ty->isFPOrFPVectorTy() &&
- "This is an illegal uint to floating point cast!");
- return getFoldedCast(Instruction::UIToFP, C, Ty, OnlyIfReduced);
-}
-
-Constant *ConstantExpr::getSIToFP(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() && Ty->isFPOrFPVectorTy() &&
- "This is an illegal sint to floating point cast!");
- return getFoldedCast(Instruction::SIToFP, C, Ty, OnlyIfReduced);
-}
-
-Constant *ConstantExpr::getFPToUI(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()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
- "This is an illegal floating point to uint cast!");
- return getFoldedCast(Instruction::FPToUI, C, Ty, OnlyIfReduced);
-}
-
-Constant *ConstantExpr::getFPToSI(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()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
- "This is an illegal floating point to sint cast!");
- return getFoldedCast(Instruction::FPToSI, C, Ty, OnlyIfReduced);
-}
-
Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy,
bool OnlyIfReduced) {
assert(C->getType()->isPtrOrPtrVectorTy() &&
@@ -2288,15 +2131,15 @@ bool ConstantExpr::isDesirableBinOp(unsigned Opcode) {
case Instruction::FMul:
case Instruction::FDiv:
case Instruction::FRem:
+ case Instruction::And:
+ case Instruction::Or:
+ case Instruction::LShr:
+ case Instruction::AShr:
return false;
case Instruction::Add:
case Instruction::Sub:
case Instruction::Mul:
case Instruction::Shl:
- case Instruction::LShr:
- case Instruction::AShr:
- case Instruction::And:
- case Instruction::Or:
case Instruction::Xor:
return true;
default:
@@ -2315,15 +2158,15 @@ bool ConstantExpr::isSupportedBinOp(unsigned Opcode) {
case Instruction::FMul:
case Instruction::FDiv:
case Instruction::FRem:
+ case Instruction::And:
+ case Instruction::Or:
+ case Instruction::LShr:
+ case Instruction::AShr:
return false;
case Instruction::Add:
case Instruction::Sub:
case Instruction::Mul:
case Instruction::Shl:
- case Instruction::LShr:
- case Instruction::AShr:
- case Instruction::And:
- case Instruction::Or:
case Instruction::Xor:
return true;
default:
@@ -2331,6 +2174,50 @@ bool ConstantExpr::isSupportedBinOp(unsigned Opcode) {
}
}
+bool ConstantExpr::isDesirableCastOp(unsigned Opcode) {
+ switch (Opcode) {
+ case Instruction::ZExt:
+ case Instruction::SExt:
+ case Instruction::FPTrunc:
+ case Instruction::FPExt:
+ case Instruction::UIToFP:
+ case Instruction::SIToFP:
+ case Instruction::FPToUI:
+ case Instruction::FPToSI:
+ return false;
+ case Instruction::Trunc:
+ case Instruction::PtrToInt:
+ case Instruction::IntToPtr:
+ case Instruction::BitCast:
+ case Instruction::AddrSpaceCast:
+ return true;
+ default:
+ llvm_unreachable("Argument must be cast opcode");
+ }
+}
+
+bool ConstantExpr::isSupportedCastOp(unsigned Opcode) {
+ switch (Opcode) {
+ case Instruction::ZExt:
+ case Instruction::SExt:
+ case Instruction::FPTrunc:
+ case Instruction::FPExt:
+ case Instruction::UIToFP:
+ case Instruction::SIToFP:
+ case Instruction::FPToUI:
+ case Instruction::FPToSI:
+ return false;
+ case Instruction::Trunc:
+ 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.
@@ -2345,7 +2232,7 @@ Constant *ConstantExpr::getAlignOf(Type* Ty) {
// alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
// Note that a non-inbounds gep is used, as null isn't within any object.
Type *AligningTy = StructType::get(Type::getInt1Ty(Ty->getContext()), Ty);
- Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo(0));
+ Constant *NullPtr = Constant::getNullValue(PointerType::getUnqual(AligningTy->getContext()));
Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Constant *Indices[2] = { Zero, One };
@@ -2584,14 +2471,6 @@ Constant *ConstantExpr::getMul(Constant *C1, Constant *C2,
return get(Instruction::Mul, C1, C2, Flags);
}
-Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
- return get(Instruction::And, C1, C2);
-}
-
-Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
- return get(Instruction::Or, C1, C2);
-}
-
Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
return get(Instruction::Xor, C1, C2);
}
@@ -2603,16 +2482,6 @@ Constant *ConstantExpr::getShl(Constant *C1, Constant *C2,
return get(Instruction::Shl, C1, C2, Flags);
}
-Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) {
- return get(Instruction::LShr, C1, C2,
- isExact ? PossiblyExactOperator::IsExact : 0);
-}
-
-Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) {
- return get(Instruction::AShr, C1, C2,
- isExact ? PossiblyExactOperator::IsExact : 0);
-}
-
Constant *ConstantExpr::getExactLogBase2(Constant *C) {
Type *Ty = C->getType();
const APInt *IVal;
@@ -2687,6 +2556,32 @@ Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty,
}
}
+Constant *ConstantExpr::getIntrinsicIdentity(Intrinsic::ID ID, Type *Ty) {
+ switch (ID) {
+ case Intrinsic::umax:
+ return Constant::getNullValue(Ty);
+ case Intrinsic::umin:
+ return Constant::getAllOnesValue(Ty);
+ case Intrinsic::smax:
+ return Constant::getIntegerValue(
+ Ty, APInt::getSignedMinValue(Ty->getIntegerBitWidth()));
+ case Intrinsic::smin:
+ return Constant::getIntegerValue(
+ Ty, APInt::getSignedMaxValue(Ty->getIntegerBitWidth()));
+ default:
+ return nullptr;
+ }
+}
+
+Constant *ConstantExpr::getIdentity(Instruction *I, Type *Ty,
+ bool AllowRHSConstant, bool NSZ) {
+ if (I->isBinaryOp())
+ return getBinOpIdentity(I->getOpcode(), Ty, AllowRHSConstant, NSZ);
+ if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
+ return getIntrinsicIdentity(II->getIntrinsicID(), Ty);
+ return nullptr;
+}
+
Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) {
switch (Opcode) {
default: