@@ -51,6 +51,9 @@ STATISTIC(NumExpand, "Number of expansions");
51
51
STATISTIC (NumReassoc, " Number of reassociations" );
52
52
53
53
static Value *SimplifyAndInst (Value *, Value *, const SimplifyQuery &, unsigned );
54
+ static Value *simplifyUnOp (unsigned , Value *, const SimplifyQuery &, unsigned );
55
+ static Value *simplifyFPUnOp (unsigned , Value *, const FastMathFlags &,
56
+ const SimplifyQuery &, unsigned );
54
57
static Value *SimplifyBinOp (unsigned , Value *, Value *, const SimplifyQuery &,
55
58
unsigned );
56
59
static Value *SimplifyFPBinOp (unsigned , Value *, Value *, const FastMathFlags &,
@@ -4245,6 +4248,33 @@ Value *llvm::SimplifyShuffleVectorInst(Value *Op0, Value *Op1, Constant *Mask,
4245
4248
return ::SimplifyShuffleVectorInst (Op0, Op1, Mask, RetTy, Q, RecursionLimit);
4246
4249
}
4247
4250
4251
+ static Constant *foldConstant (Instruction::UnaryOps Opcode,
4252
+ Value *&Op, const SimplifyQuery &Q) {
4253
+ if (auto *C = dyn_cast<Constant>(Op))
4254
+ return ConstantFoldUnaryOpOperand (Opcode, C, Q.DL );
4255
+ return nullptr ;
4256
+ }
4257
+
4258
+ // / Given the operand for an FNeg, see if we can fold the result. If not, this
4259
+ // / returns null.
4260
+ static Value *simplifyFNegInst (Value *Op, FastMathFlags FMF,
4261
+ const SimplifyQuery &Q, unsigned MaxRecurse) {
4262
+ if (Constant *C = foldConstant (Instruction::FNeg, Op, Q))
4263
+ return C;
4264
+
4265
+ Value *X;
4266
+ // fneg (fneg X) ==> X
4267
+ if (match (Op, m_FNeg (m_Value (X))))
4268
+ return X;
4269
+
4270
+ return nullptr ;
4271
+ }
4272
+
4273
+ Value *llvm::SimplifyFNegInst (Value *Op, FastMathFlags FMF,
4274
+ const SimplifyQuery &Q) {
4275
+ return ::simplifyFNegInst (Op, FMF, Q, RecursionLimit);
4276
+ }
4277
+
4248
4278
static Constant *propagateNaN (Constant *In) {
4249
4279
// If the input is a vector with undef elements, just return a default NaN.
4250
4280
if (!In->isNaN ())
@@ -4472,6 +4502,38 @@ Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
4472
4502
4473
4503
// === Helper functions for higher up the class hierarchy.
4474
4504
4505
+ // / Given the operand for a UnaryOperator, see if we can fold the result.
4506
+ // / If not, this returns null.
4507
+ static Value *simplifyUnOp (unsigned Opcode, Value *Op, const SimplifyQuery &Q,
4508
+ unsigned MaxRecurse) {
4509
+ switch (Opcode) {
4510
+ case Instruction::FNeg:
4511
+ return simplifyFNegInst (Op, FastMathFlags (), Q, MaxRecurse);
4512
+ default :
4513
+ llvm_unreachable (" Unexpected opcode" );
4514
+ }
4515
+ }
4516
+
4517
+ // / Given the operand for a UnaryOperator, see if we can fold the result.
4518
+ // / If not, this returns null.
4519
+ // / In contrast to SimplifyUnOp, try to use FastMathFlag when folding the
4520
+ // / result. In case we don't need FastMathFlags, simply fall to SimplifyUnOp.
4521
+ static Value *simplifyFPUnOp (unsigned Opcode, Value *Op,
4522
+ const FastMathFlags &FMF,
4523
+ const SimplifyQuery &Q, unsigned MaxRecurse) {
4524
+ switch (Opcode) {
4525
+ case Instruction::FNeg:
4526
+ return simplifyFNegInst (Op, FMF, Q, MaxRecurse);
4527
+ default :
4528
+ return simplifyUnOp (Opcode, Op, Q, MaxRecurse);
4529
+ }
4530
+ }
4531
+
4532
+ Value *llvm::SimplifyFPUnOp (unsigned Opcode, Value *Op, FastMathFlags FMF,
4533
+ const SimplifyQuery &Q) {
4534
+ return ::simplifyFPUnOp (Opcode, Op, FMF, Q, RecursionLimit);
4535
+ }
4536
+
4475
4537
// / Given operands for a BinaryOperator, see if we can fold the result.
4476
4538
// / If not, this returns null.
4477
4539
static Value *SimplifyBinOp (unsigned Opcode, Value *LHS, Value *RHS,
@@ -4959,6 +5021,9 @@ Value *llvm::SimplifyInstruction(Instruction *I, const SimplifyQuery &SQ,
4959
5021
default :
4960
5022
Result = ConstantFoldInstruction (I, Q.DL , Q.TLI );
4961
5023
break ;
5024
+ case Instruction::FNeg:
5025
+ Result = SimplifyFNegInst (I->getOperand (0 ), I->getFastMathFlags (), Q);
5026
+ break ;
4962
5027
case Instruction::FAdd:
4963
5028
Result = SimplifyFAddInst (I->getOperand (0 ), I->getOperand (1 ),
4964
5029
I->getFastMathFlags (), Q);
0 commit comments