Skip to content

Commit 18cc980

Browse files
committed
[ConstantFold] Remove unnecessarily complicated evaluateFCmpRelation() (NFCI)
The only thing this actually handles is if the operands are the same, in which case ueq is returned. Given that nearly all FP constant expressions have already been removed, I think it's safe to say that we aren't going to extend this code in a way that makes use of the more general structure.
1 parent 0168917 commit 18cc980

File tree

1 file changed

+5
-134
lines changed

1 file changed

+5
-134
lines changed

llvm/lib/IR/ConstantFold.cpp

Lines changed: 5 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,70 +1112,6 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
11121112
return nullptr;
11131113
}
11141114

1115-
/// This function determines if there is anything we can decide about the two
1116-
/// constants provided. This doesn't need to handle simple things like
1117-
/// ConstantFP comparisons, but should instead handle ConstantExprs.
1118-
/// If we can determine that the two constants have a particular relation to
1119-
/// each other, we should return the corresponding FCmpInst predicate,
1120-
/// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
1121-
/// ConstantFoldCompareInstruction.
1122-
///
1123-
/// To simplify this code we canonicalize the relation so that the first
1124-
/// operand is always the most "complex" of the two. We consider ConstantFP
1125-
/// to be the simplest, and ConstantExprs to be the most complex.
1126-
static FCmpInst::Predicate evaluateFCmpRelation(Constant *V1, Constant *V2) {
1127-
assert(V1->getType() == V2->getType() &&
1128-
"Cannot compare values of different types!");
1129-
1130-
// We do not know if a constant expression will evaluate to a number or NaN.
1131-
// Therefore, we can only say that the relation is unordered or equal.
1132-
if (V1 == V2) return FCmpInst::FCMP_UEQ;
1133-
1134-
if (!isa<ConstantExpr>(V1)) {
1135-
if (!isa<ConstantExpr>(V2)) {
1136-
// Simple case, use the standard constant folder.
1137-
ConstantInt *R = nullptr;
1138-
R = dyn_cast<ConstantInt>(
1139-
ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, V1, V2));
1140-
if (R && !R->isZero())
1141-
return FCmpInst::FCMP_OEQ;
1142-
R = dyn_cast<ConstantInt>(
1143-
ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, V1, V2));
1144-
if (R && !R->isZero())
1145-
return FCmpInst::FCMP_OLT;
1146-
R = dyn_cast<ConstantInt>(
1147-
ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, V1, V2));
1148-
if (R && !R->isZero())
1149-
return FCmpInst::FCMP_OGT;
1150-
1151-
// Nothing more we can do
1152-
return FCmpInst::BAD_FCMP_PREDICATE;
1153-
}
1154-
1155-
// If the first operand is simple and second is ConstantExpr, swap operands.
1156-
FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
1157-
if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
1158-
return FCmpInst::getSwappedPredicate(SwappedRelation);
1159-
} else {
1160-
// Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1161-
// constantexpr or a simple constant.
1162-
ConstantExpr *CE1 = cast<ConstantExpr>(V1);
1163-
switch (CE1->getOpcode()) {
1164-
case Instruction::FPTrunc:
1165-
case Instruction::FPExt:
1166-
case Instruction::UIToFP:
1167-
case Instruction::SIToFP:
1168-
// We might be able to do something with these but we don't right now.
1169-
break;
1170-
default:
1171-
break;
1172-
}
1173-
}
1174-
// There are MANY other foldings that we could perform here. They will
1175-
// probably be added on demand, as they seem needed.
1176-
return FCmpInst::BAD_FCMP_PREDICATE;
1177-
}
1178-
11791115
static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1,
11801116
const GlobalValue *GV2) {
11811117
auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) {
@@ -1511,79 +1447,14 @@ Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate,
15111447
return ConstantVector::get(ResElts);
15121448
}
15131449

