[llvm-dev] Dump LLVM StoreInst

111 views
Skip to first unread message

Zhou Zhizhong via llvm-dev

unread,
Mar 9, 2018, 8:29:32 PM3/9/18
to llvm...@lists.llvm.org
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



winmail.dat

Craig Topper via llvm-dev

unread,
Mar 9, 2018, 8:37:44 PM3/9/18
to Zhou Zhizhong, llvm...@lists.llvm.org
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.

~Craig

_______________________________________________
LLVM Developers mailing list
llvm...@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


Davide Italiano via llvm-dev

unread,
Mar 9, 2018, 8:48:10 PM3/9/18
to Zhou Zhizhong, llvm...@lists.llvm.org
On Fri, Mar 9, 2018 at 9:18 AM, Zhou Zhizhong via llvm-dev
<llvm...@lists.llvm.org> wrote:
> 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 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.

--
Davide

"There are no solved problems; there are only problems that are more
or less solved" -- Henri Poincare

Zhou Zhizhong via llvm-dev

unread,
Mar 9, 2018, 11:46:52 PM3/9/18
to Craig Topper, llvm...@lists.llvm.org
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.

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.

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

I’ve attached the related files in this gist.

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

On 10 Mar 2018, at 09:37, Craig Topper <craig....@gmail.com> wrote:

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.

~Craig

On Fri, Mar 9, 2018 at 9:18 AM, Zhou Zhizhong via llvm-dev <llvm...@lists.llvm.org> wrote:
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);
   Value* v1 = I.getOperand(1);

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

Zhou Zhizhong via llvm-dev

unread,
Mar 9, 2018, 11:47:35 PM3/9/18
to Davide Italiano, llvm...@lists.llvm.org, Zhou Zhizhong
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

Reply all
Reply to author
Forward
0 new messages