[llvm-bugs] [Bug 39043] New: recognize when switch statement is used to implement goto

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Sep 21 18:49:39 PDT 2018


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

            Bug ID: 39043
           Summary: recognize when switch statement is used to implement
                    goto
           Product: new-bugs
           Version: 7.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: slandden at gmail.com
                CC: llvm-bugs at lists.llvm.org

gcc can correctly optimize this code ( https://godbolt.org/z/1GEhQV ), but
clang cannot: This optimization is important when using goto to build a state
machine, or in the zig and rust languages, which lack goto. In these languages
goto can be implemented with a switch statement, an enum, and continue, but
only correctly if this optimization is done.

#include <stdbool.h>
#include <stdio.h>

enum states {
   one,
   two,
   three,
};

void gotoexample(unsigned char num) {
    enum states state = one;
    while (true) {
        switch (state) {
        case one: {
            state = two;
            break;
        } case two: {
            state = three;
            break;
        } case three: {
            puts("foo");
            return;
        }
        }
    }
}

-- 
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/20180922/5f1a6c40/attachment.html>


More information about the llvm-bugs mailing list