[llvm] [TargetLowering] Add target-independent mul-by-constant expansion algorithm (PR #196728)

Iris Shi via llvm-commits llvm-commits at lists.llvm.org
Sat May 9 07:25:01 PDT 2026


el-ev wrote:

> ⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️
> 
> You can test this locally with the following command:
> ```shell
> git-clang-format --diff origin/main HEAD --extensions cpp,h -- llvm/lib/CodeGen/SelectionDAG/ExpandMulByConstant.cpp llvm/include/llvm/CodeGen/TargetLowering.h llvm/lib/Target/AVR/AVRISelLowering.cpp llvm/lib/Target/AVR/AVRISelLowering.h llvm/lib/Target/Lanai/LanaiISelLowering.cpp llvm/lib/Target/MSP430/MSP430ISelLowering.cpp llvm/lib/Target/MSP430/MSP430ISelLowering.h llvm/lib/Target/RISCV/RISCVISelLowering.cpp llvm/lib/Target/Sparc/SparcISelLowering.cpp llvm/lib/Target/Sparc/SparcISelLowering.h --diff_from_common_commit
> ```
> 
> ⚠️ The reproduction instructions above might return results for more than one PR in a stack if you are using a stacked PR workflow. You can limit the results by changing `origin/main` to the base branch/commit you want to compare against. ⚠️
> 
> View the diff from clang-format here.
> ```diff
> diff --git a/llvm/lib/Target/MSP430/MSP430ISelLowering.cpp b/llvm/lib/Target/MSP430/MSP430ISelLowering.cpp
> index 8dc53a827..e03b2e9ae 100644
> --- a/llvm/lib/Target/MSP430/MSP430ISelLowering.cpp
> +++ b/llvm/lib/Target/MSP430/MSP430ISelLowering.cpp
> @@ -121,7 +121,7 @@ MSP430TargetLowering::MSP430TargetLowering(const TargetMachine &TM,
>    setOperationAction(ISD::MULHU,            MVT::i8,    Promote);
>    setOperationAction(ISD::SMUL_LOHI,        MVT::i8,    Promote);
>    setOperationAction(ISD::UMUL_LOHI,        MVT::i8,    Promote);
> -  setOperationAction(ISD::MUL,              MVT::i16,   Custom);
> +  setOperationAction(ISD::MUL, MVT::i16, Custom);
>    setOperationAction(ISD::MULHS,            MVT::i16,   Expand);
>    setOperationAction(ISD::MULHU,            MVT::i16,   Expand);
>    setOperationAction(ISD::SMUL_LOHI,        MVT::i16,   Expand);
> @@ -155,7 +155,8 @@ MSP430TargetLowering::MSP430TargetLowering(const TargetMachine &TM,
>  SDValue MSP430TargetLowering::LowerOperation(SDValue Op,
>                                               SelectionDAG &DAG) const {
>    switch (Op.getOpcode()) {
> -  case ISD::MUL:              return LowerMUL(Op, DAG);
> +  case ISD::MUL:
> +    return LowerMUL(Op, DAG);
>    case ISD::SHL: // FALLTHROUGH
>    case ISD::SRL:
>    case ISD::SRA:              return LowerShifts(Op, DAG);
> @@ -790,7 +791,7 @@ SDValue MSP430TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
>                             if (ShAmt == 8)
>                               return 2; // swpb + add
>                             if (ShAmt == 9)
> -                             return 3; // swpb + shift-by-1 + add
> +                             return 3;       // swpb + shift-by-1 + add
>                             return ShAmt + 1; // ShAmt individual shifts + add
>                           });
>    if (!Result || InstrCount > 12)
> diff --git a/llvm/lib/Target/Sparc/SparcISelLowering.cpp b/llvm/lib/Target/Sparc/SparcISelLowering.cpp
> index 81ea316d3..31fc77e36 100644
> --- a/llvm/lib/Target/Sparc/SparcISelLowering.cpp
> +++ b/llvm/lib/Target/Sparc/SparcISelLowering.cpp
> @@ -3160,7 +3160,8 @@ LowerOperation(SDValue Op, SelectionDAG &DAG) const {
>    switch (Op.getOpcode()) {
>    default: llvm_unreachable("Should not custom lower this!");
>  
> -  case ISD::MUL:                return LowerMUL(Op, DAG);
> +  case ISD::MUL:
> +    return LowerMUL(Op, DAG);
>    case ISD::RETURNADDR:         return LowerRETURNADDR(Op, DAG, *this,
>                                                         Subtarget);
>    case ISD::FRAMEADDR:          return LowerFRAMEADDR(Op, DAG,
> ```

Intended.

https://github.com/llvm/llvm-project/pull/196728


More information about the llvm-commits mailing list