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 -------------- next part -------------- A non-text attachment was scrubbed... Name: winmail.dat Type: application/ms-tnef Size: 15895 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180309/17fc0773/attachment.bin>
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-dev at 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); > Instruction* op1 = dyn_cast<Instruction>(v); > errs()<< v << "\t" << v1-getName()<<"\t"<<op<<\n" > } > > Any suggestions on this? > > Thanks, > Ethan > > > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180309/ee6f05b0/attachment.html>
On Fri, Mar 9, 2018 at 9:18 AM, Zhou Zhizhong via llvm-dev <llvm-dev at 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
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. 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> On 10 Mar 2018, at 09:37, Craig Topper <craig.topper at 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-dev at lists.llvm.org <mailto:llvm-dev at 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”}> } > > Any suggestions on this? > > Thanks, > Ethan > > > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org> > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev <http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev> > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180310/227f5632/attachment-0001.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: PastedGraphic-1.png Type: image/png Size: 51949 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180310/227f5632/attachment-0003.png> -------------- next part -------------- A non-text attachment was scrubbed... Name: PastedGraphic-2.png Type: image/png Size: 50074 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180310/227f5632/attachment-0004.png> -------------- next part -------------- A non-text attachment was scrubbed... Name: PastedGraphic-3.png Type: image/png Size: 53736 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180310/227f5632/attachment-0005.png>
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> On 10 Mar 2018, at 09:47, Davide Italiano <davide at freebsd.org> wrote: > > On Fri, Mar 9, 2018 at 9:18 AM, Zhou Zhizhong via llvm-dev > <llvm-dev at 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