<span style="background-color:rgb(0,0,102)"><span style="color:rgb(0,0,102)"><span style="background-color:rgb(255,255,255)">Hi, thank you for your response ! Please find below my comments:</span></span><br></span><br><div class="gmail_quote">
On Mon, Dec 17, 2012 at 6:20 PM, John Criswell <span dir="ltr"><<a href="mailto:criswell@illinois.edu" target="_blank">criswell@illinois.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000"><div>
<div>On 12/17/12 10:34 AM, Alexandru Ionut
Diaconescu wrote:<br>
</div>
<blockquote type="cite">
Hello,<br>
<br>
I am a beginner of LLVM. I am trying to move among the
instructions of a BasicBlock and I cannot. In this particular
example, I try to get the previous instruction of the end
instruction. I am trying 2 methods: <br>
<br>
<br>
<br>
1. I have the following sequence of code:<br>
<br>
bool patternDC::runOnBasicBlock(BasicBlock &BB) {<br>
...<br>
if (BB.getTerminator())<br>
{<br>
Instruction* current = BB.getTerminator();<br>
errs() << "\n LAST:
"<<*current<<"\n";<br clear="all">
<br>
Instruction* prev = &BB.back();<br>
errs() << "\n <span lang="en"><span>PENULTIMATE</span></span>:
"<<*prev<<"\n";<br>
...<br>
<br>
The terminal prints the SAME instruction. I don't know how back()
works. (Definition at line <a href="http://llvm.org/doxygen/BasicBlock_8h_source.html#l00199" target="_blank">199</a>
of file <a href="http://llvm.org/doxygen/BasicBlock_8h_source.html" target="_blank">BasicBlock.h</a>.)<br>
</blockquote>
<br></div>
I believe BasicBlock::back() returns an iterator to the last
instruction in the BasicBlock which should be its terminator
instruction (basic blocks are required to have a TerminatorInst as
their last instruction).<div><br>
<span style="color:rgb(0,0,102)">Here is the definition of back(): return *this->getPrev(getTail()), so basically it should get the previous function of the terminator. or maybe the tail is considered the head because it is a circular list...</span><br>
</div></div></blockquote><div><pre class="fragment"><span class="keywordflow"> </span><br></pre> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div bgcolor="#FFFFFF" text="#000000">
<div>
<br>
<blockquote type="cite">
<br>
<br>
<br>
2. I also tried :<br>
<br>
bool patternDC::runOnBasicBlock(BasicBlock &BB) {<br>
...<br>
BasicBlock::const_iterator I = BB.begin();<br>
BasicBlock::const_iterator E = BB.end();<br>
BasicBlock::const_iterator prev_iter,last_iter;<br>
prev_iter = NULL; last_iter = NULL;<br>
for(;I!=E;I++){<br>
prev_iter = last_iter;<br>
last_iter = I;<br>
}<br>
if(prev_iter){<br>
errs() << "prev_iter: " <<
*(dyn_cast<Instruction>(prev_iter)) << "\n";<br>
}<br>
if(last_iter){<br>
errs() << "last_iter: " <<
*(dyn_cast<Instruction>(last_iter)) << "\n";<br>
}<br>
// not related to the main question: uncomment the next line for
an unusual behavior: lastlast is DIFFERENT from last. lastlast is
kind of parts of the BasicBlock<br>
// errs() << "lastlast: " <<
*(dyn_cast<Instruction>(I)) << "\n";<br>
...<br>
Instruction* prev = *(dyn_cast<Instruction*>(prev_iter));<br>
errs() << "\n prev: "<<*prev<<"\n";<br>
<br>
The terminal prints well prev and last, but I have compilation
errors when trying to assign to Instruction* prev<br>
The Clang error is: <br>
".....<br>
/home/alex/llvm/include/llvm/Support/Casting.h:51:28: error:
‘classof’ is not a member of ‘llvm::Instruction*’"<br>
<br>
<br>
If someone knows a better way to use any element from the basic
block or knows why these are not working, please let me know :)<br>
</blockquote>
<br></div>
Sometimes you have to dereference an iterator to get the thing that
it's pointing at. Try using:<br>
<br>
Instruction* prev = (dyn_cast<Instruction>(*prev_iter));<br></div></blockquote><div><br><span style="color:rgb(0,0,102)">error: cannot convert ‘llvm::Instruction’ to ‘llvm::Instruction*’ in initialization , and if I make dyn_cast<Instruction*> I have the previous problem...do you know other method or a pass example from the LLVM site that is passing through the instructions of a BB? And not using complicated dependencies as <a href="http://llvm.org/doxygen/EdgeBundles_8cpp_source.html#l00078">http://llvm.org/doxygen/EdgeBundles_8cpp_source.html#l00078</a> ?</span><br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div bgcolor="#FFFFFF" text="#000000">
<br>
That might work.<br>
<br>
-- John T.<br>
<br>
<blockquote type="cite"><div><br>
Thank you,<br>
Alex<br>
<br>
-- <br>
<font style="color:rgb(153,153,153)">Best
regards,</font><br style="color:rgb(153,153,153)">
<font style="color:rgb(153,153,153)">Alexandru
Ionut Diaconescu</font><br>
<br>
<br>
<fieldset></fieldset>
<br>
</div><pre>_______________________________________________
LLVM Developers mailing list
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a>
</pre>
</blockquote>
<br>
</div>
</blockquote></div><br><br clear="all"><br>-- <br><font style="color:rgb(153,153,153)">Best regards,</font><br style="color:rgb(153,153,153)"><font style="color:rgb(153,153,153)">Alexandru Ionut Diaconescu</font><br>
<br>