Explaining LLVM for loop

61 Views Asked by At

I am following along this post however I am new to C++ and LLVM and need help breaking down what everything means in this for loop


for(auto I = inst_begin(F), E = inst_end(F); I != E; ++I) {

  Instruction &Inst = *I;

  if(Inst.getOpcode() == Instruction::SDiv) {

    errs() << "Found Signed Division Instruction!\n"; 

  }

}

I seem to understand what's going on inside the for loop with the if statement, however I don't understand what 'inst_begin' and 'inst_end' are doing and 'I != E' as well as 'Instruction &Int = *I' I know the *I is referencing the original value of an allocated memory address..? I guess I also need a reference for how 'for loops' are written in LLVM as well.

1

There are 1 best solutions below

5
CaptainTrunky On

This is a valid C/C++ code. I'm not familiar with LLVM, so I might be not fully correct:

  1. inst_begin(F) returns a pointer to the first instruction within some code block F (a function?)
  2. inst_end(F) returns a pointer after the last valid instruction in a code block F
  3. I != E is an exit condition for the for loop - repeat some steps until this condition becomes false. If it's false - exit from the loop. I++ moves your pointer to the next instruction in between inst_begin(F)...inst_end(F)
  4. Instruction &Int = *I. I is a pointer to some memory location, basically, it's just a number. *I allows you to fetch the instruction itself given some memory address. later, you inspect this instruction.

There is no magic in this code, so get some decent C/C++ text book or MOOC and it will be 100% clear for you.