summaryrefslogtreecommitdiff
path: root/llvm/lib/IR/ConstantFold.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/IR/ConstantFold.cpp')
-rw-r--r--llvm/lib/IR/ConstantFold.cpp616
1 files changed, 77 insertions, 539 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 4c3325063c09..d499d74f7ba0 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -37,45 +37,6 @@ using namespace llvm::PatternMatch;
// ConstantFold*Instruction Implementations
//===----------------------------------------------------------------------===//
-/// Convert the specified vector Constant node to the specified vector type.
-/// At this point, we know that the elements of the input vector constant are
-/// all simple integer or FP values.
-static Constant *BitCastConstantVector(Constant *CV, VectorType *DstTy) {
-
- if (CV->isAllOnesValue()) return Constant::getAllOnesValue(DstTy);
- if (CV->isNullValue()) return Constant::getNullValue(DstTy);
-
- // Do not iterate on scalable vector. The num of elements is unknown at
- // compile-time.
- if (isa<ScalableVectorType>(DstTy))
- return nullptr;
-
- // If this cast changes element count then we can't handle it here:
- // doing so requires endianness information. This should be handled by
- // Analysis/ConstantFolding.cpp
- unsigned NumElts = cast<FixedVectorType>(DstTy)->getNumElements();
- if (NumElts != cast<FixedVectorType>(CV->getType())->getNumElements())
- return nullptr;
-
- Type *DstEltTy = DstTy->getElementType();
- // Fast path for splatted constants.
- if (Constant *Splat = CV->getSplatValue()) {
- return ConstantVector::getSplat(DstTy->getElementCount(),
- ConstantExpr::getBitCast(Splat, DstEltTy));
- }
-
- SmallVector<Constant*, 16> Result;
- Type *Ty = IntegerType::get(CV->getContext(), 32);
- for (unsigned i = 0; i != NumElts; ++i) {
- Constant *C =
- ConstantExpr::getExtractElement(CV, ConstantInt::get(Ty, i));
- C = ConstantExpr::getBitCast(C, DstEltTy);
- Result.push_back(C);
- }
-
- return ConstantVector::get(Result);
-}
-
/// This function determines which opcode to use to fold two constant cast
/// expressions together. It uses CastInst::isEliminableCastPair to determine
/// the opcode. Consequently its just a wrapper around that function.
@@ -114,38 +75,19 @@ static Constant *FoldBitCast(Constant *V, Type *DestTy) {
// Handle casts from one vector constant to another. We know that the src
// and dest type have the same size (otherwise its an illegal cast).
if (VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
- if (VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
- assert(DestPTy->getPrimitiveSizeInBits() ==
- SrcTy->getPrimitiveSizeInBits() &&
- "Not cast between same sized vectors!");
- SrcTy = nullptr;
- // First, check for null. Undef is already handled.
- if (isa<ConstantAggregateZero>(V))
- return Constant::getNullValue(DestTy);
-
- // Handle ConstantVector and ConstantAggregateVector.
- return BitCastConstantVector(V, DestPTy);
- }
+ if (V->isAllOnesValue())
+ return Constant::getAllOnesValue(DestTy);
// Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
// This allows for other simplifications (although some of them
// can only be handled by Analysis/ConstantFolding.cpp).
if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
return ConstantExpr::getBitCast(ConstantVector::get(V), DestPTy);
+ return nullptr;
}
- // Finally, implement bitcast folding now. The code below doesn't handle
- // bitcast right.
- if (isa<ConstantPointerNull>(V)) // ptr->ptr cast.
- return ConstantPointerNull::get(cast<PointerType>(DestTy));
-
// Handle integral constant input.
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
- if (DestTy->isIntegerTy())
- // Integral -> Integral. This is a no-op because the bit widths must
- // be the same. Consequently, we just fold to V.
- return V;
-
// See note below regarding the PPC_FP128 restriction.
if (DestTy->isFloatingPointTy() && !DestTy->isPPC_FP128Ty())
return ConstantFP::get(DestTy->getContext(),
@@ -192,7 +134,7 @@ static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
assert(C->getType()->isIntegerTy() &&
(cast<IntegerType>(C->getType())->getBitWidth() & 7) == 0 &&
"Non-byte sized integer input");
- unsigned CSize = cast<IntegerType>(C->getType())->getBitWidth()/8;
+ [[maybe_unused]] unsigned CSize = cast<IntegerType>(C->getType())->getBitWidth()/8;
assert(ByteSize && "Must be accessing some piece");
assert(ByteStart+ByteSize <= CSize && "Extracting invalid piece from input");
assert(ByteSize != CSize && "Should not extract everything");
@@ -213,58 +155,6 @@ static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
switch (CE->getOpcode()) {
default: return nullptr;
- case Instruction::Or: {
- Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize);
- if (!RHS)
- return nullptr;
-
- // X | -1 -> -1.
- if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS))
- if (RHSC->isMinusOne())
- return RHSC;
-
- Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize);
- if (!LHS)
- return nullptr;
- return ConstantExpr::getOr(LHS, RHS);
- }
- case Instruction::And: {
- Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize);
- if (!RHS)
- return nullptr;
-
- // X & 0 -> 0.
- if (RHS->isNullValue())
- return RHS;
-
- Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize);
- if (!LHS)
- return nullptr;
- return ConstantExpr::getAnd(LHS, RHS);
- }
- case Instruction::LShr: {
- ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
- if (!Amt)
- return nullptr;
- APInt ShAmt = Amt->getValue();
- // Cannot analyze non-byte shifts.
- if ((ShAmt & 7) != 0)
- return nullptr;
- ShAmt.lshrInPlace(3);
-
- // If the extract is known to be all zeros, return zero.
- if (ShAmt.uge(CSize - ByteStart))
- return Constant::getNullValue(
- IntegerType::get(CE->getContext(), ByteSize * 8));
- // If the extract is known to be fully in the input, extract it.
- if (ShAmt.ule(CSize - (ByteStart + ByteSize)))
- return ExtractConstantBytes(CE->getOperand(0),
- ByteStart + ShAmt.getZExtValue(), ByteSize);
-
- // TODO: Handle the 'partially zero' case.
- return nullptr;
- }
-
case Instruction::Shl: {
ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
if (!Amt)
@@ -287,43 +177,16 @@ static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
// TODO: Handle the 'partially zero' case.
return nullptr;
}
-
- case Instruction::ZExt: {
- unsigned SrcBitSize =
- cast<IntegerType>(CE->getOperand(0)->getType())->getBitWidth();
-
- // If extracting something that is completely zero, return 0.
- if (ByteStart*8 >= SrcBitSize)
- return Constant::getNullValue(IntegerType::get(CE->getContext(),
- ByteSize*8));
-
- // If exactly extracting the input, return it.
- if (ByteStart == 0 && ByteSize*8 == SrcBitSize)
- return CE->getOperand(0);
-
- // If extracting something completely in the input, if the input is a
- // multiple of 8 bits, recurse.
- if ((SrcBitSize&7) == 0 && (ByteStart+ByteSize)*8 <= SrcBitSize)
- return ExtractConstantBytes(CE->getOperand(0), ByteStart, ByteSize);
-
- // Otherwise, if extracting a subset of the input, which is not multiple of
- // 8 bits, do a shift and trunc to get the bits.
- if ((ByteStart+ByteSize)*8 < SrcBitSize) {
- assert((SrcBitSize&7) && "Shouldn't get byte sized case here");
- Constant *Res = CE->getOperand(0);
- if (ByteStart)
- Res = ConstantExpr::getLShr(Res,
- ConstantInt::get(Res->getType(), ByteStart*8));
- return ConstantExpr::getTrunc(Res, IntegerType::get(C->getContext(),
- ByteSize*8));
- }
-
- // TODO: Handle the 'partially zero' case.
- return nullptr;
- }
}
}
+static Constant *foldMaybeUndesirableCast(unsigned opc, Constant *V,
+ Type *DestTy) {
+ return ConstantExpr::isDesirableCastOp(opc)
+ ? ConstantExpr::getCast(opc, V, DestTy)
+ : ConstantFoldCastInstruction(opc, V, DestTy);
+}
+
Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
Type *DestTy) {
if (isa<PoisonValue>(V))
@@ -349,29 +212,7 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
if (CE->isCast()) {
// Try hard to fold cast of cast because they are often eliminable.
if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
- return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
- } else if (CE->getOpcode() == Instruction::GetElementPtr &&
- // Do not fold addrspacecast (gep 0, .., 0). It might make the
- // addrspacecast uncanonicalized.
- opc != Instruction::AddrSpaceCast &&
- // Do not fold bitcast (gep) with inrange index, as this loses
- // information.
- !cast<GEPOperator>(CE)->getInRangeIndex() &&
- // Do not fold if the gep type is a vector, as bitcasting
- // operand 0 of a vector gep will result in a bitcast between
- // different sizes.
- !CE->getType()->isVectorTy()) {
- // If all of the indexes in the GEP are null values, there is no pointer
- // adjustment going on. We might as well cast the source pointer.
- bool isAllNull = true;
- for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
- if (!CE->getOperand(i)->isNullValue()) {
- isAllNull = false;
- break;
- }
- if (isAllNull)
- // This is casting one pointer type to another, always BitCast
- return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
+ return foldMaybeUndesirableCast(newOpc, CE->getOperand(0), DestTy);
}
}
@@ -386,18 +227,22 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
Type *DstEltTy = DestVecTy->getElementType();
// Fast path for splatted constants.
if (Constant *Splat = V->getSplatValue()) {
+ Constant *Res = foldMaybeUndesirableCast(opc, Splat, DstEltTy);
+ if (!Res)
+ return nullptr;
return ConstantVector::getSplat(
- cast<VectorType>(DestTy)->getElementCount(),
- ConstantExpr::getCast(opc, Splat, DstEltTy));
+ cast<VectorType>(DestTy)->getElementCount(), Res);
}
SmallVector<Constant *, 16> res;
Type *Ty = IntegerType::get(V->getContext(), 32);
for (unsigned i = 0,
e = cast<FixedVectorType>(V->getType())->getNumElements();
i != e; ++i) {
- Constant *C =
- ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i));
- res.push_back(ConstantExpr::getCast(opc, C, DstEltTy));
+ Constant *C = ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i));
+ Constant *Casted = foldMaybeUndesirableCast(opc, C, DstEltTy);
+ if (!Casted)
+ return nullptr;
+ res.push_back(Casted);
}
return ConstantVector::get(res);
}
@@ -433,16 +278,6 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
return ConstantInt::get(FPC->getContext(), IntVal);
}
return nullptr; // Can't fold.
- case Instruction::IntToPtr: //always treated as unsigned
- if (V->isNullValue()) // Is it an integral null value?
- return ConstantPointerNull::get(cast<PointerType>(DestTy));
- return nullptr; // Other pointer types cannot be casted
- case Instruction::PtrToInt: // always treated as unsigned
- // Is it a null pointer value?
- if (V->isNullValue())
- return ConstantInt::get(DestTy, 0);
- // Other pointer types cannot be casted
- return nullptr;
case Instruction::UIToFP:
case Instruction::SIToFP:
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
@@ -491,6 +326,8 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
case Instruction::BitCast:
return FoldBitCast(V, DestTy);
case Instruction::AddrSpaceCast:
+ case Instruction::IntToPtr:
+ case Instruction::PtrToInt:
return nullptr;
}
}
@@ -1004,16 +841,6 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
return C1; // X & -1 == X
if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
- // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
- if (CE1->getOpcode() == Instruction::ZExt) {
- unsigned DstWidth = CI2->getType()->getBitWidth();
- unsigned SrcWidth =
- CE1->getOperand(0)->getType()->getPrimitiveSizeInBits();
- APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth));
- if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits)
- return C1;
- }
-
// If and'ing the address of a global with a constant, fold it.
if (CE1->getOpcode() == Instruction::PtrToInt &&
isa<GlobalValue>(CE1->getOperand(0))) {
@@ -1074,17 +901,13 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
}
}
break;
- case Instruction::AShr:
- // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
- if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1))
- if (CE1->getOpcode() == Instruction::ZExt) // Top bits known zero.
- return ConstantExpr::getLShr(C1, C2);
- break;
}
} else if (isa<ConstantInt>(C1)) {
// If C1 is a ConstantInt and C2 is not, swap the operands.
if (Instruction::isCommutative(Opcode))
- return ConstantExpr::get(Opcode, C2, C1);
+ return ConstantExpr::isDesirableBinOp(Opcode)
+ ? ConstantExpr::get(Opcode, C2, C1)
+ : ConstantFoldBinaryInstruction(Opcode, C2, C1);
}
if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
@@ -1241,8 +1064,6 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
case Instruction::Add:
case Instruction::Sub:
return ConstantExpr::getXor(C1, C2);
- case Instruction::Mul:
- return ConstantExpr::getAnd(C1, C2);
case Instruction::Shl:
case Instruction::LShr:
case Instruction::AShr:
@@ -1268,70 +1089,6 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
return nullptr;
}
-/// This function determines if there is anything we can decide about the two
-/// constants provided. This doesn't need to handle simple things like
-/// ConstantFP comparisons, but should instead handle ConstantExprs.
-/// If we can determine that the two constants have a particular relation to
-/// each other, we should return the corresponding FCmpInst predicate,
-/// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
-/// ConstantFoldCompareInstruction.
-///
-/// To simplify this code we canonicalize the relation so that the first
-/// operand is always the most "complex" of the two. We consider ConstantFP
-/// to be the simplest, and ConstantExprs to be the most complex.
-static FCmpInst::Predicate evaluateFCmpRelation(Constant *V1, Constant *V2) {
- assert(V1->getType() == V2->getType() &&
- "Cannot compare values of different types!");
-
- // We do not know if a constant expression will evaluate to a number or NaN.
- // Therefore, we can only say that the relation is unordered or equal.
- if (V1 == V2) return FCmpInst::FCMP_UEQ;
-
- if (!isa<ConstantExpr>(V1)) {
- if (!isa<ConstantExpr>(V2)) {
- // Simple case, use the standard constant folder.
- ConstantInt *R = nullptr;
- R = dyn_cast<ConstantInt>(
- ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, V1, V2));
- if (R && !R->isZero())
- return FCmpInst::FCMP_OEQ;
- R = dyn_cast<ConstantInt>(
- ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, V1, V2));
- if (R && !R->isZero())
- return FCmpInst::FCMP_OLT;
- R = dyn_cast<ConstantInt>(
- ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, V1, V2));
- if (R && !R->isZero())
- return FCmpInst::FCMP_OGT;
-
- // Nothing more we can do
- return FCmpInst::BAD_FCMP_PREDICATE;
- }
-
- // If the first operand is simple and second is ConstantExpr, swap operands.
- FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
- if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
- return FCmpInst::getSwappedPredicate(SwappedRelation);
- } else {
- // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
- // constantexpr or a simple constant.
- ConstantExpr *CE1 = cast<ConstantExpr>(V1);
- switch (CE1->getOpcode()) {
- case Instruction::FPTrunc:
- case Instruction::FPExt:
- case Instruction::UIToFP:
- case Instruction::SIToFP:
- // We might be able to do something with these but we don't right now.
- break;
- default:
- break;
- }
- }
- // There are MANY other foldings that we could perform here. They will
- // probably be added on demand, as they seem needed.
- return FCmpInst::BAD_FCMP_PREDICATE;
-}
-
static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1,
const GlobalValue *GV2) {
auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) {
@@ -1362,66 +1119,54 @@ static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1,
/// If we can determine that the two constants have a particular relation to
/// each other, we should return the corresponding ICmp predicate, otherwise
/// return ICmpInst::BAD_ICMP_PREDICATE.
-///
-/// To simplify this code we canonicalize the relation so that the first
-/// operand is always the most "complex" of the two. We consider simple
-/// constants (like ConstantInt) to be the simplest, followed by
-/// GlobalValues, followed by ConstantExpr's (the most complex).
-///
-static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
- bool isSigned) {
+static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2) {
assert(V1->getType() == V2->getType() &&
"Cannot compare different types of values!");
if (V1 == V2) return ICmpInst::ICMP_EQ;
- if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1) &&
- !isa<BlockAddress>(V1)) {
- if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2) &&
- !isa<BlockAddress>(V2)) {
- // We distilled this down to a simple case, use the standard constant
- // folder.
- ConstantInt *R = nullptr;
- ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
- R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
- if (R && !R->isZero())
- return pred;
- pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
- R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
- if (R && !R->isZero())
- return pred;
- pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
- R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
- if (R && !R->isZero())
- return pred;
+ // The following folds only apply to pointers.
+ if (!V1->getType()->isPointerTy())
+ return ICmpInst::BAD_ICMP_PREDICATE;
- // If we couldn't figure it out, bail.
- return ICmpInst::BAD_ICMP_PREDICATE;
- }
-
- // If the first operand is simple, swap operands.
- ICmpInst::Predicate SwappedRelation =
- evaluateICmpRelation(V2, V1, isSigned);
+ // To simplify this code we canonicalize the relation so that the first
+ // operand is always the most "complex" of the two. We consider simple
+ // constants (like ConstantPointerNull) to be the simplest, followed by
+ // BlockAddress, GlobalValues, and ConstantExpr's (the most complex).
+ auto GetComplexity = [](Constant *V) {
+ if (isa<ConstantExpr>(V))
+ return 3;
+ if (isa<GlobalValue>(V))
+ return 2;
+ if (isa<BlockAddress>(V))
+ return 1;
+ return 0;
+ };
+ if (GetComplexity(V1) < GetComplexity(V2)) {
+ ICmpInst::Predicate SwappedRelation = evaluateICmpRelation(V2, V1);
if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
return ICmpInst::getSwappedPredicate(SwappedRelation);
+ return ICmpInst::BAD_ICMP_PREDICATE;
+ }
- } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
- if (isa<ConstantExpr>(V2)) { // Swap as necessary.
- ICmpInst::Predicate SwappedRelation =
- evaluateICmpRelation(V2, V1, isSigned);
- if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
- return ICmpInst::getSwappedPredicate(SwappedRelation);
- return ICmpInst::BAD_ICMP_PREDICATE;
+ if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
+ // Now we know that the RHS is a BlockAddress or simple constant.
+ if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
+ // Block address in another function can't equal this one, but block
+ // addresses in the current function might be the same if blocks are
+ // empty.
+ if (BA2->getFunction() != BA->getFunction())
+ return ICmpInst::ICMP_NE;
+ } else if (isa<ConstantPointerNull>(V2)) {
+ return ICmpInst::ICMP_NE;
}
-
+ } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
// Now we know that the RHS is a GlobalValue, BlockAddress or simple
- // constant (which, since the types must match, means that it's a
- // ConstantPointerNull).
+ // constant.
if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
return areGlobalsPotentiallyEqual(GV, GV2);
} else if (isa<BlockAddress>(V2)) {
return ICmpInst::ICMP_NE; // Globals never equal labels.
- } else {
- assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
+ } else if (isa<ConstantPointerNull>(V2)) {
// GlobalVals can never be null unless they have external weak linkage.
// We don't try to evaluate aliases here.
// NOTE: We should not be doing this constant folding if null pointer
@@ -1432,30 +1177,6 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
GV->getType()->getAddressSpace()))
return ICmpInst::ICMP_UGT;
}
- } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
- if (isa<ConstantExpr>(V2)) { // Swap as necessary.
- ICmpInst::Predicate SwappedRelation =
- evaluateICmpRelation(V2, V1, isSigned);
- if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
- return ICmpInst::getSwappedPredicate(SwappedRelation);
- return ICmpInst::BAD_ICMP_PREDICATE;
- }
-
- // Now we know that the RHS is a GlobalValue, BlockAddress or simple
- // constant (which, since the types must match, means that it is a
- // ConstantPointerNull).
- if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
- // Block address in another function can't equal this one, but block
- // addresses in the current function might be the same if blocks are
- // empty.
- if (BA2->getFunction() != BA->getFunction())
- return ICmpInst::ICMP_NE;
- } else {
- // Block addresses aren't null, don't equal the address of globals.
- assert((isa<ConstantPointerNull>(V2) || isa<GlobalValue>(V2)) &&
- "Canonicalization guarantee!");
- return ICmpInst::ICMP_NE;
- }
} else {
// Ok, the LHS is known to be a constantexpr. The RHS can be any of a
// constantexpr, a global, block address, or a simple constant.
@@ -1463,39 +1184,6 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
Constant *CE1Op0 = CE1->getOperand(0);
switch (CE1->getOpcode()) {
- case Instruction::Trunc:
- case Instruction::FPTrunc:
- case Instruction::FPExt:
- case Instruction::FPToUI:
- case Instruction::FPToSI:
- break; // We can't evaluate floating point casts or truncations.
-
- case Instruction::BitCast:
- // If this is a global value cast, check to see if the RHS is also a
- // GlobalValue.
- if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0))
- if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2))
- return areGlobalsPotentiallyEqual(GV, GV2);
- [[fallthrough]];
- case Instruction::UIToFP:
- case Instruction::SIToFP:
- case Instruction::ZExt:
- case Instruction::SExt:
- // We can't evaluate floating point casts or truncations.
- if (CE1Op0->getType()->isFPOrFPVectorTy())
- break;
-
- // If the cast is not actually changing bits, and the second operand is a
- // null pointer, do the comparison with the pre-casted value.
- if (V2->isNullValue() && CE1->getType()->isIntOrPtrTy()) {
- if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
- if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
- return evaluateICmpRelation(CE1Op0,
- Constant::getNullValue(CE1Op0->getType()),
- isSigned);
- }
- break;
-
case Instruction::GetElementPtr: {
GEPOperator *CE1GEP = cast<GEPOperator>(CE1);
// Ok, since this is a getelementptr, we know that the constant has a
@@ -1541,25 +1229,6 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
return ICmpInst::BAD_ICMP_PREDICATE;
}
-static Constant *constantFoldCompareGlobalToNull(CmpInst::Predicate Predicate,
- Constant *C1, Constant *C2) {
- const GlobalValue *GV = dyn_cast<GlobalValue>(C2);
- if (!GV || !C1->isNullValue())
- return nullptr;
-
- // Don't try to evaluate aliases. External weak GV can be null.
- if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() &&
- !NullPointerIsDefined(nullptr /* F */,
- GV->getType()->getAddressSpace())) {
- if (Predicate == ICmpInst::ICMP_EQ)
- return ConstantInt::getFalse(C1->getContext());
- else if (Predicate == ICmpInst::ICMP_NE)
- return ConstantInt::getTrue(C1->getContext());
- }
-
- return nullptr;
-}
-
Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate,
Constant *C1, Constant *C2) {
Type *ResultTy;
@@ -1598,14 +1267,6 @@ Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate,
return ConstantInt::get(ResultTy, CmpInst::isUnordered(Predicate));
}
- // icmp eq/ne(null,GV) -> false/true
- if (Constant *Folded = constantFoldCompareGlobalToNull(Predicate, C1, C2))
- return Folded;
-
- // icmp eq/ne(GV,null) -> false/true
- if (Constant *Folded = constantFoldCompareGlobalToNull(Predicate, C2, C1))
- return Folded;
-
if (C2->isNullValue()) {
// The caller is expected to commute the operands if the constant expression
// is C2.
@@ -1671,83 +1332,18 @@ Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate,
return ConstantVector::get(ResElts);
}
- if (C1->getType()->isFloatingPointTy() &&
- // Only call evaluateFCmpRelation if we have a constant expr to avoid
- // infinite recursive loop
- (isa<ConstantExpr>(C1) || isa<ConstantExpr>(C2))) {
- int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
- switch (evaluateFCmpRelation(C1, C2)) {
- default: llvm_unreachable("Unknown relation!");
- case FCmpInst::FCMP_UNO:
- case FCmpInst::FCMP_ORD:
- case FCmpInst::FCMP_UNE:
- case FCmpInst::FCMP_ULT:
- case FCmpInst::FCMP_UGT:
- case FCmpInst::FCMP_ULE:
- case FCmpInst::FCMP_UGE:
- case FCmpInst::FCMP_TRUE:
- case FCmpInst::FCMP_FALSE:
- case FCmpInst::BAD_FCMP_PREDICATE:
- break; // Couldn't determine anything about these constants.
- case FCmpInst::FCMP_OEQ: // We know that C1 == C2
- Result =
- (Predicate == FCmpInst::FCMP_UEQ || Predicate == FCmpInst::FCMP_OEQ ||
- Predicate == FCmpInst::FCMP_ULE || Predicate == FCmpInst::FCMP_OLE ||
- Predicate == FCmpInst::FCMP_UGE || Predicate == FCmpInst::FCMP_OGE);
- break;
- case FCmpInst::FCMP_OLT: // We know that C1 < C2
- Result =
- (Predicate == FCmpInst::FCMP_UNE || Predicate == FCmpInst::FCMP_ONE ||
- Predicate == FCmpInst::FCMP_ULT || Predicate == FCmpInst::FCMP_OLT ||
- Predicate == FCmpInst::FCMP_ULE || Predicate == FCmpInst::FCMP_OLE);
- break;
- case FCmpInst::FCMP_OGT: // We know that C1 > C2
- Result =
- (Predicate == FCmpInst::FCMP_UNE || Predicate == FCmpInst::FCMP_ONE ||
- Predicate == FCmpInst::FCMP_UGT || Predicate == FCmpInst::FCMP_OGT ||
- Predicate == FCmpInst::FCMP_UGE || Predicate == FCmpInst::FCMP_OGE);
- break;
- case FCmpInst::FCMP_OLE: // We know that C1 <= C2
- // We can only partially decide this relation.
- if (Predicate == FCmpInst::FCMP_UGT || Predicate == FCmpInst::FCMP_OGT)
- Result = 0;
- else if (Predicate == FCmpInst::FCMP_ULT ||
- Predicate == FCmpInst::FCMP_OLT)
- Result = 1;
- break;
- case FCmpInst::FCMP_OGE: // We known that C1 >= C2
- // We can only partially decide this relation.
- if (Predicate == FCmpInst::FCMP_ULT || Predicate == FCmpInst::FCMP_OLT)
- Result = 0;
- else if (Predicate == FCmpInst::FCMP_UGT ||
- Predicate == FCmpInst::FCMP_OGT)
- Result = 1;
- break;
- case FCmpInst::FCMP_ONE: // We know that C1 != C2
- // We can only partially decide this relation.
- if (Predicate == FCmpInst::FCMP_OEQ || Predicate == FCmpInst::FCMP_UEQ)
- Result = 0;
- else if (Predicate == FCmpInst::FCMP_ONE ||
- Predicate == FCmpInst::FCMP_UNE)
- Result = 1;
- break;
- case FCmpInst::FCMP_UEQ: // We know that C1 == C2 || isUnordered(C1, C2).
- // We can only partially decide this relation.
+ if (C1->getType()->isFPOrFPVectorTy()) {
+ if (C1 == C2) {
+ // We know that C1 == C2 || isUnordered(C1, C2).
if (Predicate == FCmpInst::FCMP_ONE)
- Result = 0;
+ return ConstantInt::getFalse(ResultTy);
else if (Predicate == FCmpInst::FCMP_UEQ)
- Result = 1;
- break;
+ return ConstantInt::getTrue(ResultTy);
}
-
- // If we evaluated the result, return it now.
- if (Result != -1)
- return ConstantInt::get(ResultTy, Result);
-
} else {
// Evaluate the relation between the two constants, per the predicate.
int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
- switch (evaluateICmpRelation(C1, C2, CmpInst::isSigned(Predicate))) {
+ switch (evaluateICmpRelation(C1, C2)) {
default: llvm_unreachable("Unknown relational!");
case ICmpInst::BAD_ICMP_PREDICATE:
break; // Couldn't determine anything about these constants.
@@ -1832,38 +1428,6 @@ Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate,
if (Result != -1)
return ConstantInt::get(ResultTy, Result);
- // If the right hand side is a bitcast, try using its inverse to simplify
- // it by moving it to the left hand side. We can't do this if it would turn
- // a vector compare into a scalar compare or visa versa, or if it would turn
- // the operands into FP values.
- if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(C2)) {
- Constant *CE2Op0 = CE2->getOperand(0);
- if (CE2->getOpcode() == Instruction::BitCast &&
- CE2->getType()->isVectorTy() == CE2Op0->getType()->isVectorTy() &&
- !CE2Op0->getType()->isFPOrFPVectorTy()) {
- Constant *Inverse = ConstantExpr::getBitCast(C1, CE2Op0->getType());
- return ConstantExpr::getICmp(Predicate, Inverse, CE2Op0);
- }
- }
-
- // If the left hand side is an extension, try eliminating it.
- if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
- if ((CE1->getOpcode() == Instruction::SExt &&
- ICmpInst::isSigned(Predicate)) ||
- (CE1->getOpcode() == Instruction::ZExt &&
- !ICmpInst::isSigned(Predicate))) {
- Constant *CE1Op0 = CE1->getOperand(0);
- Constant *CE1Inverse = ConstantExpr::getTrunc(CE1, CE1Op0->getType());
- if (CE1Inverse == CE1Op0) {
- // Check whether we can safely truncate the right hand side.
- Constant *C2Inverse = ConstantExpr::getTrunc(C2, CE1Op0->getType());
- if (ConstantExpr::getCast(CE1->getOpcode(), C2Inverse,
- C2->getType()) == C2)
- return ConstantExpr::getICmp(Predicate, CE1Inverse, C2Inverse);
- }
- }
- }
-
if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
(C1->isNullValue() && !C2->isNullValue())) {
// If C2 is a constant expr and C1 isn't, flip them around and fold the
@@ -1974,8 +1538,13 @@ static Constant *foldGEPOfGEP(GEPOperator *GEP, Type *PointeeTy, bool InBounds,
Type *CommonTy =
Type::getIntNTy(LastIdxTy->getContext(), CommonExtendedWidth);
- Idx0 = ConstantExpr::getSExtOrBitCast(Idx0, CommonTy);
- LastIdx = ConstantExpr::getSExtOrBitCast(LastIdx, CommonTy);
+ if (Idx0->getType() != CommonTy)
+ Idx0 = ConstantFoldCastInstruction(Instruction::SExt, Idx0, CommonTy);
+ if (LastIdx->getType() != CommonTy)
+ LastIdx =
+ ConstantFoldCastInstruction(Instruction::SExt, LastIdx, CommonTy);
+ if (!Idx0 || !LastIdx)
+ return nullptr;
}
NewIndices.push_back(ConstantExpr::get(Instruction::Add, Idx0, LastIdx));
@@ -2025,39 +1594,6 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
cast<VectorType>(GEPTy)->getElementCount(), C)
: C;
- if (C->isNullValue()) {
- bool isNull = true;
- for (Value *Idx : Idxs)
- if (!isa<UndefValue>(Idx) && !cast<Constant>(Idx)->isNullValue()) {
- isNull = false;
- break;
- }
- if (isNull) {
- PointerType *PtrTy = cast<PointerType>(C->getType()->getScalarType());
- Type *Ty = GetElementPtrInst::getIndexedType(PointeeTy, Idxs);
-
- assert(Ty && "Invalid indices for GEP!");
- Type *OrigGEPTy = PointerType::get(Ty, PtrTy->getAddressSpace());
- Type *GEPTy = PointerType::get(Ty, PtrTy->getAddressSpace());
- if (VectorType *VT = dyn_cast<VectorType>(C->getType()))
- GEPTy = VectorType::get(OrigGEPTy, VT->getElementCount());
-
- // The GEP returns a vector of pointers when one of more of
- // its arguments is a vector.
- for (Value *Idx : Idxs) {
- if (auto *VT = dyn_cast<VectorType>(Idx->getType())) {
- assert((!isa<VectorType>(GEPTy) || isa<ScalableVectorType>(GEPTy) ==
- isa<ScalableVectorType>(VT)) &&
- "Mismatched GEPTy vector types");
- GEPTy = VectorType::get(OrigGEPTy, VT->getElementCount());
- break;
- }
- }
-
- return Constant::getNullValue(GEPTy);
- }
- }
-
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
if (auto *GEP = dyn_cast<GEPOperator>(CE))
if (Constant *C = foldGEPOfGEP(GEP, PointeeTy, InBounds, Idxs))
@@ -2193,11 +1729,13 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
: cast<FixedVectorType>(CurrIdx->getType())->getNumElements());
if (!PrevIdx->getType()->isIntOrIntVectorTy(CommonExtendedWidth))
- PrevIdx = ConstantExpr::getSExt(PrevIdx, ExtendedTy);
+ PrevIdx =
+ ConstantFoldCastInstruction(Instruction::SExt, PrevIdx, ExtendedTy);
if (!Div->getType()->isIntOrIntVectorTy(CommonExtendedWidth))
- Div = ConstantExpr::getSExt(Div, ExtendedTy);
+ Div = ConstantFoldCastInstruction(Instruction::SExt, Div, ExtendedTy);
+ assert(PrevIdx && Div && "Should have folded");
NewIdxs[i - 1] = ConstantExpr::getAdd(PrevIdx, Div);
}