Skip to content

Commit a1547d2

Browse files
author
kissholicma
committed
Optimize fptrunc(x)>=C1 --> x>=C2
1 parent 4a19be5 commit a1547d2

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
#include "llvm/Analysis/Utils/Local.h"
2323
#include "llvm/Analysis/VectorUtils.h"
2424
#include "llvm/IR/ConstantRange.h"
25+
#include "llvm/IR/Constants.h"
2526
#include "llvm/IR/DataLayout.h"
2627
#include "llvm/IR/InstrTypes.h"
28+
#include "llvm/IR/Instruction.h"
2729
#include "llvm/IR/IntrinsicInst.h"
2830
#include "llvm/IR/PatternMatch.h"
31+
#include "llvm/Support/Casting.h"
2932
#include "llvm/Support/KnownBits.h"
3033
#include "llvm/Transforms/InstCombine/InstCombiner.h"
3134
#include <bitset>
@@ -7882,6 +7885,30 @@ static Instruction *foldFCmpReciprocalAndZero(FCmpInst &I, Instruction *LHSI,
78827885
return new FCmpInst(Pred, LHSI->getOperand(1), RHSC, "", &I);
78837886
}
78847887

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+
78857912
/// Optimize fabs(X) compared with zero.
78867913
static Instruction *foldFabsWithFcmpZero(FCmpInst &I, InstCombinerImpl &IC) {
78877914
Value *X;
@@ -8244,6 +8271,10 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
82448271
cast<LoadInst>(LHSI), GEP, GV, I))
82458272
return Res;
82468273
break;
8274+
case Instruction::FPTrunc:
8275+
if (Instruction *NV = foldFCmpFpTrunc(I, LHSI, RHSC))
8276+
return NV;
8277+
break;
82478278
}
82488279
}
82498280

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
; RUN: opt -passes=instcombine -S < %s | FileCheck %s
2+
3+
4+
;CHECK-LABEL: @src(
5+
;CHECK: %result = fcmp oge double %0, 1.000000e+02
6+
;CHECK-NEXT: ret i1 %result
7+
define i1 @src(double %0) {
8+
%trunc = fptrunc double %0 to float
9+
%result = fcmp oge float %trunc, 1.000000e+02
10+
ret i1 %result
11+
}

0 commit comments

Comments
 (0)