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

            Bug ID: 35760
           Summary: [Clang-optimization] wrong code for the optimization
                    of static global variable with malloc.
           Product: clang
           Version: 4.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangb...@nondot.org
          Reporter: yoshimatsu2...@gmail.com
                CC: llvm-bugs@lists.llvm.org

#include <stdio.h>
#include <stdlib.h>

static int* g_i = NULL;

static void PutsSomething() {
    if (g_i == NULL) {
        g_i = (int*)malloc(sizeof(*g_i));
        puts("0"); // -O0
    } else {
        puts("1"); // -O1, -O2
    }
}

int main(void) {
    g_i = NULL;
    PutsSomething();
    // free(g_i); 
    return EXIT_SUCCESS;
}

$ clang++-4.0 -O0 test.cpp; ./a.out
0
$ clang++-4.0 -O1 test.cpp; ./a.out
1
$ clang++-4.0 -O2 test.cpp; ./a.out
1

The output is different while the optimization is enabled.
When I cancel comment for free(g_i), this behavior is not seen.

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

Reply via email to