-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[InstCombine] Optimize unneeded float to int cast when icmp #155501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8e6daa1
09d9caa
bf38c1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,6 +12,7 @@ | |||||
|
||||||
#include "InstCombineInternal.h" | ||||||
#include "llvm/ADT/APFloat.h" | ||||||
#include "llvm/ADT/APInt.h" | ||||||
#include "llvm/ADT/APSInt.h" | ||||||
#include "llvm/ADT/SetVector.h" | ||||||
#include "llvm/ADT/Statistic.h" | ||||||
|
@@ -21,6 +22,7 @@ | |||||
#include "llvm/Analysis/InstructionSimplify.h" | ||||||
#include "llvm/Analysis/Utils/Local.h" | ||||||
#include "llvm/Analysis/VectorUtils.h" | ||||||
#include "llvm/IR/CmpPredicate.h" | ||||||
#include "llvm/IR/ConstantRange.h" | ||||||
#include "llvm/IR/Constants.h" | ||||||
#include "llvm/IR/DataLayout.h" | ||||||
|
@@ -7611,6 +7613,87 @@ Instruction *InstCombinerImpl::foldICmpCommutative(CmpPredicate Pred, | |||||
return nullptr; | ||||||
} | ||||||
|
||||||
/// Cast integral constant (either scalar or vector) to an appropriate vector | ||||||
/// one | ||||||
/// | ||||||
/// \param C integral contsant to cast | ||||||
/// \param FPType floating point type to cast to | ||||||
/// \param Addend addend to add before casting | ||||||
/// \param DL target data layout | ||||||
/// | ||||||
/// \return result constant | ||||||
static Constant *castIntegralConstantToFloat(Constant *C, Type *FPType, | ||||||
int Addend, const DataLayout &DL) { | ||||||
assert(FPType->isFPOrFPVectorTy() && | ||||||
"fptosi operand must have floating point type"); | ||||||
|
||||||
Constant *CWithAddend = ConstantFoldBinaryOpOperands( | ||||||
Instruction::Add, C, ConstantInt::getSigned(C->getType(), Addend), DL); | ||||||
if (!CWithAddend) | ||||||
return nullptr; | ||||||
return ConstantFoldCastOperand(Instruction::SIToFP, CWithAddend, FPType, DL); | ||||||
} | ||||||
|
||||||
/// Fold icmp (fptosi %arg) C -> fcmp $arg | ||||||
/// Folds: | ||||||
/// - icmp sgt %arg <negative> -> fcmp ogt %arg <negative> | ||||||
/// - icmp sgt %arg <non-negative> -> fcmp oge %arg (<non-negative> + 1) | ||||||
/// - icmp slt %arg <positive> -> fcmp olt %arg <positive> | ||||||
/// - icmp slt %arg <non-positive> -> fcmp ole %arg (<non-positive> - 1) | ||||||
/// | ||||||
/// \param ICmp icmp instruction | ||||||
/// \param IC InstCombiner isntance | ||||||
/// \param DL target data layout | ||||||
/// | ||||||
/// \return folded instruction or nullptr, if failed to combine instructions | ||||||
static Instruction *foldICmpFToSIToFCmp(ICmpInst &ICmp, InstCombiner &IC, | ||||||
const DataLayout &DL) { | ||||||
// Expect that canonical form: first argument is fptosi, second is constant | ||||||
CmpPredicate Pred; | ||||||
Value *FloatOp; | ||||||
Constant *C; | ||||||
if (!match(&ICmp, m_ICmp(Pred, m_OneUse(m_FPToSI(m_Value(FloatOp))), | ||||||
m_ImmConstant(C)))) | ||||||
return nullptr; | ||||||
|
||||||
if (Pred != ICmpInst::ICMP_SGT && Pred != ICmpInst::ICMP_SLT) | ||||||
return nullptr; | ||||||
|
||||||
|
||||||
FCmpInst::Predicate FCmpPredicate; | ||||||
Constant *FCmpConstant{}; | ||||||
|
Constant *FCmpConstant{}; | |
Constant *FCmpConstant; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as below
Uh oh!
There was an error while loading. Please reload this page.