title: LLVM LoopPass values used outside the loop
body: <p>I'm writing an LLVM LoopPass in which I need to know <strong>which values
  are used outside the loop</strong>. For that I have this code:</p>
  
  <pre><code>virtual bool runOnLoop(Loop *loop, LPPassManager &amp;LPM)
  {
      for (auto it = loop-&gt;block_begin(); it != loop-&gt;block_end(); it++)
      {
          for (auto inst = (*it)-&gt;begin(); inst != (*it)-&gt;end(); inst++)
          {
              if (Is_Used_Outside_This_loop(loop,(Instruction *) inst))
              {
                  errs() &lt;&lt; inst-&gt;getName().str();
                  errs() &lt;&lt; " is used outside the loop\n";
              }
          }
      }
      // ...
  }
  </code></pre>
  
  <p>The inner function seemed right at first, but with the *.ll file below,
  it gives incorrect classification for <code>%tmp5</code> since it is <strong>used twice</strong>
  inside a basic block of the loop. </p>
  
  <pre><code>bool Is_Used_Outside_This_loop(Loop *loop, Value *v)
  {
      int n=0;
      int numUses = v-&gt;getNumUses();
      for (auto it = loop-&gt;block_begin(); it != loop-&gt;block_end(); it++)
      {
          if (v-&gt;isUsedInBasicBlock(*it))
          {
              n++;
          }
      }
      if (n == numUses) return false;
      else              return true;
  }
  </code></pre>
  
  <p>The following *.ll code shows that <code>%tmp5</code> is used twice
  inside a basic block of the loop. When I carefully searched the API,
  I couldn't find anything like Value::numUsesInBasicBlock( ... )</p>
  
  <pre><code>; Function Attrs: nounwind uwtable
  define internal void @foo(i8* %s) #0 {
  entry:
    %s.addr = alloca i8*, align 8
    %c = alloca i8, align 1
    store i8* %s, i8** %s.addr, align 8
    store i8 0, i8* %c, align 1
    br label %while.cond
  
  while.cond:    ; preds = %while.body, %entry
    %tmp = load i8*, i8** %s.addr, align 8
    %tmp1 = load i8, i8* %tmp, align 1
    %conv = sext i8 %tmp1 to i32
    %cmp = icmp eq i32 %conv, 97
    br i1 %cmp, label %lor.end, label %lor.rhs
  
  lor.rhs:    ; preds = %while.cond
    %tmp2 = load i8*, i8** %s.addr, align 8
    %tmp3 = load i8, i8* %tmp2, align 1
    %conv2 = sext i8 %tmp3 to i32
    %cmp3 = icmp eq i32 %conv2, 98
    br label %lor.end
  
  lor.end:; preds = %lor.rhs, %while.cond
    %tmp4 = phi i1 [ true, %while.cond ], [ %cmp3, %lor.rhs ]
    br i1 %tmp4, label %while.body, label %while.end
  
  while.body:    ; preds = %lor.end
    %tmp5 = load i8*, i8** %s.addr, align 8
    %incdec.ptr = getelementptr inbounds i8, i8* %tmp5, i32 1
    store i8* %incdec.ptr, i8** %s.addr, align 8
    %tmp6 = load i8, i8* %tmp5, align 1
    store i8 %tmp6, i8* %c, align 1
    br label %while.cond
  
  while.end:    ; preds = %lor.end
    %tmp7 = load i8*, i8** %s.addr, align 8
    %tmp8 = load i8, i8* %tmp7, align 1
    %conv5 = sext i8 %tmp8 to i32
    %cmp6 = icmp eq i32 %conv5, 99
    br i1 %cmp6, label %if.then, label %if.end
  
  if.then:    ; preds = %while.end
    %tmp9 = load i8*, i8** %s.addr, align 8
    %incdec.ptr8 = getelementptr inbounds i8, i8* %tmp9, i32 1
    store i8* %incdec.ptr8, i8** %s.addr, align 8
    br label %if.end
  
  if.end:    ; preds = %if.then, %while.end
    ret void
  }
  </code></pre>
  
  <p>Clearly, there's got to be a way of doing this, right? Thanks!</p>