<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Missed optimization is switch"
   href="http://llvm.org/bugs/show_bug.cgi?id=18737">18737</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Missed optimization is switch
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Scalar Optimizations
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>chfast@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=12012" name="attach_12012" title="C++ source">attachment 12012</a> <a href="attachment.cgi?id=12012&action=edit" title="C++ source">[details]</a></span>
C++ source

Example code in C++:

enum class Enum {
    A, B, C, D, E
};

int enum2int(Enum e) {
    switch (e) {
    case Enum::A:   return 0;
    case Enum::B:   return 1;
    case Enum::C:   return 2;
    case Enum::D:   return 3;
    case Enum::E:   return 4;
    }
}

In this case clang and opt with option -O3 used jump tables as an optimization.

@switch.table3 = private unnamed_addr constant [5 x i32] [i32 0, i32 1, i32 2,
i32 3, i32 4]

; Function Attrs: nounwind
define i32 @"\01?enum2int@@YAHW4Enum@@@Z"(i32 %e) #0 {
entry:
  %0 = icmp ult i32 %e, 5
  br i1 %0, label %switch.lookup, label %sw.epilog

sw.epilog:                                        ; preds = %entry
  tail call void @llvm.trap()
  unreachable

switch.lookup:                                    ; preds = %entry
  %switch.gep = getelementptr inbounds [5 x i32]* @switch.table3, i32 0, i32 %e
  %switch.load = load i32* %switch.gep, align 4
  ret i32 %switch.load
}

I think it could be optimized further as the input value is the same as output
value. @switch.table3 looks a bit silly.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>