LLVM: Instruction does not dominate all uses - No control flow

2.4k views Asked by At

I implemented a function pass which iterates over basic block instructions and tracks all instructions that have a type of IntegerTy.

Here is the snippet of the pass that does it:

if (!I->isTerminator()){
  Type::TypeID datatype = I->getType()->getTypeID();
  if (datatype == llvm::Type::IntegerTyID) {
    IRBuilder<> IRB(I);
    Value* v_value = IRB.CreateZExt(I, IRB.getInt64Ty());
    Value *args[] = {v_value};
    IRB.CreateCall(NNT_log_int, args);
  }
}

However the IRB.CreateZExt(I, IRB.getInt64Ty()); command seems to create a Instruction does not dominate all uses! problem.

I understand the nature of the issue (here and here there are similar problems).

My point of confusion that I apply this pass to a toy program with no if statements or any other control flow statements, yet I still encounter this problem.

The error message:

    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

Note the fact that the inserted zext instructions name a constant with a counter number less than the previous instruction - I think this is the problem but I have no idea why my pass does this!!!

Here is the IR of my toy program before the application of the pass:

; 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
}

Also, note that that the problematic instructions are before a terminator - Again I think that this is related.

Any ideas will be highly appreciated !

1

There are 1 answers

0
Ismail Badawi On

Your zext instruction uses I, but you're inserting it before I. When you create the IRBuilder, you should pass in the instruction after I as the insert point. For example like this:

 IRBuilder<> IRB(I->getNextNode());