1514-
if (C1->getType()->isFloatingPointTy() &&
1515-
// Only call evaluateFCmpRelation if we have a constant expr to avoid
1516-
// infinite recursive loop
1517-
(isa<ConstantExpr>(C1) || isa<ConstantExpr>(C2))) {
1518-
int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
1519-
switch (evaluateFCmpRelation(C1, C2)) {
1520-
default: llvm_unreachable("Unknown relation!");
1521-
case FCmpInst::FCMP_UNO:
1522-
case FCmpInst::FCMP_ORD:
1523-
case FCmpInst::FCMP_UNE:
1524-
case FCmpInst::FCMP_ULT:
1525-
case FCmpInst::FCMP_UGT:
1526-
case FCmpInst::FCMP_ULE:
1527-
case FCmpInst::FCMP_UGE:
1528-
case FCmpInst::FCMP_TRUE:
1529-
case FCmpInst::FCMP_FALSE:
1530-
case FCmpInst::BAD_FCMP_PREDICATE:
1531-
break; // Couldn't determine anything about these constants.
1532-
case FCmpInst::FCMP_OEQ: // We know that C1 == C2
1533-
Result =
1534-
(Predicate == FCmpInst::FCMP_UEQ || Predicate == FCmpInst::FCMP_OEQ ||
1535-
Predicate == FCmpInst::FCMP_ULE || Predicate == FCmpInst::FCMP_OLE ||
1536-
Predicate == FCmpInst::FCMP_UGE || Predicate == FCmpInst::FCMP_OGE);
1537-
break;
1538-
case FCmpInst::FCMP_OLT: // We know that C1 < C2
1539-
Result =
1540-
(Predicate == FCmpInst::FCMP_UNE || Predicate == FCmpInst::FCMP_ONE ||
1541-
Predicate == FCmpInst::FCMP_ULT || Predicate == FCmpInst::FCMP_OLT ||
1542-
Predicate == FCmpInst::FCMP_ULE || Predicate == FCmpInst::FCMP_OLE);
1543-
break;
1544-
case FCmpInst::FCMP_OGT: // We know that C1 > C2
1545-
Result =
1546-
(Predicate == FCmpInst::FCMP_UNE || Predicate == FCmpInst::FCMP_ONE ||
1547-
Predicate == FCmpInst::FCMP_UGT || Predicate == FCmpInst::FCMP_OGT ||
1548-
Predicate == FCmpInst::FCMP_UGE || Predicate == FCmpInst::FCMP_OGE);
1549-
break;
1550-
case FCmpInst::FCMP_OLE: // We know that C1 <= C2
1551-
// We can only partially decide this relation.
1552-
if (Predicate == FCmpInst::FCMP_UGT || Predicate == FCmpInst::FCMP_OGT)
1553-
Result = 0;
1554-
else if (Predicate == FCmpInst::FCMP_ULT ||
1555-
Predicate == FCmpInst::FCMP_OLT)
1556-
Result = 1;
1557-
break;
1558-
case FCmpInst::FCMP_OGE: // We known that C1 >= C2
1559-
// We can only partially decide this relation.
1560-
if (Predicate == FCmpInst::FCMP_ULT || Predicate == FCmpInst::FCMP_OLT)
1561-
Result = 0;
1562-
else if (Predicate == FCmpInst::FCMP_UGT ||
1563-
Predicate == FCmpInst::FCMP_OGT)
1564-
Result = 1;
1565-
break;
1566-
case FCmpInst::FCMP_ONE: // We know that C1 != C2
1567-
// We can only partially decide this relation.
1568-
if (Predicate == FCmpInst::FCMP_OEQ || Predicate == FCmpInst::FCMP_UEQ)
1569-
Result = 0;
1570-
else if (Predicate == FCmpInst::FCMP_ONE ||
1571-
Predicate == FCmpInst::FCMP_UNE)
1572-
Result = 1;
1573-
break;
1574-
case FCmpInst::FCMP_UEQ: // We know that C1 == C2 || isUnordered(C1, C2).
1575-
// We can only partially decide this relation.
1450+
if (C1->getType()->isFPOrFPVectorTy()) {
1451+
if (C1 == C2) {
1452+
// We know that C1 == C2 || isUnordered(C1, C2).
15761453
if (Predicate == FCmpInst::FCMP_ONE)
1577-
Result = 0;
1454+
return ConstantInt::getFalse(ResultTy);
15781455
else if (Predicate == FCmpInst::FCMP_UEQ)
1579-
Result = 1;
1580-
break;
1456+
return ConstantInt::getTrue(ResultTy);
15811457
}
1582-
1583-
// If we evaluated the result, return it now.
1584-
if (Result != -1)
1585-
return ConstantInt::get(ResultTy, Result);
1586-
15871458
} else {
15881459
// Evaluate the relation between the two constants, per the predicate.
15891460
int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.

0 commit comments

Comments
 (0)