Path: csiph.com!xmission!usenet.csail.mit.edu!news.iecc.com!.POSTED.news.iecc.com!nerds-end From: gah4 Newsgroups: comp.compilers Subject: Re: Undefined Behavior Optimizations in C Date: Fri, 6 Jan 2023 11:39:42 -0800 (PST) Organization: Compilers Central Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <23-01-019@comp.compilers> References: <23-01-009@comp.compilers> <23-01-011@comp.compilers> <23-01-012@comp.compilers> <23-01-017@comp.compilers> <23-01-018@comp.compilers> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="65439"; mail-complaints-to="abuse@iecc.com" Keywords: optimize, history, comment Posted-Date: 06 Jan 2023 15:04:26 EST X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com In-Reply-To: <23-01-018@comp.compilers> Xref: csiph.com comp.compilers:3288 On Friday, January 6, 2023 at 10:49:59 AM UTC-8, gah4 wrote: (snip) > Now, this would not be a problem at all in C, with short-circuit IF > evaluation required, but even now Fortran doesn't do short > circuit IF evaluation. The way Fortran did static allocation in 1977, > there was pretty much no chance that you would access outside > your address space evaluating X(0). There is a chance, though, > that the value 3 is stored there. (snip) > [Both Fortran 66 and 77 could do short-circuit evaluation, but it's > not required. I think most compilers did, since it was easy, but > of course you couldn't count on it. -John] Sorry, yes, you can't count on it. But if you can't count on it, then you can't use it in standard programs. But now that you mention it, I was compiling this last year with gfortran, and ran into those, so it wasn't doing it. At least it wasn't doing all of them. I would run the program with bounds check on, and see where it failed. Reminds me, though, of another one from years ago. DO 11 I=l,10 DO 12 J=l,10 IF (B(I}.LT.0.)GO TO 11 12 C(J)=SQRT(B(I)} 11 CONTINUE comes from an IBM manual explaining the Fortran H optimizations. It seems that the optimizer can move the SQRT out of the inner loop, but not the test on B(I). This is actually documented, so isn't UB anymore. (Well, not IBM UB.) That is in an IBM manual from 1973. [It's on page 65 of this 1967 edition. I guess they did data flow but not control flow. http://bitsavers.org/pdf/ibm/360/fortran/C28-6602-3_OS_FORTRAN_IV_H_Programmers_Guide_1967.pdf -John]