RE: Undefined Behavior Optimizations in C

"Nuno Lopes" <>
Thu, 5 Jan 2023 10:24:13 -0000

          From comp.compilers

Related articles
Undefined Behavior Optimizations in C (Lucian Popescu) (2023-01-05)
RE: Undefined Behavior Optimizations in C (Nuno Lopes) (2023-01-05)
Re: Re: Undefined Behavior Optimizations in C (Jon Chesterfield) (2023-01-10)
| List of all articles for this month |

From: "Nuno Lopes" <>
Newsgroups: comp.compilers
Date: Thu, 5 Jan 2023 10:24:13 -0000
Organization: Compilers Central
References: 23-01-009
Injection-Info:; posting-host=""; logging-data="50911"; mail-complaints-to=""
Keywords: semantics, optimize
Posted-Date: 05 Jan 2023 11:52:36 EST
Content-Language: en-gb

Hi Lucian,

I've a fair bit of experience with UB-related optimizations in LLVM. I don't
think the speedup and code size reduction (also important) from certain UB
exploitation is negligible. I think a better project would be to quantify
the impact of each of the UB features.
As a counter argument to your thesis, see the slides of my presentation
"Semantics for Compiler IRs: Undefined Behavior is not Evil!":

See an example here that gives a 39% speedup in a loop:
More data on how alias analysis loves UB:

There's a key implementation difficulty in doing your study. While it's
feasible to disable most UB exploitation at the clang side (not with
compiler switches, you need to change the code). At the LLVM side is
trickier to do a fair comparison. Because optimizations themselves produce
UB code, but that's fine; it's just their way of expressing invariants. But
then you cannot distinguish between what's an invariant and what's an
exploitation of UB in the program.

Don't get me wrong, I would be very keen to read such a study. But it's a
lot more work than you think to do it right.

Do ping me privately if you want more information/pointers.


-----Original Message-----
From: Lucian Popescu
Sent: Thursday, January 5, 2023 10:06 AM
Subject: Undefined Behavior Optimizations in C


I'm currently working on an academic project that analyzes the speedup gain
of Undefined Behavior Optimizations in C. I argue that UB Optimizations
introduce more risks than actual speedup gains. This happens because most of
the time the compiler does not have the context to understand the intention
of the programmer in code that contains UB. ...

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.