[llvm] [WIP] [AMDGPU] [GlobalIsel] Combine Fmul with Select into ldexp instruction. (PR #120104)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 23 00:28:57 PST 2024
================
@@ -445,3 +445,75 @@ void AMDGPUCombinerHelper::applyExpandPromotedF16FMed3(MachineInstr &MI,
Builder.buildFMinNumIEEE(MI.getOperand(0), B1, C1);
MI.eraseFromParent();
}
+
+bool AMDGPUCombinerHelper::matchCombineFmulWithSelectToFldexp(
+ MachineInstr &MI, MachineInstr &Sel,
+ std::function<void(MachineIRBuilder &)> &MatchInfo) {
+ assert(MI.getOpcode() == TargetOpcode::G_FMUL);
+ assert(Sel.getOpcode() == TargetOpcode::G_SELECT);
+
+ Register Dst = MI.getOperand(0).getReg();
+ LLT DestTy = MRI.getType(Dst);
+ LLT ScalarDestTy = DestTy.getScalarType();
+
+ if ((ScalarDestTy == LLT::float64() || ScalarDestTy == LLT::float32() ||
+ ScalarDestTy == LLT::float16()) &&
+ (MRI.hasOneNonDBGUse(Sel.getOperand(0).getReg()))) {
+ Register SelectCond = Sel.getOperand(1).getReg();
+ Register SelectTrue = Sel.getOperand(2).getReg();
+ Register SelectFalse = Sel.getOperand(3).getReg();
+
+ const auto SelectTrueCst =
+ DestTy.isVector()
+ ? getFConstantSplat(SelectTrue, MRI, /*allowUndef=*/false)
+ : getFConstantVRegValWithLookThrough(SelectTrue, MRI);
+ if (!SelectTrueCst)
+ return false;
+ const auto SelectFalseCst =
+ DestTy.isVector()
+ ? getFConstantSplat(SelectFalse, MRI, /*allowUndef=*/false)
+ : getFConstantVRegValWithLookThrough(SelectFalse, MRI);
+ if (!SelectFalseCst)
+ return false;
+
+ if (SelectTrueCst->Value.isNegative() != SelectFalseCst->Value.isNegative())
+ return false;
+
+ // For f32, only non-inline constants should be transformed.
+ const SIInstrInfo *TII =
+ (MI.getMF()->getSubtarget<GCNSubtarget>()).getInstrInfo();
+ if (ScalarDestTy == LLT::float32() &&
+ TII->isInlineConstant(SelectTrueCst->Value) &&
+ TII->isInlineConstant(SelectFalseCst->Value))
+ return false;
+
+ int SelectTrueVal = SelectTrueCst->Value.getExactLog2Abs();
+ if (SelectTrueVal == INT_MIN)
+ return false;
+ int SelectFalseVal = SelectFalseCst->Value.getExactLog2Abs();
+ if (SelectFalseVal == INT_MIN)
+ return false;
+
+ MatchInfo = [=, &MI](MachineIRBuilder &Builder) {
+ LLT IntDestTy = DestTy.changeElementType(LLT::scalar(32));
+ auto NewSel =
+ Builder.buildSelect(IntDestTy, SelectCond,
+ Builder.buildConstant(IntDestTy, SelectTrueVal),
+ Builder.buildConstant(IntDestTy, SelectFalseVal));
+
+ if (SelectTrueCst->Value.isNegative()) {
+ auto NegX = Builder.buildFNeg(
+ DestTy, MI.getOperand(1).getReg(),
+ MRI.getVRegDef(MI.getOperand(1).getReg())->getFlags());
+ Builder.buildFLdexp(Dst, NegX, NewSel, MI.getFlags());
+ } else {
+ Builder.buildFLdexp(Dst, MI.getOperand(1).getReg(), NewSel,
+ MI.getFlags());
----------------
arsenm wrote:
Avoid repeating the getOperand(1) checks, give it a named value. The getVRegDef also looks weird just to get the flags, but I guess it matches the DAG behavior. Maybe need a MIR test to track the flag behavior
https://github.com/llvm/llvm-project/pull/120104
More information about the llvm-commits
mailing list