<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 --- - poor optimization of code that has a monotone i1 flag"
   href="http://llvm.org/bugs/show_bug.cgi?id=18684">18684</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>poor optimization of code that has a monotone i1 flag
          </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>Linux
          </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>richard-llvm@metafoo.co.uk
          </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>Consider:

#include <vector>                                                  
thread_local std::vector<int> v;
int main(){
  for(long i=0;i<400000000;++i){
    v.push_back(i);
  }
  return v.size();
}

Building this with -O3 generates a loop like this:

static thread_local bool __tls_guard = false;
int main() {
  long i = 0;

for_cond:
  bool need_init = !__tls_guard;
  __tls_guard = true;
  if (need_init) {
    construct v;
  }
  v.push_back(i);
  if (++i < 4000000000)
    goto for_cond;

  return v.size();
}

We should be able to hoist the initialization and construction of 'v' out of
the loop. In particular, we have an internal thread_local global, and the only
stores to it within the module store 'i1 1', so the value of the variable is
always 1 on the second and subsequent loop iterations.

Clang could emit an @llvm.invariant.start after the store to __tls_guard if
it'd be helpful, but that seems redundant in this case, since the variable is
internal and only ever has one value stored to it, and in any case, manually
adding that to the IR doesn't enable any further optimization currently.</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>