On 11/17/26 2:39 AM, Kito Cheng wrote:
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 :)
Yea.  That's the core problem -- we need the full insn to cost this way which is real unfortuntate.  THere's all kinds of places where we cost RTL fragments that could be simplified.

I did consider the possibility to creating a dummy insn so that we could pass that off to the recognizer and get back a type to simplify riscv_rtx_costs.  It'd likely be way too expensive to really be viable.

If your C++-fu is good enough to make your idea work, then I'm all for it.

FWIW, I've got a patch here from Shreya which fixes a number of cost problems.  It focuses on cases seen cpu2017 from a build I did a while back; I'm seriously considering writing some LLM bits that would grub through the .md files and try to build testcases for every pattern/alternative.  Then we verify the cost/length in a semi-automated way then we go and fix all the goofs.

It's an idea bubbling.  But if someone wanted to run with it, go for it, just let me know so I don't duplicate your effort.

Jeff

Reply via email to