[LLVMbugs] [Bug 17937] New: Do-nothing static variable not optimized out

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Thu Nov 14 16:29:01 PST 2013


http://llvm.org/bugs/show_bug.cgi?id=17937

            Bug ID: 17937
           Summary: Do-nothing static variable not optimized out
           Product: new-bugs
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: htam at apple.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

Clang does not optimize out a do-nothing static variable at O3.  The first load
could be removed as rec_count should always be zero when the function is
called.

This program:
    const int rec_limit = 30;
    void foo(int& x) {
        static int rec_count = 0;
        if (rec_count > rec_limit) { return; }
        rec_count += 1;
        x = 37;
        rec_count -= 1;
    }

With this command:
    % clang++ -S foo.cc -o - -O3 -I. -emit-llvm

Produces this output:
    @_ZZ3fooRiE9rec_count = internal unnamed_addr global i32 0, align 4

    ; Function Attrs: nounwind uwtable
    define void @_Z3fooRi(i32* nocapture %x) #0 {
     %1 = load i32* @_ZZ3fooRiE9rec_count, align 4, !tbaa !0
     %2 = icmp sgt i32 %1, 30
     br i1 %2, label %7, label %3

    ; <label>:3                                       ; preds = %0
     %4 = add nsw i32 %1, 1
     store i32 %4, i32* @_ZZ3fooRiE9rec_count, align 4, !tbaa !0
     store i32 37, i32* %x, align 4, !tbaa !0
     %5 = load i32* @_ZZ3fooRiE9rec_count, align 4, !tbaa !0
     %6 = add nsw i32 %5, -1
     store i32 %6, i32* @_ZZ3fooRiE9rec_count, align 4, !tbaa !0
     br label %7

    ; <label>:7                                       ; preds = %0, %3
     ret void
    }

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20131115/6e7b8f7f/attachment.html>


More information about the llvm-bugs mailing list