Dump LLVM StoreInst

Hi,

I’m writing a loop-free LLVM pass, my thought is to track if the value inside the loop is changed, so I look up the Instruction StoreInst first and try to get its value in a set. I checked getValueOperand(), getValueName() in the API document but unfortunately they failed the compilation.

if (isa<StoreInst>(I)){
    Value* v = I.getOperand(0);
    Instruction* op1 = dyn_cast<Instruction>(v);
    errs()<< v << "\t" << v1-getName()<<"\t"<<op<<\n"
}

Any suggestions on this?

Thanks,
Ethan

The code you’ve pasted there is inconsistent in variable names. Your created an instruction called ‘op1’ but your print uses ‘v1’. What should be an ‘->’ is just a ‘-’. You also have a variable in your print called ‘op’ but that’s not declared in your code. Also the result of dyn_cast should always be checked for null before using the result.

If you want to check for loop invariance, LICM has logic for doing
what you want. To track the evolution of a variable inside a loop,
there's already an
analysis, ScalarEvolution. In general, I'd recommend to check how loop
passes in LLVM perform this kind of analysis before writing your own.

Hi Craig,

Sorry for my typos, yes you are right the print variables and ‘-’ are wrong, and thanks for the useful tip for checking dyn_cast result before using it.

Actually it is how to get the value of the stored Instruction which stuck me.

Eg. (I’ve modified the code below)

int b = 2, (below shows the correspond bitcode)
store i32 2, i32* %b, align 4

v1 would dump the memory address of this value, through v1->getName() I was able to get the variable name which is b. They are close to what I want to achieve.
PastedGraphic-1.png

In the same time, consider c = b; then the code becomes:
%4 = load i32* %b , align 4
store i32 %4, i32* %c, align 4

There is an intermediate register name %4 which I have no idea how to print it out.
PastedGraphic-2.png

Perhaps the most weird thing is this, I can dump inc this time although it was not named.
%5 = load i32* %i, align 4
%inc = add nsw i32 %5, 1
store i32 %inc, i32* %i, align 4
PastedGraphic-3.png

I’ve attached the related files in this gist.
https://gist.github.com/zzz686970/a147e894ff79c09069caf9704cbffbeb

Sorry don’t want to take too much of your time, but in summary my question would be:

  1. How to print out values of my variable in llvm?
  2. In terms of intermediate register names, is there a way to print out them too? If not, maybe I can print the memory address, but it may not help me track the updated values.

Thanks,
Ethan

The code you’ve pasted there is inconsistent in variable names. Your created an instruction called ‘op1’ but your print uses ‘v1’. What should be an ‘->’ is just a ‘-’. You also have a variable in your print called ‘op’ but that’s not declared in your code. Also the result of dyn_cast should always be checked for null before using the result.

Value* v1 = I.getOperand(1);

errs() <<“v1:" << “\t” <<v1<< “,\t"<< v1->getName()<<“\n”;
if(op1 != nullptr){

}

Hi Davide,

Thanks for the useful tips, I’ll dive into LICM and ScalarEvolution to get more insights.

From what I have done yesterday, I inserted the lable name of this for loop into a std::stack, so when its successor jumps to the start of the loop, my stored stack can detect this and break out of the loop, now I’m considering to add a Counter to control the loop times to reach the lowest fixed point, but the problem would be how many times should I set, and that’s related with the value of my variables(that’s why I ask this question). If the affected values remain unchanged somewhat, then I should stop the loop and get the final result.

Anyway, I’ll see the analysis code first.

Thanks,
Ethan