llvm.org GIT mirror llvm / 2bd4b63
InstSimplify: simplify 0 / X if nnan and nsz From: Fiona Glaser <fglaser@apple.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@230238 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 4 years ago
4 changed file(s) with 45 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
118118
119119 /// SimplifyFDivInst - Given operands for an FDiv, see if we can
120120 /// fold the result. If not, this returns null.
121 Value *SimplifyFDivInst(Value *LHS, Value *RHS,
121 Value *SimplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF,
122122 const DataLayout *TD = nullptr,
123123 const TargetLibraryInfo *TLI = nullptr,
124124 const DominatorTree *DT = nullptr,
145145
146146 /// SimplifyFRemInst - Given operands for an FRem, see if we can
147147 /// fold the result. If not, this returns null.
148 Value *SimplifyFRemInst(Value *LHS, Value *RHS,
148 Value *SimplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF,
149149 const DataLayout *TD = nullptr,
150150 const TargetLibraryInfo *TLI = nullptr,
151151 const DominatorTree *DT = nullptr,
11171117 RecursionLimit);
11181118 }
11191119
1120 static Value *SimplifyFDivInst(Value *Op0, Value *Op1, const Query &Q,
1121 unsigned) {
1120 static Value *SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
1121 const Query &Q, unsigned) {
11221122 // undef / X -> undef (the undef could be a snan).
11231123 if (match(Op0, m_Undef()))
11241124 return Op0;
11271127 if (match(Op1, m_Undef()))
11281128 return Op1;
11291129
1130 // 0 / X -> 0
1131 // Requires that NaNs are off (X could be zero) and signed zeroes are
1132 // ignored (X could be positive or negative, so the output sign is unknown).
1133 if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZero()))
1134 return Op0;
1135
11301136 return nullptr;
11311137 }
11321138
1133 Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, const DataLayout *DL,
1139 Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
1140 const DataLayout *DL,
11341141 const TargetLibraryInfo *TLI,
11351142 const DominatorTree *DT, AssumptionCache *AC,
11361143 const Instruction *CxtI) {
1137 return ::SimplifyFDivInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
1144 return ::SimplifyFDivInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI),
11381145 RecursionLimit);
11391146 }
11401147
12351242 RecursionLimit);
12361243 }
12371244
1238 static Value *SimplifyFRemInst(Value *Op0, Value *Op1, const Query &,
1239 unsigned) {
1245 static Value *SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
1246 const Query &, unsigned) {
12401247 // undef % X -> undef (the undef could be a snan).
12411248 if (match(Op0, m_Undef()))
12421249 return Op0;
12451252 if (match(Op1, m_Undef()))
12461253 return Op1;
12471254
1255 // 0 % X -> 0
1256 // Requires that NaNs are off (X could be zero) and signed zeroes are
1257 // ignored (X could be positive or negative, so the output sign is unknown).
1258 if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZero()))
1259 return Op0;
1260
12481261 return nullptr;
12491262 }
12501263
1251 Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, const DataLayout *DL,
1264 Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
1265 const DataLayout *DL,
12521266 const TargetLibraryInfo *TLI,
12531267 const DominatorTree *DT, AssumptionCache *AC,
12541268 const Instruction *CxtI) {
1255 return ::SimplifyFRemInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
1269 return ::SimplifyFRemInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI),
12561270 RecursionLimit);
12571271 }
12581272
34223436 return SimplifyFMulInst (LHS, RHS, FastMathFlags(), Q, MaxRecurse);
34233437 case Instruction::SDiv: return SimplifySDivInst(LHS, RHS, Q, MaxRecurse);
34243438 case Instruction::UDiv: return SimplifyUDivInst(LHS, RHS, Q, MaxRecurse);
3425 case Instruction::FDiv: return SimplifyFDivInst(LHS, RHS, Q, MaxRecurse);
3439 case Instruction::FDiv:
3440 return SimplifyFDivInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
34263441 case Instruction::SRem: return SimplifySRemInst(LHS, RHS, Q, MaxRecurse);
34273442 case Instruction::URem: return SimplifyURemInst(LHS, RHS, Q, MaxRecurse);
3428 case Instruction::FRem: return SimplifyFRemInst(LHS, RHS, Q, MaxRecurse);
3443 case Instruction::FRem:
3444 return SimplifyFRemInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
34293445 case Instruction::Shl:
34303446 return SimplifyShlInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false,
34313447 Q, MaxRecurse);
36503666 AC, I);
36513667 break;
36523668 case Instruction::FDiv:
3653 Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT,
3654 AC, I);
3669 Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1),
3670 I->getFastMathFlags(), DL, TLI, DT, AC, I);
36553671 break;
36563672 case Instruction::SRem:
36573673 Result = SimplifySRemInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT,
36623678 AC, I);
36633679 break;
36643680 case Instruction::FRem:
3665 Result = SimplifyFRemInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT,
3666 AC, I);
3681 Result = SimplifyFRemInst(I->getOperand(0), I->getOperand(1),
3682 I->getFastMathFlags(), DL, TLI, DT, AC, I);
36673683 break;
36683684 case Instruction::Shl:
36693685 Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1),
12051205 if (Value *V = SimplifyVectorOp(I))
12061206 return ReplaceInstUsesWith(I, V);
12071207
1208 if (Value *V = SimplifyFDivInst(Op0, Op1, DL, TLI, DT, AC))
1208 if (Value *V = SimplifyFDivInst(Op0, Op1, I.getFastMathFlags(),
1209 DL, TLI, DT, AC))
12091210 return ReplaceInstUsesWith(I, V);
12101211
12111212 if (isa(Op0))
14801481 if (Value *V = SimplifyVectorOp(I))
14811482 return ReplaceInstUsesWith(I, V);
14821483
1483 if (Value *V = SimplifyFRemInst(Op0, Op1, DL, TLI, DT, AC))
1484 if (Value *V = SimplifyFRemInst(Op0, Op1, I.getFastMathFlags(),
1485 DL, TLI, DT, AC))
14841486 return ReplaceInstUsesWith(I, V);
14851487
14861488 // Handle cases involving: rem X, (select Cond, Y, Z)
104104 ; CHECK: ret float %no_zero
105105 ret float %no_zero
106106 }
107
108 ; fdiv nsz nnan 0, X ==> 0
109 define double @fdiv_zero_by_x(double %X) {
110 ; CHECK-LABEL: @fdiv_zero_by_x(
111 ; 0 / X -> 0
112 %r = fdiv nnan nsz double 0.0, %X
113 ret double %r
114 ; CHECK: ret double 0
115 }