llvm.org GIT mirror llvm / be9ce13
[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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@346282 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjay Patel 11 months ago
4 changed file(s) with 31 addition(s) and 41 deletion(s). Raw diff Collapse all Expand all
676676 protected:
677677 CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred,
678678 Value *LHS, Value *RHS, const Twine &Name = "",
679 Instruction *InsertBefore = nullptr);
679 Instruction *InsertBefore = nullptr,
680 Instruction *FlagsSource = nullptr);
680681
681682 CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred,
682683 Value *LHS, Value *RHS, const Twine &Name,
12981298
12991299 /// Constructor with no-insertion semantics
13001300 FCmpInst(
1301 Predicate pred, ///< The predicate to use for the comparison
1301 Predicate Pred, ///< The predicate to use for the comparison
13021302 Value *LHS, ///< The left-hand-side of the expression
13031303 Value *RHS, ///< The right-hand-side of the expression
1304 const Twine &NameStr = "" ///< Name of the instruction
1305 ) : CmpInst(makeCmpResultType(LHS->getType()),
1306 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1304 const Twine &NameStr = "", ///< Name of the instruction
1305 Instruction *FlagsSource = nullptr
1306 ) : CmpInst(makeCmpResultType(LHS->getType()), Instruction::FCmp, Pred, LHS,
1307 RHS, NameStr, nullptr, FlagsSource) {
13071308 AssertOK();
13081309 }
13091310
31483148 //===----------------------------------------------------------------------===//
31493149
31503150 CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3151 Value *RHS, const Twine &Name, Instruction *InsertBefore)
3151 Value *RHS, const Twine &Name, Instruction *InsertBefore,
3152 Instruction *FlagsSource)
31523153 : Instruction(ty, op,
31533154 OperandTraits::op_begin(this),
31543155 OperandTraits::operands(this),
31553156 InsertBefore) {
3156 Op<0>() = LHS;
3157 Op<1>() = RHS;
3157 Op<0>() = LHS;
3158 Op<1>() = RHS;
31583159 setPredicate((Predicate)predicate);
31593160 setName(Name);
3161 if (FlagsSource)
3162 copyIRFlags(FlagsSource);
31603163 }
31613164
31623165 CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
52805280 if (C->isNegative())
52815281 Pred = I.getSwappedPredicate();
52825282
5283 // Finally emit the new fcmp.
5284 Value *X = LHSI->getOperand(1);
5285 FCmpInst *NewFCI = new FCmpInst(Pred, X, RHSC);
5286 NewFCI->copyFastMathFlags(&I);
5287 return NewFCI;
5283 return new FCmpInst(Pred, LHSI->getOperand(1), RHSC, "", &I);
52885284 }
52895285
52905286 /// Optimize fabs(X) compared with zero.
54335429 if (Instruction *Res = foldCmpLoadFromIndexedGlobal(GEP, GV, I))
54345430 return Res;
54355431 break;
5436 case Instruction::Call:
5437 if (Instruction *X = foldFabsWithFcmpZero(I))
5438 return X;
5439 break;
5440 }
5441 }
5432 }
5433 }
5434
5435 if (Instruction *R = foldFabsWithFcmpZero(I))
5436 return R;
54425437
54435438 Value *X, *Y;
54445439 if (match(Op0, m_FNeg(m_Value(X)))) {
5445 if (match(Op1, m_FNeg(m_Value(Y)))) {
5446 // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y
5447 Instruction *NewFCmp = new FCmpInst(I.getSwappedPredicate(), X, Y);
5448 NewFCmp->copyFastMathFlags(&I);
5449 return NewFCmp;
5450 }
5451
5440 // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y
5441 if (match(Op1, m_FNeg(m_Value(Y))))
5442 return new FCmpInst(I.getSwappedPredicate(), X, Y, "", &I);
5443
5444 // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C
54525445 Constant *C;
54535446 if (match(Op1, m_Constant(C))) {
5454 // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C
54555447 Constant *NegC = ConstantExpr::getFNeg(C);
5456 Instruction *NewFCmp = new FCmpInst(I.getSwappedPredicate(), X, NegC);
5457 NewFCmp->copyFastMathFlags(&I);
5458 return NewFCmp;
5448 return new FCmpInst(I.getSwappedPredicate(), X, NegC, "", &I);
54595449 }
54605450 }
54615451
54625452 if (match(Op0, m_FPExt(m_Value(X)))) {
5463 if (match(Op1, m_FPExt(m_Value(Y))) && X->getType() == Y->getType()) {
5464 // fcmp (fpext X), (fpext Y) -> fcmp X, Y
5465 Instruction *NewFCmp = new FCmpInst(Pred, X, Y);
5466 NewFCmp->copyFastMathFlags(&I);
5467 return NewFCmp;
5468 }
5469
5453 // fcmp (fpext X), (fpext Y) -> fcmp X, Y
5454 if (match(Op1, m_FPExt(m_Value(Y))) && X->getType() == Y->getType())
5455 return new FCmpInst(Pred, X, Y, "", &I);
5456
5457 // fcmp (fpext X), C -> fcmp X, (fptrunc C) if fptrunc is lossless
54705458 const APFloat *C;
54715459 if (match(Op1, m_APFloat(C))) {
5472 // fcmp (fpext X), C -> fcmp X, (fptrunc C) if fptrunc is lossless
54735460 const fltSemantics &FPSem =
54745461 X->getType()->getScalarType()->getFltSemantics();
54755462 bool Lossy;
54845471 ((Fabs.compare(APFloat::getSmallestNormalized(FPSem)) !=
54855472 APFloat::cmpLessThan) || Fabs.isZero())) {
54865473 Constant *NewC = ConstantFP::get(X->getType(), TruncC);
5487 Instruction *NewFCmp = new FCmpInst(Pred, X, NewC);
5488 NewFCmp->copyFastMathFlags(&I);
5489 return NewFCmp;
5474 return new FCmpInst(Pred, X, NewC, "", &I);
54905475 }
54915476 }
54925477 }