[llvm] d86e2cc - [NFC] Method for evaluation of FCmpInst for constant operands
Serge Pavlov via llvm-commits
llvm-commits at lists.llvm.org
Sat Dec 25 03:22:12 PST 2021
Author: Serge Pavlov
Date: 2021-12-25T17:37:38+07:00
New Revision: d86e2cc2e37c9051a5cf5a4b9b3ae4b9a961ef11
URL: https://github.com/llvm/llvm-project/commit/d86e2cc2e37c9051a5cf5a4b9b3ae4b9a961ef11
DIFF: https://github.com/llvm/llvm-project/commit/d86e2cc2e37c9051a5cf5a4b9b3ae4b9a961ef11.diff
LOG: [NFC] Method for evaluation of FCmpInst for constant operands
New method `FCmpInst::compare` is added, which evaluates the given
compare predicate for constant operands. Interface is made similar to
`ICmpInst::compare`.
Differential Revision: https://reviews.llvm.org/D116168
Added:
Modified:
llvm/include/llvm/IR/Instructions.h
llvm/lib/IR/ConstantFold.cpp
llvm/lib/IR/Instructions.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h
index ccf17628e265b..84ebb461ebefd 100644
--- a/llvm/include/llvm/IR/Instructions.h
+++ b/llvm/include/llvm/IR/Instructions.h
@@ -1456,6 +1456,10 @@ class FCmpInst: public CmpInst {
///
static auto predicates() { return FCmpPredicates(); }
+ /// Return result of `LHS Pred RHS` comparison.
+ static bool compare(const APFloat &LHS, const APFloat &RHS,
+ FCmpInst::Predicate Pred);
+
/// Methods for support type inquiry through isa, cast, and dyn_cast:
static bool classof(const Instruction *I) {
return I->getOpcode() == Instruction::FCmp;
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index ae48d0333d67c..8668fe82601c2 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -1801,46 +1801,8 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
} else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
const APFloat &C1V = cast<ConstantFP>(C1)->getValueAPF();
const APFloat &C2V = cast<ConstantFP>(C2)->getValueAPF();
- APFloat::cmpResult R = C1V.compare(C2V);
- switch (pred) {
- default: llvm_unreachable("Invalid FCmp Predicate");
- case FCmpInst::FCMP_FALSE: return Constant::getNullValue(ResultTy);
- case FCmpInst::FCMP_TRUE: return Constant::getAllOnesValue(ResultTy);
- case FCmpInst::FCMP_UNO:
- return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered);
- case FCmpInst::FCMP_ORD:
- return ConstantInt::get(ResultTy, R!=APFloat::cmpUnordered);
- case FCmpInst::FCMP_UEQ:
- return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
- R==APFloat::cmpEqual);
- case FCmpInst::FCMP_OEQ:
- return ConstantInt::get(ResultTy, R==APFloat::cmpEqual);
- case FCmpInst::FCMP_UNE:
- return ConstantInt::get(ResultTy, R!=APFloat::cmpEqual);
- case FCmpInst::FCMP_ONE:
- return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
- R==APFloat::cmpGreaterThan);
- case FCmpInst::FCMP_ULT:
- return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
- R==APFloat::cmpLessThan);
- case FCmpInst::FCMP_OLT:
- return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan);
- case FCmpInst::FCMP_UGT:
- return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
- R==APFloat::cmpGreaterThan);
- case FCmpInst::FCMP_OGT:
- return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan);
- case FCmpInst::FCMP_ULE:
- return ConstantInt::get(ResultTy, R!=APFloat::cmpGreaterThan);
- case FCmpInst::FCMP_OLE:
- return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
- R==APFloat::cmpEqual);
- case FCmpInst::FCMP_UGE:
- return ConstantInt::get(ResultTy, R!=APFloat::cmpLessThan);
- case FCmpInst::FCMP_OGE:
- return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan ||
- R==APFloat::cmpEqual);
- }
+ CmpInst::Predicate Predicate = CmpInst::Predicate(pred);
+ return ConstantInt::get(ResultTy, FCmpInst::compare(C1V, C2V, Predicate));
} else if (auto *C1VTy = dyn_cast<VectorType>(C1->getType())) {
// Fast path for splatted constants.
diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index b40fe0cfc8600..7798af3b19b99 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -4160,6 +4160,47 @@ bool ICmpInst::compare(const APInt &LHS, const APInt &RHS,
};
}
+bool FCmpInst::compare(const APFloat &LHS, const APFloat &RHS,
+ FCmpInst::Predicate Pred) {
+ APFloat::cmpResult R = LHS.compare(RHS);
+ switch (Pred) {
+ default:
+ llvm_unreachable("Invalid FCmp Predicate");
+ case FCmpInst::FCMP_FALSE:
+ return false;
+ case FCmpInst::FCMP_TRUE:
+ return true;
+ case FCmpInst::FCMP_UNO:
+ return R == APFloat::cmpUnordered;
+ case FCmpInst::FCMP_ORD:
+ return R != APFloat::cmpUnordered;
+ case FCmpInst::FCMP_UEQ:
+ return R == APFloat::cmpUnordered || R == APFloat::cmpEqual;
+ case FCmpInst::FCMP_OEQ:
+ return R == APFloat::cmpEqual;
+ case FCmpInst::FCMP_UNE:
+ return R != APFloat::cmpEqual;
+ case FCmpInst::FCMP_ONE:
+ return R == APFloat::cmpLessThan || R == APFloat::cmpGreaterThan;
+ case FCmpInst::FCMP_ULT:
+ return R == APFloat::cmpUnordered || R == APFloat::cmpLessThan;
+ case FCmpInst::FCMP_OLT:
+ return R == APFloat::cmpLessThan;
+ case FCmpInst::FCMP_UGT:
+ return R == APFloat::cmpUnordered || R == APFloat::cmpGreaterThan;
+ case FCmpInst::FCMP_OGT:
+ return R == APFloat::cmpGreaterThan;
+ case FCmpInst::FCMP_ULE:
+ return R != APFloat::cmpGreaterThan;
+ case FCmpInst::FCMP_OLE:
+ return R == APFloat::cmpLessThan || R == APFloat::cmpEqual;
+ case FCmpInst::FCMP_UGE:
+ return R != APFloat::cmpLessThan;
+ case FCmpInst::FCMP_OGE:
+ return R == APFloat::cmpGreaterThan || R == APFloat::cmpEqual;
+ }
+}
+
CmpInst::Predicate CmpInst::getFlippedSignednessPredicate(Predicate pred) {
assert(CmpInst::isRelational(pred) &&
"Call only with non-equality predicates!");
More information about the llvm-commits
mailing list