Denis Bakhvalov via llvm-dev
2017-Nov-18 18:35 UTC
[llvm-dev] How and where optimizing of undefined behavior happens
Dear community, I was wondering how exactly clang/llvm handles undefined behavior (UB). Because of this experiment: https://godbolt.org/g/cEmGHa, I tend to think that clang is light-weight in this subject and does not detect UB. And all the optimizations related to UB are done in llvm via some optimization pass (passes). Is it so? If so than what is the name of that pass? And do different optimization levels detect different UB cases, i.e. will situation change with O1, O2, O3? -- Best regards, Denis.
John Regehr via llvm-dev
2017-Nov-18 20:55 UTC
[llvm-dev] How and where optimizing of undefined behavior happens
By default, Clang has little or nothing to do with UB, its job is to do a straightforward translation to IR. UB-related optimizations are spread throughout the passes. You can get a bit of an idea of what's going on by running this command from an LLVM source tree: find ./lib -name '*.cpp' | xargs grep -i undef But also, a lot of sanitizer code lives in Clang. John On 11/18/2017 11:35 AM, Denis Bakhvalov via llvm-dev wrote:> Dear community, > > I was wondering how exactly clang/llvm handles undefined behavior (UB). > Because of this experiment: https://godbolt.org/g/cEmGHa, > I tend to think that clang is light-weight in this subject and does > not detect UB. > And all the optimizations related to UB are done in llvm via some > optimization pass (passes). > > Is it so? If so than what is the name of that pass? And do different > optimization levels detect different UB cases, i.e. will situation > change with O1, O2, O3? >
David Chisnall via llvm-dev
2017-Nov-19 12:13 UTC
[llvm-dev] How and where optimizing of undefined behavior happens
On 18 Nov 2017, at 20:55, John Regehr via llvm-dev <llvm-dev at lists.llvm.org> wrote:> > By default, Clang has little or nothing to do with UB, its job is to do a straightforward translation to IR.That’s not quite true, because the mapping to IR helps define what is UB in the source language. For example, if you don’t enable the undefined behaviour sanitiser or pass -ftrapv / -fwrapv, clang will set the nsw (no signed wrap) flag on IR arithmetic operations on types that are signed in C, which then permits the optimisers to treat wrapping behaviour as undefined. David