[llvm] [DAG] Constant Folding for U/SMUL_LOHI (PR #69437)
Jay Foad via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 18 02:11:13 PDT 2023
================
@@ -9879,6 +9879,27 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
VTList.VTs[0] == Ops[0].getValueType() &&
VTList.VTs[0] == Ops[1].getValueType() &&
"Binary operator types must match!");
+ // Constant fold.
+ ConstantSDNode* LHS = dyn_cast<ConstantSDNode>(Ops[0]);
+ ConstantSDNode* RHS = dyn_cast<ConstantSDNode>(Ops[1]);
+ if(LHS && RHS) {
+ unsigned Width = VTList.VTs[0].getScalarSizeInBits();
+ unsigned OutWidth = Width * 2;
+ APInt Val = LHS->getAPIntValue();
+ APInt Mul = RHS->getAPIntValue();
+ if(Opcode == ISD::SMUL_LOHI) {
+ Val = Val.sext(OutWidth);
+ Mul = Mul.sext(OutWidth);
+ } else {
+ Val = Val.zext(OutWidth);
+ Mul = Mul.zext(OutWidth);
+ }
+ Val *= Mul;
+
+ SDValue Hi = getConstant(Val.getHiBits(Width).trunc(Width), DL, VTList.VTs[0]);
+ SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
----------------
jayfoad wrote:
Use `Val.extractBits`?
https://github.com/llvm/llvm-project/pull/69437
More information about the llvm-commits
mailing list