[llvm] r346282 - [IR] add optional parameter for copying IR flags to compare instructions
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 6 16:00:42 PST 2018
Author: spatel
Date: Tue Nov 6 16:00:42 2018
New Revision: 346282
URL: http://llvm.org/viewvc/llvm-project?rev=346282&view=rev
Log:
[IR] add optional parameter for copying IR flags to compare instructions
As shown, this is used to eliminate redundant code in InstCombine,
and there are more cases where we should be using this pattern, but
we're currently unintentionally dropping flags.
Modified:
llvm/trunk/include/llvm/IR/InstrTypes.h
llvm/trunk/include/llvm/IR/Instructions.h
llvm/trunk/lib/IR/Instructions.cpp
llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
Modified: llvm/trunk/include/llvm/IR/InstrTypes.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/InstrTypes.h?rev=346282&r1=346281&r2=346282&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/InstrTypes.h (original)
+++ llvm/trunk/include/llvm/IR/InstrTypes.h Tue Nov 6 16:00:42 2018
@@ -677,7 +677,8 @@ public:
protected:
CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred,
Value *LHS, Value *RHS, const Twine &Name = "",
- Instruction *InsertBefore = nullptr);
+ Instruction *InsertBefore = nullptr,
+ Instruction *FlagsSource = nullptr);
CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred,
Value *LHS, Value *RHS, const Twine &Name,
Modified: llvm/trunk/include/llvm/IR/Instructions.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Instructions.h?rev=346282&r1=346281&r2=346282&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/Instructions.h (original)
+++ llvm/trunk/include/llvm/IR/Instructions.h Tue Nov 6 16:00:42 2018
@@ -1299,12 +1299,13 @@ public:
/// Constructor with no-insertion semantics
FCmpInst(
- Predicate pred, ///< The predicate to use for the comparison
+ Predicate Pred, ///< The predicate to use for the comparison
Value *LHS, ///< The left-hand-side of the expression
Value *RHS, ///< The right-hand-side of the expression
- const Twine &NameStr = "" ///< Name of the instruction
- ) : CmpInst(makeCmpResultType(LHS->getType()),
- Instruction::FCmp, pred, LHS, RHS, NameStr) {
+ const Twine &NameStr = "", ///< Name of the instruction
+ Instruction *FlagsSource = nullptr
+ ) : CmpInst(makeCmpResultType(LHS->getType()), Instruction::FCmp, Pred, LHS,
+ RHS, NameStr, nullptr, FlagsSource) {
AssertOK();
}
Modified: llvm/trunk/lib/IR/Instructions.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Instructions.cpp?rev=346282&r1=346281&r2=346282&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Instructions.cpp (original)
+++ llvm/trunk/lib/IR/Instructions.cpp Tue Nov 6 16:00:42 2018
@@ -3149,15 +3149,18 @@ AddrSpaceCastInst::AddrSpaceCastInst(
//===----------------------------------------------------------------------===//
CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
- Value *RHS, const Twine &Name, Instruction *InsertBefore)
+ Value *RHS, const Twine &Name, Instruction *InsertBefore,
+ Instruction *FlagsSource)
: Instruction(ty, op,
OperandTraits<CmpInst>::op_begin(this),
OperandTraits<CmpInst>::operands(this),
InsertBefore) {
- Op<0>() = LHS;
- Op<1>() = RHS;
+ Op<0>() = LHS;
+ Op<1>() = RHS;
setPredicate((Predicate)predicate);
setName(Name);
+ if (FlagsSource)
+ copyIRFlags(FlagsSource);
}
CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=346282&r1=346281&r2=346282&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Tue Nov 6 16:00:42 2018
@@ -5281,11 +5281,7 @@ static Instruction *foldFCmpReciprocalAn
if (C->isNegative())
Pred = I.getSwappedPredicate();
- // Finally emit the new fcmp.
- Value *X = LHSI->getOperand(1);
- FCmpInst *NewFCI = new FCmpInst(Pred, X, RHSC);
- NewFCI->copyFastMathFlags(&I);
- return NewFCI;
+ return new FCmpInst(Pred, LHSI->getOperand(1), RHSC, "", &I);
}
/// Optimize fabs(X) compared with zero.
@@ -5434,43 +5430,34 @@ Instruction *InstCombiner::visitFCmpInst
if (Instruction *Res = foldCmpLoadFromIndexedGlobal(GEP, GV, I))
return Res;
break;
- case Instruction::Call:
- if (Instruction *X = foldFabsWithFcmpZero(I))
- return X;
- break;
}
}
+ if (Instruction *R = foldFabsWithFcmpZero(I))
+ return R;
+
Value *X, *Y;
if (match(Op0, m_FNeg(m_Value(X)))) {
- if (match(Op1, m_FNeg(m_Value(Y)))) {
- // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y
- Instruction *NewFCmp = new FCmpInst(I.getSwappedPredicate(), X, Y);
- NewFCmp->copyFastMathFlags(&I);
- return NewFCmp;
- }
+ // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y
+ if (match(Op1, m_FNeg(m_Value(Y))))
+ return new FCmpInst(I.getSwappedPredicate(), X, Y, "", &I);
+ // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C
Constant *C;
if (match(Op1, m_Constant(C))) {
- // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C
Constant *NegC = ConstantExpr::getFNeg(C);
- Instruction *NewFCmp = new FCmpInst(I.getSwappedPredicate(), X, NegC);
- NewFCmp->copyFastMathFlags(&I);
- return NewFCmp;
+ return new FCmpInst(I.getSwappedPredicate(), X, NegC, "", &I);
}
}
if (match(Op0, m_FPExt(m_Value(X)))) {
- if (match(Op1, m_FPExt(m_Value(Y))) && X->getType() == Y->getType()) {
- // fcmp (fpext X), (fpext Y) -> fcmp X, Y
- Instruction *NewFCmp = new FCmpInst(Pred, X, Y);
- NewFCmp->copyFastMathFlags(&I);
- return NewFCmp;
- }
+ // fcmp (fpext X), (fpext Y) -> fcmp X, Y
+ if (match(Op1, m_FPExt(m_Value(Y))) && X->getType() == Y->getType())
+ return new FCmpInst(Pred, X, Y, "", &I);
+ // fcmp (fpext X), C -> fcmp X, (fptrunc C) if fptrunc is lossless
const APFloat *C;
if (match(Op1, m_APFloat(C))) {
- // fcmp (fpext X), C -> fcmp X, (fptrunc C) if fptrunc is lossless
const fltSemantics &FPSem =
X->getType()->getScalarType()->getFltSemantics();
bool Lossy;
@@ -5485,9 +5472,7 @@ Instruction *InstCombiner::visitFCmpInst
((Fabs.compare(APFloat::getSmallestNormalized(FPSem)) !=
APFloat::cmpLessThan) || Fabs.isZero())) {
Constant *NewC = ConstantFP::get(X->getType(), TruncC);
- Instruction *NewFCmp = new FCmpInst(Pred, X, NewC);
- NewFCmp->copyFastMathFlags(&I);
- return NewFCmp;
+ return new FCmpInst(Pred, X, NewC, "", &I);
}
}
}
More information about the llvm-commits
mailing list