<html>
    <head>
      <base href="https://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 --- - [llvm.expect] no metadata created if intrinsic is not used directly by a cmp"
   href="https://llvm.org/bugs/show_bug.cgi?id=27541">27541</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[llvm.expect] no metadata created if intrinsic is not used directly by a cmp
          </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>All
          </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>spatel+llvm@rotateright.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>int foo(int n) {
  int b = __builtin_expect(n, 1);
  if (b) { return 24; }
  return 234;
}

$ ./clang -O1 expect.c  -S -o - -emit-llvm |grep weights


----------------------------------------------------------------------------

The problem is in the LowerExpectIntrinsic pass. It discards llvm.expect
intrinsics without adding metadata if the expect isn't used directly by a
branch or compare:

define i32 @foo(i32 %n) {
entry:
  %conv = sext i32 %n to i64
  %expval = call i64 @llvm.expect.i64(i64 %conv, i64 1)
  %conv1 = trunc i64 %expval to i32
  %tobool = icmp ne i32 %conv1, 0
  br i1 %tobool, label %if.then, label %if.end

if.then:
  br label %cleanup

if.end:
  br label %cleanup

cleanup:
  %retval.0 = phi i32 [ 24, %if.then ], [ 234, %if.end ]
  ret i32 %retval.0
}

declare i64 @llvm.expect.i64(i64, i64)

------------------------------------------------------------------------------

$ ./opt -lower-expect exp.ll -S
; ModuleID = 'exp.ll'
source_filename = "exp.ll"

define i32 @foo(i32 %n) {
entry:
  %conv = sext i32 %n to i64
  %conv1 = trunc i64 %conv to i32
  %tobool = icmp ne i32 %conv1, 0
  br i1 %tobool, label %if.then, label %if.end

if.then:                                          ; preds = %entry
  br label %cleanup

if.end:                                           ; preds = %entry
  br label %cleanup

cleanup:                                          ; preds = %if.end, %if.then
  %retval.0 = phi i32 [ 24, %if.then ], [ 234, %if.end ]
  ret i32 %retval.0
}

; Function Attrs: nounwind readnone
declare i64 @llvm.expect.i64(i64, i64) #0

attributes #0 = { nounwind readnone }</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>