[llvm] [RISCV] Cost UDIV/UREM by a constant power of 2 as a SHL/AND in getArithmeticInstrCost() (PR #179570)
Ryan Buchner via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 4 11:46:11 PST 2026
================
@@ -1048,6 +1048,30 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
unsigned getMaxInterleaveFactor(ElementCount VF) const override { return 1; }
+ bool getConvertedArithmeticInstructionCost(unsigned ISDOpcode, Type *Ty,
----------------
bababuck wrote:
It seems the overarching issue is that we use class composition with `TargetTransformInfoImpl` but the `getArithmeticInstCost()` method gets called externally (e.g. from `SLPVectorizer.cpp` through `TargetTransformInfo`) and internally (e.g. from `getInstructionCost() within `TargetTransformInfoImpl`).
We could:
1. Give `TargetTransformInfoImpl` a pointer to its encapsulating `TargetTransformInfo` object, and then `TargetTransformInfoImpl::getInstructionCost()` could call `TargetTransformInfo::getArithmeticInstrCost()`.
This would also require updating other calls to `getArithmeticInstrCost()` that occur from within `TargetTransformInfoImpl` child classes.
2. Refactor `TargetTransformInfoImpl::getArithmeticInstrCost()` so that it is a wrapper around `TargetTransformInfoImpl::getArithmeticInstrCostInner()`. Only `getArithmeticInstrCostInner()` would get overriden in the child classes.
```
TargetTransformInfoImpl::getArithmeticInstrCost() {
// Vector unsigned division/remainder will be simplified to shifts/masks.
if ((Opcode == Instruction::UDiv || Opcode == Instruction::URem) &&
Op2Info.isConstant() && Op2Info.isPowerOf2())
return getArithmeticInstrCost(
Opcode == Instruction::UDiv ? Instruction::LShr : Instruction::And, Ty,
CostKind, Op1Info.getNoProps(), Op2Info.getNoProps());
return getArithmeticInstCostInner();
}
```
The main drawback I see is that this complicated the control flow by adding another layer of encapsulation.
https://github.com/llvm/llvm-project/pull/179570
More information about the llvm-commits
mailing list