> Compilers don't do clever masking tricks to avoid branches, in general
They actually do a lot of such tricks. Check out the GCC sources, for example tree-ssa-phiopt.c[1] and ifcvt.c[2]
[1] https://github.com/gcc-mirror/gcc/blob/master/gcc/tree-ssa-p...
[2] https://github.com/gcc-mirror/gcc/blob/master/gcc/ifcvt.c
They do it a bit, but they leave a very great deal on the table. Clang will convert
a = (b&m)|(c&~m)
into cmov, but gcc won't.Well I was thinking of ifcvt when I mentioned a few special cases. Particularly things like minmax and abs are very specific.
In other words, two more instructions, instead of zero.
That compilers fail to do clever masking tricks to avoid branches frequently results in 2x slower programs.
No, as jleahy said, it cost a single instruction to transform 0 to 0b00..00 and 1 to 0b11..11 - SUB X, $0, X.
And your proposal - using the zero register as a flag register - breaks way more micro-optimizations (like the simple ones described in "The RISC-V Reader" book) than it allows
RISC-V was designed to be a good target for a C compiler, given that's what people almost always need. Compilers frequently see code line `int x = (a > b)` and SLT is perfect for that. Compilers don't do clever masking tricks to avoid branches, in general (there are a few hard-coded tricks like divide by signed constant).