|
22 | 22 | #include "llvm/Analysis/Utils/Local.h"
|
23 | 23 | #include "llvm/Analysis/VectorUtils.h"
|
24 | 24 | #include "llvm/IR/ConstantRange.h"
|
| 25 | +#include "llvm/IR/Constants.h" |
25 | 26 | #include "llvm/IR/DataLayout.h"
|
26 | 27 | #include "llvm/IR/InstrTypes.h"
|
| 28 | +#include "llvm/IR/Instruction.h" |
27 | 29 | #include "llvm/IR/IntrinsicInst.h"
|
28 | 30 | #include "llvm/IR/PatternMatch.h"
|
| 31 | +#include "llvm/Support/Casting.h" |
29 | 32 | #include "llvm/Support/KnownBits.h"
|
30 | 33 | #include "llvm/Transforms/InstCombine/InstCombiner.h"
|
31 | 34 | #include <bitset>
|
@@ -7882,6 +7885,30 @@ static Instruction *foldFCmpReciprocalAndZero(FCmpInst &I, Instruction *LHSI,
|
7882 | 7885 | return new FCmpInst(Pred, LHSI->getOperand(1), RHSC, "", &I);
|
7883 | 7886 | }
|
7884 | 7887 |
|
| 7888 | +// Fold trunc(x) < constant --> x < constant if possible. |
| 7889 | +static Instruction *foldFCmpFpTrunc(FCmpInst &I, Instruction *LHSI, |
| 7890 | + Constant *RHSC) { |
| 7891 | + // |
| 7892 | + FCmpInst::Predicate Pred = I.getPredicate(); |
| 7893 | + |
| 7894 | + // Check that predicates are valid. |
| 7895 | + if ((Pred != FCmpInst::FCMP_OGT) && (Pred != FCmpInst::FCMP_OLT) && |
| 7896 | + (Pred != FCmpInst::FCMP_OGE) && (Pred != FCmpInst::FCMP_OLE)) |
| 7897 | + return nullptr; |
| 7898 | + |
| 7899 | + auto *LType = LHSI->getOperand(0)->getType(); |
| 7900 | + auto *RType = RHSC->getType(); |
| 7901 | + |
| 7902 | + if (!(LType->isFloatingPointTy() && RType->isFloatingPointTy() && |
| 7903 | + LType->getTypeID() >= RType->getTypeID())) |
| 7904 | + return nullptr; |
| 7905 | + |
| 7906 | + auto *ROperand = llvm::ConstantFP::get( |
| 7907 | + LType, dyn_cast<ConstantFP>(RHSC)->getValue().convertToDouble()); |
| 7908 | + |
| 7909 | + return new FCmpInst(Pred, LHSI->getOperand(0), ROperand, "", &I); |
| 7910 | +} |
| 7911 | + |
7885 | 7912 | /// Optimize fabs(X) compared with zero.
|
7886 | 7913 | static Instruction *foldFabsWithFcmpZero(FCmpInst &I, InstCombinerImpl &IC) {
|
7887 | 7914 | Value *X;
|
@@ -8244,6 +8271,10 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
|
8244 | 8271 | cast<LoadInst>(LHSI), GEP, GV, I))
|
8245 | 8272 | return Res;
|
8246 | 8273 | break;
|
| 8274 | + case Instruction::FPTrunc: |
| 8275 | + if (Instruction *NV = foldFCmpFpTrunc(I, LHSI, RHSC)) |
| 8276 | + return NV; |
| 8277 | + break; |
8247 | 8278 | }
|
8248 | 8279 | }
|
8249 | 8280 |
|
|
0 commit comments