[llvm] [InstCombine] InstCombine should fold frexp of select to select of frexp (PR #121227)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Sun Jan 5 21:13:44 PST 2025
================
@@ -4043,6 +4043,52 @@ InstCombinerImpl::foldExtractOfOverflowIntrinsic(ExtractValueInst &EV) {
return nullptr;
}
+static Value *foldFrexpOfSelect(ExtractValueInst &EV, CallInst *FrexpCall,
+ SelectInst *SelectInst,
+ InstCombiner::BuilderTy &Builder) {
+ // Helper to fold frexp of select to select of frexp.
+ Value *Cond = SelectInst->getCondition();
+ Value *TrueVal = SelectInst->getTrueValue();
+ Value *FalseVal = SelectInst->getFalseValue();
+ ConstantFP *ConstOp = nullptr;
+ Value *VarOp = nullptr;
+ bool ConstIsTrue = false;
+
+ if (auto *TrueConst = dyn_cast<ConstantFP>(TrueVal)) {
+ ConstOp = TrueConst;
+ VarOp = FalseVal;
+ ConstIsTrue = true;
+ } else if (auto *FalseConst = dyn_cast<ConstantFP>(FalseVal)) {
+ ConstOp = FalseConst;
+ VarOp = TrueVal;
+ ConstIsTrue = false;
+ }
+
+ if (!ConstOp || !VarOp)
+ return nullptr;
+
+ CallInst *NewFrexp =
+ Builder.CreateCall(FrexpCall->getCalledFunction(), {VarOp}, "frexp");
+
+ Value *NewEV = Builder.CreateExtractValue(NewFrexp, 0, "mantissa");
+
+ APFloat ConstVal = ConstOp->getValueAPF();
+ int Exp = 0;
+ APFloat Mantissa = ConstVal;
+
+ if (ConstVal.isFiniteNonZero()) {
+ Mantissa = frexp(ConstVal, Exp, APFloat::rmNearestTiesToEven);
+ }
+
+ Constant *ConstantMantissa = ConstantFP::get(ConstOp->getType(), Mantissa);
+
+ Value *NewSel = Builder.CreateSelect(
+ Cond, ConstIsTrue ? ConstantMantissa : NewEV,
+ ConstIsTrue ? NewEV : ConstantMantissa, "select.frexp");
----------------
arsenm wrote:
Can this preserve the fast math flags?
https://github.com/llvm/llvm-project/pull/121227
More information about the llvm-commits
mailing list