[InstSimplify] generalize FP folds with undef/NaN; NFC

We can reuse this logic for things like fma.

llvm-svn: 373119
This commit is contained in:
Sanjay Patel 2019-09-27 20:09:09 +00:00
parent 4b343fd84c
commit 8cecc30c99
1 changed files with 14 additions and 12 deletions

View File

@ -4482,14 +4482,16 @@ static Constant *propagateNaN(Constant *In) {
return In;
}
static Constant *simplifyFPBinop(Value *Op0, Value *Op1) {
if (isa<UndefValue>(Op0) || isa<UndefValue>(Op1))
return ConstantFP::getNaN(Op0->getType());
/// Perform folds that are common to any floating-point operation. This implies
/// transforms based on undef/NaN because the operation itself makes no
/// difference to the result.
static Constant *simplifyFPOp(ArrayRef<Value *> Ops) {
if (any_of(Ops, [](Value *V) { return isa<UndefValue>(V); }))
return ConstantFP::getNaN(Ops[0]->getType());
if (match(Op0, m_NaN()))
return propagateNaN(cast<Constant>(Op0));
if (match(Op1, m_NaN()))
return propagateNaN(cast<Constant>(Op1));
for (Value *V : Ops)
if (match(V, m_NaN()))
return propagateNaN(cast<Constant>(V));
return nullptr;
}
@ -4501,7 +4503,7 @@ static Value *SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
if (Constant *C = foldOrCommuteConstant(Instruction::FAdd, Op0, Op1, Q))
return C;
if (Constant *C = simplifyFPBinop(Op0, Op1))
if (Constant *C = simplifyFPOp({Op0, Op1}))
return C;
// fadd X, -0 ==> X
@ -4548,7 +4550,7 @@ static Value *SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
if (Constant *C = foldOrCommuteConstant(Instruction::FSub, Op0, Op1, Q))
return C;
if (Constant *C = simplifyFPBinop(Op0, Op1))
if (Constant *C = simplifyFPOp({Op0, Op1}))
return C;
// fsub X, +0 ==> X
@ -4624,7 +4626,7 @@ static Value *SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF,
if (Constant *C = foldOrCommuteConstant(Instruction::FMul, Op0, Op1, Q))
return C;
if (Constant *C = simplifyFPBinop(Op0, Op1))
if (Constant *C = simplifyFPOp({Op0, Op1}))
return C;
// Now apply simplifications that do not require rounding.
@ -4657,7 +4659,7 @@ static Value *SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
if (Constant *C = foldOrCommuteConstant(Instruction::FDiv, Op0, Op1, Q))
return C;
if (Constant *C = simplifyFPBinop(Op0, Op1))
if (Constant *C = simplifyFPOp({Op0, Op1}))
return C;
// X / 1.0 -> X
@ -4702,7 +4704,7 @@ static Value *SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
if (Constant *C = foldOrCommuteConstant(Instruction::FRem, Op0, Op1, Q))
return C;
if (Constant *C = simplifyFPBinop(Op0, Op1))
if (Constant *C = simplifyFPOp({Op0, Op1}))
return C;
// Unlike fdiv, the result of frem always matches the sign of the dividend.