[llvm] Optimize fptrunc(x)>=C1 --> x>=C2 (PR #99475)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 25 01:56:26 PDT 2024


================
@@ -7888,25 +7894,48 @@ static Instruction *foldFCmpReciprocalAndZero(FCmpInst &I, Instruction *LHSI,
 // Fold trunc(x) < constant --> x < constant if possible.
 static Instruction *foldFCmpFpTrunc(FCmpInst &I, Instruction *LHSI,
                                     Constant *RHSC) {
-  //
   FCmpInst::Predicate Pred = I.getPredicate();
 
   // Check that predicates are valid.
   if ((Pred != FCmpInst::FCMP_OGT) && (Pred != FCmpInst::FCMP_OLT) &&
       (Pred != FCmpInst::FCMP_OGE) && (Pred != FCmpInst::FCMP_OLE))
     return nullptr;
 
-  auto *LType = LHSI->getOperand(0)->getType();
-  auto *RType = RHSC->getType();
+  if (ConstantFP *ConstRFp = dyn_cast<ConstantFP>(RHSC)) {
+    Type *LType = LHSI->getOperand(0)->getType();
+    bool lossInfo;
+    APFloat RValue = ConstRFp->getValue();
+    RValue.convert(LType->getFltSemantics(), APFloat::rmNearestTiesToEven,
+                   &lossInfo);
 
-  if (!(LType->isFloatingPointTy() && RType->isFloatingPointTy() &&
-        LType->getTypeID() >= RType->getTypeID()))
-    return nullptr;
+    return new FCmpInst(Pred, LHSI->getOperand(0),
+                        ConstantFP::get(LType, RValue), "", &I);
+  }
+
+  if (RHSC->getType()->isVectorTy()) {
+    Type *LVecType = LHSI->getOperand(0)->getType();
+    Type *LEleType = dyn_cast<VectorType>(LVecType)->getElementType();
----------------
arsenm wrote:

unchecked dyn_cast. Use the dyn_cast in the if expression instead of using isVectorTy 

https://github.com/llvm/llvm-project/pull/99475


More information about the llvm-commits mailing list