Sorry, my reply last night was not very clear. Let me explain what I
had in mind.
My main goal was to make that long if condition simpler. I know
riscv_rtx_costs already has many checks written in the same style, but
I would still like to make them easier to read if we can. So my first
idea was TARGET_INSN_COST, that is, riscv_insn_cost. Combine compares
costs with insn_cost, and it sets INSN_CODE before it calls insn_cost.
So riscv_insn_cost can just look at the recog result, for example by
checking the type attribute: any insn of type "crypto" counts as one
instruction. Then we would not need to match the md pattern again in C.
But after I looked more closely with grep, I found that TARGET_INSN_COST
covers much less than TARGET_RTX_COSTS. insn_cost is only used on insns
that are already recognized, in places like combine, late-combine,
fwprop (through changes_are_worthwhile in rtl-ssa), ifcvt and dse. But
places that ask for the cost of plain RTL, such as expand, expmed, CSE
and ivopts, still only call rtx_costs.
On the other hand, riscv_insn_cost itself calls pattern_cost, which
still ends up in riscv_rtx_costs. So with the current patch, all the
passes above that use insn_cost see the new cost too. Nothing is
missed, and it actually covers more places.
Besides TARGET_INSN_COST, I had another idea: borrow the PatternMatch
style from LLVM and build a similar matcher for RTL. In LLVM, matching
the same pattern on IR looks like this:
```
// LLVM IR
if (match (V, m_Or (m_Shl (m_Value (X), m_SpecificInt (32)),
m_ZExt (m_Value (Y)))))
```
If RTL had a similar set of helpers, the condition in this patch could
look like this:
```
if (TARGET_ZBKB
&& mode == word_mode
&& (code == IOR || code == XOR || code == PLUS)
&& rtx_match (x, m_binop (m_ashift (m_reg (word_mode),
m_const_int (BITS_PER_WORD / 2)),
m_zero_extend (m_reg (half_mode)))))
```
This has almost the same shape as the pattern in the md file, so it is
easy to see what is being matched. The existing checks for packh,
shNadd, bset and others could use the same helpers too. It only needs a
few small template structs, and GCC now requires C++14, so that is not
a problem.
But we are in stage 3 now, so this is not the right time to add a new
mechanism. So I am OK with this patch using the current
TARGET_RTX_COSTS approach :)
Jiawei <[email protected]> 於 2026年11月17日週二 上午11:00寫道:
>
> Hi Jeff and Kito,
>
> Thanks for the clarification. I've moved the handling back to
> riscv_rtx_costs and generalized the existing packh checks in a shared
> helper. It covers pack and packh for IOR, XOR and PLUS, while retaining
> the existing single-byte packh rule.
>
> I also checked the expression costs directly. Both packing forms now
> cost one instruction, and expressions with memory operands fall back to
> the normal costing. The pack-family tests and RV32/RV64 QEMU checks pass.
>
> I'll send this as v3.
>
> Best regards,
> Jiawei
>
> On 2026/11/17 5:30, Jeff Law wrote:
> >
> >
> > On 11/16/26 9:07 AM, Kito Cheng wrote:
> >> LGTM
> > But the costing code should be recognizing the pattern in
> > riscv_rtx_costs, not doing a one-off special case on the insn type.
> >
> > There's other costing bits already in place for other variants of
> > pack. They likely just need to be generalized.
> >
> > jeff
>