On Fri, 27 Jun 2025, Jeff Law wrote: > > > On 6/27/25 12:30 PM, Andrew Pinski wrote: > > > > > > On Fri, Jun 27, 2025, 11:06 AM Raphael Moreira Zinsly > > <rzin...@ventanamicro.com <mailto:rzin...@ventanamicro.com>> wrote: > > > > Hi all, > > > > For targets that have expensive shifts this may not get a better > > sequence right now, specially for AVR and MSP430 according to > > our tests. > > Before I start looking for a fix on those targets I want to know > > if someone has any advise or other concerns with this transformation. > > > > > > There is lshift_cheap_p to see if left shit is cheap but there is not one > > for right shift. > > Now doing `-(a<0)` is cheap but the secondary shift is the expensive in > > those cases. But maybe there is a decent way of selecting the a? CST0:CST1 > > right before expand might be a good idea. > I think we want to turn it into straightline code as early as possible *if* we > can do so without regressing other targets and meet the gimple cost criteria. > The latter is more complex in this case because we're dealing with conditional > branches, so just counting expression evaluations isn't necessarily sufficient > here.
Well, there's eliding a branch and there's using shifts intead of cmov. On GIMPLE you can have a COND_EXPR as a stmt RHS, so you can if-convert separately from deciding on whether to use cmov or not. The fact is that recovering a cmov from the suggested shift operations is hard and this definitely is a very target dependent decision. For GIMPLE it's also cost metrics for say early inlining where a larger stmt sequence is bad. A branch (gcond) is currently costed 1 plus the cost of the conditional operator (a < here). Clearly the shift sequence is more expensive for inlining. If we want to special case sign-bit operations it might be worth considering a (set of) builtin that allows target specific optimal expansion. Alternatively this seems to be something for a pre-expand transform that needs to use target costs to decide whether it's worth. There are definitely a lot of embedded targets that prefer jumps over cmov. Richard. > > There's a whole mess of cases where we can take advantage of splatting the > sign bit across a GPR. Selecting across constants is a start, but not the end > of the line. Consider a < 0 ? b : 0; That's just > > t = a >> BITS_PER_WORD-1 > res = t & b; > > > jeff > > > -- Richard Biener <rguent...@suse.de> SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, Germany; GF: Ivo Totev, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg)