title: LLVM: Instruction does not dominate all uses - No control flow
body: <p>I implemented a function pass which iterates over basic block instructions and tracks all instructions that have a type of IntegerTy.</p>
  
  <p>Here is the snippet of the pass that does it:</p>
  
  <pre><code>if (!I-&gt;isTerminator()){
    Type::TypeID datatype = I-&gt;getType()-&gt;getTypeID();
    if (datatype == llvm::Type::IntegerTyID) {
      IRBuilder&lt;&gt; IRB(I);
      Value* v_value = IRB.CreateZExt(I, IRB.getInt64Ty());
      Value *args[] = {v_value};
      IRB.CreateCall(NNT_log_int, args);
    }
  }
  </code></pre>
  
  <p>However the <strong>IRB.CreateZExt(I, IRB.getInt64Ty());</strong> command seems to create a <strong>Instruction does not dominate all uses!</strong> problem.</p>
  
  <p>I understand the nature of the issue (<a href="https://stackoverflow.com/questions/27817331/llvm-instruction-does-not-dominate-all-uses-inserting-new-instruction">here</a> and <a href="https://stackoverflow.com/questions/19832757/llvm-instruction-does-not-dominate-all-uses?rq=1">here</a> there are similar problems). </p>
  
  <p>My point of confusion that I apply this pass to a toy program with no <strong>if</strong> statements or any other control flow statements, yet I still encounter this problem.</p>
  
  <p>The error message:</p>
  
  <pre><code>    Instruction does not dominate all uses!
      %2 = load i32, i32* %y, align 4
      %1 = zext i32 %2 to i64
      Instruction does not dominate all uses!
      %4 = load i32, i32* %y, align 4
      %3 = zext i32 %4 to i64
  </code></pre>
  
  <p><strong>Note</strong> the fact that the inserted <strong>zext</strong> instructions name a constant with a counter number <strong>less</strong> than the previous instruction - I think this is the problem but I have no idea why my pass does this!!!</p>
  
  <p>Here is the IR of my toy program before the application of the pass:</p>
  
  <pre><code>; Function Attrs: noinline nounwind optnone uwtable
  define i32 @_Z3fooi(i32 %x) #4 {
  entry:
    %x.addr = alloca i32, align 4
    %y = alloca i32, align 4
    %z = alloca i32, align 4
    store i32 %x, i32* %x.addr, align 4
    store i32 0, i32* %y, align 4
    %0 = load i32, i32* %x.addr, align 4
    %add = add nsw i32 %0, 3
    store i32 %add, i32* %y, align 4
    %1 = load i32, i32* %y, align 4
    store i32 %1, i32* %x.addr, align 4
    %2 = load i32, i32* %y, align 4
    ret i32 %2
  }
  
  ; Function Attrs: noinline nounwind optnone uwtable
  define i32 @_Z3bari(i32 %panos) #4 {
  entry:
    %panos.addr = alloca i32, align 4
    %y = alloca i32, align 4
    store i32 %panos, i32* %panos.addr, align 4
    %0 = load i32, i32* %panos.addr, align 4
    %add = add nsw i32 %0, 2
    store i32 %add, i32* %y, align 4
    %1 = load i32, i32* %y, align 4
    ret i32 %1
  }
  </code></pre>
  
  <p>Also, note that that the problematic instructions are before a terminator - Again I think that this is related.</p>
  
  <p>Any ideas will be highly appreciated !</p>