https://bugs.llvm.org/show_bug.cgi?id=44676

            Bug ID: 44676
           Summary: Missed optimization: Reverted modification of a
                    global/thread-local that need not be visible to any
                    external calls not optimized out
           Product: new-bugs
           Version: unspecified
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedb...@nondot.org
          Reporter: psko...@gmail.com
                CC: htmldevelo...@gmail.com, llvm-bugs@lists.llvm.org

Gcc can very nicely optimize out modifications to global/thread objects if the
new value is only used locally (no opaque calls made) and the modification is
then reverted.

For example, for:

//_Thread_local //thread local make's clangs code better, but still not optimal
_Bool do_log;

void errlog(char const*);

static inline _Bool sadd(int A, int B, int *R)
{
    if(__builtin_add_overflow(A,B,R)){
        if (do_log) errlog("overflow");
        return 1;
    }
    return 0;
}

_Bool sadd_nolog(int A,int B, int *R)
{
    _Bool r;
    _Bool old_log_settings=do_log; do_log=0;
    r = sadd(A,B,R);
    do_log=old_log_settings;
    return r;
}
/////////////////////////
https://gcc.godbolt.org/z/UL79D3

gcc -Os generates an 8-byte function on x86-64 while clang -Os generates one
that's 64 bytes large.

I don't know how difficult it is to implement such an optimization, but it
would be convenient if it could be an optimization that could be counted upon.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to