-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 02/23/11 14:47, Lu, John wrote: > Hi, > > I'm trying to improve the asm code generated for C code like: > > long f(long a, long b) { > _int64 s; > > s = (((long long) a) + ((long long) b)); > > s = (s > 0x7fffffffL ? (long) 0x7fffffffL : > (s <-0x80000000L ? (long)-0x80000000L : > s)); > > return((long) s); > } > > A key step is minmax detection in tree-ssa-phiopt.c. However, in my test > cases sometimes minmax detection fails because of input like: > > if (D.5591_11 <= 2147483647) > goto <bb 3>; > else > goto <bb 4>; > > <bb 3>: > D.5594_19 = MAX_EXPR <D.5591_11, -2147483648>; > iftmp.0_20 = (long int) D.5594_19; > > <bb 4>: > # iftmp.0_1 = PHI <iftmp.0_20(3), 2147483647(2)> > > > Minmax detection expects the middle block to have one statement, but in this > case there is an additional cast. Minmax would be detected if the cast > was moved after the middle block: > > ... > <bb 3>: > D.5594_19 = MAX_EXPR <D.5591_11, -2147483648>; > > <bb 4>: > # s_1 = PHI <D.5594_19, 2147483647(2)> > iftmp.0_20 = (long int) s_1; > > The limitation occurs around line 725 in tree-ssa-phiopt.c in GCC 4.5.2: > > /* Recognize the following case, assuming d <= u: > > if (a <= u) > b = MAX (a, d); > x = PHI <b, u> > > This is equivalent to > > b = MAX (a, d); > x = MIN (b, u); */ > > gimple assign = last_and_only_stmt (middle_bb); > tree lhs, op0, op1, bound; > > I was wondering if anyone could give me guidance on how to add flexibility > to minmax detection in order to handle this case. Well, it's not that much work. You could either try to do it within the phiopt pass. Or you could try to do it as a separate pass that runs just before phiopt.
The former is more complex, but less likely to cause a performance regression since you can avoid the transformation when it's not going to result in additional phiopt opportunities. The latter is simpler, but may result in a performance loss as you're moving a expression from an arm of a conditional branch into the merge point and thus it'll always be executed, rather than only executed when the appropriate arm is executed. Jeff -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJNZrAmAAoJEBRtltQi2kC7sa0H/2BtuK4epbEmY71U9KmuV93s KFMtiyIJNAJTPgZOiqCaQ43Cd+iLlIB0IRccAa1+Ae/Q+EL6vYrGPS1iyUgFfZwq JQqZNE2ocsA6xplD44a3VWu1ewtdCHD+FXidf4ooGZZmsdwxEWt0ryaj/VFv4wha /N9Q7E6ypLqTUGjlAdostOFiv7UO8lWOPGbT600iFYjuLp8CcMXC8tWSgh6D17Tp zuMa8Y7Ucs9s2jw4mFcTpX5/5vzCh2YkcHwwsa0L8A4Tpg2FR9bTocIEsrDnUjNk 9Zu7aBK0Xr/cIkZCEajirUbi+ziPl7BITdCzNi8almJejKecVZeZcQpXyXAsyvo= =ypm1 -----END PGP SIGNATURE-----